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.



505
506
507
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 505

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

Instance Attribute Details

#roleObject (readonly)

Returns the value of attribute role.



504
505
506
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 504

def role
  @role
end

#userObject (readonly)

Returns the value of attribute user.



504
505
506
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 504

def user
  @user
end

Class Method Details

.specific_actions(candidate) ⇒ Object



492
493
494
495
496
497
498
499
500
501
502
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 492

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



509
510
511
512
513
514
515
516
517
518
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 509

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



520
521
522
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 520

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

#resembles?(spec) ⇒ Boolean

Returns:

  • (Boolean)


530
531
532
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 530

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

#reverse?(other) ⇒ Boolean

Returns:

  • (Boolean)


524
525
526
527
528
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 524

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

#to_aObject



534
535
536
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 534

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