Class: Kaui::RoleDefinitionsController

Inherits:
EngineController show all
Defined in:
app/controllers/kaui/role_definitions_controller.rb

Constant Summary

Constants included from EngineControllerUtil

EngineControllerUtil::MAXIMUM_NUMBER_OF_RECORDS_DOWNLOAD, EngineControllerUtil::SIMPLE_PAGINATION_THRESHOLD

Instance Method Summary collapse

Methods inherited from EngineController

#check_for_redirect_to_tenant_screen, #current_ability, #current_user, #options_for_klient, #populate_account_details, #retrieve_allowed_users_for_current_user, #retrieve_tenants_for_current_user

Methods included from ErrorHandler

#perform_redirect_after_error

Instance Method Details

#createObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/kaui/role_definitions_controller.rb', line 20

def create
  # Sanity is done on the server side
  @role_definition = Kaui::RoleDefinition.new(params.require(:role_definition))
  @role_definition.permissions = @role_definition.permissions.split(',')

  begin
    @role_definition = @role_definition.create(current_user.kb_username, params[:reason], params[:comment], options_for_klient)

    # Check if we need to return to user form with the new role
    return_context = session.delete(:role_return_context)
    if return_context.present?
      # Add the new role to the existing roles
      existing_roles = return_context[:roles].to_s.split(',').compact_blank
      existing_roles << @role_definition.role unless existing_roles.include?(@role_definition.role)
      return_context[:roles] = existing_roles.join(',')

      # Redirect back to user form (new or edit)
      if return_context[:allowed_user_id].present? && return_context[:allowed_user_id] != ''
        redirect_to edit_admin_allowed_user_path(return_context[:allowed_user_id], user_context: return_context),
                    notice: "Role '#{@role_definition.role}' was successfully created"
      else
        redirect_to new_admin_allowed_user_path(user_context: return_context),
                    notice: "Role '#{@role_definition.role}' was successfully created"
      end
    else
      redirect_to admin_allowed_users_path, notice: 'Role was successfully created'
    end
  rescue StandardError => e
    flash.now[:error] = "Error while creating role: #{as_string(e)}"
    render action: :new
  end
end

#newObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/controllers/kaui/role_definitions_controller.rb', line 5

def new
  @role_definition = Kaui::RoleDefinition.new

  # Store user form context if coming from user creation/edit
  return if params[:return_to_user].blank?

  session[:role_return_context] = {
    kb_username: params[:kb_username],
    description: params[:description],
    roles: params[:roles],
    external: params[:external],
    allowed_user_id: params[:allowed_user_id]
  }
end