New migration to add better auditing to user_roles and better column names there...
[rails.git] / app / controllers / user_blocks_controller.rb
1 class UserBlocksController < ApplicationController
2   layout 'site'
3
4   before_filter :authorize_web
5   before_filter :set_locale
6   before_filter :require_user, :only => [:new, :create, :edit, :delete]
7   before_filter :require_moderator, :only => [:new, :create, :edit, :delete]
8
9   def index
10     @user_blocks_pages, @user_blocks = paginate(:user_blocks,
11                                             :include => [:user, :creator, :revoker],
12                                             :order => "user_blocks.ends_at DESC",
13                                             :per_page => 20)
14   end
15
16   def show
17     @user_block = UserBlock.find(params[:id])
18
19     if @user and @user.id == @user_block.user_id
20       @user_block.needs_view = false
21       @user_block.save!
22     end
23   end
24
25   def new
26     @user_block = UserBlock.new
27     @display_name = params[:display_name]
28     @this_user = User.find_by_display_name(@display_name, :conditions => {:visible => true})
29   end
30
31   # GET /user_blocks/1/edit
32   def edit
33     @user_block = UserBlock.find(params[:id])
34     params[:user_block_period] = ((@user_block.ends_at - Time.now.getutc) / 1.hour).ceil.to_s
35   end
36
37   def create
38     @display_name = params[:display_name]
39     @this_user = User.find_by_display_name(@display_name, :conditions => {:visible => true})
40     block_period = [UserBlock::PERIODS.max, params[:user_block_period].to_i].min
41
42     @user_block = UserBlock.new(:user_id => @this_user.id,
43                                 :creator_id => @user.id,
44                                 :reason => params[:user_block][:reason],
45                                 :ends_at => Time.now.getutc() + block_period.hours,
46                                 :needs_view => params[:user_block][:needs_view])
47     
48     if (@this_user and @user.moderator? and 
49         params[:tried_contacting] == "yes" and
50         params[:tried_waiting] == "yes" and
51         block_period >= 0)
52       if @user_block.save
53         flash[:notice] = t('user_block.create.flash', :name => @display_name)
54         redirect_to @user_block
55       else
56         render :action => "new"
57       end
58     else
59       if !@user.moderator?
60         flash[:notice] = t('user_block.create.not_a_moderator')
61       elsif params[:tried_contacting] != "yes"
62         flash[:notice] = t('user_block.create.try_contacting')
63       elsif params[:tried_waiting] != "yes"
64         flash[:notice] = t('user_block.create.try_waiting')
65       else
66         flash[:notice] = t('user_block.create.bad_parameters')
67       end
68       @display_name = @this_user.nil? ? '' : @this_user.display_name
69
70       render :action => "new"
71     end
72   end
73
74   def update
75     @user_block = UserBlock.find(params[:id])
76     block_period = [72, params[:user_block_period].to_i].min
77     
78     if @user_block.creator_id != @user.id
79       flash[:notice] = t('user_block.update.only_creator_can_edit')
80       redirect_to(@user_block)
81
82     elsif !@user_block.active?
83       flash[:notice] = t('user_block.update.block_expired')
84       redirect_to(@user_block)
85       
86     elsif @user_block.update_attributes({ :ends_at => Time.now.getutc() + block_period.hours,
87                                           :reason => params[:user_block][:reason],
88                                           :needs_view => params[:user_block][:needs_view] })
89       flash[:notice] = t('user_block.update.success')
90       redirect_to(@user_block)
91     else
92       render :action => "edit"
93     end
94   end
95
96   ##
97   # revokes the block, setting the end_time to now
98   def revoke
99     @user_block = UserBlock.find(params[:id])
100     
101     if !@user.moderator?
102       flash[:notice] = t('user_block.create.not_a_moderator')
103       redirect_to @user_block
104
105     elsif params[:confirm]
106       if @user_block.revoke!
107         flash[:notice] = t'user_block.revoke.flash'
108         redirect_to(@user_block)
109       else
110         flash[:notice] = t'user_block.revoke.error'
111         render :action => "edit"
112       end
113     end
114   end
115
116   ##
117   # shows a list of all the blocks on the given user
118   def blocks_on
119     @this_user = User.find_by_display_name(params[:display_name])
120
121     @user_blocks_pages, @user_blocks = paginate(:user_blocks,
122                                                 :include => [:user, :creator, :revoker],
123                                                 :conditions => {:user_id => @this_user.id},
124                                                 :order => "user_blocks.ends_at DESC",
125                                                 :per_page => 20)
126   end
127
128   ##
129   # shows a list of all the blocks by the given user.
130   def blocks_by
131     @this_user = User.find_by_display_name(params[:display_name])
132
133     @user_blocks_pages, @user_blocks = paginate(:user_blocks,
134                                             :include => [:user, :creator, :revoker],
135                                             :conditions => {:creator_id => @this_user.id},
136                                             :order => "user_blocks.ends_at DESC",
137                                             :per_page => 20)
138   end
139
140   private
141   def require_moderator
142     redirect_to "/403.html" unless @user.moderator?
143   end
144
145 end