about summary refs log tree commit diff
path: root/app/controllers/admin/roles_controller.rb
blob: 3e502ccc4d3fad6f9037471b7f8c8acee55d6998 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true

module Admin
  class RolesController < BaseController
    before_action :set_role, except: [:index, :new, :create]

    def index
      authorize :user_role, :index?

      @roles = UserRole.order(position: :desc).page(params[:page])
    end

    def new
      authorize :user_role, :create?

      @role = UserRole.new
    end

    def create
      authorize :user_role, :create?

      @role = UserRole.new(resource_params)
      @role.current_account = current_account

      if @role.save
        redirect_to admin_roles_path
      else
        render :new
      end
    end

    def edit
      authorize @role, :update?
    end

    def update
      authorize @role, :update?

      @role.current_account = current_account

      if @role.update(resource_params)
        redirect_to admin_roles_path
      else
        render :edit
      end
    end

    def destroy
      authorize @role, :destroy?
      @role.destroy!
      redirect_to admin_roles_path
    end

    private

    def set_role
      @role = UserRole.find(params[:id])
    end

    def resource_params
      params.require(:user_role).permit(:name, :color, :highlighted, :position, permissions_as_keys: [])
    end
  end
end