2014-04-10 09:13:42 +00:00
|
|
|
#this class handles user login and password. User has the attributes user name, email and password which he / she can choose
|
2014-04-10 04:01:34 +00:00
|
|
|
class User
|
|
|
|
include Mongoid::Document
|
|
|
|
include Mongoid::Timestamps
|
|
|
|
include ActiveModel::SecurePassword
|
|
|
|
|
|
|
|
field :user_name, type: String
|
2014-04-14 10:40:17 +00:00
|
|
|
field :password_digest, type: String
|
2014-05-09 06:03:55 +00:00
|
|
|
field :confirmation_token, type: String
|
|
|
|
field :reset_token, type: String
|
2014-05-26 08:43:51 +00:00
|
|
|
field :approved, type: Boolean, :default => false
|
2014-05-16 06:49:42 +00:00
|
|
|
has_many :assets
|
2014-05-20 02:31:44 +00:00
|
|
|
has_many :user_actions, :dependent => :destroy
|
2014-05-09 06:03:55 +00:00
|
|
|
|
2015-03-06 09:13:09 +00:00
|
|
|
# index({ confirmation_token: 1}, { unique: true })
|
2014-05-28 10:47:19 +00:00
|
|
|
scope :unapproved, ->{ where(approved: false) }
|
2014-04-10 04:01:34 +00:00
|
|
|
|
|
|
|
has_secure_password
|
|
|
|
|
2014-05-01 07:14:16 +00:00
|
|
|
belongs_to :workgroup
|
|
|
|
has_many :authorizations
|
|
|
|
belongs_to :member_profile
|
2015-03-19 06:24:40 +00:00
|
|
|
has_and_belongs_to_many :groups
|
2015-04-14 07:18:58 +00:00
|
|
|
has_many :group_post_comments
|
2014-05-26 08:43:51 +00:00
|
|
|
has_one :facebook, :autosave => true, :dependent => :destroy
|
2015-03-04 10:31:36 +00:00
|
|
|
has_one :google, :autosave => true, :dependent => :destroy
|
2015-03-06 10:03:43 +00:00
|
|
|
# has_one :desktop, :dependent => :destroy
|
2014-05-26 08:43:51 +00:00
|
|
|
|
2014-05-01 07:14:16 +00:00
|
|
|
|
2014-10-15 07:57:58 +00:00
|
|
|
validates :user_name, uniqueness: true
|
|
|
|
validates :password, :on => :create, length: {:in => 8..20}
|
2014-05-26 01:45:46 +00:00
|
|
|
|
|
|
|
#Add getter and setter for email virtual field
|
2014-05-26 07:18:59 +00:00
|
|
|
attr_accessor :email, :first_name, :last_name
|
2014-05-01 07:14:16 +00:00
|
|
|
|
2014-05-09 06:03:55 +00:00
|
|
|
def generate_confirmation_token
|
|
|
|
self.confirmation_token = SecureRandom.hex(5)
|
|
|
|
self.save
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.confirm_email(confirmation_token = nil)
|
|
|
|
if confirmation_token
|
|
|
|
user = self.find_by(confirmation_token: confirmation_token) rescue nil
|
|
|
|
token_status = user.present?
|
|
|
|
case token_status
|
|
|
|
when true
|
|
|
|
user.confirmation_token = nil
|
|
|
|
user.save
|
|
|
|
return {:success => "true", :id => user.id.to_s}
|
|
|
|
when false
|
|
|
|
return {:success => "false"}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
return {:success => "false"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_reset_token
|
|
|
|
self.reset_token = SecureRandom.hex(5)
|
|
|
|
self.save
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_password_reset_email
|
|
|
|
self.generate_reset_token
|
|
|
|
ResetPasswordMailer.reset_user_password(self).deliver
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.check_password_token(reset_token = nil)
|
|
|
|
user = self.find_by(reset_token: reset_token) rescue nil
|
|
|
|
token_status = user.present?
|
|
|
|
if token_status
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_password(password, password_confirmation)
|
|
|
|
self.update_attributes(password: password, password_confirmation: password_confirmation, reset_token: nil)
|
|
|
|
self.save
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_confirmed?
|
|
|
|
if self.confirmation_token.present?
|
|
|
|
false
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-01 07:14:16 +00:00
|
|
|
def is_admin?
|
|
|
|
if (self.workgroup.present? && self.workgroup.key.eql?("admin"))
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-27 10:18:23 +00:00
|
|
|
def is_approved?
|
|
|
|
self.approved
|
|
|
|
end
|
|
|
|
|
2014-05-01 07:14:16 +00:00
|
|
|
def is_manager?(module_app)
|
|
|
|
if ((module_app.user_module_managers.include?(self.id) rescue nil) && (!self.is_admin?))
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_sub_manager?(module_app)
|
|
|
|
module_app_categories = module_app.categories.map {|c| c.id} rescue nil
|
|
|
|
authorized_categories = self.authorizations.map {|a| a.category.id if (a.category.present? && a.workgroup.key.eql?("sub_managers"))}
|
|
|
|
intersection = (module_app_categories & authorized_categories)
|
|
|
|
if ((intersection.count > 0 if intersection.present?) && !self.is_admin? && !self.is_manager?(module_app))
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-05 04:29:39 +00:00
|
|
|
def is_manager_with_role?(module_app)
|
|
|
|
user_roles = self.member_profile.role_ids.map {|r| r}
|
|
|
|
authorized_roles = module_app.role_managers rescue []
|
|
|
|
intersection = (user_roles & authorized_roles)
|
|
|
|
|
2014-08-05 13:51:05 +00:00
|
|
|
if (intersection.count > 0 if intersection.present?)
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_sub_manager_with_role?(module_app)
|
|
|
|
user_roles = self.member_profile.role_ids.map {|r| r}
|
|
|
|
authorized_categories = []
|
|
|
|
wg = Workgroup.find_by(:key => "sub_managers")
|
|
|
|
user_roles.each do |r|
|
|
|
|
auths = Authorization.find_by(:rold_id => r, :workgroup_id => wg.id) rescue []
|
|
|
|
auths = auths.to_a if !auths.kind_of?(Array)
|
|
|
|
auths.each do |a|
|
|
|
|
authorized_categories << a.category
|
|
|
|
end
|
|
|
|
end
|
|
|
|
module_app_categories = module_app.categories.map {|c| c.id} rescue nil
|
|
|
|
intersection = (module_app_categories & authorized_categories)
|
|
|
|
if (intersection.count > 0 if intersection.present?)
|
2014-05-05 04:29:39 +00:00
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-30 13:40:02 +00:00
|
|
|
def is_normal_user?
|
|
|
|
if self.is_admin?
|
|
|
|
return false
|
|
|
|
elsif self.authorizations.empty?
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-05 13:51:05 +00:00
|
|
|
def approved_categories_for_module(module_app)
|
|
|
|
module_app_categories = module_app.categories rescue []
|
|
|
|
authorized_categories = self.authorizations.map {|a| a.category if (a.category.present? && a.workgroup.key.eql?("sub_managers"))}
|
|
|
|
intersection = (module_app_categories & authorized_categories)
|
|
|
|
intersection
|
|
|
|
end
|
|
|
|
|
2014-07-30 13:40:02 +00:00
|
|
|
def approved_categories
|
|
|
|
categories = []
|
|
|
|
if self.is_admin?
|
|
|
|
Category.all.each do |c|
|
|
|
|
categories << c
|
|
|
|
end
|
|
|
|
else
|
2014-08-05 13:51:05 +00:00
|
|
|
authorizations = self.authorizations.collect{|a| a}
|
|
|
|
user_roles = self.member_profile.roles rescue []
|
|
|
|
user_roles.each do |r|
|
|
|
|
authorizations.concat((r.authorizations rescue []))
|
|
|
|
end
|
|
|
|
|
|
|
|
authorizations.each do |auth|
|
2014-07-30 13:40:02 +00:00
|
|
|
case auth.workgroup.key
|
|
|
|
when "managers"
|
2014-08-05 13:51:05 +00:00
|
|
|
cats = auth.module_app.categories rescue []
|
|
|
|
if !cats.blank?
|
|
|
|
cats.each do|c|
|
2014-07-30 13:40:02 +00:00
|
|
|
categories << c
|
|
|
|
end
|
|
|
|
end
|
|
|
|
when "sub_managers"
|
|
|
|
c = Category.find(auth.category_id) rescue nil
|
|
|
|
categories << c if !c.nil?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
categories
|
|
|
|
end
|
|
|
|
|
2014-05-05 04:29:39 +00:00
|
|
|
def user_workgroup(module_app)
|
|
|
|
if self.is_admin?
|
|
|
|
"Admin"
|
|
|
|
elsif (self.is_manager?(module_app) || is_manager_with_role?(module_app))
|
|
|
|
"Manager"
|
|
|
|
elsif self.is_sub_manager?(module_app)
|
|
|
|
"Sub Manager"
|
|
|
|
end
|
2014-05-01 07:14:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.not_admins
|
|
|
|
workgroup = Workgroup.find_by(key: "admin")
|
|
|
|
self.where(:workgroup_id.ne => workgroup.id)
|
|
|
|
end
|
|
|
|
|
2014-04-10 04:01:34 +00:00
|
|
|
end
|