Class: CommandTower::Services::Admin::Users::ReplaceUserRoles

Inherits:
CommandTower::Services::ApplicationService show all
Defined in:
app/services/command_tower/services/admin/users.rb

Constant Summary

Constants inherited from CommandTower::ServiceBase

CommandTower::ServiceBase::ON_ARGUMENT_VALIDATION

Instance Method Summary collapse

Methods inherited from CommandTower::Services::ApplicationService

call, inherited

Methods included from Transactional

#fail_transaction!, #transaction

Methods inherited from CommandTower::ServiceBase

#command_tower_lifecycle, inherited, #internal_validate, #service_lifecycle_error_codes, #service_lifecycle_log_level, #validate!

Methods included from Logging::LifecycleDeclaration

included

Methods included from Execution::ContextAccess

#audit, #execution_context, #log_debug, #log_error, #log_info, #log_warn, #publish_event

Methods included from ArgumentValidation

included

Methods included from CommandTower::ServiceLogging

included

Instance Method Details

#callObject



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'app/services/command_tower/services/admin/users.rb', line 338

def call
  assignable = CommandTower::Authorization::AssignableRoles.names
  desired = desired_roles.map(&:to_s).uniq
  current = Array(user.roles).map(&:to_s)
  current_assignable = current.select { |name| assignable.include?(name) }
  preserved = current.reject { |name| assignable.include?(name) }
  next_roles = (preserved + desired).uniq

  assigned = desired - current_assignable
  revoked = current_assignable - desired
  changed = assigned.any? || revoked.any?

  unless changed
    context.user = user
    context.assigned_roles = []
    context.revoked_roles = []
    context.changed = false
    return
  end

  user.roles = next_roles
  unless user.save
    return context.fail!(
      application_error: CommandTower::Errors::ValidationError.new(
        details: { roles: "could_not_save" }
      )
    )
  end

  context.user = user
  context.assigned_roles = assigned
  context.revoked_roles = revoked
  context.changed = true
end