1 class UserBlocksController < ApplicationController
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 => [:create, :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]
15 @user_blocks_pages, @user_blocks = paginate(:user_blocks,
16 :include => [:user, :creator, :revoker],
17 :order => "user_blocks.ends_at DESC",
22 if @user and @user.id == @user_block.user_id
23 @user_block.needs_view = false
29 @user_block = UserBlock.new
33 params[:user_block_period] = ((@user_block.ends_at - Time.now.getutc) / 1.hour).ceil.to_s
38 redirect_to :action => "new"
42 @user_block = UserBlock.new(:user_id => @this_user.id,
43 :creator_id => @user.id,
44 :reason => params[:user_block][:reason],
45 :ends_at => Time.now.getutc() + @block_period.hours,
46 :needs_view => params[:user_block][:needs_view])
49 flash[:notice] = t('user_block.create.flash', :name => @this_user.display_name)
50 redirect_to @user_block
52 render :action => "new"
58 redirect_to :action => "edit"
62 if @user_block.creator_id != @user.id
63 flash[:error] = t('user_block.update.only_creator_can_edit')
64 redirect_to :action => "edit"
68 if @user_block.update_attributes({ :ends_at => Time.now.getutc() + @block_period.hours,
69 :reason => params[:user_block][:reason],
70 :needs_view => params[:user_block][:needs_view] })
71 flash[:notice] = t('user_block.update.success')
72 redirect_to(@user_block)
74 render :action => "edit"
79 # revokes the block, setting the end_time to now
82 if @user_block.revoke! @user
83 flash[:notice] = t'user_block.revoke.flash'
84 redirect_to(@user_block)
90 # shows a list of all the blocks on the given user
92 @user_blocks_pages, @user_blocks = paginate(:user_blocks,
93 :include => [:user, :creator, :revoker],
94 :conditions => {:user_id => @this_user.id},
95 :order => "user_blocks.ends_at DESC",
100 # shows a list of all the blocks by the given user.
102 @user_blocks_pages, @user_blocks = paginate(:user_blocks,
103 :include => [:user, :creator, :revoker],
104 :conditions => {:creator_id => @this_user.id},
105 :order => "user_blocks.ends_at DESC",
111 # require that the user is a moderator, or fill out a helpful error message
112 # and return them to the blocks index.
113 def require_moderator
114 unless @user.moderator?
115 flash[:error] = t('user_block.filter.not_a_moderator')
116 redirect_to :action => 'index'
121 # ensure that there is a "this_user" instance variable
123 @this_user = User.find_by_display_name(params[:display_name])
124 rescue ActiveRecord::RecordNotFound
125 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user
129 # ensure that there is a "user_block" instance variable
130 def lookup_user_block
131 @user_block = UserBlock.find(params[:id])
132 rescue ActiveRecord::RecordNotFound
133 render :action => "not_found", :status => :not_found
137 # check that the input parameters are valid, setting an instance
138 # variable if not. note that this doesn't do any redirection, as it's
139 # called before two different actions, each of which should redirect
140 # to a different place.
141 def require_valid_params
142 @block_period = params[:user_block_period].to_i
143 @valid_params = false
145 if !UserBlock::PERIODS.include?(@block_period)
146 flash[:error] = t('user_block.filter.block_period')
148 elsif @user_block and !@user_block.active?
149 flash[:error] = t('user_block.filter.block_expired')