]> git.openstreetmap.org Git - rails.git/blob - app/controllers/user_blocks_controller.rb
Strip illegal characters from member roles in AMF uploads, part 2
[rails.git] / app / controllers / user_blocks_controller.rb
1 class UserBlocksController < ApplicationController
2   layout 'site'
3
4   before_filter :authorize_web
5   before_filter :set_locale
6   before_filter :require_user, :only => [:new, :create, :edit, :update, :revoke]
7   before_filter :require_moderator, :only => [:new, :create, :edit, :update, :revoke]
8   before_filter :lookup_this_user, :only => [:new, :create, :blocks_on, :blocks_by]
9   before_filter :lookup_user_block, :only => [:show, :edit, :update, :revoke]
10   before_filter :require_valid_params, :only => [:create, :update]
11   before_filter :check_database_readable
12   before_filter :check_database_writable, :only => [:create, :update, :revoke]
13
14   def index
15     @user_blocks_pages, @user_blocks = paginate(:user_blocks,
16                                                 :include => [:user, :creator, :revoker],
17                                                 :order => "user_blocks.ends_at DESC",
18                                                 :per_page => 20)
19   end
20
21   def show
22     if @user and @user.id == @user_block.user_id
23       @user_block.needs_view = false
24       @user_block.save!
25     end
26   end
27
28   def new
29     @user_block = UserBlock.new
30   end
31
32   def edit
33     params[:user_block_period] = ((@user_block.ends_at - Time.now.getutc) / 1.hour).ceil.to_s
34   end
35
36   def create
37     if @valid_params 
38       @user_block = UserBlock.new(
39         :user_id => @this_user.id,
40         :creator_id => @user.id,
41         :reason => params[:user_block][:reason],
42         :ends_at => Time.now.getutc() + @block_period.hours,
43         :needs_view => params[:user_block][:needs_view]
44       )
45     
46       if @user_block.save
47         flash[:notice] = t('user_block.create.flash', :name => @this_user.display_name)
48         redirect_to @user_block
49       else
50         render :action => "new"
51       end
52     else
53       redirect_to new_user_block_path(:display_name => params[:display_name])
54     end
55   end
56
57   def update  
58     if @valid_params 
59       if @user_block.creator_id != @user.id
60         flash[:error] = t('user_block.update.only_creator_can_edit')
61         redirect_to :action => "edit"
62       elsif @user_block.update_attributes(
63               :ends_at => Time.now.getutc() + @block_period.hours,
64               :reason => params[:user_block][:reason],
65               :needs_view => params[:user_block][:needs_view]
66             )
67         flash[:notice] = t('user_block.update.success')
68         redirect_to(@user_block)
69       else
70         render :action => "edit"
71       end
72     else
73       redirect_to edit_user_block_path(:id => params[:id])
74     end
75   end
76
77   ##
78   # revokes the block, setting the end_time to now
79   def revoke
80     if params[:confirm]
81       if @user_block.revoke! @user
82         flash[:notice] = t'user_block.revoke.flash'
83         redirect_to(@user_block)
84       end
85     end
86   end
87
88   ##
89   # shows a list of all the blocks on the given user
90   def blocks_on
91     @user_blocks_pages, @user_blocks = paginate(:user_blocks,
92                                                 :include => [:user, :creator, :revoker],
93                                                 :conditions => {:user_id => @this_user.id},
94                                                 :order => "user_blocks.ends_at DESC",
95                                                 :per_page => 20)
96   end
97
98   ##
99   # shows a list of all the blocks by the given user.
100   def blocks_by
101     @user_blocks_pages, @user_blocks = paginate(:user_blocks,
102                                                 :include => [:user, :creator, :revoker],
103                                                 :conditions => {:creator_id => @this_user.id},
104                                                 :order => "user_blocks.ends_at DESC",
105                                                 :per_page => 20)
106   end
107
108   private
109   ##
110   # ensure that there is a "user_block" instance variable
111   def lookup_user_block
112     @user_block = UserBlock.find(params[:id])
113   rescue ActiveRecord::RecordNotFound
114     render :action => "not_found", :status => :not_found
115   end
116
117   ##
118   # check that the input parameters are valid, setting an instance
119   # variable if not. note that this doesn't do any redirection, as it's
120   # called before two different actions, each of which should redirect
121   # to a different place.
122   def require_valid_params
123     @block_period = params[:user_block_period].to_i
124     @valid_params = false
125
126     if !UserBlock::PERIODS.include?(@block_period)
127       flash[:error] = t('user_block.filter.block_period')
128       
129     elsif @user_block and !@user_block.active?
130       flash[:error] = t('user_block.filter.block_expired')
131       
132     else
133       @valid_params = true
134     end
135   end
136
137 end