You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

account_controller.rb 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  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
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. class AccountController < ApplicationController
  18. helper :custom_fields
  19. include CustomFieldsHelper
  20. self.main_menu = false
  21. # prevents login action to be filtered by check_if_login_required application scope filter
  22. skip_before_action :check_if_login_required, :check_password_change
  23. # Overrides ApplicationController#verify_authenticity_token to disable
  24. # token verification on openid callbacks
  25. def verify_authenticity_token
  26. unless using_open_id?
  27. super
  28. end
  29. end
  30. # Login request and validation
  31. def login
  32. if request.post?
  33. authenticate_user
  34. else
  35. if User.current.logged?
  36. redirect_back_or_default home_url, :referer => true
  37. end
  38. end
  39. rescue AuthSourceException => e
  40. logger.error "An error occurred when authenticating #{params[:username]}: #{e.message}"
  41. render_error :message => e.message
  42. end
  43. # Log out current user and redirect to welcome page
  44. def logout
  45. if User.current.anonymous?
  46. redirect_to home_url
  47. elsif request.post?
  48. logout_user
  49. redirect_to home_url
  50. end
  51. # display the logout form
  52. end
  53. # Lets user choose a new password
  54. def lost_password
  55. (redirect_to(home_url); return) unless Setting.lost_password?
  56. if prt = (params[:token] || session[:password_recovery_token])
  57. @token = Token.find_token("recovery", prt.to_s)
  58. if @token.nil?
  59. redirect_to home_url
  60. return
  61. elsif @token.expired?
  62. # remove expired token from session and let user try again
  63. session[:password_recovery_token] = nil
  64. flash[:error] = l(:error_token_expired)
  65. redirect_to lost_password_url
  66. return
  67. end
  68. # redirect to remove the token query parameter from the URL and add it to the session
  69. if request.query_parameters[:token].present?
  70. session[:password_recovery_token] = @token.value
  71. redirect_to lost_password_url
  72. return
  73. end
  74. @user = @token.user
  75. unless @user && @user.active?
  76. redirect_to home_url
  77. return
  78. end
  79. if request.post?
  80. if @user.must_change_passwd? && @user.check_password?(params[:new_password])
  81. flash.now[:error] = l(:notice_new_password_must_be_different)
  82. else
  83. @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
  84. @user.must_change_passwd = false
  85. if @user.save
  86. @token.destroy
  87. Mailer.deliver_password_updated(@user, User.current)
  88. flash[:notice] = l(:notice_account_password_updated)
  89. redirect_to signin_path
  90. return
  91. end
  92. end
  93. end
  94. render :template => "account/password_recovery"
  95. return
  96. else
  97. if request.post?
  98. email = params[:mail].to_s.strip
  99. user = User.find_by_mail(email)
  100. # user not found
  101. unless user
  102. flash.now[:error] = l(:notice_account_unknown_email)
  103. return
  104. end
  105. unless user.active?
  106. handle_inactive_user(user, lost_password_path)
  107. return
  108. end
  109. # user cannot change its password
  110. unless user.change_password_allowed?
  111. flash.now[:error] = l(:notice_can_t_change_password)
  112. return
  113. end
  114. # create a new token for password recovery
  115. token = Token.new(:user => user, :action => "recovery")
  116. if token.save
  117. # Don't use the param to send the email
  118. recipent = user.mails.detect {|e| email.casecmp(e) == 0} || user.mail
  119. Mailer.deliver_lost_password(user, token, recipent)
  120. flash[:notice] = l(:notice_account_lost_email_sent)
  121. redirect_to signin_path
  122. return
  123. end
  124. end
  125. end
  126. end
  127. # User self-registration
  128. def register
  129. (redirect_to(home_url); return) unless Setting.self_registration? || session[:auth_source_registration]
  130. if !request.post?
  131. session[:auth_source_registration] = nil
  132. @user = User.new(:language => current_language.to_s)
  133. else
  134. user_params = params[:user] || {}
  135. @user = User.new
  136. @user.safe_attributes = user_params
  137. @user.pref.safe_attributes = params[:pref]
  138. @user.admin = false
  139. @user.register
  140. if session[:auth_source_registration]
  141. @user.activate
  142. @user.login = session[:auth_source_registration][:login]
  143. @user.auth_source_id = session[:auth_source_registration][:auth_source_id]
  144. if @user.save
  145. session[:auth_source_registration] = nil
  146. self.logged_user = @user
  147. flash[:notice] = l(:notice_account_activated)
  148. redirect_to my_account_path
  149. end
  150. else
  151. unless user_params[:identity_url].present? && user_params[:password].blank? && user_params[:password_confirmation].blank?
  152. @user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation]
  153. end
  154. case Setting.self_registration
  155. when '1'
  156. register_by_email_activation(@user)
  157. when '3'
  158. register_automatically(@user)
  159. else
  160. register_manually_by_administrator(@user)
  161. end
  162. end
  163. end
  164. end
  165. # Token based account activation
  166. def activate
  167. (redirect_to(home_url); return) unless Setting.self_registration? && params[:token].present?
  168. token = Token.find_token('register', params[:token].to_s)
  169. (redirect_to(home_url); return) unless token and !token.expired?
  170. user = token.user
  171. (redirect_to(home_url); return) unless user.registered?
  172. user.activate
  173. if user.save
  174. token.destroy
  175. flash[:notice] = l(:notice_account_activated)
  176. end
  177. redirect_to signin_path
  178. end
  179. # Sends a new account activation email
  180. def activation_email
  181. if session[:registered_user_id] && Setting.self_registration == '1'
  182. user_id = session.delete(:registered_user_id).to_i
  183. user = User.find_by_id(user_id)
  184. if user && user.registered?
  185. register_by_email_activation(user)
  186. return
  187. end
  188. end
  189. redirect_to(home_url)
  190. end
  191. private
  192. def authenticate_user
  193. if Setting.openid? && using_open_id?
  194. open_id_authenticate(params[:openid_url])
  195. else
  196. password_authentication
  197. end
  198. end
  199. def password_authentication
  200. user = User.try_to_login(params[:username], params[:password], false)
  201. if user.nil?
  202. invalid_credentials
  203. elsif user.new_record?
  204. onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id })
  205. else
  206. # Valid user
  207. if user.active?
  208. successful_authentication(user)
  209. update_sudo_timestamp! # activate Sudo Mode
  210. else
  211. handle_inactive_user(user)
  212. end
  213. end
  214. end
  215. def open_id_authenticate(openid_url)
  216. back_url = signin_url(:autologin => params[:autologin])
  217. authenticate_with_open_id(
  218. openid_url, :required => [:nickname, :fullname, :email],
  219. :return_to => back_url, :method => :post
  220. ) do |result, identity_url, registration|
  221. if result.successful?
  222. user = User.find_or_initialize_by_identity_url(identity_url)
  223. if user.new_record?
  224. # Self-registration off
  225. (redirect_to(home_url); return) unless Setting.self_registration?
  226. # Create on the fly
  227. user.login = registration['nickname'] unless registration['nickname'].nil?
  228. user.mail = registration['email'] unless registration['email'].nil?
  229. user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
  230. user.random_password
  231. user.register
  232. case Setting.self_registration
  233. when '1'
  234. register_by_email_activation(user) do
  235. onthefly_creation_failed(user)
  236. end
  237. when '3'
  238. register_automatically(user) do
  239. onthefly_creation_failed(user)
  240. end
  241. else
  242. register_manually_by_administrator(user) do
  243. onthefly_creation_failed(user)
  244. end
  245. end
  246. else
  247. # Existing record
  248. if user.active?
  249. successful_authentication(user)
  250. else
  251. handle_inactive_user(user)
  252. end
  253. end
  254. end
  255. end
  256. end
  257. def successful_authentication(user)
  258. logger.info "Successful authentication for '#{user.login}' from #{request.remote_ip} at #{Time.now.utc}"
  259. # Valid user
  260. self.logged_user = user
  261. # generate a key and set cookie if autologin
  262. if params[:autologin] && Setting.autologin?
  263. set_autologin_cookie(user)
  264. end
  265. call_hook(:controller_account_success_authentication_after, {:user => user })
  266. redirect_back_or_default my_page_path
  267. end
  268. def set_autologin_cookie(user)
  269. token = user.generate_autologin_token
  270. secure = Redmine::Configuration['autologin_cookie_secure']
  271. if secure.nil?
  272. secure = request.ssl?
  273. end
  274. cookie_options = {
  275. :value => token,
  276. :expires => 1.year.from_now,
  277. :path => (Redmine::Configuration['autologin_cookie_path'] || RedmineApp::Application.config.relative_url_root || '/'),
  278. :secure => secure,
  279. :httponly => true
  280. }
  281. cookies[autologin_cookie_name] = cookie_options
  282. end
  283. # Onthefly creation failed, display the registration form to fill/fix attributes
  284. def onthefly_creation_failed(user, auth_source_options = { })
  285. @user = user
  286. session[:auth_source_registration] = auth_source_options unless auth_source_options.empty?
  287. render :action => 'register'
  288. end
  289. def invalid_credentials
  290. logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}"
  291. flash.now[:error] = l(:notice_account_invalid_credentials)
  292. end
  293. # Register a user for email activation.
  294. #
  295. # Pass a block for behavior when a user fails to save
  296. def register_by_email_activation(user, &block)
  297. token = Token.new(:user => user, :action => "register")
  298. if user.save and token.save
  299. Mailer.deliver_register(user, token)
  300. flash[:notice] = l(:notice_account_register_done, :email => ERB::Util.h(user.mail))
  301. redirect_to signin_path
  302. else
  303. yield if block_given?
  304. end
  305. end
  306. # Automatically register a user
  307. #
  308. # Pass a block for behavior when a user fails to save
  309. def register_automatically(user, &block)
  310. # Automatic activation
  311. user.activate
  312. user.last_login_on = Time.now
  313. if user.save
  314. self.logged_user = user
  315. flash[:notice] = l(:notice_account_activated)
  316. redirect_to my_account_path
  317. else
  318. yield if block_given?
  319. end
  320. end
  321. # Manual activation by the administrator
  322. #
  323. # Pass a block for behavior when a user fails to save
  324. def register_manually_by_administrator(user, &block)
  325. if user.save
  326. # Sends an email to the administrators
  327. Mailer.deliver_account_activation_request(user)
  328. account_pending(user)
  329. else
  330. yield if block_given?
  331. end
  332. end
  333. def handle_inactive_user(user, redirect_path=signin_path)
  334. if user.registered?
  335. account_pending(user, redirect_path)
  336. else
  337. account_locked(user, redirect_path)
  338. end
  339. end
  340. def account_pending(user, redirect_path=signin_path)
  341. if Setting.self_registration == '1'
  342. flash[:error] = l(:notice_account_not_activated_yet, :url => activation_email_path)
  343. session[:registered_user_id] = user.id
  344. else
  345. flash[:error] = l(:notice_account_pending)
  346. end
  347. redirect_to redirect_path
  348. end
  349. def account_locked(user, redirect_path=signin_path)
  350. flash[:error] = l(:notice_account_locked)
  351. redirect_to redirect_path
  352. end
  353. end