annotate app/controllers/users_controller.rb @ 1158:00e9a262ffe5 redmine-2.2-integration

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