]> git.openstreetmap.org Git - rails.git/blob - app/controllers/user_blocks_controller.rb
3b7ef30f9c96256962187173e5ee317408e05d58
[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   end
25
26   def show
27     if current_user && current_user == @user_block.user
28       @user_block.needs_view = false
29       @user_block.save!
30     end
31   end
32
33   def new
34     @user_block = UserBlock.new
35   end
36
37   def edit
38     params[:user_block_period] = ((@user_block.ends_at - Time.now.utc) / 1.hour).ceil.to_s
39   end
40
41   def create
42     if @valid_params
43       now = Time.now.utc
44       @user_block = UserBlock.new(
45         :user => @user,
46         :creator => current_user,
47         :reason => params[:user_block][:reason],
48         :created_at => now,
49         :ends_at => now + @block_period.hours,
50         :needs_view => params[:user_block][:needs_view]
51       )
52
53       if @user_block.save
54         flash[:notice] = t(".flash", :name => @user.display_name)
55         redirect_to @user_block
56       else
57         render :action => "new"
58       end
59     else
60       redirect_to new_user_block_path(:display_name => params[:display_name])
61     end
62   end
63
64   def update
65     if @valid_params
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       else
71         user_block_was_active = @user_block.active?
72         @user_block.reason = params[:user_block][:reason]
73         @user_block.needs_view = params[:user_block][:needs_view]
74         @user_block.ends_at = Time.now.utc + @block_period.hours
75         if !user_block_was_active && @user_block.active?
76           flash.now[:error] = t(".inactive_block_cannot_be_reactivated")
77           render :action => "edit"
78         elsif @user_block.save
79           flash[:notice] = t(".success")
80           redirect_to @user_block
81         else
82           render :action => "edit"
83         end
84       end
85     else
86       redirect_to edit_user_block_path(:id => params[:id])
87     end
88   end
89
90   ##
91   # revokes the block, setting the end_time to now
92   def revoke
93     if request.post? && params[:confirm] && @user_block.revoke!(current_user)
94       flash[:notice] = t ".flash"
95       redirect_to(@user_block)
96     end
97   end
98
99   ##
100   # revokes all active blocks
101   def revoke_all
102     if request.post? && params[:confirm]
103       @user.blocks.active.each { |block| block.revoke!(current_user) }
104       flash[:notice] = t ".flash"
105       redirect_to user_blocks_on_path(@user)
106     end
107   end
108
109   ##
110   # shows a list of all the blocks on the given user
111   def blocks_on
112     @params = params.permit(:display_name)
113
114     user_blocks = UserBlock.where(:user => @user)
115
116     @user_blocks, @newer_user_blocks_id, @older_user_blocks_id = get_page_items(user_blocks, :includes => [:user, :creator, :revoker])
117   end
118
119   ##
120   # shows a list of all the blocks by the given user.
121   def blocks_by
122     @params = params.permit(:display_name)
123
124     user_blocks = UserBlock.where(:creator => @user)
125
126     @user_blocks, @newer_user_blocks_id, @older_user_blocks_id = get_page_items(user_blocks, :includes => [:user, :creator, :revoker])
127   end
128
129   private
130
131   ##
132   # ensure that there is a "user_block" instance variable
133   def lookup_user_block
134     @user_block = UserBlock.find(params[:id])
135   rescue ActiveRecord::RecordNotFound
136     render :action => "not_found", :status => :not_found
137   end
138
139   ##
140   # check that the input parameters are valid, setting an instance
141   # variable if not. note that this doesn't do any redirection, as it's
142   # called before two different actions, each of which should redirect
143   # to a different place.
144   def require_valid_params
145     @block_period = params[:user_block_period].to_i
146     @valid_params = false
147
148     if UserBlock::PERIODS.exclude?(@block_period)
149       flash[:error] = t("user_blocks.filter.block_period")
150
151     else
152       @valid_params = true
153     end
154   end
155 end