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({
15 :role => @role, :granter_id => @user.id
16 }, :without_protection => true)
17 redirect_to :controller => 'user', :action => 'view', :display_name => @this_user.display_name
21 UserRole.delete_all({:user_id => @this_user.id, :role => @role})
22 redirect_to :controller => 'user', :action => 'view', :display_name => @this_user.display_name
26 def require_administrator
27 unless @user.administrator?
28 flash[:error] = t'user_role.filter.not_an_administrator'
29 redirect_to :controller => 'user', :action => 'view', :display_name => @this_user.display_name
34 # ensure that there is a "this_user" instance variable
36 @this_user = User.find_by_display_name(params[:display_name])
37 rescue ActiveRecord::RecordNotFound
38 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user
42 # the random nonce here which isn't predictable, making an CSRF
43 # procedure much, much more difficult. setup the nonce. if the given
44 # nonce matches the session nonce then yield into the actual method.
45 # otherwise, just sets up the nonce for the form.
47 if params[:nonce] and params[:nonce] == session[:nonce]
48 @nonce = params[:nonce]
51 @nonce = OAuth::Helper.generate_nonce
52 session[:nonce] = @nonce
58 # require that the given role is valid. the role is a URL
59 # parameter, so should always be present.
60 def require_valid_role
62 unless UserRole::ALL_ROLES.include?(@role)
63 flash[:error] = t('user_role.filter.not_a_role', :role => @role)
64 redirect_to :controller => 'user', :action => 'view', :display_name => @this_user.display_name
69 # checks that the user doesn't already have this role
71 if @this_user.has_role? @role
72 flash[:error] = t('user_role.filter.already_has_role', :role => @role)
73 redirect_to :controller => 'user', :action => 'view', :display_name => @this_user.display_name
78 # checks that the user already has this role
80 unless @this_user.has_role? @role
81 flash[:error] = t('user_role.filter.doesnt_have_role', :role => @role)
82 redirect_to :controller => 'user', :action => 'view', :display_name => @this_user.display_name