annotate .svn/pristine/b0/b01b1d9455c0cc103023931e45cd6cdc1d9c80a4.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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