1 class UserBlocksController < ApplicationController
4 before_filter :authorize_web
5 before_filter :set_locale
6 before_filter :require_user, :only => [:new, :create, :edit, :delete]
7 before_filter :require_moderator, :only => [:new, :create, :edit, :delete]
10 @user_blocks_pages, @user_blocks = paginate(:user_blocks,
11 :include => [:user, :creator, :revoker],
12 :order => "user_blocks.ends_at DESC",
17 @user_block = UserBlock.find(params[:id])
19 if @user and @user.id == @user_block.user_id
20 @user_block.needs_view = false
26 @user_block = UserBlock.new
27 @display_name = params[:display_name]
28 @this_user = User.find_by_display_name(@display_name, :conditions => {:visible => true})
31 # GET /user_blocks/1/edit
33 @user_block = UserBlock.find(params[:id])
34 params[:user_block_period] = ((@user_block.ends_at - Time.now.getutc) / 1.hour).ceil.to_s
38 @display_name = params[:display_name]
39 @this_user = User.find_by_display_name(@display_name, :conditions => {:visible => true})
40 block_period = [UserBlock::PERIODS.max, params[:user_block_period].to_i].min
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])
48 if (@this_user and @user.moderator? and
49 params[:tried_contacting] == "yes" and
50 params[:tried_waiting] == "yes" and
53 flash[:notice] = t('user_block.create.flash', :name => @display_name)
54 redirect_to @user_block
56 render :action => "new"
60 flash[:notice] = t('user_block.create.not_a_moderator')
61 elsif params[:tried_contacting] != "yes"
62 flash[:notice] = t('user_block.create.try_contacting')
63 elsif params[:tried_waiting] != "yes"
64 flash[:notice] = t('user_block.create.try_waiting')
66 flash[:notice] = t('user_block.create.bad_parameters')
68 @display_name = @this_user.nil? ? '' : @this_user.display_name
70 render :action => "new"
75 @user_block = UserBlock.find(params[:id])
76 block_period = [72, params[:user_block_period].to_i].min
78 if @user_block.creator_id != @user.id
79 flash[:notice] = t('user_block.update.only_creator_can_edit')
80 redirect_to(@user_block)
82 elsif !@user_block.active?
83 flash[:notice] = t('user_block.update.block_expired')
84 redirect_to(@user_block)
86 elsif @user_block.update_attributes({ :ends_at => Time.now.getutc() + block_period.hours,
87 :reason => params[:user_block][:reason],
88 :needs_view => params[:user_block][:needs_view] })
89 flash[:notice] = t('user_block.update.success')
90 redirect_to(@user_block)
92 render :action => "edit"
97 # revokes the block, setting the end_time to now
99 @user_block = UserBlock.find(params[:id])
102 flash[:notice] = t('user_block.create.not_a_moderator')
103 redirect_to @user_block
105 elsif params[:confirm]
106 if @user_block.revoke!
107 flash[:notice] = t'user_block.revoke.flash'
108 redirect_to(@user_block)
110 flash[:notice] = t'user_block.revoke.error'
111 render :action => "edit"
117 # shows a list of all the blocks on the given user
119 @this_user = User.find_by_display_name(params[:display_name])
121 @user_blocks_pages, @user_blocks = paginate(:user_blocks,
122 :include => [:user, :creator, :revoker],
123 :conditions => {:user_id => @this_user.id},
124 :order => "user_blocks.ends_at DESC",
129 # shows a list of all the blocks by the given user.
131 @this_user = User.find_by_display_name(params[:display_name])
133 @user_blocks_pages, @user_blocks = paginate(:user_blocks,
134 :include => [:user, :creator, :revoker],
135 :conditions => {:creator_id => @this_user.id},
136 :order => "user_blocks.ends_at DESC",
141 def require_moderator
142 redirect_to "/403.html" unless @user.moderator?