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.



134
135
136
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 134

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

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



133
134
135
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 133

def engine
  @engine
end

#failed_testsObject (readonly)

Returns the value of attribute failed_tests.



133
134
135
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 133

def failed_tests
  @failed_tests
end

#stepsObject (readonly)

Returns the value of attribute steps.



133
134
135
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 133

def steps
  @steps
end

#usersObject (readonly)

Returns the value of attribute users.



133
134
135
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 133

def users
  @users
end

Instance Method Details

#<=>(other) ⇒ Object



225
226
227
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 225

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

#abstract_actionsObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 164

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

#affected_users(original_engine, original_users, privilege, context) ⇒ Object



145
146
147
148
149
150
151
152
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 145

def affected_users (original_engine, original_users, privilege, context)
  (0...@users.length).select do |i|
    original_engine.permit?(privilege, :context => context,
      :skip_attribute_test => true, :user => original_users[i]) !=
        @engine.permit?(privilege, :context => context,
          :skip_attribute_test => true, :user => @users[i])
  end.collect {|i| original_users[i]}
end

#apply(action) ⇒ Object



184
185
186
187
188
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 184

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

#changesObject



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

def changes
  @steps
end

#check(approach_checker) ⇒ Object



138
139
140
141
142
143
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 138

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



154
155
156
157
158
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 154

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

#inspectObject



219
220
221
222
223
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 219

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)


180
181
182
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 180

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

#similar_to(other) ⇒ Object



213
214
215
216
217
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 213

def similar_to (other)
  other.weight == weight and
      other.changes.map {|change| change.class.name}.sort ==
        changes.map {|change| change.class.name}.sort
end

#sort_valueObject



205
206
207
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 205

def sort_value
  weight + @failed_tests.length
end

#state_hashObject



195
196
197
198
199
200
201
202
203
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 195

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)


190
191
192
193
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 190

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

#weightObject



209
210
211
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 209

def weight
  changes.sum(&:weight)
end