annotate .svn/pristine/ae/ae59ba764b863f990d0884c0c55bada9eb65b729.svn-base @ 1464:261b3d9a4903 redmine-2.4

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