comparison app/controllers/users_controller.rb @ 1338:25603efa57b5

Merge from live branch
author Chris Cannam
date Thu, 20 Jun 2013 13:14:14 +0100
parents 96790506f247
children 4f746d8966dd 51364c0cd58f
comparison
equal deleted inserted replaced
1209:1b1138f6f55e 1338:25603efa57b5
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang 2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
36 @offset, @limit = api_offset_and_limit 36 @offset, @limit = api_offset_and_limit
37 else 37 else
38 @limit = per_page_option 38 @limit = per_page_option
39 end 39 end
40 40
41 scope = User 41 @status = params[:status] || 1
42 scope = scope.in_group(params[:group_id].to_i) if params[:group_id].present? 42
43 43 scope = User.logged.status(@status)
44 @status = params[:status] ? params[:status].to_i : 1 44 scope = scope.like(params[:name]) if params[:name].present?
45 c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status]) 45 scope = scope.in_group(params[:group_id]) if params[:group_id].present?
46 46
47 unless params[:name].blank? 47 @user_count = scope.count
48 name = "%#{params[:name].strip.downcase}%"
49 c << ["LOWER(login) LIKE ? OR LOWER(firstname) LIKE ? OR LOWER(lastname) LIKE ? OR LOWER(mail) LIKE ?", name, name, name, name]
50 end
51
52 @user_count = scope.count(:conditions => c.conditions)
53 @user_pages = Paginator.new self, @user_count, @limit, params['page'] 48 @user_pages = Paginator.new self, @user_count, @limit, params['page']
54 @offset ||= @user_pages.current.offset 49 @offset ||= @user_pages.current.offset
55 @users = scope.find :all, 50 @users = scope.find :all,
56 :order => sort_clause, 51 :order => sort_clause,
57 :conditions => c.conditions,
58 :limit => @limit, 52 :limit => @limit,
59 :offset => @offset 53 :offset => @offset
60 54
61 respond_to do |format| 55 respond_to do |format|
62 format.html { 56 format.html {
69 63
70 def show 64 def show
71 65
72 if @user.ssamr_user_detail != nil 66 if @user.ssamr_user_detail != nil
73 @description = @user.ssamr_user_detail.description 67 @description = @user.ssamr_user_detail.description
74 68 @institution_name = @user.ssamr_user_detail.institution_name
75 if @user.ssamr_user_detail.institution_type != nil
76 # institution_type is true for listed institutions
77 if (@user.ssamr_user_detail.institution_type)
78 @institution_name = Institution.find(@user.ssamr_user_detail.institution_id).name
79 else
80 @institution_name = @user.ssamr_user_detail.other_institution
81 end
82 end
83 end 69 end
84 70
85 # show projects based on current user visibility 71 # show projects based on current user visibility
86 @memberships = @user.memberships.all(:conditions => Project.visible_condition(User.current)) 72 @memberships = @user.memberships.all(:conditions => Project.visible_condition(User.current))
87 73
106 @auth_sources = AuthSource.find(:all) 92 @auth_sources = AuthSource.find(:all)
107 93
108 @ssamr_user_details = SsamrUserDetail.new 94 @ssamr_user_details = SsamrUserDetail.new
109 end 95 end
110 96
111 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
112 def create 97 def create
113 @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option) 98 @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
114 @user.safe_attributes = params[:user] 99 @user.safe_attributes = params[:user]
115 @user = User.new(params[:user]) 100 @user = User.new(params[:user])
116 @user.admin = params[:user][:admin] || false 101 @user.admin = params[:user][:admin] || false
125 110
126 # associates the 2 objects 111 # associates the 2 objects
127 @user.ssamr_user_detail = @ssamr_user_details 112 @user.ssamr_user_detail = @ssamr_user_details
128 113
129 if @user.save 114 if @user.save
115 @user.pref.attributes = params[:pref]
116 @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
130 @user.pref.save 117 @user.pref.save
131 118
132 @ssamr_user_details.save! 119 @ssamr_user_details.save!
133 120
134 121
135 Mailer.deliver_account_information(@user, params[:user][:password]) if params[:send_information] 122 Mailer.account_information(@user, params[:user][:password]).deliver if params[:send_information]
136 123
137 respond_to do |format| 124 respond_to do |format|
138 format.html { 125 format.html {
139 flash[:notice] = l(:notice_successful_create) 126 flash[:notice] = l(:notice_user_successful_create, :id => view_context.link_to(@user.login, user_path(@user)))
140 redirect_to(params[:continue] ? 127 redirect_to(params[:continue] ?
141 {:controller => 'users', :action => 'new'} : 128 {:controller => 'users', :action => 'new'} :
142 {:controller => 'users', :action => 'edit', :id => @user} 129 {:controller => 'users', :action => 'edit', :id => @user}
143 ) 130 )
144 } 131 }
168 155
169 @auth_sources = AuthSource.find(:all) 156 @auth_sources = AuthSource.find(:all)
170 @membership ||= Member.new 157 @membership ||= Member.new
171 end 158 end
172 159
173 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
174 def update 160 def update
175 @user.admin = params[:user][:admin] if params[:user][:admin] 161 @user.admin = params[:user][:admin] if params[:user][:admin]
176 @user.login = params[:user][:login] if params[:user][:login] 162 @user.login = params[:user][:login] if params[:user][:login]
177 if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?) 163 if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?)
178 @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] 164 @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation]
207 if @user.save 193 if @user.save
208 @user.pref.save 194 @user.pref.save
209 @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : []) 195 @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : [])
210 196
211 if was_activated 197 if was_activated
212 Mailer.deliver_account_activated(@user) 198 Mailer.account_activated(@user).deliver
213 elsif @user.active? && params[:send_information] && !params[:user][:password].blank? && @user.auth_source_id.nil? 199 elsif @user.active? && params[:send_information] && !params[:user][:password].blank? && @user.auth_source_id.nil?
214 Mailer.deliver_account_information(@user, params[:user][:password]) 200 Mailer.account_information(@user, params[:user][:password]).deliver
215 end 201 end
216 202
217 respond_to do |format| 203 respond_to do |format|
218 format.html { 204 format.html {
219 flash[:notice] = l(:notice_successful_update) 205 flash[:notice] = l(:notice_successful_update)
220 redirect_to :back 206 redirect_to_referer_or edit_user_path(@user)
221 } 207 }
222 format.api { head :ok } 208 format.api { render_api_ok }
223 end 209 end
224 else 210 else
225 @auth_sources = AuthSource.find(:all) 211 @auth_sources = AuthSource.find(:all)
226 @membership ||= Member.new 212 @membership ||= Member.new
227 # Clear password input 213 # Clear password input
230 respond_to do |format| 216 respond_to do |format|
231 format.html { render :action => :edit } 217 format.html { render :action => :edit }
232 format.api { render_validation_errors(@user) } 218 format.api { render_validation_errors(@user) }
233 end 219 end
234 end 220 end
235 rescue ::ActionController::RedirectBackError 221 end
236 redirect_to :controller => 'users', :action => 'edit', :id => @user 222
237 end
238
239 verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed }
240 def destroy 223 def destroy
241 @user.destroy 224 @user.destroy
242 respond_to do |format| 225 respond_to do |format|
243 format.html { redirect_to(users_url) } 226 format.html { redirect_back_or_default(users_url) }
244 format.api { head :ok } 227 format.api { render_api_ok }
245 end 228 end
246 end 229 end
247 230
248 def edit_membership 231 def edit_membership
249 @membership = Member.edit_membership(params[:membership_id], params[:membership], @user) 232 @membership = Member.edit_membership(params[:membership_id], params[:membership], @user)
250 @membership.save if request.post? 233 @membership.save
251 respond_to do |format| 234 respond_to do |format|
252 if @membership.valid? 235 format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
253 format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } 236 format.js
254 format.js {
255 render(:update) {|page|
256 page.replace_html "tab-content-memberships", :partial => 'users/memberships'
257 page.visual_effect(:highlight, "member-#{@membership.id}")
258 }
259 }
260 else
261 format.js {
262 render(:update) {|page|
263 page.alert(l(:notice_failed_to_save_members, :errors => @membership.errors.full_messages.join(', ')))
264 }
265 }
266 end
267 end 237 end
268 end 238 end
269 239
270 def destroy_membership 240 def destroy_membership
271 @membership = Member.find(params[:membership_id]) 241 @membership = Member.find(params[:membership_id])
272 if request.post? && @membership.deletable? 242 if @membership.deletable?
273 @membership.destroy 243 @membership.destroy
274 end 244 end
275 respond_to do |format| 245 respond_to do |format|
276 format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } 246 format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
277 format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} } 247 format.js
278 end 248 end
279 end 249 end
280 250
281 private 251 private
282 252