annotate app/controllers/users_controller.rb @ 1466:834828a14f2b bug_598

Close obsolete branch bug_598
author Chris Cannam
date Fri, 21 Jun 2013 14:51:55 +0100
parents 96790506f247
children 4f746d8966dd 51364c0cd58f
rev   line source
Chris@0 1 # Redmine - project management software
Chris@1115 2 # Copyright (C) 2006-2012 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@117 34 case params[:format]
Chris@117 35 when 'xml', 'json'
Chris@117 36 @offset, @limit = api_offset_and_limit
Chris@117 37 else
Chris@117 38 @limit = per_page_option
Chris@117 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@117 48 @user_pages = Paginator.new self, @user_count, @limit, params['page']
Chris@117 49 @offset ||= @user_pages.current.offset
Chris@441 50 @users = scope.find :all,
Chris@117 51 :order => sort_clause,
Chris@117 52 :limit => @limit,
Chris@117 53 :offset => @offset
Chris@0 54
Chris@441 55 respond_to do |format|
Chris@441 56 format.html {
Chris@441 57 @groups = Group.all.sort
Chris@441 58 render :layout => !request.xhr?
Chris@441 59 }
Chris@117 60 format.api
Chris@441 61 end
Chris@0 62 end
Chris@909 63
Chris@0 64 def show
luisf@92 65
luisf@92 66 if @user.ssamr_user_detail != nil
luisf@92 67 @description = @user.ssamr_user_detail.description
chris@1217 68 @institution_name = @user.ssamr_user_detail.institution_name
luisf@92 69 end
chris@37 70
Chris@14 71 # show projects based on current user visibility
Chris@441 72 @memberships = @user.memberships.all(:conditions => Project.visible_condition(User.current))
Chris@909 73
Chris@0 74 events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10)
Chris@0 75 @events_by_day = events.group_by(&:event_date)
Chris@909 76
Chris@0 77 unless User.current.admin?
Chris@0 78 if !@user.active? || (@user != User.current && @memberships.empty? && events.empty?)
Chris@0 79 render_404
Chris@0 80 return
Chris@0 81 end
Chris@0 82 end
Chris@909 83
Chris@117 84 respond_to do |format|
Chris@117 85 format.html { render :layout => 'base' }
Chris@117 86 format.api
Chris@117 87 end
Chris@0 88 end
Chris@0 89
luisf@61 90 def new
Chris@117 91 @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
chris@37 92 @auth_sources = AuthSource.find(:all)
luisf@60 93
luisf@60 94 @ssamr_user_details = SsamrUserDetail.new
chris@37 95 end
Chris@909 96
chris@37 97 def create
Chris@117 98 @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
Chris@117 99 @user.safe_attributes = params[:user]
luisf@60 100 @user = User.new(params[:user])
chris@37 101 @user.admin = params[:user][:admin] || false
chris@37 102 @user.login = params[:user][:login]
Chris@117 103 @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id
chris@37 104
chris@37 105 # TODO: Similar to My#account
chris@37 106 @user.pref.attributes = params[:pref]
chris@37 107 @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
chris@37 108
luisf@61 109 @ssamr_user_details = SsamrUserDetail.new(params[:ssamr_user_details])
luisf@61 110
luisf@61 111 # associates the 2 objects
luisf@61 112 @user.ssamr_user_detail = @ssamr_user_details
luisf@61 113
chris@37 114 if @user.save
Chris@1115 115 @user.pref.attributes = params[:pref]
Chris@1115 116 @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
chris@37 117 @user.pref.save
luisf@61 118
luisf@60 119 @ssamr_user_details.save!
luisf@60 120
chris@37 121
Chris@1115 122 Mailer.account_information(@user, params[:user][:password]).deliver if params[:send_information]
Chris@909 123
Chris@117 124 respond_to do |format|
Chris@117 125 format.html {
Chris@1115 126 flash[:notice] = l(:notice_user_successful_create, :id => view_context.link_to(@user.login, user_path(@user)))
Chris@909 127 redirect_to(params[:continue] ?
Chris@909 128 {:controller => 'users', :action => 'new'} :
Chris@117 129 {:controller => 'users', :action => 'edit', :id => @user}
Chris@117 130 )
Chris@117 131 }
Chris@117 132 format.api { render :action => 'show', :status => :created, :location => user_url(@user) }
Chris@117 133 end
Chris@0 134 else
chris@37 135 @auth_sources = AuthSource.find(:all)
Chris@117 136 # Clear password input
Chris@117 137 @user.password = @user.password_confirmation = nil
chris@37 138
Chris@117 139 respond_to do |format|
Chris@117 140 format.html { render :action => 'new' }
Chris@117 141 format.api { render_validation_errors(@user) }
Chris@117 142 end
Chris@0 143 end
Chris@0 144 end
Chris@0 145
Chris@0 146 def edit
luisf@55 147
luisf@60 148 @ssamr_user_details = @user.ssamr_user_detail
luisf@161 149
luisf@185 150 if @user.ssamr_user_detail == nil
luisf@185 151 @selected_institution_id = nil
luisf@185 152 else
luisf@185 153 @selected_institution_id = @user.ssamr_user_detail.institution_id.to_i
luisf@185 154 end
luisf@60 155
Chris@0 156 @auth_sources = AuthSource.find(:all)
Chris@0 157 @membership ||= Member.new
chris@37 158 end
Chris@909 159
chris@37 160 def update
chris@37 161 @user.admin = params[:user][:admin] if params[:user][:admin]
chris@37 162 @user.login = params[:user][:login] if params[:user][:login]
Chris@117 163 if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?)
Chris@117 164 @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation]
chris@37 165 end
Chris@117 166 @user.safe_attributes = params[:user]
chris@37 167 # Was the account actived ? (do it before User#save clears the change)
chris@37 168 was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
chris@37 169 # TODO: Similar to My#account
chris@37 170 @user.pref.attributes = params[:pref]
chris@37 171 @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
chris@37 172
luisf@92 173 if @user.ssamr_user_detail == nil
luisf@92 174 @ssamr_user_details = SsamrUserDetail.new()
luisf@92 175 @user.ssamr_user_detail = @ssamr_user_details
luisf@92 176 else
luisf@92 177 @ssamr_user_details = @user.ssamr_user_detail
luisf@92 178 end
luisf@108 179
luisf@63 180 if params[:ssamr_user_details].nil? or params[:ssamr_user_details].empty?
luisf@63 181 @ssamr_user_details.description = @user.ssamr_user_detail.description
luisf@108 182 @ssamr_user_details.institution_id = @user.ssamr_user_detail.institution_id
luisf@158 183 @ssamr_user_details.other_institution = @user.ssamr_user_detail.other_institution
luisf@158 184 @ssamr_user_details.institution_type = @user.ssamr_user_detail.institution_type
luisf@158 185
luisf@63 186 else
luisf@63 187 @ssamr_user_details.description = params[:ssamr_user_details][:description]
luisf@108 188 @ssamr_user_details.institution_id = params[:ssamr_user_details][:institution_id]
luisf@158 189 @ssamr_user_details.other_institution = params[:ssamr_user_details][:other_institution]
luisf@158 190 @ssamr_user_details.institution_type = params[:ssamr_user_details][:institution_type]
luisf@63 191 end
luisf@63 192
chris@37 193 if @user.save
chris@37 194 @user.pref.save
Chris@117 195 @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : [])
chris@37 196
chris@37 197 if was_activated
Chris@1115 198 Mailer.account_activated(@user).deliver
Chris@117 199 elsif @user.active? && params[:send_information] && !params[:user][:password].blank? && @user.auth_source_id.nil?
Chris@1115 200 Mailer.account_information(@user, params[:user][:password]).deliver
chris@37 201 end
Chris@909 202
Chris@117 203 respond_to do |format|
Chris@117 204 format.html {
Chris@117 205 flash[:notice] = l(:notice_successful_update)
Chris@1115 206 redirect_to_referer_or edit_user_path(@user)
Chris@117 207 }
Chris@1115 208 format.api { render_api_ok }
Chris@117 209 end
chris@37 210 else
chris@37 211 @auth_sources = AuthSource.find(:all)
chris@37 212 @membership ||= Member.new
Chris@117 213 # Clear password input
Chris@117 214 @user.password = @user.password_confirmation = nil
chris@37 215
Chris@117 216 respond_to do |format|
Chris@117 217 format.html { render :action => :edit }
Chris@117 218 format.api { render_validation_errors(@user) }
Chris@117 219 end
chris@37 220 end
Chris@0 221 end
chris@37 222
Chris@128 223 def destroy
Chris@128 224 @user.destroy
Chris@128 225 respond_to do |format|
Chris@1115 226 format.html { redirect_back_or_default(users_url) }
Chris@1115 227 format.api { render_api_ok }
Chris@128 228 end
Chris@128 229 end
Chris@128 230
Chris@0 231 def edit_membership
Chris@0 232 @membership = Member.edit_membership(params[:membership_id], params[:membership], @user)
Chris@1115 233 @membership.save
Chris@0 234 respond_to do |format|
Chris@1115 235 format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
Chris@1115 236 format.js
Chris@14 237 end
Chris@0 238 end
Chris@909 239
Chris@0 240 def destroy_membership
Chris@0 241 @membership = Member.find(params[:membership_id])
Chris@1115 242 if @membership.deletable?
Chris@0 243 @membership.destroy
Chris@0 244 end
Chris@0 245 respond_to do |format|
Chris@0 246 format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
Chris@1115 247 format.js
Chris@0 248 end
Chris@0 249 end
Chris@909 250
Chris@117 251 private
Chris@909 252
Chris@117 253 def find_user
Chris@117 254 if params[:id] == 'current'
Chris@117 255 require_login || return
Chris@117 256 @user = User.current
Chris@117 257 else
Chris@117 258 @user = User.find(params[:id])
Chris@117 259 end
Chris@117 260 rescue ActiveRecord::RecordNotFound
Chris@117 261 render_404
Chris@117 262 end
Chris@0 263 end