1 class UserBlocksController < ApplicationController
6 before_action :authorize_web
7 before_action :set_locale
11 before_action :lookup_user, :only => [:new, :create, :revoke_all, :blocks_on, :blocks_by]
12 before_action :lookup_user_block, :only => [:show, :edit, :update, :revoke]
13 before_action :require_valid_params, :only => [:create, :update]
14 before_action :check_database_readable
15 before_action :check_database_writable, :only => [:create, :update, :revoke]
18 @params = params.permit
19 @user_blocks_pages, @user_blocks = paginate(:user_blocks,
20 :include => [:user, :creator, :revoker],
21 :order => "user_blocks.ends_at DESC",
26 if current_user && current_user == @user_block.user
27 @user_block.needs_view = false
33 @user_block = UserBlock.new
37 params[:user_block_period] = ((@user_block.ends_at - Time.now.utc) / 1.hour).ceil.to_s
43 @user_block = UserBlock.new(
45 :creator => current_user,
46 :reason => params[:user_block][:reason],
48 :ends_at => now + @block_period.hours,
49 :needs_view => params[:user_block][:needs_view]
53 flash[:notice] = t(".flash", :name => @user.display_name)
54 redirect_to @user_block
56 render :action => "new"
59 redirect_to new_user_block_path(:display_name => params[:display_name])
65 if @user_block.creator != current_user
66 flash[:error] = t(".only_creator_can_edit")
67 redirect_to :action => "edit"
68 elsif @user_block.update(
69 :ends_at => Time.now.utc + @block_period.hours,
70 :reason => params[:user_block][:reason],
71 :needs_view => params[:user_block][:needs_view]
73 flash[:notice] = t(".success")
74 redirect_to(@user_block)
76 render :action => "edit"
79 redirect_to edit_user_block_path(:id => params[:id])
84 # revokes the block, setting the end_time to now
86 if request.post? && params[:confirm] && @user_block.revoke!(current_user)
87 flash[:notice] = t ".flash"
88 redirect_to(@user_block)
93 # revokes all active blocks
99 # shows a list of all the blocks on the given user
101 @params = params.permit(:display_name)
102 @user_blocks_pages, @user_blocks = paginate(:user_blocks,
103 :include => [:user, :creator, :revoker],
104 :conditions => { :user_id => @user.id },
105 :order => "user_blocks.ends_at DESC",
110 # shows a list of all the blocks by the given user.
112 @params = params.permit(:display_name)
113 @user_blocks_pages, @user_blocks = paginate(:user_blocks,
114 :include => [:user, :creator, :revoker],
115 :conditions => { :creator_id => @user.id },
116 :order => "user_blocks.ends_at DESC",
123 # ensure that there is a "user_block" instance variable
124 def lookup_user_block
125 @user_block = UserBlock.find(params[:id])
126 rescue ActiveRecord::RecordNotFound
127 render :action => "not_found", :status => :not_found
131 # check that the input parameters are valid, setting an instance
132 # variable if not. note that this doesn't do any redirection, as it's
133 # called before two different actions, each of which should redirect
134 # to a different place.
135 def require_valid_params
136 @block_period = params[:user_block_period].to_i
137 @valid_params = false
139 if UserBlock::PERIODS.exclude?(@block_period)
140 flash[:error] = t("user_blocks.filter.block_period")
142 elsif @user_block && !@user_block.active?
143 flash[:error] = t("user_blocks.filter.block_expired")