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

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