Class: Authorization::DevelopmentSupport::ChangeSupporter::AssignRoleToUserAction

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractAction

#eql?, #inspect, readable_info, #resembles_any?, #weight

Constructor Details

#initialize(user, role_sym) ⇒ AssignRoleToUserAction

Returns a new instance of AssignRoleToUserAction.



318
319
320
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 318

def initialize (user, role_sym)
  @user, @role = user, role_sym
end

Instance Attribute Details

#roleObject (readonly)

Returns the value of attribute role.



317
318
319
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 317

def role
  @role
end

#userObject (readonly)

Returns the value of attribute user.



317
318
319
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 317

def user
  @user
end

Class Method Details

.specific_actions(candidate) ⇒ Object



308
309
310
311
312
313
314
315
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 308

def self.specific_actions (candidate)
  privilege = candidate.failed_tests.first.privilege
  context = candidate.failed_tests.first.context
  user = candidate.failed_tests.first.user
  AnalyzerEngine::Role.all_for_privilege(privilege, context, candidate.engine).collect do |role|
    new(user, role.to_sym)
  end
end

Instance Method Details

#apply(candidate) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 322

def apply (candidate)
  if candidate.engine.roles_with_hierarchy_for(@user).include?(@role)
    false
  else
    # beware of shallow copies!
    cloned_user = @user.clone
    user_index = candidate.users.index(@user)
    raise "Cannot find #{@user.inspect} in users array" unless user_index
    candidate.users[user_index] = cloned_user
    # possible on real user objects?
    cloned_user.role_symbols << @role
    raise "User#role_symbols immutable or user only shallowly cloned!" if cloned_user.role_symbols == @user.role_symbols
    true
  end
end

#hashObject



338
339
340
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 338

def hash
  to_a[0,2].hash + @user..hash
end

#resembles?(spec) ⇒ Boolean

Returns:

  • (Boolean)


348
349
350
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 348

def resembles? (spec)
  super(spec[0,2]) and (spec.length == 2 or spec[2] == @user.)
end

#reverse?(other) ⇒ Boolean

Returns:

  • (Boolean)


342
343
344
345
346
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 342

def reverse? (other)
  other.is_a?(RemoveRoleFromUserAction) and
      other.user. == @user. and
      other.role == @role
end

#to_aObject



352
353
354
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 352

def to_a
  [:assign_role_to_user, @role, @user]
end