comparison app/controllers/.svn/text-base/users_controller.rb.svn-base @ 511:107d36338b70 live

Merge from branch "cannam"
author Chris Cannam
date Thu, 14 Jul 2011 10:43:07 +0100
parents 0c939c159af4
children
comparison
equal deleted inserted replaced
451:a9f6345cb43d 511:107d36338b70
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2009 Jean-Philippe Lang 2 # Copyright (C) 2006-2011 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.
17 17
18 class UsersController < ApplicationController 18 class UsersController < ApplicationController
19 layout 'admin' 19 layout 'admin'
20 20
21 before_filter :require_admin, :except => :show 21 before_filter :require_admin, :except => :show
22 before_filter :find_user, :only => [:show, :edit, :update, :destroy, :edit_membership, :destroy_membership]
23 accept_api_auth :index, :show, :create, :update, :destroy
22 24
23 helper :sort 25 helper :sort
24 include SortHelper 26 include SortHelper
25 helper :custom_fields 27 helper :custom_fields
26 include CustomFieldsHelper 28 include CustomFieldsHelper
27 29
28 def index 30 def index
29 sort_init 'login', 'asc' 31 sort_init 'login', 'asc'
30 sort_update %w(login firstname lastname mail admin created_on last_login_on) 32 sort_update %w(login firstname lastname mail admin created_on last_login_on)
31 33
34 case params[:format]
35 when 'xml', 'json'
36 @offset, @limit = api_offset_and_limit
37 else
38 @limit = per_page_option
39 end
40
41 scope = User
42 scope = scope.in_group(params[:group_id].to_i) if params[:group_id].present?
43
32 @status = params[:status] ? params[:status].to_i : 1 44 @status = params[:status] ? params[:status].to_i : 1
33 c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status]) 45 c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status])
34 46
35 unless params[:name].blank? 47 unless params[:name].blank?
36 name = "%#{params[:name].strip.downcase}%" 48 name = "%#{params[:name].strip.downcase}%"
37 c << ["LOWER(login) LIKE ? OR LOWER(firstname) LIKE ? OR LOWER(lastname) LIKE ? OR LOWER(mail) LIKE ?", name, name, name, name] 49 c << ["LOWER(login) LIKE ? OR LOWER(firstname) LIKE ? OR LOWER(lastname) LIKE ? OR LOWER(mail) LIKE ?", name, name, name, name]
38 end 50 end
39 51
40 @user_count = User.count(:conditions => c.conditions) 52 @user_count = scope.count(:conditions => c.conditions)
41 @user_pages = Paginator.new self, @user_count, 53 @user_pages = Paginator.new self, @user_count, @limit, params['page']
42 per_page_option, 54 @offset ||= @user_pages.current.offset
43 params['page'] 55 @users = scope.find :all,
44 @users = User.find :all,:order => sort_clause, 56 :order => sort_clause,
45 :conditions => c.conditions, 57 :conditions => c.conditions,
46 :limit => @user_pages.items_per_page, 58 :limit => @limit,
47 :offset => @user_pages.current.offset 59 :offset => @offset
48 60
49 render :layout => !request.xhr? 61 respond_to do |format|
62 format.html {
63 @groups = Group.all.sort
64 render :layout => !request.xhr?
65 }
66 format.api
67 end
50 end 68 end
51 69
52 def show 70 def show
53 @user = User.find(params[:id])
54
55 # show projects based on current user visibility 71 # show projects based on current user visibility
56 @memberships = @user.memberships.all(:conditions => Project.visible_by(User.current)) 72 @memberships = @user.memberships.all(:conditions => Project.visible_condition(User.current))
57 73
58 events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10) 74 events = Redmine::Activity::Fetcher.new(User.current, :author => @user).events(nil, nil, :limit => 10)
59 @events_by_day = events.group_by(&:event_date) 75 @events_by_day = events.group_by(&:event_date)
60 76
61 unless User.current.admin? 77 unless User.current.admin?
62 if !@user.active? || (@user != User.current && @memberships.empty? && events.empty?) 78 if !@user.active? || (@user != User.current && @memberships.empty? && events.empty?)
63 render_404 79 render_404
64 return 80 return
65 end 81 end
66 end 82 end
67 render :layout => 'base' 83
68 84 respond_to do |format|
69 rescue ActiveRecord::RecordNotFound 85 format.html { render :layout => 'base' }
70 render_404 86 format.api
87 end
71 end 88 end
72 89
73 def new 90 def new
74 @notification_options = User::MAIL_NOTIFICATION_OPTIONS 91 @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
75 @notification_option = Setting.default_notification_option
76
77 @user = User.new(:language => Setting.default_language)
78 @auth_sources = AuthSource.find(:all) 92 @auth_sources = AuthSource.find(:all)
79 end 93 end
80 94
81 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed } 95 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
82 def create 96 def create
83 @notification_options = User::MAIL_NOTIFICATION_OPTIONS 97 @user = User.new(:language => Setting.default_language, :mail_notification => Setting.default_notification_option)
84 @notification_option = Setting.default_notification_option 98 @user.safe_attributes = params[:user]
85
86 @user = User.new(params[:user])
87 @user.admin = params[:user][:admin] || false 99 @user.admin = params[:user][:admin] || false
88 @user.login = params[:user][:login] 100 @user.login = params[:user][:login]
89 @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless @user.auth_source_id 101 @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation] unless @user.auth_source_id
90 102
91 # TODO: Similar to My#account 103 # TODO: Similar to My#account
92 @user.mail_notification = params[:notification_option] || 'only_my_events'
93 @user.pref.attributes = params[:pref] 104 @user.pref.attributes = params[:pref]
94 @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') 105 @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
95 106
96 if @user.save 107 if @user.save
97 @user.pref.save 108 @user.pref.save
98 @user.notified_project_ids = (params[:notification_option] == 'selected' ? params[:notified_project_ids] : []) 109 @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : [])
99 110
100 Mailer.deliver_account_information(@user, params[:password]) if params[:send_information] 111 Mailer.deliver_account_information(@user, params[:user][:password]) if params[:send_information]
101 flash[:notice] = l(:notice_successful_create) 112
102 redirect_to(params[:continue] ? {:controller => 'users', :action => 'new'} : 113 respond_to do |format|
103 {:controller => 'users', :action => 'edit', :id => @user}) 114 format.html {
104 return 115 flash[:notice] = l(:notice_successful_create)
116 redirect_to(params[:continue] ?
117 {:controller => 'users', :action => 'new'} :
118 {:controller => 'users', :action => 'edit', :id => @user}
119 )
120 }
121 format.api { render :action => 'show', :status => :created, :location => user_url(@user) }
122 end
105 else 123 else
106 @auth_sources = AuthSource.find(:all) 124 @auth_sources = AuthSource.find(:all)
107 @notification_option = @user.mail_notification 125 # Clear password input
108 126 @user.password = @user.password_confirmation = nil
109 render :action => 'new' 127
128 respond_to do |format|
129 format.html { render :action => 'new' }
130 format.api { render_validation_errors(@user) }
131 end
110 end 132 end
111 end 133 end
112 134
113 def edit 135 def edit
114 @user = User.find(params[:id])
115 @notification_options = @user.valid_notification_options
116 @notification_option = @user.mail_notification
117
118 @auth_sources = AuthSource.find(:all) 136 @auth_sources = AuthSource.find(:all)
119 @membership ||= Member.new 137 @membership ||= Member.new
120 end 138 end
121 139
122 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed } 140 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
123 def update 141 def update
124 @user = User.find(params[:id])
125 @notification_options = @user.valid_notification_options
126 @notification_option = @user.mail_notification
127
128 @user.admin = params[:user][:admin] if params[:user][:admin] 142 @user.admin = params[:user][:admin] if params[:user][:admin]
129 @user.login = params[:user][:login] if params[:user][:login] 143 @user.login = params[:user][:login] if params[:user][:login]
130 if params[:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?) 144 if params[:user][:password].present? && (@user.auth_source_id.nil? || params[:user][:auth_source_id].blank?)
131 @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] 145 @user.password, @user.password_confirmation = params[:user][:password], params[:user][:password_confirmation]
132 end 146 end
133 @user.group_ids = params[:user][:group_ids] if params[:user][:group_ids] 147 @user.safe_attributes = params[:user]
134 @user.attributes = params[:user]
135 # Was the account actived ? (do it before User#save clears the change) 148 # Was the account actived ? (do it before User#save clears the change)
136 was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE]) 149 was_activated = (@user.status_change == [User::STATUS_REGISTERED, User::STATUS_ACTIVE])
137 # TODO: Similar to My#account 150 # TODO: Similar to My#account
138 @user.mail_notification = params[:notification_option] || 'only_my_events'
139 @user.pref.attributes = params[:pref] 151 @user.pref.attributes = params[:pref]
140 @user.pref[:no_self_notified] = (params[:no_self_notified] == '1') 152 @user.pref[:no_self_notified] = (params[:no_self_notified] == '1')
141 153
142 if @user.save 154 if @user.save
143 @user.pref.save 155 @user.pref.save
144 @user.notified_project_ids = (params[:notification_option] == 'selected' ? params[:notified_project_ids] : []) 156 @user.notified_project_ids = (@user.mail_notification == 'selected' ? params[:notified_project_ids] : [])
145 157
146 if was_activated 158 if was_activated
147 Mailer.deliver_account_activated(@user) 159 Mailer.deliver_account_activated(@user)
148 elsif @user.active? && params[:send_information] && !params[:password].blank? && @user.auth_source_id.nil? 160 elsif @user.active? && params[:send_information] && !params[:user][:password].blank? && @user.auth_source_id.nil?
149 Mailer.deliver_account_information(@user, params[:password]) 161 Mailer.deliver_account_information(@user, params[:user][:password])
150 end 162 end
151 flash[:notice] = l(:notice_successful_update) 163
152 redirect_to :back 164 respond_to do |format|
165 format.html {
166 flash[:notice] = l(:notice_successful_update)
167 redirect_to :back
168 }
169 format.api { head :ok }
170 end
153 else 171 else
154 @auth_sources = AuthSource.find(:all) 172 @auth_sources = AuthSource.find(:all)
155 @membership ||= Member.new 173 @membership ||= Member.new
156 174 # Clear password input
157 render :action => :edit 175 @user.password = @user.password_confirmation = nil
176
177 respond_to do |format|
178 format.html { render :action => :edit }
179 format.api { render_validation_errors(@user) }
180 end
158 end 181 end
159 rescue ::ActionController::RedirectBackError 182 rescue ::ActionController::RedirectBackError
160 redirect_to :controller => 'users', :action => 'edit', :id => @user 183 redirect_to :controller => 'users', :action => 'edit', :id => @user
161 end 184 end
162 185
186 verify :method => :delete, :only => :destroy, :render => {:nothing => true, :status => :method_not_allowed }
187 def destroy
188 @user.destroy
189 respond_to do |format|
190 format.html { redirect_to(users_url) }
191 format.api { head :ok }
192 end
193 end
194
163 def edit_membership 195 def edit_membership
164 @user = User.find(params[:id])
165 @membership = Member.edit_membership(params[:membership_id], params[:membership], @user) 196 @membership = Member.edit_membership(params[:membership_id], params[:membership], @user)
166 @membership.save if request.post? 197 @membership.save if request.post?
167 respond_to do |format| 198 respond_to do |format|
168 if @membership.valid? 199 if @membership.valid?
169 format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } 200 format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
182 end 213 end
183 end 214 end
184 end 215 end
185 216
186 def destroy_membership 217 def destroy_membership
187 @user = User.find(params[:id])
188 @membership = Member.find(params[:membership_id]) 218 @membership = Member.find(params[:membership_id])
189 if request.post? && @membership.deletable? 219 if request.post? && @membership.deletable?
190 @membership.destroy 220 @membership.destroy
191 end 221 end
192 respond_to do |format| 222 respond_to do |format|
193 format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' } 223 format.html { redirect_to :controller => 'users', :action => 'edit', :id => @user, :tab => 'memberships' }
194 format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} } 224 format.js { render(:update) {|page| page.replace_html "tab-content-memberships", :partial => 'users/memberships'} }
195 end 225 end
196 end 226 end
227
228 private
229
230 def find_user
231 if params[:id] == 'current'
232 require_login || return
233 @user = User.current
234 else
235 @user = User.find(params[:id])
236 end
237 rescue ActiveRecord::RecordNotFound
238 render_404
239 end
197 end 240 end