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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2016 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.get?
  33. if User.current.logged?
  34. redirect_back_or_default home_url, :referer => true
  35. end
  36. else
  37. authenticate_user
  38. end
  39. rescue AuthSourceException => e
  40. logger.error "An error occured 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 params[:token]
  57. @token = Token.find_token("recovery", params[:token].to_s)
  58. if @token.nil? || @token.expired?
  59. redirect_to home_url
  60. return
  61. end
  62. @user = @token.user
  63. unless @user && @user.active?
  64. redirect_to home_url
  65. return
  66. end
  67. if request.post?
  68. @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
  69. if @user.save
  70. @token.destroy
  71. Mailer.password_updated(@user)
  72. flash[:notice] = l(:notice_account_password_updated)
  73. redirect_to signin_path
  74. return
  75. end
  76. end
  77. render :template => "account/password_recovery"
  78. return
  79. else
  80. if request.post?
  81. email = params[:mail].to_s
  82. user = User.find_by_mail(email)
  83. # user not found
  84. unless user
  85. flash.now[:error] = l(:notice_account_unknown_email)
  86. return
  87. end
  88. unless user.active?
  89. handle_inactive_user(user, lost_password_path)
  90. return
  91. end
  92. # user cannot change its password
  93. unless user.change_password_allowed?
  94. flash.now[:error] = l(:notice_can_t_change_password)
  95. return
  96. end
  97. # create a new token for password recovery
  98. token = Token.new(:user => user, :action => "recovery")
  99. if token.save
  100. # Don't use the param to send the email
  101. recipent = user.mails.detect {|e| email.casecmp(e) == 0} || user.mail
  102. Mailer.lost_password(token, recipent).deliver
  103. flash[:notice] = l(:notice_account_lost_email_sent)
  104. redirect_to signin_path
  105. return
  106. end
  107. end
  108. end
  109. end
  110. # User self-registration
  111. def register
  112. (redirect_to(home_url); return) unless Setting.self_registration? || session[:auth_source_registration]
  113. if request.get?
  114. session[:auth_source_registration] = nil
  115. @user = User.new(:language => current_language.to_s)
  116. else
  117. user_params = params[:user] || {}
  118. @user = User.new
  119. @user.safe_attributes = user_params
  120. @user.pref.safe_attributes = params[:pref]
  121. @user.admin = false
  122. @user.register
  123. if session[:auth_source_registration]
  124. @user.activate
  125. @user.login = session[:auth_source_registration][:login]
  126. @user.auth_source_id = session[:auth_source_registration][:auth_source_id]
  127. if @user.save
  128. session[:auth_source_registration] = nil
  129. self.logged_user = @user
  130. flash[:notice] = l(:notice_account_activated)
  131. redirect_to my_account_path
  132. end
  133. else
  134. unless user_params[:identity_url].present? && user_params[:password].blank? && user_params[:password_confirmation].blank?
  135. @user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation]
  136. end
  137. case Setting.self_registration
  138. when '1'
  139. register_by_email_activation(@user)
  140. when '3'
  141. register_automatically(@user)
  142. else
  143. register_manually_by_administrator(@user)
  144. end
  145. end
  146. end
  147. end
  148. # Token based account activation
  149. def activate
  150. (redirect_to(home_url); return) unless Setting.self_registration? && params[:token].present?
  151. token = Token.find_token('register', params[:token].to_s)
  152. (redirect_to(home_url); return) unless token and !token.expired?
  153. user = token.user
  154. (redirect_to(home_url); return) unless user.registered?
  155. user.activate
  156. if user.save
  157. token.destroy
  158. flash[:notice] = l(:notice_account_activated)
  159. end
  160. redirect_to signin_path
  161. end
  162. # Sends a new account activation email
  163. def activation_email
  164. if session[:registered_user_id] && Setting.self_registration == '1'
  165. user_id = session.delete(:registered_user_id).to_i
  166. user = User.find_by_id(user_id)
  167. if user && user.registered?
  168. register_by_email_activation(user)
  169. return
  170. end
  171. end
  172. redirect_to(home_url)
  173. end
  174. private
  175. def authenticate_user
  176. if Setting.openid? && using_open_id?
  177. open_id_authenticate(params[:openid_url])
  178. else
  179. password_authentication
  180. end
  181. end
  182. def password_authentication
  183. user = User.try_to_login(params[:username], params[:password], false)
  184. if user.nil?
  185. invalid_credentials
  186. elsif user.new_record?
  187. onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id })
  188. else
  189. # Valid user
  190. if user.active?
  191. successful_authentication(user)
  192. update_sudo_timestamp! # activate Sudo Mode
  193. else
  194. handle_inactive_user(user)
  195. end
  196. end
  197. end
  198. def open_id_authenticate(openid_url)
  199. back_url = signin_url(:autologin => params[:autologin])
  200. authenticate_with_open_id(
  201. openid_url, :required => [:nickname, :fullname, :email],
  202. :return_to => back_url, :method => :post
  203. ) do |result, identity_url, registration|
  204. if result.successful?
  205. user = User.find_or_initialize_by_identity_url(identity_url)
  206. if user.new_record?
  207. # Self-registration off
  208. (redirect_to(home_url); return) unless Setting.self_registration?
  209. # Create on the fly
  210. user.login = registration['nickname'] unless registration['nickname'].nil?
  211. user.mail = registration['email'] unless registration['email'].nil?
  212. user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
  213. user.random_password
  214. user.register
  215. case Setting.self_registration
  216. when '1'
  217. register_by_email_activation(user) do
  218. onthefly_creation_failed(user)
  219. end
  220. when '3'
  221. register_automatically(user) do
  222. onthefly_creation_failed(user)
  223. end
  224. else
  225. register_manually_by_administrator(user) do
  226. onthefly_creation_failed(user)
  227. end
  228. end
  229. else
  230. # Existing record
  231. if user.active?
  232. successful_authentication(user)
  233. else
  234. handle_inactive_user(user)
  235. end
  236. end
  237. end
  238. end
  239. end
  240. def successful_authentication(user)
  241. logger.info "Successful authentication for '#{user.login}' from #{request.remote_ip} at #{Time.now.utc}"
  242. # Valid user
  243. self.logged_user = user
  244. # generate a key and set cookie if autologin
  245. if params[:autologin] && Setting.autologin?
  246. set_autologin_cookie(user)
  247. end
  248. call_hook(:controller_account_success_authentication_after, {:user => user })
  249. redirect_back_or_default my_page_path
  250. end
  251. def set_autologin_cookie(user)
  252. token = Token.create(:user => user, :action => 'autologin')
  253. secure = Redmine::Configuration['autologin_cookie_secure']
  254. if secure.nil?
  255. secure = request.ssl?
  256. end
  257. cookie_options = {
  258. :value => token.value,
  259. :expires => 1.year.from_now,
  260. :path => (Redmine::Configuration['autologin_cookie_path'] || RedmineApp::Application.config.relative_url_root || '/'),
  261. :secure => secure,
  262. :httponly => true
  263. }
  264. cookies[autologin_cookie_name] = cookie_options
  265. end
  266. # Onthefly creation failed, display the registration form to fill/fix attributes
  267. def onthefly_creation_failed(user, auth_source_options = { })
  268. @user = user
  269. session[:auth_source_registration] = auth_source_options unless auth_source_options.empty?
  270. render :action => 'register'
  271. end
  272. def invalid_credentials
  273. logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}"
  274. flash.now[:error] = l(:notice_account_invalid_credentials)
  275. end
  276. # Register a user for email activation.
  277. #
  278. # Pass a block for behavior when a user fails to save
  279. def register_by_email_activation(user, &block)
  280. token = Token.new(:user => user, :action => "register")
  281. if user.save and token.save
  282. Mailer.register(token).deliver
  283. flash[:notice] = l(:notice_account_register_done, :email => ERB::Util.h(user.mail))
  284. redirect_to signin_path
  285. else
  286. yield if block_given?
  287. end
  288. end
  289. # Automatically register a user
  290. #
  291. # Pass a block for behavior when a user fails to save
  292. def register_automatically(user, &block)
  293. # Automatic activation
  294. user.activate
  295. user.last_login_on = Time.now
  296. if user.save
  297. self.logged_user = user
  298. flash[:notice] = l(:notice_account_activated)
  299. redirect_to my_account_path
  300. else
  301. yield if block_given?
  302. end
  303. end
  304. # Manual activation by the administrator
  305. #
  306. # Pass a block for behavior when a user fails to save
  307. def register_manually_by_administrator(user, &block)
  308. if user.save
  309. # Sends an email to the administrators
  310. Mailer.account_activation_request(user).deliver
  311. account_pending(user)
  312. else
  313. yield if block_given?
  314. end
  315. end
  316. def handle_inactive_user(user, redirect_path=signin_path)
  317. if user.registered?
  318. account_pending(user, redirect_path)
  319. else
  320. account_locked(user, redirect_path)
  321. end
  322. end
  323. def account_pending(user, redirect_path=signin_path)
  324. if Setting.self_registration == '1'
  325. flash[:error] = l(:notice_account_not_activated_yet, :url => activation_email_path)
  326. session[:registered_user_id] = user.id
  327. else
  328. flash[:error] = l(:notice_account_pending)
  329. end
  330. redirect_to redirect_path
  331. end
  332. def account_locked(user, redirect_path=signin_path)
  333. flash[:error] = l(:notice_account_locked)
  334. redirect_to redirect_path
  335. end
  336. end