Class: Securial::RoleAssignmentsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/securial/role_assignments_controller.rb

Overview

RoleAssignmentsController

Controller for managing role assignments in the Securial authorization system.

This controller handles role management operations including:

- Assigning roles to users
- Removing roles from users

All operations require admin authentication and are typically used for user permission management within the application.

Routes typically mounted at Securial/admins/role_assignments/* in the host application.

Instance Method Summary collapse

Methods inherited from ApplicationController

#render_400, #render_404

Instance Method Details

#createvoid

This method returns an undefined value.

Assigns a role to a user.

Creates a new role assignment between the specified user and role. Validates that the assignment doesn’t already exist.

Parameters:

  • params[:user_id] (Integer)

    The ID of the user to assign the role to

  • params[:role_id] (Integer)

    The ID of the role to be assigned



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/securial/role_assignments_controller.rb', line 25

def create
  return unless define_user_and_role

  if @securial_user.roles.exists?(@securial_role.id)
    render json: {
      errors: ["Role already assigned to user"],
      instructions: "Please check the user's current roles before assigning a new one.",
      }, status: :unprocessable_entity
    return
  end
  @securial_role_assignment = RoleAssignment.new(securial_role_assignment_params)
  @securial_role_assignment.save
  render :show, status: :created
end

#define_user_and_roleBoolean (private)

Looks up and validates the existence of both the user and role.

Sets @securial_user and @securial_role instance variables if both exist. Renders error responses if either cannot be found.

Returns:

  • (Boolean)

    true if both user and role were found, false otherwise



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/controllers/securial/role_assignments_controller.rb', line 70

def define_user_and_role
  @securial_user = User.find_by(id: params.expect(securial_role_assignment: [:user_id]).dig(:user_id))
  @securial_role = Role.find_by(id: params.expect(securial_role_assignment: [:role_id]).dig(:role_id))
  if @securial_user.nil?
    render json: {
      errors: ["User not found"],
      instructions: "Please check the user ID and try again.",
      }, status: :unprocessable_entity
    return false
  end
  if @securial_role.nil?
    render json: {
      errors: ["Role not found"],
      instructions: "Please check the role ID and try again.",
    }, status: :unprocessable_entity
    return false
  end

  true
end

#destroyvoid

This method returns an undefined value.

Removes a role from a user.

Deletes an existing role assignment between the specified user and role. Validates that the assignment exists before attempting deletion.

Parameters:

  • params[:user_id] (Integer)

    The ID of the user to remove the role from

  • params[:role_id] (Integer)

    The ID of the role to be removed



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/securial/role_assignments_controller.rb', line 48

def destroy
  return unless define_user_and_role
  @role_assignment = RoleAssignment.find_by(securial_role_assignment_params)
  if @role_assignment
    @role_assignment.destroy!
    render :show, status: :ok
  else
    render json: {
      errors: ["Role is not assigned to user"],
      instructions: "Please check the user's current roles before attempting to remove a role.",
      }, status: :unprocessable_entity
  end
end

#securial_role_assignment_paramsActionController::Parameters (private)

Permits and extracts role assignment parameters from the request.

Returns:

  • (ActionController::Parameters)

    Permitted role assignment parameters



94
95
96
# File 'app/controllers/securial/role_assignments_controller.rb', line 94

def securial_role_assignment_params
  params.expect(securial_role_assignment: [:user_id, :role_id])
end