1 # Redmine - project management software
2 # Copyright (C) 2006-2009 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
26 include CustomFieldsHelper
29 sort_init 'login', 'asc'
30 sort_update %w(login firstname lastname mail admin created_on last_login_on)
32 @status = params[:status] ? params[:status].to_i : 1
33 c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status])
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]
40 @user_count = User.count(:conditions => c.conditions)
41 @user_pages = Paginator.new self, @user_count,
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
49 render :layout => !request.xhr?
53 @user = User.find(params[:id])
55 # show projects based on current user visibility
56 @memberships = @user.memberships.all(:conditions => Project.visible_by(User.current))
58 events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10)
59 @events_by_day = events.group_by(&:event_date)
61 unless User.current.admin?
62 if !@user.active? || (@user != User.current && @memberships.empty? && events.empty?)
67 render :layout => 'base'
69 rescue ActiveRecord::RecordNotFound
74 @notification_options = User::MAIL_NOTIFICATION_OPTIONS
75 @notification_option = Setting.default_notification_option
77 @user = User.new(:language => Setting.default_language)
78 @auth_sources = AuthSource.find(:all)
81 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
83 @notification_options = User::MAIL_NOTIFICATION_OPTIONS
84 @notification_option = Setting.default_notification_option
86 @user = User.new(params[:user])
87 @user.admin = params[:user][:admin] || false
88 @user.login = params[:user][:login]
89 @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless @user.auth_source_id
91 # TODO: Similar to My#account
92 @user.mail_notification = params[:notification_option] || 'only_my_events'
93 @user.pref.attributes = params[:pref]
94 @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
98 @user.notified_project_ids = (params[:notification_option] == 'selected' ? params[:notified_project_ids] : [])
100 Mailer.deliver_account_information(@user, params[:password]) if params[:send_information]
101 flash[:notice] = l(:notice_successful_create)
102 redirect_to(params[:continue] ? {:controller => 'users', :action => 'new'} :
103 {:controller => 'users', :action => 'edit', :id => @user})
106 @auth_sources = AuthSource.find(:all)
107 @notification_option = @user.mail_notification
109 render :action => 'new'
114 @user = User.find(params[:id])
115 @notification_options = @user.valid_notification_options
116 @notification_option = @user.mail_notification
118 @auth_sources = AuthSource.find(:all)
119 @membership ||= Member.new
122 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
124 @user = User.find(params[:id])
125 @notification_options = @user.valid_notification_options
126 @notification_option = @user.mail_notification
128 @user.admin = params[:user][:admin] if params[:user][:admin]
129 @user.login = params[:user][:login] if params[:user][:login]
130 if params[:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?)
131 @user.password, @user.password_confirmation = params[:password], params[:password_confirmation]
133 @user.group_ids = params[:user][:group_ids] if params[:user][:group_ids]
134 @user.attributes = params[:user]
135 # Was the account actived ? (do it before User#save clears the change)
136 was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
137 # TODO: Similar to My#account
138 @user.mail_notification = params[:notification_option] || 'only_my_events'
139 @user.pref.attributes = params[:pref]
140 @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
144 @user.notified_project_ids = (params[:notification_option] == 'selected' ? params[:notified_project_ids] : [])
147 Mailer.deliver_account_activated(@user)
148 elsif @user.active? && params[:send_information] && !params[:password].blank? && @user.auth_source_id.nil?
149 Mailer.deliver_account_information(@user, params[:password])
151 flash[:notice] = l(:notice_successful_update)
154 @auth_sources = AuthSource.find(:all)
155 @membership ||= Member.new
157 render :action => :edit
159 rescue ::ActionController::RedirectBackError
160 redirect_to :controller => 'users', :action => 'edit', :id => @user
164 @user = User.find(params[:id])
165 @membership = Member.edit_membership(params[:membership_id], params[:membership], @user)
166 @membership.save if request.post?
167 respond_to do |format|
168 if @membership.valid?
169 format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
171 render(:update) {|page|
172 page.replace_html "tab-content-memberships", :partial => 'users/memberships'
173 page.visual_effect(:highlight, "member-#{@membership.id}")
178 render(:update) {|page|
179 page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', ')))
186 def destroy_membership
187 @user = User.find(params[:id])
188 @membership = Member.find(params[:membership_id])
189 if request.post? && @membership.deletable?
192 respond_to do |format|
193 format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
194 format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} }