]> git.openstreetmap.org Git - rails.git/blob - app/controllers/user_blocks_controller.rb
Merge remote-tracking branch 'upstream/pull/5107'
[rails.git] / app / controllers / user_blocks_controller.rb
1 class UserBlocksController < ApplicationController
2   include UserMethods
3   include PaginationMethods
4
5   layout "site"
6
7   before_action :authorize_web
8   before_action :set_locale
9
10   authorize_resource
11
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]
17
18   def index
19     @params = params.permit
20
21     user_blocks = UserBlock.all
22
23     @user_blocks, @newer_user_blocks_id, @older_user_blocks_id = get_page_items(user_blocks, :includes => [:user, :creator, :revoker])
24
25     @show_user_name = true
26     @show_creator_name = true
27
28     render :partial => "blocks" if turbo_frame_request_id == "pagination"
29   end
30
31   def show
32     if current_user && current_user == @user_block.user
33       @user_block.needs_view = false
34       @user_block.deactivates_at = [@user_block.ends_at, Time.now.utc].max
35       @user_block.save!
36     end
37   end
38
39   def new
40     @user_block = UserBlock.new
41   end
42
43   def edit
44     params[:user_block_period] = ((@user_block.ends_at - Time.now.utc) / 1.hour).ceil.to_s
45   end
46
47   def create
48     if @valid_params
49       now = Time.now.utc
50       @user_block = UserBlock.new(
51         :user => @user,
52         :creator => current_user,
53         :reason => params[:user_block][:reason],
54         :created_at => now,
55         :ends_at => now + @block_period.hours,
56         :needs_view => params[:user_block][:needs_view]
57       )
58       @user_block.deactivates_at = @user_block.ends_at unless @user_block.needs_view
59
60       if @user_block.save
61         flash[:notice] = t(".flash", :name => @user.display_name)
62         redirect_to @user_block
63       else
64         render :action => "new"
65       end
66     else
67       redirect_to new_user_block_path(:display_name => params[:display_name])
68     end
69   end
70
71   def update
72     if @valid_params
73       if cannot?(:update, @user_block)
74         flash[:error] = t(@user_block.revoker ? ".only_creator_or_revoker_can_edit" : ".only_creator_can_edit")
75         redirect_to :action => "edit"
76       else
77         user_block_was_active = @user_block.active?
78         @user_block.reason = params[:user_block][:reason]
79         @user_block.needs_view = params[:user_block][:needs_view]
80         @user_block.ends_at = Time.now.utc + @block_period.hours
81         @user_block.deactivates_at = (@user_block.ends_at unless @user_block.needs_view)
82         @user_block.revoker = current_user if user_block_was_active && !@user_block.active?
83         if user_block_was_active && @user_block.active? && current_user != @user_block.creator
84           flash.now[:error] = t(".only_creator_can_edit_without_revoking")
85           render :action => "edit"
86         elsif !user_block_was_active && @user_block.active?
87           flash.now[:error] = t(".inactive_block_cannot_be_reactivated")
88           render :action => "edit"
89         else
90           unless user_block_was_active
91             @user_block.ends_at = @user_block.ends_at_was
92             @user_block.deactivates_at = @user_block.deactivates_at_was
93             @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
94           end
95           if @user_block.save
96             flash[:notice] = t(".success")
97             redirect_to @user_block
98           else
99             render :action => "edit"
100           end
101         end
102       end
103     else
104       redirect_to edit_user_block_path(:id => params[:id])
105     end
106   end
107
108   ##
109   # revokes the block, setting the end_time to now
110   def revoke
111     if request.post? && params[:confirm] && @user_block.revoke!(current_user)
112       flash[:notice] = t ".flash"
113       redirect_to(@user_block)
114     end
115   end
116
117   ##
118   # revokes all active blocks
119   def revoke_all
120     if request.post? && params[:confirm]
121       @user.blocks.active.each { |block| block.revoke!(current_user) }
122       flash[:notice] = t ".flash"
123       redirect_to user_blocks_on_path(@user)
124     end
125   end
126
127   ##
128   # shows a list of all the blocks on the given user
129   def blocks_on
130     @params = params.permit(:display_name)
131
132     user_blocks = UserBlock.where(:user => @user)
133
134     @user_blocks, @newer_user_blocks_id, @older_user_blocks_id = get_page_items(user_blocks, :includes => [:user, :creator, :revoker])
135
136     @show_user_name = false
137     @show_creator_name = true
138
139     render :partial => "blocks" if turbo_frame_request_id == "pagination"
140   end
141
142   ##
143   # shows a list of all the blocks by the given user.
144   def blocks_by
145     @params = params.permit(:display_name)
146
147     user_blocks = UserBlock.where(:creator => @user)
148
149     @user_blocks, @newer_user_blocks_id, @older_user_blocks_id = get_page_items(user_blocks, :includes => [:user, :creator, :revoker])
150
151     @show_user_name = true
152     @show_creator_name = false
153
154     render :partial => "blocks" if turbo_frame_request_id == "pagination"
155   end
156
157   private
158
159   ##
160   # ensure that there is a "user_block" instance variable
161   def lookup_user_block
162     @user_block = UserBlock.find(params[:id])
163   rescue ActiveRecord::RecordNotFound
164     render :action => "not_found", :status => :not_found
165   end
166
167   ##
168   # check that the input parameters are valid, setting an instance
169   # variable if not. note that this doesn't do any redirection, as it's
170   # called before two different actions, each of which should redirect
171   # to a different place.
172   def require_valid_params
173     @block_period = params[:user_block_period].to_i
174     @valid_params = false
175
176     if UserBlock::PERIODS.exclude?(@block_period)
177       flash[:error] = t("user_blocks.filter.block_period")
178
179     else
180       @valid_params = true
181     end
182   end
183 end