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
25 def require_administrator
26 unless @user.administrator?
27 flash[:error] = t'user_role.filter.not_an_administrator'
28 redirect_to :controller => 'user', :action => 'view', :display_name => @this_user.display_name
33 # ensure that there is a "this_user" instance variable
35 @this_user = User.find_by_display_name(params[:display_name])
36 rescue ActiveRecord::RecordNotFound
37 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user
41 # require that the given role is valid. the role is a URL
42 # parameter, so should always be present.
43 def require_valid_role
45 unless UserRole::ALL_ROLES.include?(@role)
46 flash[:error] = t('user_role.filter.not_a_role', :role => @role)
47 redirect_to :controller => 'user', :action => 'view', :display_name => @this_user.display_name
52 # checks that the user doesn't already have this role
54 if @this_user.has_role? @role
55 flash[:error] = t('user_role.filter.already_has_role', :role => @role)
56 redirect_to :controller => 'user', :action => 'view', :display_name => @this_user.display_name
61 # checks that the user already has this role
63 unless @this_user.has_role? @role
64 flash[:error] = t('user_role.filter.doesnt_have_role', :role => @role)
65 redirect_to :controller => 'user', :action => 'view', :display_name => @this_user.display_name