forked from saurabh/orbit4-5
60 lines
1.6 KiB
Ruby
60 lines
1.6 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_authorized_with_role?(module_app)
|
|
end
|
|
|
|
def self.not_admins
|
|
workgroup = Workgroup.find_by(key: "admin")
|
|
self.where(:workgroup_id.ne => workgroup.id)
|
|
end
|
|
|
|
end
|