Class: Authorization::DevelopmentSupport::ChangeSupporter::Approach

Inherits:
Object
  • Object
show all
Defined in:
lib/declarative_authorization/development_support/change_supporter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine, users, steps) ⇒ Approach

Returns a new instance of Approach.



111
112
113
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 111

def initialize (engine, users, steps)
  @engine, @users, @steps = engine, users, steps
end

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



110
111
112
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 110

def engine
  @engine
end

#failed_testsObject (readonly)

Returns the value of attribute failed_tests.



110
111
112
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 110

def failed_tests
  @failed_tests
end

#stepsObject (readonly)

Returns the value of attribute steps.



110
111
112
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 110

def steps
  @steps
end

#usersObject (readonly)

Returns the value of attribute users.



110
111
112
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 110

def users
  @users
end

Instance Method Details

#<=>(other) ⇒ Object



183
184
185
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 183

def <=> (other)
  sort_value <=> other.sort_value
end

#abstract_actionsObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 132

def abstract_actions
  if failed_tests.first.positive
    [
      AssignPrivilegeToRoleAction,
      AssignRoleToUserAction,
      CreateAndAssignRoleToUserAction,
      AddPrivilegeAndAssignRoleToUserAction
    ]
  else
    [
      RemovePrivilegeFromRoleAction,
      RemoveRoleFromUserAction
    ]
  end
end

#apply(action) ⇒ Object



152
153
154
155
156
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 152

def apply (action)
  ok = action.apply(self)
  @steps << action if ok
  ok
end

#changesObject



128
129
130
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 128

def changes
  @steps
end

#check(approach_checker) ⇒ Object



115
116
117
118
119
120
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 115

def check (approach_checker)
  res = approach_checker.check(@engine, @users)
  @failed_tests = approach_checker.failed_tests
  #puts "CHECKING #{inspect} (#{res}, #{sort_value})"
  res
end

#initialize_copy(other) ⇒ Object



122
123
124
125
126
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 122

def initialize_copy (other)
  @engine = @engine.clone
  @users = @users.clone
  @steps = @steps.clone
end

#inspectObject



177
178
179
180
181
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 177

def inspect
  "Approach: Steps: #{changes.map(&:inspect) * ', '}"# +
     # "\n  Roles: #{AnalyzerEngine.roles(@engine).map(&:to_sym).inspect}; " +
     # "\n  Users: #{@users.map(&:role_symbols).inspect}"
end

#reverse_of_previous?(specific_action) ⇒ Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 148

def reverse_of_previous? (specific_action)
  changes.any? {|step| step.reverse?(specific_action)}
end

#sort_valueObject



173
174
175
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 173

def sort_value
  changes.sum(&:weight) + @failed_tests.length
end

#state_hashObject



163
164
165
166
167
168
169
170
171
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 163

def state_hash
  @state_hash ||= @engine.auth_rules.inject(0) do |memo, rule|
      memo + rule.privileges.hash + rule.contexts.hash +
          rule.attributes.hash + rule.role.hash
    end +
      @users.inject(0) {|memo, user| memo + user.role_symbols.hash } +
      @engine.privileges.hash + @engine.privilege_hierarchy.hash +
      @engine.roles.hash + @engine.role_hierarchy.hash
end

#subset?(other_approach) ⇒ Boolean

Returns:

  • (Boolean)


158
159
160
161
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 158

def subset? (other_approach)
  other_approach.changes.length >= changes.length &&
      changes.all? {|step| other_approach.changes.any? {|step_2| step_2.eql?(step)} }
end