1 class UserRolesController < ApplicationController
4 before_action :authorize_web
8 before_action :lookup_user
9 before_action :require_valid_role
10 before_action :not_in_role, :only => [:grant]
11 before_action :in_role, :only => [:revoke]
14 @user.roles.create(:role => @role, :granter => current_user)
15 redirect_to user_path(@user)
19 # checks that administrator role is not revoked from current user
20 if current_user == @user && @role == "administrator"
21 flash[:error] = t("user_role.filter.not_revoke_admin_current_user")
23 UserRole.where(:user_id => @user.id, :role => @role).delete_all
25 redirect_to user_path(@user)
31 # require that the given role is valid. the role is a URL
32 # parameter, so should always be present.
33 def require_valid_role
35 unless UserRole::ALL_ROLES.include?(@role)
36 flash[:error] = t("user_role.filter.not_a_role", :role => @role)
37 redirect_to user_path(@user)
42 # checks that the user doesn't already have this role
45 flash[:error] = t("user_role.filter.already_has_role", :role => @role)
46 redirect_to user_path(@user)
51 # checks that the user already has this role
53 unless @user.role? @role
54 flash[:error] = t("user_role.filter.doesnt_have_role", :role => @role)
55 redirect_to user_path(@user)