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]
11 around_filter :setup_nonce
14 @this_user.roles.create(:role => @role, :granter_id => @user.id)
15 redirect_to :controller => 'user', :action => 'view', :display_name => @this_user.display_name
19 UserRole.delete_all({:user_id => @this_user.id, :role => @role})
20 redirect_to :controller => 'user', :action => 'view', :display_name => @this_user.display_name
24 def require_administrator
25 unless @user.administrator?
26 flash[:error] = t'user_role.filter.not_an_administrator'
27 redirect_to :controller => 'user', :action => 'view', :display_name => @this_user.display_name
32 # ensure that there is a "this_user" instance variable
34 @this_user = User.find_by_display_name(params[:display_name])
35 rescue ActiveRecord::RecordNotFound
36 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user
40 # the random nonce here which isn't predictable, making an CSRF
41 # procedure much, much more difficult. setup the nonce. if the given
42 # nonce matches the session nonce then yield into the actual method.
43 # otherwise, just sets up the nonce for the form.
45 if params[:nonce] and params[:nonce] == session[:nonce]
46 @nonce = params[:nonce]
49 @nonce = OAuth::Helper.generate_nonce
50 session[:nonce] = @nonce
56 # require that the given role is valid. the role is a URL
57 # parameter, so should always be present.
58 def require_valid_role
60 unless UserRole::ALL_ROLES.include?(@role)
61 flash[:error] = t('user_role.filter.not_a_role', :role => @role)
62 redirect_to :controller => 'user', :action => 'view', :display_name => @this_user.display_name
67 # checks that the user doesn't already have this role
69 if @this_user.has_role? @role
70 flash[:error] = t('user_role.filter.already_has_role', :role => @role)
71 redirect_to :controller => 'user', :action => 'view', :display_name => @this_user.display_name
76 # checks that the user already has this role
78 unless @this_user.has_role? @role
79 flash[:error] = t('user_role.filter.doesnt_have_role', :role => @role)
80 redirect_to :controller => 'user', :action => 'view', :display_name => @this_user.display_name