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.



360
361
362
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 360

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

Instance Attribute Details

#roleObject (readonly)

Returns the value of attribute role.



359
360
361
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 359

def role
  @role
end

#userObject (readonly)

Returns the value of attribute user.



359
360
361
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 359

def user
  @user
end

Class Method Details

.specific_actions(candidate) ⇒ Object



350
351
352
353
354
355
356
357
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 350

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



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 364

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



380
381
382
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 380

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

#resembles?(spec) ⇒ Boolean

Returns:

  • (Boolean)


390
391
392
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 390

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

#reverse?(other) ⇒ Boolean

Returns:

  • (Boolean)


384
385
386
387
388
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 384

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

#to_aObject



394
395
396
# File 'lib/declarative_authorization/development_support/change_supporter.rb', line 394

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