1 # Redmine - project management software
2 # Copyright (C) 2006-2010 Jean-Philippe Lang
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.
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.
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.
18 class UsersController < ApplicationController
21 before_filter :require_admin, :except => :show
22 accept_key_auth :index, :show, :create, :update
27 include CustomFieldsHelper
30 sort_init 'login', 'asc'
31 sort_update %w(login firstname lastname mail admin created_on last_login_on)
35 @offset, @limit = api_offset_and_limit
37 @limit = per_page_option
40 @status = params[:status] ? params[:status].to_i : 1
41 c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status])
43 unless params[:name].blank?
44 name = "%#{params[:name].strip.downcase}%"
45 c << ["LOWER(login) LIKE ? OR LOWER(firstname) LIKE ? OR LOWER(lastname) LIKE ? OR LOWER(mail) LIKE ?", name, name, name, name]
48 @user_count = User.count(:conditions => c.conditions)
49 @user_pages = Paginator.new self, @user_count, @limit, params['page']
50 @offset ||= @user_pages.current.offset
51 @users = User.find :all,
52 :order => sort_clause,
53 :conditions => c.conditions,
57 respond_to do |format|
58 format.html { render :layout => !request.xhr? }
64 @user = User.find(params[:id])
66 # show projects based on current user visibility
67 @memberships = @user.memberships.all(:conditions => Project.visible_by(User.current))
69 events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10)
70 @events_by_day = events.group_by(&:event_date)
72 unless User.current.admin?
73 if !@user.active? || (@user != User.current && @memberships.empty? && events.empty?)
79 respond_to do |format|
80 format.html { render :layout => 'base' }
83 rescue ActiveRecord::RecordNotFound
88 @notification_options = User::MAIL_NOTIFICATION_OPTIONS
89 @notification_option = Setting.default_notification_option
91 @user = User.new(:language => Setting.default_language)
92 @auth_sources = AuthSource.find(:all)
95 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
97 @notification_options = User::MAIL_NOTIFICATION_OPTIONS
98 @notification_option = Setting.default_notification_option
100 @user = User.new(params[:user])
101 @user.admin = params[:user][:admin] || false
102 @user.login = params[:user][:login]
103 @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless @user.auth_source_id
105 # TODO: Similar to My#account
106 @user.mail_notification = params[:notification_option] || 'only_my_events'
107 @user.pref.attributes = params[:pref]
108 @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
112 @user.notified_project_ids = (params[:notification_option] == 'selected' ? params[:notified_project_ids] : [])
114 Mailer.deliver_account_information(@user, params[:password]) if params[:send_information]
116 respond_to do |format|
118 flash[:notice] = l(:notice_successful_create)
119 redirect_to(params[:continue] ?
120 {:controller => 'users', :action => 'new'} :
121 {:controller => 'users', :action => 'edit', :id => @user}
124 format.api { render :action => 'show', :status => :created, :location => user_url(@user) }
127 @auth_sources = AuthSource.find(:all)
128 @notification_option = @user.mail_notification
130 respond_to do |format|
131 format.html { render :action => 'new' }
132 format.api { render_validation_errors(@user) }
138 @user = User.find(params[:id])
139 @notification_options = @user.valid_notification_options
140 @notification_option = @user.mail_notification
142 @auth_sources = AuthSource.find(:all)
143 @membership ||= Member.new
146 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
148 @user = User.find(params[:id])
149 @notification_options = @user.valid_notification_options
150 @notification_option = @user.mail_notification
152 @user.admin = params[:user][:admin] if params[:user][:admin]
153 @user.login = params[:user][:login] if params[:user][:login]
154 if params[:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?)
155 @user.password, @user.password_confirmation = params[:password], params[:password_confirmation]
157 @user.group_ids = params[:user][:group_ids] if params[:user][:group_ids]
158 @user.attributes = params[:user]
159 # Was the account actived ? (do it before User#save clears the change)
160 was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
161 # TODO: Similar to My#account
162 @user.mail_notification = params[:notification_option] || 'only_my_events'
163 @user.pref.attributes = params[:pref]
164 @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
168 @user.notified_project_ids = (params[:notification_option] == 'selected' ? params[:notified_project_ids] : [])
171 Mailer.deliver_account_activated(@user)
172 elsif @user.active? && params[:send_information] && !params[:password].blank? && @user.auth_source_id.nil?
173 Mailer.deliver_account_information(@user, params[:password])
176 respond_to do |format|
178 flash[:notice] = l(:notice_successful_update)
181 format.api { head :ok }
184 @auth_sources = AuthSource.find(:all)
185 @membership ||= Member.new
187 respond_to do |format|
188 format.html { render :action => :edit }
189 format.api { render_validation_errors(@user) }
192 rescue ::ActionController::RedirectBackError
193 redirect_to :controller => 'users', :action => 'edit', :id => @user
197 @user = User.find(params[:id])
198 @membership = Member.edit_membership(params[:membership_id], params[:membership], @user)
199 @membership.save if request.post?
200 respond_to do |format|
201 if @membership.valid?
202 format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
204 render(:update) {|page|
205 page.replace_html "tab-content-memberships", :partial => 'users/memberships'
206 page.visual_effect(:highlight, "member-#{@membership.id}")
211 render(:update) {|page|
212 page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', ')))
219 def destroy_membership
220 @user = User.find(params[:id])
221 @membership = Member.find(params[:membership_id])
222 if request.post? && @membership.deletable?
225 respond_to do |format|
226 format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
227 format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} }