annotate .svn/pristine/c5/c5ac85b1b901fa66f9ca32fcaa2fc8702a7953a0.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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