]> git.openstreetmap.org Git - rails.git/blob - app/controllers/user_roles_controller.rb
Merge pull request #6394 from openstreetmap/dependabot/github_actions/ruby/setup...
[rails.git] / app / controllers / user_roles_controller.rb
1 # frozen_string_literal: true
2
3 class UserRolesController < ApplicationController
4   include UserMethods
5
6   layout :site_layout
7
8   before_action :authorize_web
9
10   authorize_resource
11
12   before_action :lookup_user
13   before_action :require_valid_role
14   before_action :not_in_role, :only => :create
15   before_action :in_role, :only => :destroy
16
17   def create
18     @user.roles.create(:role => @role, :granter => current_user)
19     redirect_to user_path(@user)
20   end
21
22   def destroy
23     # checks that administrator role is not revoked from current user
24     if current_user == @user && @role == "administrator"
25       flash[:error] = t("user_role.filter.not_revoke_admin_current_user")
26     else
27       UserRole.where(:user => @user, :role => @role).delete_all
28     end
29     redirect_to user_path(@user)
30   end
31
32   private
33
34   ##
35   # require that the given role is valid. the role is a URL
36   # parameter, so should always be present.
37   def require_valid_role
38     @role = params[:role]
39     unless UserRole::ALL_ROLES.include?(@role)
40       flash[:error] = t("user_role.filter.not_a_role", :role => @role)
41       redirect_to user_path(@user)
42     end
43   end
44
45   ##
46   # checks that the user doesn't already have this role
47   def not_in_role
48     if @user.role? @role
49       flash[:error] = t("user_role.filter.already_has_role", :role => @role)
50       redirect_to user_path(@user)
51     end
52   end
53
54   ##
55   # checks that the user already has this role
56   def in_role
57     unless @user.role? @role
58       flash[:error] = t("user_role.filter.doesnt_have_role", :role => @role)
59       redirect_to user_path(@user)
60     end
61   end
62 end