3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/roleback/user_extension.rb', line 3
def self.extend!(user_class)
user_instance = user_class.new
unless user_instance.respond_to?(:roles) && user_instance.method(:roles).arity == 0
raise ::Roleback::InvalidOrMisconfiguredUserClass, "User class #{user_class.name} should have a method call roles that returns an array of role names"
end
if user_class.instance_methods.include?(:can?)
raise ::Roleback::InvalidOrMisconfiguredUserClass, "User class #{user_class.name} already has a method called can?"
end
user_class.class_eval do
def can?(resource: Roleback.any, scope: Roleback.any, action: Roleback.any)
roles = self.roles
return false if roles.empty?
if !roles.is_a?(Array)
raise ::Roleback::InvalidOrMisconfiguredUserClass, "User class #{self.class}#roles should return an array of role names"
end
roles.each do |role|
return true if ::Roleback.can?(role.to_sym, resource: resource, scope: scope, action: action)
end
false
end
end
end
|