orbit4-5/app/models/user.rb

79 lines
2.2 KiB
Ruby

#this class handles user login and password. User has the attributes user name, email and password which he / she can choose
class User
include Mongoid::Document
include Mongoid::Timestamps
include ActiveModel::SecurePassword
field :user_name, type: String
field :email, type: String
field :password_digest, type: String
has_secure_password
belongs_to :workgroup
has_many :authorizations
belongs_to :member_profile
VALID_EMAIL_FORMAT = /\A[^@\s]+@([^@.\s]+\.)+[^@.\s]+\z/
validates :user_name, presence: true, uniqueness: true
validates :password, presence: true, :on => :create, length: {:in => 8..20}
validates :email, presence: true, uniqueness: true, format: { with: VALID_EMAIL_FORMAT }
def is_admin?
if (self.workgroup.present? && self.workgroup.key.eql?("admin"))
true
else
false
end
end
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
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)
if ((intersection.count > 0 if intersection.present?) && !self.is_admin? && !self.is_manager?(module_app) && !self.is_sub_manager?(module_app))
true
else
false
end
end
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
end
def self.not_admins
workgroup = Workgroup.find_by(key: "admin")
self.where(:workgroup_id.ne => workgroup.id)
end
end