2011-12-20 08:47:17 +00:00
|
|
|
class AppAuth
|
|
|
|
include Mongoid::Document
|
|
|
|
include Mongoid::Timestamps
|
|
|
|
# after_save :update_block_list,:update_privilage_list
|
|
|
|
|
|
|
|
field :title
|
|
|
|
field :token
|
|
|
|
field :all ,type: Boolean,default: false
|
|
|
|
belongs_to :module_app
|
|
|
|
|
|
|
|
belongs_to :users
|
|
|
|
# belongs_to :users,as: :block_users, :inverse_of => :privilege_apps
|
|
|
|
has_and_belongs_to_many :blocked_users, :inverse_of => nil, :class_name => "User"
|
|
|
|
has_and_belongs_to_many :privilege_users, :inverse_of => nil, :class_name => "User"
|
|
|
|
|
|
|
|
|
|
|
|
has_and_belongs_to_many :roles
|
2012-01-05 08:20:51 +00:00
|
|
|
has_and_belongs_to_many :sub_roles
|
2011-12-20 08:47:17 +00:00
|
|
|
|
2012-01-05 08:20:51 +00:00
|
|
|
attr_protected :roles,:sub_roles,:privilege_users,:blocked_users,:users
|
|
|
|
|
|
|
|
def add_role role
|
|
|
|
add_operation(:roles,role)
|
2011-12-20 08:47:17 +00:00
|
|
|
end
|
|
|
|
|
2012-01-05 08:20:51 +00:00
|
|
|
def add_sub_role role
|
|
|
|
add_operation(:sub_roles,role)
|
|
|
|
end
|
2011-12-20 08:47:17 +00:00
|
|
|
|
2012-01-05 08:20:51 +00:00
|
|
|
def remove_role role
|
|
|
|
remove_operation(:roles,role)
|
2011-12-20 08:47:17 +00:00
|
|
|
end
|
|
|
|
|
2012-01-05 08:20:51 +00:00
|
|
|
def remove_sub_role role
|
|
|
|
remove_operation(:sub_roles,role)
|
|
|
|
end
|
2011-12-20 08:47:17 +00:00
|
|
|
|
2012-01-05 08:20:51 +00:00
|
|
|
def add_user_to_black_list user
|
|
|
|
add_operation(:blocked_users,user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_user_from_black_list user
|
|
|
|
remove_operation(:blocked_users,user)
|
2011-12-20 08:47:17 +00:00
|
|
|
end
|
|
|
|
|
2012-01-05 08:20:51 +00:00
|
|
|
def add_user_to_privilege_list user
|
|
|
|
add_operation(:privilege_users,user)
|
|
|
|
end
|
2011-12-20 08:47:17 +00:00
|
|
|
|
|
|
|
def remove_user_from_privilege_list user
|
2012-01-05 08:20:51 +00:00
|
|
|
remove_operation(:privilege_users,user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_operation(item,obj)
|
|
|
|
if (self.send item).include? obj
|
|
|
|
(self.send item).delete obj
|
2011-12-20 08:47:17 +00:00
|
|
|
self.save!
|
|
|
|
else
|
|
|
|
false #should put error message for user not existed in list
|
2012-01-05 08:20:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_operation(item,obj)
|
|
|
|
unless (self.send item).include?(obj)
|
|
|
|
(self.send item) << obj
|
|
|
|
self.save!
|
|
|
|
else
|
|
|
|
false #should put error message for user existed in list already
|
2011-12-20 08:47:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def auth_users
|
|
|
|
if self.all?
|
|
|
|
User.all.entries
|
|
|
|
else
|
2012-01-05 08:20:51 +00:00
|
|
|
ary=[]
|
|
|
|
[:roles,:sub_roles].each do |t_role|
|
|
|
|
ary += (self.send t_role).collect do |role|
|
|
|
|
role.users
|
|
|
|
end
|
2011-12-20 08:47:17 +00:00
|
|
|
end
|
|
|
|
ary << self.privilege_users
|
2012-01-05 08:20:51 +00:00
|
|
|
ary.flatten!.uniq
|
2011-12-20 08:47:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def auth_users_after_block_list
|
|
|
|
auth_users - self.blocked_users
|
|
|
|
end
|
|
|
|
|
|
|
|
# protected
|
|
|
|
|
|
|
|
|
|
|
|
end
|