Class: Securial::RoleAssignmentsController
- Inherits:
-
ApplicationController
- Object
- ActionController::API
- ApplicationController
- Securial::RoleAssignmentsController
- 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
-
#create ⇒ void
Assigns a role to a user.
-
#define_user_and_role ⇒ Boolean
private
Looks up and validates the existence of both the user and role.
-
#destroy ⇒ void
Removes a role from a user.
-
#securial_role_assignment_params ⇒ ActionController::Parameters
private
Permits and extracts role assignment parameters from the request.
Methods inherited from ApplicationController
Instance Method Details
#create ⇒ void
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.
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_role ⇒ Boolean (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.
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 |
#destroy ⇒ void
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.
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_params ⇒ ActionController::Parameters (private)
Permits and extracts role assignment parameters from the request.
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 |