OSDN Git Service

Allow admins to edit user's email notifications and preferences. #3503
[redminele/redmine.git] / app / controllers / users_controller.rb
1 # Redmine - project management software
2 # Copyright (C) 2006-2009  Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17
18 class UsersController < ApplicationController
19   layout 'admin'
20   
21   before_filter :require_admin, :except => :show
22
23   helper :sort
24   include SortHelper
25   helper :custom_fields
26   include CustomFieldsHelper   
27
28   def index
29     sort_init 'login', 'asc'
30     sort_update %w(login firstname lastname mail admin created_on last_login_on)
31     
32     @status = params[:status] ? params[:status].to_i : 1
33     c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status])
34
35     unless params[:name].blank?
36       name = "%#{params[:name].strip.downcase}%"
37       c << ["LOWER(login) LIKE ? OR LOWER(firstname) LIKE ? OR LOWER(lastname) LIKE ? OR LOWER(mail) LIKE ?", name, name, name, name]
38     end
39     
40     @user_count = User.count(:conditions => c.conditions)
41     @user_pages = Paginator.new self, @user_count,
42                                                                 per_page_option,
43                                                                 params['page']                                                          
44     @users =  User.find :all,:order => sort_clause,
45                         :conditions => c.conditions,
46                                                 :limit  =>  @user_pages.items_per_page,
47                                                 :offset =>  @user_pages.current.offset
48
49     render :layout => !request.xhr?     
50   end
51   
52   def show
53     @user = User.find(params[:id])
54     @custom_values = @user.custom_values
55     
56     # show projects based on current user visibility
57     @memberships = @user.memberships.all(:conditions => Project.visible_by(User.current))
58     
59     events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10)
60     @events_by_day = events.group_by(&:event_date)
61     
62     unless User.current.admin?
63       if !@user.active? || (@user != User.current  && @memberships.empty? && events.empty?)
64         render_404
65         return
66       end
67     end
68     render :layout => 'base'
69
70   rescue ActiveRecord::RecordNotFound
71     render_404
72   end
73
74   def add
75     @notification_options = User::MAIL_NOTIFICATION_OPTIONS
76     @notification_option = Setting.default_notification_option
77
78     @user = User.new(:language => Setting.default_language)
79     @auth_sources = AuthSource.find(:all)
80
81     # TODO: Similar to My#account
82     # Only users that belong to more than 1 project can select projects for which they are notified
83     # Note that @user.membership.size would fail since AR ignores
84     # :include association option when doing a count
85     if @user.memberships.length < 1
86       @notification_options.delete_if {|option| option.first == :selected}
87     end
88   end
89   
90   verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
91   def create
92     @notification_options = User::MAIL_NOTIFICATION_OPTIONS
93     @notification_option = Setting.default_notification_option
94
95     @user = User.new(params[:user])
96     @user.admin = params[:user][:admin] || false
97     @user.login = params[:user][:login]
98     @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless @user.auth_source_id
99
100     # TODO: Similar to My#account
101     @user.mail_notification = params[:notification_option] || 'only_my_events'
102     @user.pref.attributes = params[:pref]
103     @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
104
105     if @user.save
106       @user.pref.save
107       @user.notified_project_ids = (params[:notification_option] == 'selected' ? params[:notified_project_ids] : [])
108
109       Mailer.deliver_account_information(@user, params[:password]) if params[:send_information]
110       flash[:notice] = l(:notice_successful_create)
111       redirect_to(params[:continue] ? {:controller => 'users', :action => 'add'} : 
112                                       {:controller => 'users', :action => 'edit', :id => @user})
113       return
114     else
115       @auth_sources = AuthSource.find(:all)
116       @notification_option = @user.mail_notification
117
118       render :action => 'add'
119     end
120   end
121
122   def edit
123     @user = User.find(params[:id])
124     # TODO: Similar to My#account
125     @notification_options = User::MAIL_NOTIFICATION_OPTIONS
126     # Only users that belong to more than 1 project can select projects for which they are notified
127     # Note that @user.membership.size would fail since AR ignores
128     # :include association option when doing a count
129     if @user.memberships.length < 1
130       @notification_options.delete_if {|option| option.first == :selected}
131     end
132     @notification_option = @user.mail_notification
133
134     if request.post?
135       @user.admin = params[:user][:admin] if params[:user][:admin]
136       @user.login = params[:user][:login] if params[:user][:login]
137       if params[:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?)
138         @user.password, @user.password_confirmation = params[:password], params[:password_confirmation]
139       end
140       @user.group_ids = params[:user][:group_ids] if params[:user][:group_ids]
141       @user.attributes = params[:user]
142       # Was the account actived ? (do it before User#save clears the change)
143       was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
144       # TODO: Similar to My#account
145       @user.mail_notification = params[:notification_option] || 'only_my_events'
146       @user.pref.attributes = params[:pref]
147       @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
148
149       if @user.save
150         @user.pref.save
151         @user.notified_project_ids = (params[:notification_option] == 'selected' ? params[:notified_project_ids] : [])
152
153         if was_activated
154           Mailer.deliver_account_activated(@user)
155         elsif @user.active? && params[:send_information] && !params[:password].blank? && @user.auth_source_id.nil?
156           Mailer.deliver_account_information(@user, params[:password])
157         end
158         flash[:notice] = l(:notice_successful_update)
159         redirect_to :back
160       end
161     end
162     @auth_sources = AuthSource.find(:all)
163     @membership ||= Member.new
164   rescue ::ActionController::RedirectBackError
165     redirect_to :controller => 'users', :action => 'edit', :id => @user
166   end
167   
168   def edit_membership
169     @user = User.find(params[:id])
170     @membership = Member.edit_membership(params[:membership_id], params[:membership], @user)
171     @membership.save if request.post?
172     respond_to do |format|
173       if @membership.valid?
174         format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
175         format.js {
176           render(:update) {|page|
177             page.replace_html "tab-content-memberships", :partial => 'users/memberships'
178             page.visual_effect(:highlight, "member-#{@membership.id}")
179           }
180         }
181       else
182         format.js {
183           render(:update) {|page|
184             page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', ')))
185           }
186         }
187       end
188     end
189   end
190   
191   def destroy_membership
192     @user = User.find(params[:id])
193     @membership = Member.find(params[:membership_id])
194     if request.post? && @membership.deletable?
195       @membership.destroy
196     end
197     respond_to do |format|
198       format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
199       format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} }
200     end
201   end
202 end