1 class UserBlocksController < ApplicationController
3 include PaginationMethods
7 before_action :authorize_web
8 before_action :set_locale
12 before_action :lookup_user, :only => [:new, :create, :revoke_all, :blocks_on, :blocks_by]
13 before_action :lookup_user_block, :only => [:show, :edit, :update, :revoke]
14 before_action :require_valid_params, :only => [:create, :update]
15 before_action :check_database_readable
16 before_action :check_database_writable, :only => [:create, :update, :revoke, :revoke_all]
19 @params = params.permit
21 user_blocks = UserBlock.all
23 @user_blocks, @newer_user_blocks_id, @older_user_blocks_id = get_page_items(user_blocks, :includes => [:user, :creator, :revoker])
27 if current_user && current_user == @user_block.user
28 @user_block.needs_view = false
34 @user_block = UserBlock.new
38 params[:user_block_period] = ((@user_block.ends_at - Time.now.utc) / 1.hour).ceil.to_s
44 @user_block = UserBlock.new(
46 :creator => current_user,
47 :reason => params[:user_block][:reason],
49 :ends_at => now + @block_period.hours,
50 :needs_view => params[:user_block][:needs_view]
54 flash[:notice] = t(".flash", :name => @user.display_name)
55 redirect_to @user_block
57 render :action => "new"
60 redirect_to new_user_block_path(:display_name => params[:display_name])
66 if current_user != @user_block.creator &&
67 current_user != @user_block.revoker
68 flash[:error] = t(@user_block.revoker ? ".only_creator_or_revoker_can_edit" : ".only_creator_can_edit")
69 redirect_to :action => "edit"
70 elsif @user_block.update(
71 :ends_at => Time.now.utc + @block_period.hours,
72 :reason => params[:user_block][:reason],
73 :needs_view => params[:user_block][:needs_view]
75 flash[:notice] = t(".success")
76 redirect_to(@user_block)
78 render :action => "edit"
81 redirect_to edit_user_block_path(:id => params[:id])
86 # revokes the block, setting the end_time to now
88 if request.post? && params[:confirm] && @user_block.revoke!(current_user)
89 flash[:notice] = t ".flash"
90 redirect_to(@user_block)
95 # revokes all active blocks
97 if request.post? && params[:confirm]
98 @user.blocks.active.each { |block| block.revoke!(current_user) }
99 flash[:notice] = t ".flash"
100 redirect_to user_blocks_on_path(@user)
105 # shows a list of all the blocks on the given user
107 @params = params.permit(:display_name)
109 user_blocks = UserBlock.where(:user => @user)
111 @user_blocks, @newer_user_blocks_id, @older_user_blocks_id = get_page_items(user_blocks, :includes => [:user, :creator, :revoker])
115 # shows a list of all the blocks by the given user.
117 @params = params.permit(:display_name)
119 user_blocks = UserBlock.where(:creator => @user)
121 @user_blocks, @newer_user_blocks_id, @older_user_blocks_id = get_page_items(user_blocks, :includes => [:user, :creator, :revoker])
127 # ensure that there is a "user_block" instance variable
128 def lookup_user_block
129 @user_block = UserBlock.find(params[:id])
130 rescue ActiveRecord::RecordNotFound
131 render :action => "not_found", :status => :not_found
135 # check that the input parameters are valid, setting an instance
136 # variable if not. note that this doesn't do any redirection, as it's
137 # called before two different actions, each of which should redirect
138 # to a different place.
139 def require_valid_params
140 @block_period = params[:user_block_period].to_i
141 @valid_params = false
143 if UserBlock::PERIODS.exclude?(@block_period)
144 flash[:error] = t("user_blocks.filter.block_period")