lib/access-granted/policy.rb in access-granted-0.1.0 vs lib/access-granted/policy.rb in access-granted-0.1.1
- old
+ new
@@ -3,11 +3,10 @@
attr_accessor :roles
def initialize(user)
@user = user
@roles = []
- @last_priority = 0
configure(@user)
end
def configure(user)
end
@@ -15,36 +14,32 @@
def role(name, conditions_or_klass = nil, conditions = nil, &block)
name = name.to_sym
if roles.select {|r| r.name == name }.any?
raise DuplicateRole, "Role '#{name}' already defined"
end
- @last_priority += 1
r = if conditions_or_klass.is_a?(Class) && conditions_or_klass <= AccessGranted::Role
- conditions_or_klass.new(name, @last_priority, conditions, @user, block)
+ conditions_or_klass.new(name, conditions, @user, block)
else
- Role.new(name, @last_priority, conditions_or_klass, @user, block)
+ Role.new(name, conditions_or_klass, @user, block)
end
roles << r
- roles.sort_by! {|r| r.priority }
r
end
def can?(action, subject)
- match_roles(@user).each do |role|
+ matching_roles.each do |role|
permission = role.find_permission(action, subject)
return permission.granted if permission
end
false
end
def cannot?(*args)
!can?(*args)
end
- def match_roles(user)
- roles.select do |role|
- role.applies_to?(user)
- end
+ def matching_roles
+ roles.select { |role| role.applies_to?(@user) }
end
def authorize!(action, subject)
if cannot?(action, subject)
raise AccessDenied