]> git.openstreetmap.org Git - rails.git/blob - app/controllers/user_roles_controller.rb
Fix typo
[rails.git] / app / controllers / user_roles_controller.rb
1 class UserRolesController < ApplicationController
2   layout 'site'
3
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
12
13   def grant
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
18   end
19
20   def revoke
21     UserRole.delete_all({:user_id => @this_user.id, :role => @role})
22     redirect_to :controller => 'user', :action => 'view', :display_name => @this_user.display_name
23   end
24
25   private
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
30     end
31   end
32
33   ##
34   # ensure that there is a "this_user" instance variable
35   def lookup_this_user
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
39   end
40
41   ##
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.
46   def setup_nonce
47     if params[:nonce] and params[:nonce] == session[:nonce]
48       @nonce = params[:nonce]
49       yield
50     else
51       @nonce = OAuth::Helper.generate_nonce
52       session[:nonce] = @nonce
53       render
54     end
55   end    
56
57   ##
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
61     @role = params[: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
65     end
66   end
67
68   ##
69   # checks that the user doesn't already have this role
70   def not_in_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
74     end
75   end
76
77   ##
78   # checks that the user already has this role
79   def in_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
83     end
84   end
85 end