@@ -1,298 +1,298 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2013 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2013 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. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class AccountController < ApplicationController |
|
18 | class AccountController < ApplicationController | |
19 | helper :custom_fields |
|
19 | helper :custom_fields | |
20 | include CustomFieldsHelper |
|
20 | include CustomFieldsHelper | |
21 |
|
21 | |||
22 | # prevents login action to be filtered by check_if_login_required application scope filter |
|
22 | # prevents login action to be filtered by check_if_login_required application scope filter | |
23 | skip_before_filter :check_if_login_required |
|
23 | skip_before_filter :check_if_login_required | |
24 |
|
24 | |||
25 | # Login request and validation |
|
25 | # Login request and validation | |
26 | def login |
|
26 | def login | |
27 | if request.get? |
|
27 | if request.get? | |
28 | if User.current.logged? |
|
28 | if User.current.logged? | |
29 | redirect_to home_url |
|
29 | redirect_to home_url | |
30 | end |
|
30 | end | |
31 | else |
|
31 | else | |
32 | authenticate_user |
|
32 | authenticate_user | |
33 | end |
|
33 | end | |
34 | rescue AuthSourceException => e |
|
34 | rescue AuthSourceException => e | |
35 | logger.error "An error occured when authenticating #{params[:username]}: #{e.message}" |
|
35 | logger.error "An error occured when authenticating #{params[:username]}: #{e.message}" | |
36 | render_error :message => e.message |
|
36 | render_error :message => e.message | |
37 | end |
|
37 | end | |
38 |
|
38 | |||
39 | # Log out current user and redirect to welcome page |
|
39 | # Log out current user and redirect to welcome page | |
40 | def logout |
|
40 | def logout | |
41 | logout_user |
|
41 | logout_user | |
42 | redirect_to home_url |
|
42 | redirect_to home_url | |
43 | end |
|
43 | end | |
44 |
|
44 | |||
45 | # Lets user choose a new password |
|
45 | # Lets user choose a new password | |
46 | def lost_password |
|
46 | def lost_password | |
47 |
redirect_to(home_url) |
|
47 | (redirect_to(home_url); return) unless Setting.lost_password? | |
48 | if params[:token] |
|
48 | if params[:token] | |
49 | @token = Token.find_by_action_and_value("recovery", params[:token].to_s) |
|
49 | @token = Token.find_by_action_and_value("recovery", params[:token].to_s) | |
50 | if @token.nil? || @token.expired? |
|
50 | if @token.nil? || @token.expired? | |
51 | redirect_to home_url |
|
51 | redirect_to home_url | |
52 | return |
|
52 | return | |
53 | end |
|
53 | end | |
54 | @user = @token.user |
|
54 | @user = @token.user | |
55 | unless @user && @user.active? |
|
55 | unless @user && @user.active? | |
56 | redirect_to home_url |
|
56 | redirect_to home_url | |
57 | return |
|
57 | return | |
58 | end |
|
58 | end | |
59 | if request.post? |
|
59 | if request.post? | |
60 | @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation] |
|
60 | @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation] | |
61 | if @user.save |
|
61 | if @user.save | |
62 | @token.destroy |
|
62 | @token.destroy | |
63 | flash[:notice] = l(:notice_account_password_updated) |
|
63 | flash[:notice] = l(:notice_account_password_updated) | |
64 | redirect_to signin_path |
|
64 | redirect_to signin_path | |
65 | return |
|
65 | return | |
66 | end |
|
66 | end | |
67 | end |
|
67 | end | |
68 | render :template => "account/password_recovery" |
|
68 | render :template => "account/password_recovery" | |
69 | return |
|
69 | return | |
70 | else |
|
70 | else | |
71 | if request.post? |
|
71 | if request.post? | |
72 | user = User.find_by_mail(params[:mail].to_s) |
|
72 | user = User.find_by_mail(params[:mail].to_s) | |
73 | # user not found or not active |
|
73 | # user not found or not active | |
74 | unless user && user.active? |
|
74 | unless user && user.active? | |
75 | flash.now[:error] = l(:notice_account_unknown_email) |
|
75 | flash.now[:error] = l(:notice_account_unknown_email) | |
76 | return |
|
76 | return | |
77 | end |
|
77 | end | |
78 | # user cannot change its password |
|
78 | # user cannot change its password | |
79 | unless user.change_password_allowed? |
|
79 | unless user.change_password_allowed? | |
80 | flash.now[:error] = l(:notice_can_t_change_password) |
|
80 | flash.now[:error] = l(:notice_can_t_change_password) | |
81 | return |
|
81 | return | |
82 | end |
|
82 | end | |
83 | # create a new token for password recovery |
|
83 | # create a new token for password recovery | |
84 | token = Token.new(:user => user, :action => "recovery") |
|
84 | token = Token.new(:user => user, :action => "recovery") | |
85 | if token.save |
|
85 | if token.save | |
86 | Mailer.lost_password(token).deliver |
|
86 | Mailer.lost_password(token).deliver | |
87 | flash[:notice] = l(:notice_account_lost_email_sent) |
|
87 | flash[:notice] = l(:notice_account_lost_email_sent) | |
88 | redirect_to signin_path |
|
88 | redirect_to signin_path | |
89 | return |
|
89 | return | |
90 | end |
|
90 | end | |
91 | end |
|
91 | end | |
92 | end |
|
92 | end | |
93 | end |
|
93 | end | |
94 |
|
94 | |||
95 | # User self-registration |
|
95 | # User self-registration | |
96 | def register |
|
96 | def register | |
97 |
redirect_to(home_url) |
|
97 | (redirect_to(home_url); return) unless Setting.self_registration? || session[:auth_source_registration] | |
98 | if request.get? |
|
98 | if request.get? | |
99 | session[:auth_source_registration] = nil |
|
99 | session[:auth_source_registration] = nil | |
100 | @user = User.new(:language => current_language.to_s) |
|
100 | @user = User.new(:language => current_language.to_s) | |
101 | else |
|
101 | else | |
102 | user_params = params[:user] || {} |
|
102 | user_params = params[:user] || {} | |
103 | @user = User.new |
|
103 | @user = User.new | |
104 | @user.safe_attributes = user_params |
|
104 | @user.safe_attributes = user_params | |
105 | @user.admin = false |
|
105 | @user.admin = false | |
106 | @user.register |
|
106 | @user.register | |
107 | if session[:auth_source_registration] |
|
107 | if session[:auth_source_registration] | |
108 | @user.activate |
|
108 | @user.activate | |
109 | @user.login = session[:auth_source_registration][:login] |
|
109 | @user.login = session[:auth_source_registration][:login] | |
110 | @user.auth_source_id = session[:auth_source_registration][:auth_source_id] |
|
110 | @user.auth_source_id = session[:auth_source_registration][:auth_source_id] | |
111 | if @user.save |
|
111 | if @user.save | |
112 | session[:auth_source_registration] = nil |
|
112 | session[:auth_source_registration] = nil | |
113 | self.logged_user = @user |
|
113 | self.logged_user = @user | |
114 | flash[:notice] = l(:notice_account_activated) |
|
114 | flash[:notice] = l(:notice_account_activated) | |
115 | redirect_to my_account_path |
|
115 | redirect_to my_account_path | |
116 | end |
|
116 | end | |
117 | else |
|
117 | else | |
118 | @user.login = params[:user][:login] |
|
118 | @user.login = params[:user][:login] | |
119 | unless user_params[:identity_url].present? && user_params[:password].blank? && user_params[:password_confirmation].blank? |
|
119 | unless user_params[:identity_url].present? && user_params[:password].blank? && user_params[:password_confirmation].blank? | |
120 | @user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation] |
|
120 | @user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation] | |
121 | end |
|
121 | end | |
122 |
|
122 | |||
123 | case Setting.self_registration |
|
123 | case Setting.self_registration | |
124 | when '1' |
|
124 | when '1' | |
125 | register_by_email_activation(@user) |
|
125 | register_by_email_activation(@user) | |
126 | when '3' |
|
126 | when '3' | |
127 | register_automatically(@user) |
|
127 | register_automatically(@user) | |
128 | else |
|
128 | else | |
129 | register_manually_by_administrator(@user) |
|
129 | register_manually_by_administrator(@user) | |
130 | end |
|
130 | end | |
131 | end |
|
131 | end | |
132 | end |
|
132 | end | |
133 | end |
|
133 | end | |
134 |
|
134 | |||
135 | # Token based account activation |
|
135 | # Token based account activation | |
136 | def activate |
|
136 | def activate | |
137 | (redirect_to(home_url); return) unless Setting.self_registration? && params[:token].present? |
|
137 | (redirect_to(home_url); return) unless Setting.self_registration? && params[:token].present? | |
138 | token = Token.find_by_action_and_value('register', params[:token].to_s) |
|
138 | token = Token.find_by_action_and_value('register', params[:token].to_s) | |
139 | (redirect_to(home_url); return) unless token and !token.expired? |
|
139 | (redirect_to(home_url); return) unless token and !token.expired? | |
140 | user = token.user |
|
140 | user = token.user | |
141 | (redirect_to(home_url); return) unless user.registered? |
|
141 | (redirect_to(home_url); return) unless user.registered? | |
142 | user.activate |
|
142 | user.activate | |
143 | if user.save |
|
143 | if user.save | |
144 | token.destroy |
|
144 | token.destroy | |
145 | flash[:notice] = l(:notice_account_activated) |
|
145 | flash[:notice] = l(:notice_account_activated) | |
146 | end |
|
146 | end | |
147 | redirect_to signin_path |
|
147 | redirect_to signin_path | |
148 | end |
|
148 | end | |
149 |
|
149 | |||
150 | private |
|
150 | private | |
151 |
|
151 | |||
152 | def authenticate_user |
|
152 | def authenticate_user | |
153 | if Setting.openid? && using_open_id? |
|
153 | if Setting.openid? && using_open_id? | |
154 | open_id_authenticate(params[:openid_url]) |
|
154 | open_id_authenticate(params[:openid_url]) | |
155 | else |
|
155 | else | |
156 | password_authentication |
|
156 | password_authentication | |
157 | end |
|
157 | end | |
158 | end |
|
158 | end | |
159 |
|
159 | |||
160 | def password_authentication |
|
160 | def password_authentication | |
161 | user = User.try_to_login(params[:username], params[:password]) |
|
161 | user = User.try_to_login(params[:username], params[:password]) | |
162 |
|
162 | |||
163 | if user.nil? |
|
163 | if user.nil? | |
164 | invalid_credentials |
|
164 | invalid_credentials | |
165 | elsif user.new_record? |
|
165 | elsif user.new_record? | |
166 | onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id }) |
|
166 | onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id }) | |
167 | else |
|
167 | else | |
168 | # Valid user |
|
168 | # Valid user | |
169 | successful_authentication(user) |
|
169 | successful_authentication(user) | |
170 | end |
|
170 | end | |
171 | end |
|
171 | end | |
172 |
|
172 | |||
173 | def open_id_authenticate(openid_url) |
|
173 | def open_id_authenticate(openid_url) | |
174 | authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url, :method => :post) do |result, identity_url, registration| |
|
174 | authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url, :method => :post) do |result, identity_url, registration| | |
175 | if result.successful? |
|
175 | if result.successful? | |
176 | user = User.find_or_initialize_by_identity_url(identity_url) |
|
176 | user = User.find_or_initialize_by_identity_url(identity_url) | |
177 | if user.new_record? |
|
177 | if user.new_record? | |
178 | # Self-registration off |
|
178 | # Self-registration off | |
179 |
redirect_to(home_url) |
|
179 | (redirect_to(home_url); return) unless Setting.self_registration? | |
180 |
|
180 | |||
181 | # Create on the fly |
|
181 | # Create on the fly | |
182 | user.login = registration['nickname'] unless registration['nickname'].nil? |
|
182 | user.login = registration['nickname'] unless registration['nickname'].nil? | |
183 | user.mail = registration['email'] unless registration['email'].nil? |
|
183 | user.mail = registration['email'] unless registration['email'].nil? | |
184 | user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil? |
|
184 | user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil? | |
185 | user.random_password |
|
185 | user.random_password | |
186 | user.register |
|
186 | user.register | |
187 |
|
187 | |||
188 | case Setting.self_registration |
|
188 | case Setting.self_registration | |
189 | when '1' |
|
189 | when '1' | |
190 | register_by_email_activation(user) do |
|
190 | register_by_email_activation(user) do | |
191 | onthefly_creation_failed(user) |
|
191 | onthefly_creation_failed(user) | |
192 | end |
|
192 | end | |
193 | when '3' |
|
193 | when '3' | |
194 | register_automatically(user) do |
|
194 | register_automatically(user) do | |
195 | onthefly_creation_failed(user) |
|
195 | onthefly_creation_failed(user) | |
196 | end |
|
196 | end | |
197 | else |
|
197 | else | |
198 | register_manually_by_administrator(user) do |
|
198 | register_manually_by_administrator(user) do | |
199 | onthefly_creation_failed(user) |
|
199 | onthefly_creation_failed(user) | |
200 | end |
|
200 | end | |
201 | end |
|
201 | end | |
202 | else |
|
202 | else | |
203 | # Existing record |
|
203 | # Existing record | |
204 | if user.active? |
|
204 | if user.active? | |
205 | successful_authentication(user) |
|
205 | successful_authentication(user) | |
206 | else |
|
206 | else | |
207 | account_pending |
|
207 | account_pending | |
208 | end |
|
208 | end | |
209 | end |
|
209 | end | |
210 | end |
|
210 | end | |
211 | end |
|
211 | end | |
212 | end |
|
212 | end | |
213 |
|
213 | |||
214 | def successful_authentication(user) |
|
214 | def successful_authentication(user) | |
215 | logger.info "Successful authentication for '#{user.login}' from #{request.remote_ip} at #{Time.now.utc}" |
|
215 | logger.info "Successful authentication for '#{user.login}' from #{request.remote_ip} at #{Time.now.utc}" | |
216 | # Valid user |
|
216 | # Valid user | |
217 | self.logged_user = user |
|
217 | self.logged_user = user | |
218 | # generate a key and set cookie if autologin |
|
218 | # generate a key and set cookie if autologin | |
219 | if params[:autologin] && Setting.autologin? |
|
219 | if params[:autologin] && Setting.autologin? | |
220 | set_autologin_cookie(user) |
|
220 | set_autologin_cookie(user) | |
221 | end |
|
221 | end | |
222 | call_hook(:controller_account_success_authentication_after, {:user => user }) |
|
222 | call_hook(:controller_account_success_authentication_after, {:user => user }) | |
223 | redirect_back_or_default my_page_path |
|
223 | redirect_back_or_default my_page_path | |
224 | end |
|
224 | end | |
225 |
|
225 | |||
226 | def set_autologin_cookie(user) |
|
226 | def set_autologin_cookie(user) | |
227 | token = Token.create(:user => user, :action => 'autologin') |
|
227 | token = Token.create(:user => user, :action => 'autologin') | |
228 | cookie_name = Redmine::Configuration['autologin_cookie_name'] || 'autologin' |
|
228 | cookie_name = Redmine::Configuration['autologin_cookie_name'] || 'autologin' | |
229 | cookie_options = { |
|
229 | cookie_options = { | |
230 | :value => token.value, |
|
230 | :value => token.value, | |
231 | :expires => 1.year.from_now, |
|
231 | :expires => 1.year.from_now, | |
232 | :path => (Redmine::Configuration['autologin_cookie_path'] || '/'), |
|
232 | :path => (Redmine::Configuration['autologin_cookie_path'] || '/'), | |
233 | :secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false), |
|
233 | :secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false), | |
234 | :httponly => true |
|
234 | :httponly => true | |
235 | } |
|
235 | } | |
236 | cookies[cookie_name] = cookie_options |
|
236 | cookies[cookie_name] = cookie_options | |
237 | end |
|
237 | end | |
238 |
|
238 | |||
239 | # Onthefly creation failed, display the registration form to fill/fix attributes |
|
239 | # Onthefly creation failed, display the registration form to fill/fix attributes | |
240 | def onthefly_creation_failed(user, auth_source_options = { }) |
|
240 | def onthefly_creation_failed(user, auth_source_options = { }) | |
241 | @user = user |
|
241 | @user = user | |
242 | session[:auth_source_registration] = auth_source_options unless auth_source_options.empty? |
|
242 | session[:auth_source_registration] = auth_source_options unless auth_source_options.empty? | |
243 | render :action => 'register' |
|
243 | render :action => 'register' | |
244 | end |
|
244 | end | |
245 |
|
245 | |||
246 | def invalid_credentials |
|
246 | def invalid_credentials | |
247 | logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}" |
|
247 | logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}" | |
248 | flash.now[:error] = l(:notice_account_invalid_creditentials) |
|
248 | flash.now[:error] = l(:notice_account_invalid_creditentials) | |
249 | end |
|
249 | end | |
250 |
|
250 | |||
251 | # Register a user for email activation. |
|
251 | # Register a user for email activation. | |
252 | # |
|
252 | # | |
253 | # Pass a block for behavior when a user fails to save |
|
253 | # Pass a block for behavior when a user fails to save | |
254 | def register_by_email_activation(user, &block) |
|
254 | def register_by_email_activation(user, &block) | |
255 | token = Token.new(:user => user, :action => "register") |
|
255 | token = Token.new(:user => user, :action => "register") | |
256 | if user.save and token.save |
|
256 | if user.save and token.save | |
257 | Mailer.register(token).deliver |
|
257 | Mailer.register(token).deliver | |
258 | flash[:notice] = l(:notice_account_register_done) |
|
258 | flash[:notice] = l(:notice_account_register_done) | |
259 | redirect_to signin_path |
|
259 | redirect_to signin_path | |
260 | else |
|
260 | else | |
261 | yield if block_given? |
|
261 | yield if block_given? | |
262 | end |
|
262 | end | |
263 | end |
|
263 | end | |
264 |
|
264 | |||
265 | # Automatically register a user |
|
265 | # Automatically register a user | |
266 | # |
|
266 | # | |
267 | # Pass a block for behavior when a user fails to save |
|
267 | # Pass a block for behavior when a user fails to save | |
268 | def register_automatically(user, &block) |
|
268 | def register_automatically(user, &block) | |
269 | # Automatic activation |
|
269 | # Automatic activation | |
270 | user.activate |
|
270 | user.activate | |
271 | user.last_login_on = Time.now |
|
271 | user.last_login_on = Time.now | |
272 | if user.save |
|
272 | if user.save | |
273 | self.logged_user = user |
|
273 | self.logged_user = user | |
274 | flash[:notice] = l(:notice_account_activated) |
|
274 | flash[:notice] = l(:notice_account_activated) | |
275 | redirect_to my_account_path |
|
275 | redirect_to my_account_path | |
276 | else |
|
276 | else | |
277 | yield if block_given? |
|
277 | yield if block_given? | |
278 | end |
|
278 | end | |
279 | end |
|
279 | end | |
280 |
|
280 | |||
281 | # Manual activation by the administrator |
|
281 | # Manual activation by the administrator | |
282 | # |
|
282 | # | |
283 | # Pass a block for behavior when a user fails to save |
|
283 | # Pass a block for behavior when a user fails to save | |
284 | def register_manually_by_administrator(user, &block) |
|
284 | def register_manually_by_administrator(user, &block) | |
285 | if user.save |
|
285 | if user.save | |
286 | # Sends an email to the administrators |
|
286 | # Sends an email to the administrators | |
287 | Mailer.account_activation_request(user).deliver |
|
287 | Mailer.account_activation_request(user).deliver | |
288 | account_pending |
|
288 | account_pending | |
289 | else |
|
289 | else | |
290 | yield if block_given? |
|
290 | yield if block_given? | |
291 | end |
|
291 | end | |
292 | end |
|
292 | end | |
293 |
|
293 | |||
294 | def account_pending |
|
294 | def account_pending | |
295 | flash[:notice] = l(:notice_account_pending) |
|
295 | flash[:notice] = l(:notice_account_pending) | |
296 | redirect_to signin_path |
|
296 | redirect_to signin_path | |
297 | end |
|
297 | end | |
298 | end |
|
298 | end |
@@ -1,262 +1,263 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2013 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2013 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. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class ProjectsController < ApplicationController |
|
18 | class ProjectsController < ApplicationController | |
19 | menu_item :overview |
|
19 | menu_item :overview | |
20 | menu_item :roadmap, :only => :roadmap |
|
20 | menu_item :roadmap, :only => :roadmap | |
21 | menu_item :settings, :only => :settings |
|
21 | menu_item :settings, :only => :settings | |
22 |
|
22 | |||
23 | before_filter :find_project, :except => [ :index, :list, :new, :create, :copy ] |
|
23 | before_filter :find_project, :except => [ :index, :list, :new, :create, :copy ] | |
24 | before_filter :authorize, :except => [ :index, :list, :new, :create, :copy, :archive, :unarchive, :destroy] |
|
24 | before_filter :authorize, :except => [ :index, :list, :new, :create, :copy, :archive, :unarchive, :destroy] | |
25 | before_filter :authorize_global, :only => [:new, :create] |
|
25 | before_filter :authorize_global, :only => [:new, :create] | |
26 | before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ] |
|
26 | before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ] | |
27 | accept_rss_auth :index |
|
27 | accept_rss_auth :index | |
28 | accept_api_auth :index, :show, :create, :update, :destroy |
|
28 | accept_api_auth :index, :show, :create, :update, :destroy | |
29 |
|
29 | |||
30 | after_filter :only => [:create, :edit, :update, :archive, :unarchive, :destroy] do |controller| |
|
30 | after_filter :only => [:create, :edit, :update, :archive, :unarchive, :destroy] do |controller| | |
31 | if controller.request.post? |
|
31 | if controller.request.post? | |
32 | controller.send :expire_action, :controller => 'welcome', :action => 'robots' |
|
32 | controller.send :expire_action, :controller => 'welcome', :action => 'robots' | |
33 | end |
|
33 | end | |
34 | end |
|
34 | end | |
35 |
|
35 | |||
36 | helper :sort |
|
36 | helper :sort | |
37 | include SortHelper |
|
37 | include SortHelper | |
38 | helper :custom_fields |
|
38 | helper :custom_fields | |
39 | include CustomFieldsHelper |
|
39 | include CustomFieldsHelper | |
40 | helper :issues |
|
40 | helper :issues | |
41 | helper :queries |
|
41 | helper :queries | |
42 | include QueriesHelper |
|
42 | include QueriesHelper | |
43 | helper :repositories |
|
43 | helper :repositories | |
44 | include RepositoriesHelper |
|
44 | include RepositoriesHelper | |
45 | include ProjectsHelper |
|
45 | include ProjectsHelper | |
46 | helper :members |
|
46 | helper :members | |
47 |
|
47 | |||
48 | # Lists visible projects |
|
48 | # Lists visible projects | |
49 | def index |
|
49 | def index | |
50 | respond_to do |format| |
|
50 | respond_to do |format| | |
51 | format.html { |
|
51 | format.html { | |
52 | scope = Project |
|
52 | scope = Project | |
53 | unless params[:closed] |
|
53 | unless params[:closed] | |
54 | scope = scope.active |
|
54 | scope = scope.active | |
55 | end |
|
55 | end | |
56 | @projects = scope.visible.order('lft').all |
|
56 | @projects = scope.visible.order('lft').all | |
57 | } |
|
57 | } | |
58 | format.api { |
|
58 | format.api { | |
59 | @offset, @limit = api_offset_and_limit |
|
59 | @offset, @limit = api_offset_and_limit | |
60 | @project_count = Project.visible.count |
|
60 | @project_count = Project.visible.count | |
61 | @projects = Project.visible.offset(@offset).limit(@limit).order('lft').all |
|
61 | @projects = Project.visible.offset(@offset).limit(@limit).order('lft').all | |
62 | } |
|
62 | } | |
63 | format.atom { |
|
63 | format.atom { | |
64 | projects = Project.visible.order('created_on DESC').limit(Setting.feeds_limit.to_i).all |
|
64 | projects = Project.visible.order('created_on DESC').limit(Setting.feeds_limit.to_i).all | |
65 | render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}") |
|
65 | render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}") | |
66 | } |
|
66 | } | |
67 | end |
|
67 | end | |
68 | end |
|
68 | end | |
69 |
|
69 | |||
70 | def new |
|
70 | def new | |
71 | @issue_custom_fields = IssueCustomField.sorted.all |
|
71 | @issue_custom_fields = IssueCustomField.sorted.all | |
72 | @trackers = Tracker.sorted.all |
|
72 | @trackers = Tracker.sorted.all | |
73 | @project = Project.new |
|
73 | @project = Project.new | |
74 | @project.safe_attributes = params[:project] |
|
74 | @project.safe_attributes = params[:project] | |
75 | end |
|
75 | end | |
76 |
|
76 | |||
77 | def create |
|
77 | def create | |
78 | @issue_custom_fields = IssueCustomField.sorted.all |
|
78 | @issue_custom_fields = IssueCustomField.sorted.all | |
79 | @trackers = Tracker.sorted.all |
|
79 | @trackers = Tracker.sorted.all | |
80 | @project = Project.new |
|
80 | @project = Project.new | |
81 | @project.safe_attributes = params[:project] |
|
81 | @project.safe_attributes = params[:project] | |
82 |
|
82 | |||
83 | if validate_parent_id && @project.save |
|
83 | if validate_parent_id && @project.save | |
84 | @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') |
|
84 | @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') | |
85 | # Add current user as a project member if he is not admin |
|
85 | # Add current user as a project member if he is not admin | |
86 | unless User.current.admin? |
|
86 | unless User.current.admin? | |
87 | r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first |
|
87 | r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first | |
88 | m = Member.new(:user => User.current, :roles => [r]) |
|
88 | m = Member.new(:user => User.current, :roles => [r]) | |
89 | @project.members << m |
|
89 | @project.members << m | |
90 | end |
|
90 | end | |
91 | respond_to do |format| |
|
91 | respond_to do |format| | |
92 | format.html { |
|
92 | format.html { | |
93 | flash[:notice] = l(:notice_successful_create) |
|
93 | flash[:notice] = l(:notice_successful_create) | |
94 | if params[:continue] |
|
94 | if params[:continue] | |
95 | attrs = {:parent_id => @project.parent_id}.reject {|k,v| v.nil?} |
|
95 | attrs = {:parent_id => @project.parent_id}.reject {|k,v| v.nil?} | |
96 | redirect_to new_project_path(attrs) |
|
96 | redirect_to new_project_path(attrs) | |
97 | else |
|
97 | else | |
98 | redirect_to settings_project_path(@project) |
|
98 | redirect_to settings_project_path(@project) | |
99 | end |
|
99 | end | |
100 | } |
|
100 | } | |
101 | format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) } |
|
101 | format.api { render :action => 'show', :status => :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) } | |
102 | end |
|
102 | end | |
103 | else |
|
103 | else | |
104 | respond_to do |format| |
|
104 | respond_to do |format| | |
105 | format.html { render :action => 'new' } |
|
105 | format.html { render :action => 'new' } | |
106 | format.api { render_validation_errors(@project) } |
|
106 | format.api { render_validation_errors(@project) } | |
107 | end |
|
107 | end | |
108 | end |
|
108 | end | |
109 | end |
|
109 | end | |
110 |
|
110 | |||
111 | def copy |
|
111 | def copy | |
112 | @issue_custom_fields = IssueCustomField.sorted.all |
|
112 | @issue_custom_fields = IssueCustomField.sorted.all | |
113 | @trackers = Tracker.sorted.all |
|
113 | @trackers = Tracker.sorted.all | |
114 | @source_project = Project.find(params[:id]) |
|
114 | @source_project = Project.find(params[:id]) | |
115 | if request.get? |
|
115 | if request.get? | |
116 | @project = Project.copy_from(@source_project) |
|
116 | @project = Project.copy_from(@source_project) | |
117 | @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers? |
|
117 | @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers? | |
118 | else |
|
118 | else | |
119 | Mailer.with_deliveries(params[:notifications] == '1') do |
|
119 | Mailer.with_deliveries(params[:notifications] == '1') do | |
120 | @project = Project.new |
|
120 | @project = Project.new | |
121 | @project.safe_attributes = params[:project] |
|
121 | @project.safe_attributes = params[:project] | |
122 | if validate_parent_id && @project.copy(@source_project, :only => params[:only]) |
|
122 | if validate_parent_id && @project.copy(@source_project, :only => params[:only]) | |
123 | @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') |
|
123 | @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') | |
124 | flash[:notice] = l(:notice_successful_create) |
|
124 | flash[:notice] = l(:notice_successful_create) | |
125 | redirect_to settings_project_path(@project) |
|
125 | redirect_to settings_project_path(@project) | |
126 | elsif !@project.new_record? |
|
126 | elsif !@project.new_record? | |
127 | # Project was created |
|
127 | # Project was created | |
128 | # But some objects were not copied due to validation failures |
|
128 | # But some objects were not copied due to validation failures | |
129 | # (eg. issues from disabled trackers) |
|
129 | # (eg. issues from disabled trackers) | |
130 | # TODO: inform about that |
|
130 | # TODO: inform about that | |
131 | redirect_to settings_project_path(@project) |
|
131 | redirect_to settings_project_path(@project) | |
132 | end |
|
132 | end | |
133 | end |
|
133 | end | |
134 | end |
|
134 | end | |
135 | rescue ActiveRecord::RecordNotFound |
|
135 | rescue ActiveRecord::RecordNotFound | |
136 | # source_project not found |
|
136 | # source_project not found | |
137 | render_404 |
|
137 | render_404 | |
138 | end |
|
138 | end | |
139 |
|
139 | |||
140 | # Show @project |
|
140 | # Show @project | |
141 | def show |
|
141 | def show | |
142 | if params[:jump] |
|
142 | if params[:jump] | |
143 | # try to redirect to the requested menu item |
|
143 | # try to redirect to the requested menu item | |
144 |
redirect_to_project_menu_item(@project, params[:jump]) |
|
144 | redirect_to_project_menu_item(@project, params[:jump]) | |
|
145 | return | |||
145 | end |
|
146 | end | |
146 |
|
147 | |||
147 | @users_by_role = @project.users_by_role |
|
148 | @users_by_role = @project.users_by_role | |
148 | @subprojects = @project.children.visible.all |
|
149 | @subprojects = @project.children.visible.all | |
149 | @news = @project.news.limit(5).includes(:author, :project).reorder("#{News.table_name}.created_on DESC").all |
|
150 | @news = @project.news.limit(5).includes(:author, :project).reorder("#{News.table_name}.created_on DESC").all | |
150 | @trackers = @project.rolled_up_trackers |
|
151 | @trackers = @project.rolled_up_trackers | |
151 |
|
152 | |||
152 | cond = @project.project_condition(Setting.display_subprojects_issues?) |
|
153 | cond = @project.project_condition(Setting.display_subprojects_issues?) | |
153 |
|
154 | |||
154 | @open_issues_by_tracker = Issue.visible.open.where(cond).count(:group => :tracker) |
|
155 | @open_issues_by_tracker = Issue.visible.open.where(cond).count(:group => :tracker) | |
155 | @total_issues_by_tracker = Issue.visible.where(cond).count(:group => :tracker) |
|
156 | @total_issues_by_tracker = Issue.visible.where(cond).count(:group => :tracker) | |
156 |
|
157 | |||
157 | if User.current.allowed_to?(:view_time_entries, @project) |
|
158 | if User.current.allowed_to?(:view_time_entries, @project) | |
158 | @total_hours = TimeEntry.visible.sum(:hours, :include => :project, :conditions => cond).to_f |
|
159 | @total_hours = TimeEntry.visible.sum(:hours, :include => :project, :conditions => cond).to_f | |
159 | end |
|
160 | end | |
160 |
|
161 | |||
161 | @key = User.current.rss_key |
|
162 | @key = User.current.rss_key | |
162 |
|
163 | |||
163 | respond_to do |format| |
|
164 | respond_to do |format| | |
164 | format.html |
|
165 | format.html | |
165 | format.api |
|
166 | format.api | |
166 | end |
|
167 | end | |
167 | end |
|
168 | end | |
168 |
|
169 | |||
169 | def settings |
|
170 | def settings | |
170 | @issue_custom_fields = IssueCustomField.sorted.all |
|
171 | @issue_custom_fields = IssueCustomField.sorted.all | |
171 | @issue_category ||= IssueCategory.new |
|
172 | @issue_category ||= IssueCategory.new | |
172 | @member ||= @project.members.new |
|
173 | @member ||= @project.members.new | |
173 | @trackers = Tracker.sorted.all |
|
174 | @trackers = Tracker.sorted.all | |
174 | @wiki ||= @project.wiki |
|
175 | @wiki ||= @project.wiki | |
175 | end |
|
176 | end | |
176 |
|
177 | |||
177 | def edit |
|
178 | def edit | |
178 | end |
|
179 | end | |
179 |
|
180 | |||
180 | def update |
|
181 | def update | |
181 | @project.safe_attributes = params[:project] |
|
182 | @project.safe_attributes = params[:project] | |
182 | if validate_parent_id && @project.save |
|
183 | if validate_parent_id && @project.save | |
183 | @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') |
|
184 | @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id') | |
184 | respond_to do |format| |
|
185 | respond_to do |format| | |
185 | format.html { |
|
186 | format.html { | |
186 | flash[:notice] = l(:notice_successful_update) |
|
187 | flash[:notice] = l(:notice_successful_update) | |
187 | redirect_to settings_project_path(@project) |
|
188 | redirect_to settings_project_path(@project) | |
188 | } |
|
189 | } | |
189 | format.api { render_api_ok } |
|
190 | format.api { render_api_ok } | |
190 | end |
|
191 | end | |
191 | else |
|
192 | else | |
192 | respond_to do |format| |
|
193 | respond_to do |format| | |
193 | format.html { |
|
194 | format.html { | |
194 | settings |
|
195 | settings | |
195 | render :action => 'settings' |
|
196 | render :action => 'settings' | |
196 | } |
|
197 | } | |
197 | format.api { render_validation_errors(@project) } |
|
198 | format.api { render_validation_errors(@project) } | |
198 | end |
|
199 | end | |
199 | end |
|
200 | end | |
200 | end |
|
201 | end | |
201 |
|
202 | |||
202 | def modules |
|
203 | def modules | |
203 | @project.enabled_module_names = params[:enabled_module_names] |
|
204 | @project.enabled_module_names = params[:enabled_module_names] | |
204 | flash[:notice] = l(:notice_successful_update) |
|
205 | flash[:notice] = l(:notice_successful_update) | |
205 | redirect_to settings_project_path(@project, :tab => 'modules') |
|
206 | redirect_to settings_project_path(@project, :tab => 'modules') | |
206 | end |
|
207 | end | |
207 |
|
208 | |||
208 | def archive |
|
209 | def archive | |
209 | if request.post? |
|
210 | if request.post? | |
210 | unless @project.archive |
|
211 | unless @project.archive | |
211 | flash[:error] = l(:error_can_not_archive_project) |
|
212 | flash[:error] = l(:error_can_not_archive_project) | |
212 | end |
|
213 | end | |
213 | end |
|
214 | end | |
214 | redirect_to admin_projects_path(:status => params[:status]) |
|
215 | redirect_to admin_projects_path(:status => params[:status]) | |
215 | end |
|
216 | end | |
216 |
|
217 | |||
217 | def unarchive |
|
218 | def unarchive | |
218 | @project.unarchive if request.post? && !@project.active? |
|
219 | @project.unarchive if request.post? && !@project.active? | |
219 | redirect_to admin_projects_path(:status => params[:status]) |
|
220 | redirect_to admin_projects_path(:status => params[:status]) | |
220 | end |
|
221 | end | |
221 |
|
222 | |||
222 | def close |
|
223 | def close | |
223 | @project.close |
|
224 | @project.close | |
224 | redirect_to project_path(@project) |
|
225 | redirect_to project_path(@project) | |
225 | end |
|
226 | end | |
226 |
|
227 | |||
227 | def reopen |
|
228 | def reopen | |
228 | @project.reopen |
|
229 | @project.reopen | |
229 | redirect_to project_path(@project) |
|
230 | redirect_to project_path(@project) | |
230 | end |
|
231 | end | |
231 |
|
232 | |||
232 | # Delete @project |
|
233 | # Delete @project | |
233 | def destroy |
|
234 | def destroy | |
234 | @project_to_destroy = @project |
|
235 | @project_to_destroy = @project | |
235 | if api_request? || params[:confirm] |
|
236 | if api_request? || params[:confirm] | |
236 | @project_to_destroy.destroy |
|
237 | @project_to_destroy.destroy | |
237 | respond_to do |format| |
|
238 | respond_to do |format| | |
238 | format.html { redirect_to admin_projects_path } |
|
239 | format.html { redirect_to admin_projects_path } | |
239 | format.api { render_api_ok } |
|
240 | format.api { render_api_ok } | |
240 | end |
|
241 | end | |
241 | end |
|
242 | end | |
242 | # hide project in layout |
|
243 | # hide project in layout | |
243 | @project = nil |
|
244 | @project = nil | |
244 | end |
|
245 | end | |
245 |
|
246 | |||
246 | private |
|
247 | private | |
247 |
|
248 | |||
248 | # Validates parent_id param according to user's permissions |
|
249 | # Validates parent_id param according to user's permissions | |
249 | # TODO: move it to Project model in a validation that depends on User.current |
|
250 | # TODO: move it to Project model in a validation that depends on User.current | |
250 | def validate_parent_id |
|
251 | def validate_parent_id | |
251 | return true if User.current.admin? |
|
252 | return true if User.current.admin? | |
252 | parent_id = params[:project] && params[:project][:parent_id] |
|
253 | parent_id = params[:project] && params[:project][:parent_id] | |
253 | if parent_id || @project.new_record? |
|
254 | if parent_id || @project.new_record? | |
254 | parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i) |
|
255 | parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i) | |
255 | unless @project.allowed_parents.include?(parent) |
|
256 | unless @project.allowed_parents.include?(parent) | |
256 | @project.errors.add :parent_id, :invalid |
|
257 | @project.errors.add :parent_id, :invalid | |
257 | return false |
|
258 | return false | |
258 | end |
|
259 | end | |
259 | end |
|
260 | end | |
260 | true |
|
261 | true | |
261 | end |
|
262 | end | |
262 | end |
|
263 | end |
General Comments 0
You need to be logged in to leave comments.
Login now