annotate app/controllers/.svn/text-base/users_controller.rb.svn-base @ 119:8661b858af72

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