Class: HrLite::Admin::RoleAssignmentsController

Inherits:
LeadershipController show all
Defined in:
app/controllers/hr_lite/admin/role_assignments_controller.rb

Overview

Putting people into a role and taking them out again. Same gate as the roles themselves: granting somebody payroll is the same act as holding it, so it sits behind role.manage.

Instance Method Summary collapse

Instance Method Details

#createObject



10
11
12
13
14
15
16
17
18
19
20
# File 'app/controllers/hr_lite/admin/role_assignments_controller.rb', line 10

def create
  role = Role.find(params[:role_id])
  user = HrLite.user_klass.find(params[:user_id])
  RoleAssignment.create!(role: role, user_id: user.id, granted_by_id: hr_current_user.id)

  redirect_to edit_admin_role_path(role),
              notice: "#{HrLite.display_name(user)} added to #{role.name}."
rescue ActiveRecord::RecordInvalid
  redirect_to edit_admin_role_path(params[:role_id]),
              alert: "They already hold that role."
end

#destroyObject



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

def destroy
  assignment = RoleAssignment.find(params[:id])
  role = assignment.role
  name = HrLite.display_name(assignment.user)

  # Locking every last person out of role management is not recoverable
  # from inside the app — it would need a console. Refuse instead.
  if last_role_manager?(assignment)
    return redirect_to edit_admin_role_path(role),
                       alert: "#{name} is the only person who can manage roles. " \
                              "Give somebody else that permission first."
  end

  assignment.destroy!
  redirect_to edit_admin_role_path(role), notice: "#{name} removed from #{role.name}.",
              status: :see_other
end