orbit-basic/app/models/user/user.rb

149 lines
4.6 KiB
Ruby
Raw Normal View History

class User
include Mongoid::Document
2011-03-08 09:25:46 +00:00
include Mongoid::Timestamps
2012-05-22 09:55:18 +00:00
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :registerable
mount_uploader :avatar, AvatarUploader
2011-01-31 03:31:33 +00:00
field :admin, :type => Boolean, :default => true
2011-11-18 08:43:14 +00:00
field :active_role
2012-02-22 06:43:24 +00:00
field :nccu_ldap_uid
2012-05-22 09:55:18 +00:00
# field :cache_dept
# has_one :cache_dept, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy
field :cache_dept,type: Hash
2012-09-17 04:06:21 +00:00
field :status_record,type: Hash
2012-05-22 09:55:18 +00:00
has_many :attribute_values, :autosave => true, :dependent => :destroy
2011-12-20 08:47:17 +00:00
has_many :app_auths,as: :privilege_apps,:inverse_of => :privilege_lists
has_many :blocked_apps, :inverse_of => :blocked_users, :class_name => "AppAuth", :dependent => :destroy
has_many :privilege_apps, :inverse_of => :privilege_users, :class_name => "AppAuth", :dependent => :destroy
has_many :managing_apps,:class_name => "AppManager", :dependent => :destroy
2012-04-03 10:25:41 +00:00
has_one :desktop, :autosave => true, :dependent => :destroy
2012-04-27 14:14:44 +00:00
has_many :other_accounts, :autosave => true, :dependent => :destroy
has_many :journals, :autosave => true, :dependent => :destroy
2012-07-02 13:06:20 +00:00
has_many :papers, :autosave => true, :dependent => :destroy
2012-09-17 04:06:21 +00:00
has_and_belongs_to_many :sub_role_tags
2012-07-02 13:06:20 +00:00
2012-09-17 04:06:21 +00:00
has_and_belongs_to_many :statuses
2012-09-11 02:52:27 +00:00
has_and_belongs_to_many :roles
2011-12-20 06:35:28 +00:00
has_and_belongs_to_many :sub_roles
accepts_nested_attributes_for :attribute_values, :allow_destroy => true
before_create :initialize_desktop
before_save :check_status_record
scope :remote_account, where(:nccu_id.ne => nil)
def set_sub_role(sub_role_id,status_id)
self.sub_roles << SubRole.find(sub_role_id)
self.status_record.store(sub_role_id,status_id)
end
2012-10-08 08:58:33 +00:00
# def get_status(*params)
# param = params[0]
# case param.class.to_s
# when 'String'
# sr = ::SubRole.find sub_role_id
# when 'Hash'
# sr = ::SubRole.first({conditions:{key: param[:key]}})
# end
# if self.sub_roles.include?(sr)
# return ::Status.find(status_record.fetch(sr.id.to_s))
# else
# nil
# end
# end
def new_attribute_values=(fields)
fields.each do |key,field|
self.attribute_values.build(field)
end
end
2012-10-08 08:58:33 +00:00
def self.find_by_status_and_sub_role_key(sub_role_key,status_key) #Query for users by using specific sub_role and status key
sr = ::SubRole.first({conditions: { key: sub_role_key }})
status = ::Status.first({conditions:{role_id: sr.role.id,key: status_key}})
find_by_status(sr.id,status.id)
end
2012-10-08 08:58:33 +00:00
def self.find_by_status(sub_role_id,status_id) #Query for users by using specific sub_role and status key buy within ID
User.where("status_record.#{sub_role_id}" => status_id)
end
2012-05-22 09:55:18 +00:00
def create_dept_cache
dept_hash = {}
VALID_LOCALES.each do |loc|
locale = loc.to_sym
2012-07-25 21:58:09 +00:00
dept_hash[locale] = sub_roles.collect{|sr| sr.title}.join(',')
2012-05-22 09:55:18 +00:00
end
self.cache_dept = dept_hash
self.save!
end
def self.current
Thread.current[:user]
end
2012-05-18 07:20:45 +00:00
def self.current=(user)
Thread.current[:user] = user
end
2011-12-20 08:47:17 +00:00
def avb_apps
2012-01-05 08:20:51 +00:00
sub_role_ids_ary=self.sub_roles.collect{|t| t.id}
query1 = AppAuth.any_in({sub_role_ids: sub_role_ids_ary}).excludes(blocked_user_ids: self.id)
query2 = AppAuth.any_of({all: true},{privilege_user_ids: self.id},{role_ids: self.role.id}).excludes(blocked_user_ids: self.id)
(query1 + query2).uniq
2011-12-20 08:47:17 +00:00
end
2011-11-17 12:02:58 +00:00
def name
info = Class::Info.first(:conditions => {:key => 'profile'})
if info
first_name = get_attribute_values.detect {|value| value.key.to_s.eql?('first_name') }[I18n.locale.to_s] rescue nil
last_name = get_attribute_values.detect {|value| value.key.to_s.eql?('last_name') }[I18n.locale.to_s] rescue nil
2012-07-03 08:05:19 +00:00
return "#{last_name} #{first_name}"
2011-11-17 12:02:58 +00:00
else
return nil
end
end
def get_attribute_values
2011-12-20 06:35:28 +00:00
@attribute_values ||= self.attribute_values
end
def get_value_from_field_id(field_id)
values = get_attribute_values
value = values.detect {|value| value.attribute_field_id == field_id} rescue nil
2012-10-08 08:58:33 +00:00
value ? value : nil
end
2012-02-24 07:30:49 +00:00
def self.from_id(id)
User.find(id) rescue nil
end
2012-04-03 10:25:41 +00:00
def initialize_desktop
self.build_desktop
end
protected
def check_status_record
roles = sub_roles.collect{|t| t.role}.uniq #get all role from sub_roles
sub_roles_ary = sub_roles.collect{|t| t.id.to_s}
2012-10-08 08:58:33 +00:00
self.status_record = status_record.keep_if{|sub_role_id, status_id|
includeing = sub_roles_ary.include?(sub_role_id)
valide = false
if includeing
sub_role = SubRole.find sub_role_id
valide = sub_role.role.statuses.include? (Status.find status_id)
end
(includeing and valide)
2012-10-08 08:58:33 +00:00
} rescue {}
end
end