Added in memory cache for user object because of performance reasons (count of SQLs).
This commit is contained in:
parent
633db86a2b
commit
6047e63666
2 changed files with 42 additions and 21 deletions
|
@ -139,17 +139,7 @@ class ApplicationController < ActionController::Base
|
||||||
def user_data_full (user_id)
|
def user_data_full (user_id)
|
||||||
|
|
||||||
# get user
|
# get user
|
||||||
user = User.find(user_id)
|
user = User.find_fulldata(user_id)
|
||||||
|
|
||||||
# get linked accounts
|
|
||||||
user['accounts'] = {}
|
|
||||||
authorizations = user.authorizations() || []
|
|
||||||
authorizations.each do | authorization |
|
|
||||||
user['accounts'][authorization.provider] = {
|
|
||||||
:uid => authorization[:uid],
|
|
||||||
:username => authorization[:username]
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
# do not show password
|
# do not show password
|
||||||
user['password'] = ''
|
user['password'] = ''
|
||||||
|
@ -183,11 +173,6 @@ class ApplicationController < ActionController::Base
|
||||||
}
|
}
|
||||||
user['links'].push topic
|
user['links'].push topic
|
||||||
|
|
||||||
# set roles
|
|
||||||
user['roles'] = user.roles.select('id, name').where(:active => true)
|
|
||||||
user['groups'] = user.groups.select('id, name').where(:active => true)
|
|
||||||
user['organization'] = user.organization
|
|
||||||
user['organizations'] = user.organizations.select('id, name').where(:active => true)
|
|
||||||
return user
|
return user
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -5,8 +5,13 @@ class User < ActiveRecord::Base
|
||||||
has_and_belongs_to_many :organizations
|
has_and_belongs_to_many :organizations
|
||||||
belongs_to :organization, :class_name => 'Organization'
|
belongs_to :organization, :class_name => 'Organization'
|
||||||
has_many :authorizations
|
has_many :authorizations
|
||||||
|
after_create :delete_cache
|
||||||
|
after_update :delete_cache
|
||||||
|
after_destroy :delete_cache
|
||||||
|
|
||||||
def self.authenticate(username, password)
|
@@cache = {}
|
||||||
|
|
||||||
|
def self.authenticate( username, password )
|
||||||
user = User.where( :login => username, :active => true ).first
|
user = User.where( :login => username, :active => true ).first
|
||||||
return nil if user.nil?
|
return nil if user.nil?
|
||||||
logger.debug 'auth'
|
logger.debug 'auth'
|
||||||
|
@ -47,8 +52,39 @@ class User < ActiveRecord::Base
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.find_fulldata(user_id)
|
||||||
|
|
||||||
|
return @@cache[user_id] if @@cache[user_id]
|
||||||
|
|
||||||
|
# get user
|
||||||
|
user = User.find(user_id)
|
||||||
|
|
||||||
|
# get linked accounts
|
||||||
|
user['accounts'] = {}
|
||||||
|
authorizations = user.authorizations() || []
|
||||||
|
authorizations.each do | authorization |
|
||||||
|
user['accounts'][authorization.provider] = {
|
||||||
|
:uid => authorization[:uid],
|
||||||
|
:username => authorization[:username]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
# set roles
|
||||||
|
user['roles'] = user.roles.select('id, name').where(:active => true)
|
||||||
|
user['groups'] = user.groups.select('id, name').where(:active => true)
|
||||||
|
user['organization'] = user.organization
|
||||||
|
user['organizations'] = user.organizations.select('id, name').where(:active => true)
|
||||||
|
|
||||||
|
@@cache[user_id] = user
|
||||||
|
|
||||||
|
return user
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
def delete_cache
|
||||||
|
@@cache[self.id] = nil
|
||||||
|
end
|
||||||
|
|
||||||
def check_name
|
def check_name
|
||||||
if self.firstname && (!self.lastname || self.lastname == '') then
|
if self.firstname && (!self.lastname || self.lastname == '') then
|
||||||
name = self.firstname.split(' ', 2)
|
name = self.firstname.split(' ', 2)
|
||||||
|
|
Loading…
Reference in a new issue