annotate app/controllers/users_controller.rb @ 1516:b450a9d58aed redmine-2.4

Update to Redmine SVN revision 13356 on 2.4-stable branch
author Chris Cannam
date Tue, 09 Sep 2014 09:28:31 +0100
parents e248c7af89ec
children c86dacc2ef0a dffacf8a6908
rev   line source
Chris@0 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 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@909 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@909 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@909 20
Chris@0 21 before_filter :require_admin, :except => :show
Chris@128 22 before_filter :find_user, :only => [:show, :edit, :update, :destroy, :edit_membership, :destroy_membership]
Chris@507 23 accept_api_auth :index, :show, :create, :update, :destroy
Chris@0 24
Chris@0 25 helper :sort
Chris@0 26 include SortHelper
Chris@0 27 helper :custom_fields
Chris@909 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@909 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@909 40
Chris@1115 41 @status = params[:status] || 1
Chris@909 42
Chris@1115 43 scope = User.logged.status(@status)
Chris@1115 44 scope = scope.like(params[:name]) if params[:name].present?
Chris@1115 45 scope = scope.in_group(params[:group_id]) if params[:group_id].present?
Chris@0 46
Chris@1115 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@0 51
Chris@441 52 respond_to do |format|
Chris@441 53 format.html {
Chris@441 54 @groups = Group.all.sort
Chris@441 55 render :layout => !request.xhr?
Chris@441 56 }
Chris@119 57 format.api
Chris@1464 58 end
Chris@0 59 end
Chris@909 60
Chris@0 61 def show
Chris@14 62 # show projects based on current user visibility
Chris@1464 63 @memberships = @user.memberships.where(Project.visible_condition(User.current)).all
Chris@909 64
Chris@0 65 events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10)
Chris@0 66 @events_by_day = events.group_by(&:event_date)
Chris@909 67
Chris@0 68 unless User.current.admin?
Chris@0 69 if !@user.active? || (@user != User.current && @memberships.empty? && events.empty?)
Chris@0 70 render_404
Chris@0 71 return
Chris@0 72 end
Chris@0 73 end
Chris@909 74
Chris@119 75 respond_to do |format|
Chris@119 76 format.html { render :layout => 'base' }
Chris@119 77 format.api
Chris@119 78 end
Chris@0 79 end
Chris@0 80
chris@37 81 def new
Chris@119 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@37 85 end
Chris@909 86
chris@37 87 def create
Chris@119 88 @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
Chris@119 89 @user.safe_attributes = params[:user]
chris@37 90 @user.admin = params[:user][:admin] || false
chris@37 91 @user.login = params[:user][:login]
Chris@119 92 @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id
chris@37 93
chris@37 94 if @user.save
Chris@1115 95 @user.pref.attributes = params[:pref]
chris@37 96 @user.pref.save
chris@37 97
Chris@1464 98 Mailer.account_information(@user, @user.password).deliver if params[:send_information]
Chris@909 99
Chris@119 100 respond_to do |format|
Chris@119 101 format.html {
Chris@1115 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@119 109 }
Chris@119 110 format.api { render :action => 'show', :status => :created, :location => user_url(@user) }
Chris@119 111 end
Chris@0 112 else
Chris@1464 113 @auth_sources = AuthSource.all
Chris@119 114 # Clear password input
Chris@119 115 @user.password = @user.password_confirmation = nil
chris@37 116
Chris@119 117 respond_to do |format|
Chris@119 118 format.html { render :action => 'new' }
Chris@119 119 format.api { render_validation_errors(@user) }
Chris@119 120 end
Chris@0 121 end
Chris@0 122 end
Chris@0 123
Chris@0 124 def edit
Chris@1464 125 @auth_sources = AuthSource.all
Chris@0 126 @membership ||= Member.new
chris@37 127 end
Chris@909 128
chris@37 129 def update
chris@37 130 @user.admin = params[:user][:admin] if params[:user][:admin]
chris@37 131 @user.login = params[:user][:login] if params[:user][:login]
Chris@119 132 if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?)
Chris@119 133 @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation]
chris@37 134 end
Chris@119 135 @user.safe_attributes = params[:user]
chris@37 136 # Was the account actived ? (do it before User#save clears the change)
chris@37 137 was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
chris@37 138 # TODO: Similar to My#account
chris@37 139 @user.pref.attributes = params[:pref]
chris@37 140
chris@37 141 if @user.save
chris@37 142 @user.pref.save
chris@37 143
chris@37 144 if was_activated
Chris@1115 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@37 148 end
Chris@909 149
Chris@119 150 respond_to do |format|
Chris@119 151 format.html {
Chris@119 152 flash[:notice] = l(:notice_successful_update)
Chris@1115 153 redirect_to_referer_or edit_user_path(@user)
Chris@119 154 }
Chris@1115 155 format.api { render_api_ok }
Chris@119 156 end
chris@37 157 else
Chris@1464 158 @auth_sources = AuthSource.all
chris@37 159 @membership ||= Member.new
Chris@119 160 # Clear password input
Chris@119 161 @user.password = @user.password_confirmation = nil
chris@37 162
Chris@119 163 respond_to do |format|
Chris@119 164 format.html { render :action => :edit }
Chris@119 165 format.api { render_validation_errors(@user) }
Chris@119 166 end
chris@37 167 end
Chris@0 168 end
chris@37 169
Chris@128 170 def destroy
Chris@128 171 @user.destroy
Chris@128 172 respond_to do |format|
Chris@1464 173 format.html { redirect_back_or_default(users_path) }
Chris@1115 174 format.api { render_api_ok }
Chris@128 175 end
Chris@128 176 end
Chris@128 177
Chris@0 178 def edit_membership
Chris@0 179 @membership = Member.edit_membership(params[:membership_id], params[:membership], @user)
Chris@1115 180 @membership.save
Chris@0 181 respond_to do |format|
Chris@1464 182 format.html { redirect_to edit_user_path(@user, :tab => 'memberships') }
Chris@1115 183 format.js
Chris@14 184 end
Chris@0 185 end
Chris@909 186
Chris@0 187 def destroy_membership
Chris@0 188 @membership = Member.find(params[:membership_id])
Chris@1115 189 if @membership.deletable?
Chris@0 190 @membership.destroy
Chris@0 191 end
Chris@0 192 respond_to do |format|
Chris@1464 193 format.html { redirect_to edit_user_path(@user, :tab => 'memberships') }
Chris@1115 194 format.js
Chris@0 195 end
Chris@0 196 end
Chris@909 197
Chris@119 198 private
Chris@909 199
Chris@119 200 def find_user
Chris@119 201 if params[:id] == 'current'
Chris@119 202 require_login || return
Chris@119 203 @user = User.current
Chris@119 204 else
Chris@119 205 @user = User.find(params[:id])
Chris@119 206 end
Chris@119 207 rescue ActiveRecord::RecordNotFound
Chris@119 208 render_404
Chris@119 209 end
Chris@0 210 end