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.1KB

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