forked from saurabh/orbit4-5
30 lines
986 B
Ruby
30 lines
986 B
Ruby
|
class Authorization
|
||
|
include Mongoid::Document
|
||
|
|
||
|
belongs_to :module_app
|
||
|
belongs_to :user
|
||
|
belongs_to :category
|
||
|
belongs_to :workgroup
|
||
|
belongs_to :role
|
||
|
|
||
|
scope :module_authorized_users, ->(module_app){ where(module_app_id: module_app) }
|
||
|
scope :category_authorized_users, ->(category){ where(category_id: category) }
|
||
|
|
||
|
def self.create_authorization(module_app_id=nil,category_id=nil,user_id,type)
|
||
|
user = User.find(user_id)
|
||
|
if user.is_admin?
|
||
|
elsif user.workgroup.nil?
|
||
|
if type == "module_authorization"
|
||
|
workgroup = Workgroup.find_by(key: "managers")
|
||
|
user.update_attributes(workgroup_id: workgroup.id)
|
||
|
a = self.new(module_app_id: module_app_id, user_id: user_id, workgroup_id: workgroup.id)
|
||
|
a.save
|
||
|
elsif type == "category_authorization"
|
||
|
self.create(category_id: category_id, user_id: user_id )
|
||
|
workgroup = Workgroup.find_by(key: "sub_managers")
|
||
|
user.update_attributes(workgroup_id: workgroup.id)
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|