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({
43 :user_id => @this_user.id,
44 :creator_id => @user.id,
45 :reason => params[:user_block][:reason],
46 :ends_at => Time.now.getutc() + @block_period.hours,
47 :needs_view => params[:user_block][:needs_view]
48 }, :without_protection => true)
51 flash[:notice] = t('user_block.create.flash', :name => @this_user.display_name)
52 redirect_to @user_block
54 render :action => "new"
60 redirect_to :action => "edit"
64 if @user_block.creator_id != @user.id
65 flash[:error] = t('user_block.update.only_creator_can_edit')
66 redirect_to :action => "edit"
70 if @user_block.update_attributes({ :ends_at => Time.now.getutc() + @block_period.hours,
71 :reason => params[:user_block][:reason],
72 :needs_view => params[:user_block][:needs_view] }, :without_protection => true)
73 flash[:notice] = t('user_block.update.success')
74 redirect_to(@user_block)
76 render :action => "edit"
81 # revokes the block, setting the end_time to now
84 if @user_block.revoke! @user
85 flash[:notice] = t'user_block.revoke.flash'
86 redirect_to(@user_block)
92 # shows a list of all the blocks on the given user
94 @user_blocks_pages, @user_blocks = paginate(:user_blocks,
95 :include => [:user, :creator, :revoker],
96 :conditions => {:user_id => @this_user.id},
97 :order => "user_blocks.ends_at DESC",
102 # shows a list of all the blocks by the given user.
104 @user_blocks_pages, @user_blocks = paginate(:user_blocks,
105 :include => [:user, :creator, :revoker],
106 :conditions => {:creator_id => @this_user.id},
107 :order => "user_blocks.ends_at DESC",
113 # require that the user is a moderator, or fill out a helpful error message
114 # and return them to the blocks index.
115 def require_moderator
116 unless @user.moderator?
117 flash[:error] = t('user_block.filter.not_a_moderator')
118 redirect_to :action => 'index'
123 # ensure that there is a "this_user" instance variable
125 @this_user = User.find_by_display_name(params[:display_name])
126 rescue ActiveRecord::RecordNotFound
127 redirect_to :controller => 'user', :action => 'view', :display_name => params[:display_name] unless @this_user
131 # ensure that there is a "user_block" instance variable
132 def lookup_user_block
133 @user_block = UserBlock.find(params[:id])
134 rescue ActiveRecord::RecordNotFound
135 render :action => "not_found", :status => :not_found
139 # check that the input parameters are valid, setting an instance
140 # variable if not. note that this doesn't do any redirection, as it's
141 # called before two different actions, each of which should redirect
142 # to a different place.
143 def require_valid_params
144 @block_period = params[:user_block_period].to_i
145 @valid_params = false
147 if !UserBlock::PERIODS.include?(@block_period)
148 flash[:error] = t('user_block.filter.block_period')
150 elsif @user_block and !@user_block.active?
151 flash[:error] = t('user_block.filter.block_expired')