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
29 @user_block.deactivates_at = [@user_block.ends_at, Time.now.utc].max
35 @user_block = UserBlock.new
39 params[:user_block_period] = ((@user_block.ends_at - Time.now.utc) / 1.hour).ceil.to_s
45 @user_block = UserBlock.new(
47 :creator => current_user,
48 :reason => params[:user_block][:reason],
50 :ends_at => now + @block_period.hours,
51 :needs_view => params[:user_block][:needs_view]
53 @user_block.deactivates_at = @user_block.ends_at unless @user_block.needs_view
56 flash[:notice] = t(".flash", :name => @user.display_name)
57 redirect_to @user_block
59 render :action => "new"
62 redirect_to new_user_block_path(:display_name => params[:display_name])
68 if current_user != @user_block.creator &&
69 current_user != @user_block.revoker
70 flash[:error] = t(@user_block.revoker ? ".only_creator_or_revoker_can_edit" : ".only_creator_can_edit")
71 redirect_to :action => "edit"
73 user_block_was_active = @user_block.active?
74 @user_block.reason = params[:user_block][:reason]
75 @user_block.needs_view = params[:user_block][:needs_view]
76 @user_block.ends_at = Time.now.utc + @block_period.hours
77 @user_block.deactivates_at = (@user_block.ends_at unless @user_block.needs_view)
78 @user_block.revoker = current_user if user_block_was_active && !@user_block.active?
79 if !user_block_was_active && @user_block.active?
80 flash.now[:error] = t(".inactive_block_cannot_be_reactivated")
81 render :action => "edit"
83 unless user_block_was_active
84 @user_block.ends_at = @user_block.ends_at_was
85 @user_block.deactivates_at = @user_block.deactivates_at_was
86 @user_block.deactivates_at = [@user_block.ends_at, @user_block.updated_at].max unless @user_block.deactivates_at # take updated_at into account before deactivates_at is backfilled
89 flash[:notice] = t(".success")
90 redirect_to @user_block
92 render :action => "edit"
97 redirect_to edit_user_block_path(:id => params[:id])
102 # revokes the block, setting the end_time to now
104 if request.post? && params[:confirm] && @user_block.revoke!(current_user)
105 flash[:notice] = t ".flash"
106 redirect_to(@user_block)
111 # revokes all active blocks
113 if request.post? && params[:confirm]
114 @user.blocks.active.each { |block| block.revoke!(current_user) }
115 flash[:notice] = t ".flash"
116 redirect_to user_blocks_on_path(@user)
121 # shows a list of all the blocks on the given user
123 @params = params.permit(:display_name)
125 user_blocks = UserBlock.where(:user => @user)
127 @user_blocks, @newer_user_blocks_id, @older_user_blocks_id = get_page_items(user_blocks, :includes => [:user, :creator, :revoker])
131 # shows a list of all the blocks by the given user.
133 @params = params.permit(:display_name)
135 user_blocks = UserBlock.where(:creator => @user)
137 @user_blocks, @newer_user_blocks_id, @older_user_blocks_id = get_page_items(user_blocks, :includes => [:user, :creator, :revoker])
143 # ensure that there is a "user_block" instance variable
144 def lookup_user_block
145 @user_block = UserBlock.find(params[:id])
146 rescue ActiveRecord::RecordNotFound
147 render :action => "not_found", :status => :not_found
151 # check that the input parameters are valid, setting an instance
152 # variable if not. note that this doesn't do any redirection, as it's
153 # called before two different actions, each of which should redirect
154 # to a different place.
155 def require_valid_params
156 @block_period = params[:user_block_period].to_i
157 @valid_params = false
159 if UserBlock::PERIODS.exclude?(@block_period)
160 flash[:error] = t("user_blocks.filter.block_period")