1 class UserRolesController < ApplicationController
4 before_filter :authorize_web
5 before_filter :require_user
6 before_filter :lookup_this_user
7 before_filter :require_administrator
8 before_filter :require_valid_role
9 before_filter :not_in_role, :only => [:grant]
10 before_filter :in_role, :only => [:revoke]
13 @this_user.roles.create({
14 :role => @role, :granter_id => @user.id
15 }, :without_protection => true)
16 redirect_to :controller => 'user', :action => 'view', :display_name => @this_user.display_name
20 UserRole.delete_all({:user_id => @this_user.id, :role => @role})
21 redirect_to :controller => 'user', :action => 'view', :display_name => @this_user.display_name
26 # require that the user is an administrator, or fill out a helpful error message
27 # and return them to theuser page.
28 def require_administrator
29 unless @user.administrator?
30 flash[:error] = t'user_role.filter.not_an_administrator'
31 redirect_to :controller => 'user', :action => 'view', :display_name => @this_user.display_name
36 # ensure that there is a "this_user" instance variable
38 unless @this_user = User.find_by_display_name(params[:display_name])
39 render_unknown_user params[:display_name]
44 # require that the given role is valid. the role is a URL
45 # parameter, so should always be present.
46 def require_valid_role
48 unless UserRole::ALL_ROLES.include?(@role)
49 flash[:error] = t('user_role.filter.not_a_role', :role => @role)
50 redirect_to :controller => 'user', :action => 'view', :display_name => @this_user.display_name
55 # checks that the user doesn't already have this role
57 if @this_user.has_role? @role
58 flash[:error] = t('user_role.filter.already_has_role', :role => @role)
59 redirect_to :controller => 'user', :action => 'view', :display_name => @this_user.display_name
64 # checks that the user already has this role
66 unless @this_user.has_role? @role
67 flash[:error] = t('user_role.filter.doesnt_have_role', :role => @role)
68 redirect_to :controller => 'user', :action => 'view', :display_name => @this_user.display_name