1 class UserRolesController < ApplicationController
6 before_action :authorize_web
10 before_action :lookup_user
11 before_action :require_valid_role
12 before_action :not_in_role, :only => [:grant]
13 before_action :in_role, :only => [:revoke]
16 @user.roles.create(:role => @role, :granter => current_user)
17 redirect_to user_path(@user)
21 # checks that administrator role is not revoked from current user
22 if current_user == @user && @role == "administrator"
23 flash[:error] = t("user_role.filter.not_revoke_admin_current_user")
25 UserRole.where(:user_id => @user.id, :role => @role).delete_all
27 redirect_to user_path(@user)
33 # require that the given role is valid. the role is a URL
34 # parameter, so should always be present.
35 def require_valid_role
37 unless UserRole::ALL_ROLES.include?(@role)
38 flash[:error] = t("user_role.filter.not_a_role", :role => @role)
39 redirect_to user_path(@user)
44 # checks that the user doesn't already have this role
47 flash[:error] = t("user_role.filter.already_has_role", :role => @role)
48 redirect_to user_path(@user)
53 # checks that the user already has this role
55 unless @user.role? @role
56 flash[:error] = t("user_role.filter.doesnt_have_role", :role => @role)
57 redirect_to user_path(@user)