Object Auth. Now object can be included with "include OrbitCoreLib::ObjectAuthable" to use kernel method,such as
1.Object.authed_for_user(user,title_of_object_auth). title_of_object_auth is optional 2.object.authed_users(user,title_of_object_auth) . title_of_object_auth is optional if title_of_object_auth is not given,then it will return calculation across all possiblity.
This commit is contained in:
1 parent
3ed9b81df9
commit
e20739e32b
7 files changed
+96
-52
No files matched your search
@@ -0,0 +1,42 @@
|
||||
module OrbitCoreLib
|
||||
module ObjectAuthable
|
||||
def self.included(base)
|
||||
base.instance_eval("has_many :object_auths,as: :obj_authable,dependent: :delete")
|
||||
|
||||
base.define_singleton_method :authed_for_user do |user,title = nil|
|
||||
sub_role_ids_ary=user.sub_roles.collect{|t| t.id}
|
||||
if title.nil?
|
||||
auth_object_space = ObjectAuth.where(obj_authable_type: self.to_s)
|
||||
else
|
||||
auth_object_space = ObjectAuth.where(obj_authable_type: self.to_s,title: title)
|
||||
end
|
||||
|
||||
query1 = auth_object_space.any_in({sub_role_ids: sub_role_ids_ary}).excludes(blocked_user_ids: user.id)
|
||||
query2 = auth_object_space.any_of({all: true},{privilege_user_ids: user.id},{role_ids: user.role.id}).excludes(blocked_user_ids: user.id)
|
||||
result = (query1 + query2).uniq
|
||||
result.collect{|t| t.obj_authable}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def authed_users(title=nil)
|
||||
users = []
|
||||
unless title.nil?
|
||||
users = self.object_auths.where(title: title )[0].auth_users_after_block_list rescue []
|
||||
else
|
||||
users = self.object_auths.collect{|t| t.auth_users_after_block_list} rescue []
|
||||
users.flatten!.uniq!
|
||||
end
|
||||
users
|
||||
end
|
||||
|
||||
def tell_me_class
|
||||
self.class.name
|
||||
end
|
||||
|
||||
def search_object_db
|
||||
ObjectAuth.where(obj_authable_type: self.class.name)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user