]> git.openstreetmap.org Git - rails.git/blob - app/controllers/user_blocks_controller.rb
Merge pull request #4170 from osmcz/cuzk
[rails.git] / app / controllers / user_blocks_controller.rb
1 class UserBlocksController < ApplicationController
2   include UserMethods
3
4   layout "site"
5
6   before_action :authorize_web
7   before_action :set_locale
8
9   authorize_resource
10
11   before_action :lookup_user, :only => [:new, :create, :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]
16
17   def index
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",
22                                                 :per_page => 20)
23   end
24
25   def show
26     if current_user && current_user == @user_block.user
27       @user_block.needs_view = false
28       @user_block.save!
29     end
30   end
31
32   def new
33     @user_block = UserBlock.new
34   end
35
36   def edit
37     params[:user_block_period] = ((@user_block.ends_at - Time.now.utc) / 1.hour).ceil.to_s
38   end
39
40   def create
41     if @valid_params
42       @user_block = UserBlock.new(
43         :user => @user,
44         :creator => current_user,
45         :reason => params[:user_block][:reason],
46         :ends_at => Time.now.utc + @block_period.hours,
47         :needs_view => params[:user_block][:needs_view]
48       )
49
50       if @user_block.save
51         flash[:notice] = t(".flash", :name => @user.display_name)
52         redirect_to @user_block
53       else
54         render :action => "new"
55       end
56     else
57       redirect_to new_user_block_path(:display_name => params[:display_name])
58     end
59   end
60
61   def update
62     if @valid_params
63       if @user_block.creator != current_user
64         flash[:error] = t(".only_creator_can_edit")
65         redirect_to :action => "edit"
66       elsif @user_block.update(
67         :ends_at => Time.now.utc + @block_period.hours,
68         :reason => params[:user_block][:reason],
69         :needs_view => params[:user_block][:needs_view]
70       )
71         flash[:notice] = t(".success")
72         redirect_to(@user_block)
73       else
74         render :action => "edit"
75       end
76     else
77       redirect_to edit_user_block_path(:id => params[:id])
78     end
79   end
80
81   ##
82   # revokes the block, setting the end_time to now
83   def revoke
84     if request.post? && params[:confirm] && @user_block.revoke!(current_user)
85       flash[:notice] = t ".flash"
86       redirect_to(@user_block)
87     end
88   end
89
90   ##
91   # shows a list of all the blocks on the given user
92   def blocks_on
93     @params = params.permit(:display_name)
94     @user_blocks_pages, @user_blocks = paginate(:user_blocks,
95                                                 :include => [:user, :creator, :revoker],
96                                                 :conditions => { :user_id => @user.id },
97                                                 :order => "user_blocks.ends_at DESC",
98                                                 :per_page => 20)
99   end
100
101   ##
102   # shows a list of all the blocks by the given user.
103   def blocks_by
104     @params = params.permit(:display_name)
105     @user_blocks_pages, @user_blocks = paginate(:user_blocks,
106                                                 :include => [:user, :creator, :revoker],
107                                                 :conditions => { :creator_id => @user.id },
108                                                 :order => "user_blocks.ends_at DESC",
109                                                 :per_page => 20)
110   end
111
112   private
113
114   ##
115   # ensure that there is a "user_block" instance variable
116   def lookup_user_block
117     @user_block = UserBlock.find(params[:id])
118   rescue ActiveRecord::RecordNotFound
119     render :action => "not_found", :status => :not_found
120   end
121
122   ##
123   # check that the input parameters are valid, setting an instance
124   # variable if not. note that this doesn't do any redirection, as it's
125   # called before two different actions, each of which should redirect
126   # to a different place.
127   def require_valid_params
128     @block_period = params[:user_block_period].to_i
129     @valid_params = false
130
131     if UserBlock::PERIODS.exclude?(@block_period)
132       flash[:error] = t("user_blocks.filter.block_period")
133
134     elsif @user_block && !@user_block.active?
135       flash[:error] = t("user_blocks.filter.block_expired")
136
137     else
138       @valid_params = true
139     end
140   end
141 end