Class: Authorization::DevelopmentSupport::ChangeSupporter::RemoveRoleFromUserAction

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) ⇒ RemoveRoleFromUserAction

Returns a new instance of RemoveRoleFromUserAction.



463
464
465
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 463

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

Instance Attribute Details

#roleObject (readonly)

Returns the value of attribute role.



462
463
464
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 462

def role
  @role
end

#userObject (readonly)

Returns the value of attribute user.



462
463
464
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 462

def user
  @user
end

Class Method Details

.specific_actions(candidate) ⇒ Object



450
451
452
453
454
455
456
457
458
459
460
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 450

def self.specific_actions (candidate)
  privilege = candidate.failed_tests.first.privilege
  context = candidate.failed_tests.first.context
  user = candidate.failed_tests.first.user
  roles_for_privilege = AnalyzerEngine::Role.all_for_privilege(privilege, context, candidate.engine).map(&:to_sym)
  user.role_symbols.collect {|role_sym| AnalyzerEngine::Role.for_sym(role_sym, candidate.engine)}.
      select {|role| roles_for_privilege.include?(role.to_sym)}.
      collect do |role|
    new(user, role.to_sym)
  end
end

Instance Method Details

#apply(candidate) ⇒ Object



467
468
469
470
471
472
473
474
475
476
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 467

def apply (candidate)
  # 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
  cloned_user.role_symbols.delete(@role)
  raise "User#role_symbols immutable or user only shallowly cloned!" if cloned_user.role_symbols == @user.role_symbols
  true
end

#hashObject



478
479
480
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 478

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

#resembles?(spec) ⇒ Boolean

Returns:

  • (Boolean)


488
489
490
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 488

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

#reverse?(other) ⇒ Boolean

Returns:

  • (Boolean)


482
483
484
485
486
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 482

def reverse? (other)
  (other.is_a?(AssignRoleToUserAction) or
      other.is_a?(AbstractCompoundAction)) and
        other.reverse?(self)
end

#to_aObject



492
493
494
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 492

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