lib/scrivito/user_definition.rb in scrivito_sdk-1.2.0 vs lib/scrivito/user_definition.rb in scrivito_sdk-1.3.0.rc1

- old
+ new

@@ -3,11 +3,11 @@ # @api public # class UserDefinition def initialize(user_id) @user_id = user_id - @explicit_rules = {} + @explicit_rules = [] end # # Adds an explicit rule that allows the user to _always_ execute an action. # A rule consists of an action verb, the subject of the action, and an optional message. @@ -37,12 +37,12 @@ # user.can_always(:write, :workspace) # user.can_always(:publish, :workspace, 'You can always publish a workspace.') # end # def can_always(verb, subject, message = nil) - assert_no_conflict(:can_never, verb, subject) - @explicit_rules[[:can_always, verb, subject]] = message + assert_no_conflict(:never, verb, subject) + @explicit_rules << [:always, verb, subject, message] end # # Adds an explicit rule that forbids the user to execute an action. # A rule consists of an action verb, the subject of the action, and an optional message. @@ -75,22 +75,22 @@ # Scrivito::User.define('bob') do |user| # user.can_never(:create, :workspace) # end # def can_never(verb, subject, message = nil) - assert_no_conflict(:can_always, verb, subject) - @explicit_rules[[:can_never, verb, subject]] = message + assert_no_conflict(:always, verb, subject) + @explicit_rules << [:never, verb, subject, message] end # # Enable the user to create workspaces, to read from, write to, publish, delete workspaces, # and to invite others to collaborate on any workspace. # # @api public # def is_admin! - User::VERBS.each { |verb| @explicit_rules[[:can_always, verb, :workspace]] ||= nil } + @explicit_rules = User::VERBS.map { |verb| [:always, verb, :workspace, nil] } end # # Defines the user description to be displayed in the in-place GUI, e.g. in the workspace menu. # @@ -215,10 +215,10 @@ def restriction_set @restriction_set ||= RestrictionSet.new end def assert_no_conflict(type, verb, subject) - if @explicit_rules.has_key?([type, verb, subject]) + if @explicit_rules.detect { |rule| rule.take(3) == [type, verb, subject] } raise ScrivitoError.new("Conflicting rules for verb '#{verb}' and subject '#{subject}'") end end end end