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[:notice] = 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 login screen where they might be able to login as
114 def require_moderator
115 unless @user.moderator?
116 flash[:notice] = t('user_block.filter.not_a_moderator')
117 redirect_to :action => 'index'
122 # ensure that there is a "this_user" instance variable
124 @this_user = User.find_by_display_name(params[:display_name])
125 rescue ActiveRecord::RecordNotFound
126 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user
130 # ensure that there is a "user_block" instance variable
131 def lookup_user_block
132 @user_block = UserBlock.find(params[:id])
133 rescue ActiveRecord::RecordNotFound
134 render :action => "not_found", :status => :not_found
138 # check that the input parameters are valid, setting an instance
139 # variable if not. note that this doesn't do any redirection, as it's
140 # called before two different actions, each of which should redirect
141 # to a different place.
142 def require_valid_params
143 @block_period = params[:user_block_period].to_i
144 @valid_params = false
146 if !UserBlock::PERIODS.include?(@block_period)
147 flash[:notice] = t('user_block.filter.block_period')
149 elsif @user_block and !@user_block.active?
150 flash[:notice] = t('user_block.filter.block_expired')