]> git.openstreetmap.org Git - rails.git/blob - app/controllers/user_roles_controller.rb
Allow existing users to voluntarily sign up for the contributor terms
[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(:role => @role, :granter_id => @user.id)
15     redirect_to :controller => 'user', :action => 'view', :display_name => @this_user.display_name
16   end
17
18   def revoke
19     UserRole.delete_all({:user_id => @this_user.id, :role => @role})
20     redirect_to :controller => 'user', :action => 'view', :display_name => @this_user.display_name
21   end
22
23   private
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
28     end
29   end
30
31   ##
32   # ensure that there is a "this_user" instance variable
33   def lookup_this_user
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
37   end
38
39   ##
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.
44   def setup_nonce
45     if params[:nonce] and params[:nonce] == session[:nonce]
46       @nonce = params[:nonce]
47       yield
48     else
49       @nonce = OAuth::Helper.generate_nonce
50       session[:nonce] = @nonce
51       render
52     end
53   end    
54
55   ##
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
59     @role = params[: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
63     end
64   end
65
66   ##
67   # checks that the user doesn't already have this role
68   def not_in_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
72     end
73   end
74
75   ##
76   # checks that the user already has this role
77   def in_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
81     end
82   end
83 end