1 class UserRolesController < ApplicationController
4 before_action :authorize_web
5 before_action :require_user
6 before_action :lookup_user
7 before_action :require_administrator
8 before_action :require_valid_role
9 before_action :not_in_role, :only => [:grant]
10 before_action :in_role, :only => [:revoke]
13 @user.roles.create(:role => @role, :granter => current_user)
14 redirect_to user_path(@user)
18 # checks that administrator role is not revoked from current user
19 if current_user == @user && @role == "administrator"
20 flash[:error] = t("user_role.filter.not_revoke_admin_current_user")
22 UserRole.where(:user_id => @user.id, :role => @role).delete_all
24 redirect_to user_path(@user)
30 # require that the user is an administrator, or fill out a helpful error message
31 # and return them to theuser page.
32 def require_administrator
33 unless current_user.administrator?
34 flash[:error] = t "user_role.filter.not_an_administrator"
35 redirect_to user_path(@user)
40 # require that the given role is valid. the role is a URL
41 # parameter, so should always be present.
42 def require_valid_role
44 unless UserRole::ALL_ROLES.include?(@role)
45 flash[:error] = t("user_role.filter.not_a_role", :role => @role)
46 redirect_to user_path(@user)
51 # checks that the user doesn't already have this role
53 if @user.has_role? @role
54 flash[:error] = t("user_role.filter.already_has_role", :role => @role)
55 redirect_to user_path(@user)
60 # checks that the user already has this role
62 unless @user.has_role? @role
63 flash[:error] = t("user_role.filter.doesnt_have_role", :role => @role)
64 redirect_to user_path(@user)