annotate app/controllers/users_controller.rb @ 1517:dffacf8a6908 redmine-2.5

Update to Redmine SVN revision 13367 on 2.5-stable branch
author Chris Cannam
date Tue, 09 Sep 2014 09:29:00 +0100
parents e248c7af89ec
children a1bdbf8a87d5
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@1517 93 @user.pref.attributes = params[:pref]
chris@37 94
chris@37 95 if @user.save
Chris@1464 96 Mailer.account_information(@user, @user.password).deliver if params[:send_information]
Chris@909 97
Chris@119 98 respond_to do |format|
Chris@119 99 format.html {
Chris@1115 100 flash[:notice] = l(:notice_user_successful_create, :id => view_context.link_to(@user.login, user_path(@user)))
Chris@1464 101 if params[:continue]
Chris@1464 102 attrs = params[:user].slice(:generate_password)
Chris@1464 103 redirect_to new_user_path(:user => attrs)
Chris@1464 104 else
Chris@1464 105 redirect_to edit_user_path(@user)
Chris@1464 106 end
Chris@119 107 }
Chris@119 108 format.api { render :action => 'show', :status => :created, :location => user_url(@user) }
Chris@119 109 end
Chris@0 110 else
Chris@1464 111 @auth_sources = AuthSource.all
Chris@119 112 # Clear password input
Chris@119 113 @user.password = @user.password_confirmation = nil
chris@37 114
Chris@119 115 respond_to do |format|
Chris@119 116 format.html { render :action => 'new' }
Chris@119 117 format.api { render_validation_errors(@user) }
Chris@119 118 end
Chris@0 119 end
Chris@0 120 end
Chris@0 121
Chris@0 122 def edit
Chris@1464 123 @auth_sources = AuthSource.all
Chris@0 124 @membership ||= Member.new
chris@37 125 end
Chris@909 126
chris@37 127 def update
chris@37 128 @user.admin = params[:user][:admin] if params[:user][:admin]
chris@37 129 @user.login = params[:user][:login] if params[:user][:login]
Chris@119 130 if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?)
Chris@119 131 @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation]
chris@37 132 end
Chris@119 133 @user.safe_attributes = params[:user]
chris@37 134 # Was the account actived ? (do it before User#save clears the change)
chris@37 135 was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
chris@37 136 # TODO: Similar to My#account
chris@37 137 @user.pref.attributes = params[:pref]
chris@37 138
chris@37 139 if @user.save
chris@37 140 @user.pref.save
chris@37 141
chris@37 142 if was_activated
Chris@1115 143 Mailer.account_activated(@user).deliver
Chris@1464 144 elsif @user.active? && params[:send_information] && @user.password.present? && @user.auth_source_id.nil?
Chris@1464 145 Mailer.account_information(@user, @user.password).deliver
chris@37 146 end
Chris@909 147
Chris@119 148 respond_to do |format|
Chris@119 149 format.html {
Chris@119 150 flash[:notice] = l(:notice_successful_update)
Chris@1115 151 redirect_to_referer_or edit_user_path(@user)
Chris@119 152 }
Chris@1115 153 format.api { render_api_ok }
Chris@119 154 end
chris@37 155 else
Chris@1464 156 @auth_sources = AuthSource.all
chris@37 157 @membership ||= Member.new
Chris@119 158 # Clear password input
Chris@119 159 @user.password = @user.password_confirmation = nil
chris@37 160
Chris@119 161 respond_to do |format|
Chris@119 162 format.html { render :action => :edit }
Chris@119 163 format.api { render_validation_errors(@user) }
Chris@119 164 end
chris@37 165 end
Chris@0 166 end
chris@37 167
Chris@128 168 def destroy
Chris@128 169 @user.destroy
Chris@128 170 respond_to do |format|
Chris@1464 171 format.html { redirect_back_or_default(users_path) }
Chris@1115 172 format.api { render_api_ok }
Chris@128 173 end
Chris@128 174 end
Chris@128 175
Chris@0 176 def edit_membership
Chris@0 177 @membership = Member.edit_membership(params[:membership_id], params[:membership], @user)
Chris@1115 178 @membership.save
Chris@0 179 respond_to do |format|
Chris@1464 180 format.html { redirect_to edit_user_path(@user, :tab => 'memberships') }
Chris@1115 181 format.js
Chris@14 182 end
Chris@0 183 end
Chris@909 184
Chris@0 185 def destroy_membership
Chris@0 186 @membership = Member.find(params[:membership_id])
Chris@1115 187 if @membership.deletable?
Chris@0 188 @membership.destroy
Chris@0 189 end
Chris@0 190 respond_to do |format|
Chris@1464 191 format.html { redirect_to edit_user_path(@user, :tab => 'memberships') }
Chris@1115 192 format.js
Chris@0 193 end
Chris@0 194 end
Chris@909 195
Chris@119 196 private
Chris@909 197
Chris@119 198 def find_user
Chris@119 199 if params[:id] == 'current'
Chris@119 200 require_login || return
Chris@119 201 @user = User.current
Chris@119 202 else
Chris@119 203 @user = User.find(params[:id])
Chris@119 204 end
Chris@119 205 rescue ActiveRecord::RecordNotFound
Chris@119 206 render_404
Chris@119 207 end
Chris@0 208 end