2010-03-04 08:33:26 +00:00
|
|
|
class User
|
2011-04-13 10:19:51 +00:00
|
|
|
|
|
|
|
include Mongoid::Document
|
2013-09-26 10:59:23 +00:00
|
|
|
include Mongoid::Tree
|
|
|
|
include Mongoid::Tree::Ordering # use mongoid-tree because mongoid-ordering needs mongoid(~>3.0)
|
2011-03-08 09:25:46 +00:00
|
|
|
include Mongoid::Timestamps
|
2012-05-22 09:55:18 +00:00
|
|
|
|
2013-11-29 01:26:52 +00:00
|
|
|
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :registerable, :validatable, :confirmable #, :timeoutable
|
2011-04-13 10:19:51 +00:00
|
|
|
|
|
|
|
mount_uploader :avatar, AvatarUploader
|
|
|
|
|
2013-08-30 08:47:44 +00:00
|
|
|
field :admin, :type => Boolean, :default => false
|
2011-11-18 08:43:14 +00:00
|
|
|
field :active_role
|
2013-04-25 09:57:34 +00:00
|
|
|
field :user_id
|
|
|
|
field :sid
|
|
|
|
field :first_name, localize: true
|
|
|
|
field :last_name, localize: true
|
2012-10-15 05:54:29 +00:00
|
|
|
field :email
|
2013-04-25 09:57:34 +00:00
|
|
|
field :sex, :default => 'unknown'
|
2013-11-29 08:20:23 +00:00
|
|
|
field :approved, type: Boolean, :default => false
|
2013-04-25 09:57:34 +00:00
|
|
|
field :office_tel
|
2012-05-22 09:55:18 +00:00
|
|
|
field :cache_dept,type: Hash
|
2012-09-17 04:06:21 +00:00
|
|
|
field :status_record,type: Hash
|
2013-11-29 02:52:29 +00:00
|
|
|
field :approved, type: Boolean, :default => false
|
2013-12-16 08:08:12 +00:00
|
|
|
field :reset_password_sent_at, :type => Time
|
2012-05-22 09:55:18 +00:00
|
|
|
|
2013-08-19 10:54:35 +00:00
|
|
|
has_and_belongs_to_many :approving_apps, class_name: 'AuthApproval', inverse_of: 'authorized_users'
|
|
|
|
has_and_belongs_to_many :managing_apps, class_name: 'AuthManager', inverse_of: 'authorized_users'
|
|
|
|
has_and_belongs_to_many :sub_managing_apps, class_name: 'AuthSubManager', inverse_of: 'authorized_users'
|
|
|
|
|
2011-12-10 10:12:39 +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
|
2012-06-25 08:23:16 +00:00
|
|
|
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
|
2012-08-22 10:07:49 +00:00
|
|
|
|
2013-08-19 10:54:35 +00:00
|
|
|
# has_many :managing_apps,:class_name => "AppManager", :dependent => :destroy
|
2012-04-03 10:25:41 +00:00
|
|
|
has_one :desktop, :autosave => true, :dependent => :destroy
|
2013-06-14 08:08:30 +00:00
|
|
|
has_one :facebook, :autosave => true, :dependent => :destroy
|
2012-04-27 14:14:44 +00:00
|
|
|
has_many :other_accounts, :autosave => true, :dependent => :destroy
|
2013-09-09 15:10:04 +00:00
|
|
|
has_many :assets, as: :assetable
|
2012-11-14 13:12:10 +00:00
|
|
|
# has_many :journals, :autosave => true, :dependent => :destroy
|
|
|
|
# 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
|
|
|
|
2013-05-23 03:55:57 +00:00
|
|
|
has_many :user_actions, :dependent => :destroy
|
2013-09-18 09:31:53 +00:00
|
|
|
|
|
|
|
has_many :personal_plugin_intro
|
2013-05-23 03:55:57 +00:00
|
|
|
|
2013-04-25 09:57:34 +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
|
2013-04-25 09:57:34 +00:00
|
|
|
has_and_belongs_to_many :role_statuses
|
|
|
|
has_and_belongs_to_many :role_categorys
|
2013-09-18 09:31:53 +00:00
|
|
|
|
2012-08-22 10:07:49 +00:00
|
|
|
accepts_nested_attributes_for :attribute_values, :allow_destroy => true
|
|
|
|
|
|
|
|
before_create :initialize_desktop
|
2013-10-07 10:23:54 +00:00
|
|
|
before_save :assign_default_position, :if => :assign_default_position?
|
2012-10-24 06:46:25 +00:00
|
|
|
before_save :rebuild_status_record
|
2012-08-22 10:07:49 +00:00
|
|
|
scope :remote_account, where(:nccu_id.ne => nil)
|
2013-11-29 08:20:23 +00:00
|
|
|
scope :not_guest_user, all_of(:name.ne => "guest").and(:approved => true)
|
2013-12-04 03:42:39 +00:00
|
|
|
scope :not_guest_and_approved_user, all_of(:name.ne => "guest").and(:approved => false)
|
|
|
|
|
2013-11-29 08:20:23 +00:00
|
|
|
scope :approval_pending, where(:approved => false)
|
2012-05-14 04:34:15 +00:00
|
|
|
|
2013-04-25 09:57:34 +00:00
|
|
|
validates :user_id,
|
|
|
|
:presence => { :message => I18n.t("users.user_id_not_null") } ,
|
2013-11-27 09:50:33 +00:00
|
|
|
:length => {:minimum => 3, :maximum => 50, :message => I18n.t("users.user_id_length") },
|
|
|
|
:uniqueness => true
|
2013-11-29 04:03:16 +00:00
|
|
|
|
|
|
|
scope :approval_pending, where(:approved => false)
|
2013-11-25 09:03:28 +00:00
|
|
|
|
2012-11-03 21:56:29 +00:00
|
|
|
# validates_uniqueness_of :email,:message=> I18n.t("devise.registrations.email_not_unique")
|
|
|
|
|
2012-10-18 10:36:21 +00:00
|
|
|
# def new_attribute_values=(vars)
|
|
|
|
# binding.pry
|
|
|
|
# end
|
|
|
|
# def new_attribute_values(vars)
|
|
|
|
# binding.pry
|
|
|
|
# end
|
2012-10-24 06:46:25 +00:00
|
|
|
|
|
|
|
## # class << self
|
|
|
|
## def initialize
|
|
|
|
## self.define_method("asd"){ p "hi"}
|
|
|
|
## sub_roles.each do |sr|
|
|
|
|
## sr_ids = sr.attribute_fields.collect{|t| t.id.to_s}
|
|
|
|
## self.define_method("attribute_values_for_"+sr.key.to_s) {
|
|
|
|
## AttributeValue.where(user_id: id ).in(attribute_field_id: sr_ids)
|
|
|
|
## }
|
|
|
|
## end
|
|
|
|
## yield self
|
|
|
|
## end
|
|
|
|
|
|
|
|
# end
|
|
|
|
|
|
|
|
# def initialize
|
|
|
|
# binding.pry
|
|
|
|
# # yield self
|
|
|
|
# end
|
|
|
|
|
|
|
|
def get_attribute_value(attribute_field)
|
|
|
|
attribute_values.where(attribute_field_id: attribute_field.id).first
|
2012-10-18 10:36:21 +00:00
|
|
|
end
|
2012-09-18 08:22:56 +00:00
|
|
|
|
2012-10-24 06:46:25 +00:00
|
|
|
def get_sub_roles_by_role(role)
|
|
|
|
sub_roles.where(role_id: role.id)
|
|
|
|
end
|
|
|
|
|
2013-01-14 10:19:14 +00:00
|
|
|
def disable_role=(var)
|
2012-10-24 06:46:25 +00:00
|
|
|
var[:id].each do |id,val|
|
|
|
|
# binding.pry if id == '5052c5b22b5c49ab02000004'
|
|
|
|
if (val=="true")
|
2013-09-06 07:21:40 +00:00
|
|
|
self.role_ids.reject!{|t| t.to_s == id}
|
2013-01-14 10:19:14 +00:00
|
|
|
elsif(val=="false")
|
2013-09-06 07:21:40 +00:00
|
|
|
self.role_ids += Array(id)
|
2013-01-14 10:19:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def disable_sub_role=(var)
|
|
|
|
var[:id].each do |id,val|
|
|
|
|
|
|
|
|
@sr = SubRole.find(id)
|
|
|
|
|
|
|
|
@roid = Role.find(@sr.role_id.to_s)
|
|
|
|
|
|
|
|
# binding.pry if id == '5052c5b22b5c49ab02000004'
|
|
|
|
|
|
|
|
if ( self.roles.include?(@roid) == false or val=="true")
|
2013-09-06 07:21:40 +00:00
|
|
|
self.sub_role_ids.reject!{|t| t.to_s == id}
|
2012-10-24 06:46:25 +00:00
|
|
|
elsif(val=="false")
|
2013-09-06 07:21:40 +00:00
|
|
|
self.sub_role_ids += Array(id)
|
2012-10-24 06:46:25 +00:00
|
|
|
end
|
2013-01-14 10:19:14 +00:00
|
|
|
|
2012-10-24 06:46:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-04-25 09:57:34 +00:00
|
|
|
def role_status=(var)
|
|
|
|
self.role_statuses = []
|
|
|
|
var[:id].each do |id,val|
|
|
|
|
if (val=="true")
|
|
|
|
self.role_statuses << RoleStatus.find(id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def role_category=(var)
|
|
|
|
self.role_categorys = []
|
|
|
|
var[:id].each do |id,val|
|
|
|
|
if (val=="true")
|
|
|
|
self.role_categorys << RoleCategory.find(id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-24 06:46:25 +00:00
|
|
|
|
|
|
|
|
2012-09-18 08:22:56 +00:00
|
|
|
def set_sub_role(sub_role_id,status_id)
|
2012-10-24 06:46:25 +00:00
|
|
|
self.status_record.merge!(Hash[sub_role_id,status_id])
|
2012-09-18 08:22:56 +00:00
|
|
|
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)
|
2012-09-18 08:22:56 +00:00
|
|
|
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
|
2012-09-18 08:22:56 +00:00
|
|
|
sr = ::SubRole.first({conditions: { key: sub_role_key }})
|
|
|
|
status = ::Status.first({conditions:{role_id: sr.role.id,key: status_key}})
|
2012-10-24 06:46:25 +00:00
|
|
|
find_by_subrole_and_status(sr.id,status.id)
|
2012-09-18 08:22:56 +00:00
|
|
|
end
|
|
|
|
|
2012-10-24 06:46:25 +00:00
|
|
|
def self.find_by_subrole_and_status(sub_role_id,status_id) #Query for users by using specific sub_role and status key by within ID
|
2012-12-19 10:51:17 +00:00
|
|
|
User.not_guest_user.where("status_record.#{sub_role_id}" => status_id,:sub_role_ids.in => [ sub_role_id ])
|
2012-10-24 06:46:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.find_by_status(status_id)
|
|
|
|
status = ::Status.find status_id
|
|
|
|
::SubRole.where({role_id: status.role.id.to_s,disabled: false}).collect{|sr|
|
|
|
|
find_by_subrole_and_status(sr.id.to_s,status_id)
|
|
|
|
}.uniq!
|
2012-09-18 08:22:56 +00:00
|
|
|
end
|
|
|
|
|
2013-11-15 07:37:19 +00:00
|
|
|
def self.get_member_list_attribute_field(role_type,field_key)
|
2013-04-25 09:57:34 +00:00
|
|
|
@attribute = ::Attribute.where(:key => role_type).first
|
2013-11-15 07:37:19 +00:00
|
|
|
return ::AttributeField.where(:key => field_key, :attribute_id => @attribute.id).first
|
2013-04-25 09:57:34 +00:00
|
|
|
end
|
|
|
|
|
2013-11-15 07:37:19 +00:00
|
|
|
def self.get_member_list_attribute_value(user_id,field_id)
|
|
|
|
# @attribute = ::Attribute.where(:key => role_type).first
|
|
|
|
# @AttributeField = ::AttributeField.where(:key => field_key, :attribute_id => @attribute.id).first
|
|
|
|
return ::AttributeValue.where(attribute_field_id: field_id, :user_id => user_id).first
|
2013-04-25 09:57:34 +00:00
|
|
|
end
|
|
|
|
|
2013-11-15 07:37:19 +00:00
|
|
|
def self.get_role_member_data_by_sort(role, field_key, role_status_id)
|
2013-05-02 09:24:06 +00:00
|
|
|
a = Array.new
|
2013-11-15 07:37:19 +00:00
|
|
|
attribute_field_data = get_member_list_attribute_field(role,field_key)
|
|
|
|
where(:role_status_ids=>role_status_id).desc(:_id).collect{|t| a << [ get_member_list_attribute_value(t.id,attribute_field_data.id)['val'].to_i, t ] }
|
2013-05-02 09:24:06 +00:00
|
|
|
member_data = a.sort
|
|
|
|
member_data_tmp = member_data.collect {|v| v[1] }
|
|
|
|
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
|
|
|
|
|
2012-05-14 04:34:15 +00:00
|
|
|
def self.current
|
|
|
|
Thread.current[:user]
|
|
|
|
end
|
2012-05-18 07:20:45 +00:00
|
|
|
|
2012-05-14 04:34:15 +00:00
|
|
|
def self.current=(user)
|
|
|
|
Thread.current[:user] = user
|
|
|
|
end
|
2011-04-13 10:19:51 +00:00
|
|
|
|
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
|
|
|
|
|
2013-04-25 09:57:34 +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
|
|
|
|
# full_name = "#{last_name} #{first_name}"
|
|
|
|
|
|
|
|
# if full_name.strip.empty?
|
|
|
|
# full_name = email
|
|
|
|
# end
|
|
|
|
|
|
|
|
# return full_name
|
|
|
|
# else
|
|
|
|
# return nil
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
|
2011-11-17 12:02:58 +00:00
|
|
|
def name
|
2011-12-18 02:07:42 +00:00
|
|
|
info = Class::Info.first(:conditions => {:key => 'profile'})
|
|
|
|
if info
|
2013-04-25 09:57:34 +00:00
|
|
|
# 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
|
|
|
|
# full_name = (I18n.locale != 'zh_tw') ? "#{self.last_name} #{self.first_name}" : "#{self.first_name} #{self.last_name}"
|
|
|
|
full_name = (I18n.locale.to_s == 'zh_tw') ? "#{self.last_name} #{self.first_name}" : "#{self.first_name} #{self.last_name}"
|
2012-12-19 07:28:49 +00:00
|
|
|
|
|
|
|
if full_name.strip.empty?
|
|
|
|
full_name = email
|
|
|
|
end
|
|
|
|
|
|
|
|
return full_name
|
2011-11-17 12:02:58 +00:00
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-18 02:07:42 +00:00
|
|
|
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
|
2011-12-18 02:07:42 +00:00
|
|
|
end
|
2012-02-24 07:30:49 +00:00
|
|
|
|
|
|
|
def self.from_id(id)
|
|
|
|
User.find(id) rescue nil
|
|
|
|
end
|
2011-12-18 02:07:42 +00:00
|
|
|
|
2012-04-03 10:25:41 +00:00
|
|
|
def initialize_desktop
|
2012-11-05 15:04:54 +00:00
|
|
|
self.build_desktop(desktop_widget_ids: DesktopWidget.all.collect{|widget| widget.id})
|
2012-04-03 10:25:41 +00:00
|
|
|
end
|
2013-07-02 08:46:44 +00:00
|
|
|
|
|
|
|
def managed_module_apps
|
2013-08-19 10:54:35 +00:00
|
|
|
self.managing_apps.inject([]) do |managed_apps, auth_manager|
|
|
|
|
managed_apps << auth_manager.module_app
|
2013-07-02 08:46:44 +00:00
|
|
|
managed_apps
|
|
|
|
end
|
|
|
|
end
|
2013-11-29 01:26:52 +00:00
|
|
|
|
2013-11-29 02:52:29 +00:00
|
|
|
def active_for_authentication?
|
|
|
|
super && approved?
|
|
|
|
end
|
|
|
|
|
|
|
|
def inactive_message
|
|
|
|
if !approved?
|
|
|
|
:not_approved
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
2012-04-03 10:25:41 +00:00
|
|
|
|
2013-10-07 10:23:54 +00:00
|
|
|
private
|
2012-09-18 08:22:56 +00:00
|
|
|
|
2012-10-24 06:46:25 +00:00
|
|
|
def rebuild_status_record
|
|
|
|
self.status_record = {}
|
|
|
|
self.attribute_values.reject{|t| t.key!='status'}.each do |status|
|
|
|
|
# binding.pry
|
|
|
|
set_sub_role(status.attribute_field.attribute.id.to_s,status[:value]) rescue nil
|
|
|
|
end
|
2012-09-18 08:22:56 +00:00
|
|
|
end
|
|
|
|
|
2013-10-07 10:23:54 +00:00
|
|
|
# override mongoid-tree's method
|
|
|
|
def assign_default_position?
|
|
|
|
!self['name'].eql?('guest') && (self.position.nil? || self.parent_id_changed?)
|
|
|
|
end
|
|
|
|
|
2012-12-03 06:55:57 +00:00
|
|
|
end
|