annotate .svn/pristine/c5/c5ac85b1b901fa66f9ca32fcaa2fc8702a7953a0.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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