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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2012 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. # prevents login action to be filtered by check_if_login_required application scope filter
  21. skip_before_filter :check_if_login_required
  22. # Login request and validation
  23. def login
  24. if request.get?
  25. logout_user
  26. else
  27. authenticate_user
  28. end
  29. rescue AuthSourceException => e
  30. logger.error "An error occured when authenticating #{params[:username]}: #{e.message}"
  31. render_error :message => e.message
  32. end
  33. # Log out current user and redirect to welcome page
  34. def logout
  35. logout_user
  36. redirect_to home_url
  37. end
  38. # Lets user choose a new password
  39. def lost_password
  40. redirect_to(home_url) && return unless Setting.lost_password?
  41. if params[:token]
  42. @token = Token.find_by_action_and_value("recovery", params[:token].to_s)
  43. if @token.nil? || @token.expired?
  44. redirect_to home_url
  45. return
  46. end
  47. @user = @token.user
  48. unless @user && @user.active?
  49. redirect_to home_url
  50. return
  51. end
  52. if request.post?
  53. @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
  54. if @user.save
  55. @token.destroy
  56. flash[:notice] = l(:notice_account_password_updated)
  57. redirect_to signin_path
  58. return
  59. end
  60. end
  61. render :template => "account/password_recovery"
  62. return
  63. else
  64. if request.post?
  65. user = User.find_by_mail(params[:mail].to_s)
  66. # user not found or not active
  67. unless user && user.active?
  68. flash.now[:error] = l(:notice_account_unknown_email)
  69. return
  70. end
  71. # user cannot change its password
  72. unless user.change_password_allowed?
  73. flash.now[:error] = l(:notice_can_t_change_password)
  74. return
  75. end
  76. # create a new token for password recovery
  77. token = Token.new(:user => user, :action => "recovery")
  78. if token.save
  79. Mailer.lost_password(token).deliver
  80. flash[:notice] = l(:notice_account_lost_email_sent)
  81. redirect_to signin_path
  82. return
  83. end
  84. end
  85. end
  86. end
  87. # User self-registration
  88. def register
  89. redirect_to(home_url) && return unless Setting.self_registration? || session[:auth_source_registration]
  90. if request.get?
  91. session[:auth_source_registration] = nil
  92. @user = User.new(:language => Setting.default_language)
  93. else
  94. user_params = params[:user] || {}
  95. @user = User.new
  96. @user.safe_attributes = user_params
  97. @user.admin = false
  98. @user.register
  99. if session[:auth_source_registration]
  100. @user.activate
  101. @user.login = session[:auth_source_registration][:login]
  102. @user.auth_source_id = session[:auth_source_registration][:auth_source_id]
  103. if @user.save
  104. session[:auth_source_registration] = nil
  105. self.logged_user = @user
  106. flash[:notice] = l(:notice_account_activated)
  107. redirect_to :controller => 'my', :action => 'account'
  108. end
  109. else
  110. @user.login = params[:user][:login]
  111. unless user_params[:identity_url].present? && user_params[:password].blank? && user_params[:password_confirmation].blank?
  112. @user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation]
  113. end
  114. case Setting.self_registration
  115. when '1'
  116. register_by_email_activation(@user)
  117. when '3'
  118. register_automatically(@user)
  119. else
  120. register_manually_by_administrator(@user)
  121. end
  122. end
  123. end
  124. end
  125. # Token based account activation
  126. def activate
  127. redirect_to(home_url) && return unless Setting.self_registration? && params[:token]
  128. token = Token.find_by_action_and_value('register', params[:token])
  129. redirect_to(home_url) && return unless token and !token.expired?
  130. user = token.user
  131. redirect_to(home_url) && return unless user.registered?
  132. user.activate
  133. if user.save
  134. token.destroy
  135. flash[:notice] = l(:notice_account_activated)
  136. end
  137. redirect_to signin_path
  138. end
  139. private
  140. def authenticate_user
  141. if Setting.openid? && using_open_id?
  142. open_id_authenticate(params[:openid_url])
  143. else
  144. password_authentication
  145. end
  146. end
  147. def password_authentication
  148. user = User.try_to_login(params[:username], params[:password])
  149. if user.nil?
  150. invalid_credentials
  151. elsif user.new_record?
  152. onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id })
  153. else
  154. # Valid user
  155. successful_authentication(user)
  156. end
  157. end
  158. def open_id_authenticate(openid_url)
  159. authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url, :method => :post) do |result, identity_url, registration|
  160. if result.successful?
  161. user = User.find_or_initialize_by_identity_url(identity_url)
  162. if user.new_record?
  163. # Self-registration off
  164. redirect_to(home_url) && return unless Setting.self_registration?
  165. # Create on the fly
  166. user.login = registration['nickname'] unless registration['nickname'].nil?
  167. user.mail = registration['email'] unless registration['email'].nil?
  168. user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
  169. user.random_password
  170. user.register
  171. case Setting.self_registration
  172. when '1'
  173. register_by_email_activation(user) do
  174. onthefly_creation_failed(user)
  175. end
  176. when '3'
  177. register_automatically(user) do
  178. onthefly_creation_failed(user)
  179. end
  180. else
  181. register_manually_by_administrator(user) do
  182. onthefly_creation_failed(user)
  183. end
  184. end
  185. else
  186. # Existing record
  187. if user.active?
  188. successful_authentication(user)
  189. else
  190. account_pending
  191. end
  192. end
  193. end
  194. end
  195. end
  196. def successful_authentication(user)
  197. logger.info "Successful authentication for '#{user.login}' from #{request.remote_ip} at #{Time.now.utc}"
  198. # Valid user
  199. self.logged_user = user
  200. # generate a key and set cookie if autologin
  201. if params[:autologin] && Setting.autologin?
  202. set_autologin_cookie(user)
  203. end
  204. call_hook(:controller_account_success_authentication_after, {:user => user })
  205. redirect_back_or_default :controller => 'my', :action => 'page'
  206. end
  207. def set_autologin_cookie(user)
  208. token = Token.create(:user => user, :action => 'autologin')
  209. cookie_name = Redmine::Configuration['autologin_cookie_name'] || 'autologin'
  210. cookie_options = {
  211. :value => token.value,
  212. :expires => 1.year.from_now,
  213. :path => (Redmine::Configuration['autologin_cookie_path'] || '/'),
  214. :secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false),
  215. :httponly => true
  216. }
  217. cookies[cookie_name] = cookie_options
  218. end
  219. # Onthefly creation failed, display the registration form to fill/fix attributes
  220. def onthefly_creation_failed(user, auth_source_options = { })
  221. @user = user
  222. session[:auth_source_registration] = auth_source_options unless auth_source_options.empty?
  223. render :action => 'register'
  224. end
  225. def invalid_credentials
  226. logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}"
  227. flash.now[:error] = l(:notice_account_invalid_creditentials)
  228. end
  229. # Register a user for email activation.
  230. #
  231. # Pass a block for behavior when a user fails to save
  232. def register_by_email_activation(user, &block)
  233. token = Token.new(:user => user, :action => "register")
  234. if user.save and token.save
  235. Mailer.register(token).deliver
  236. flash[:notice] = l(:notice_account_register_done)
  237. redirect_to signin_path
  238. else
  239. yield if block_given?
  240. end
  241. end
  242. # Automatically register a user
  243. #
  244. # Pass a block for behavior when a user fails to save
  245. def register_automatically(user, &block)
  246. # Automatic activation
  247. user.activate
  248. user.last_login_on = Time.now
  249. if user.save
  250. self.logged_user = user
  251. flash[:notice] = l(:notice_account_activated)
  252. redirect_to :controller => 'my', :action => 'account'
  253. else
  254. yield if block_given?
  255. end
  256. end
  257. # Manual activation by the administrator
  258. #
  259. # Pass a block for behavior when a user fails to save
  260. def register_manually_by_administrator(user, &block)
  261. if user.save
  262. # Sends an email to the administrators
  263. Mailer.account_activation_request(user).deliver
  264. account_pending
  265. else
  266. yield if block_given?
  267. end
  268. end
  269. def account_pending
  270. flash[:notice] = l(:notice_account_pending)
  271. redirect_to signin_path
  272. end
  273. end