/app/controllers/devise/registrations_controller.rb

https://github.com/FundingCircle/devise · Ruby · 114 lines · 77 code · 15 blank · 22 comment · 10 complexity · b0b77cf39c05c9514c1c244fce1d2e02 MD5 · raw file

  1. class Devise::RegistrationsController < ApplicationController
  2. prepend_before_filter :require_no_authentication, :only => [ :new, :create, :cancel ]
  3. prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy]
  4. include Devise::Controllers::InternalHelpers
  5. # GET /resource/sign_up
  6. def new
  7. resource = build_resource({})
  8. respond_with_navigational(resource){ render_with_scope :new }
  9. end
  10. # POST /resource
  11. def create
  12. build_resource
  13. if resource.save
  14. if resource.active_for_authentication?
  15. set_flash_message :notice, :signed_up if is_navigational_format?
  16. sign_in(resource_name, resource)
  17. respond_with resource, :location => redirect_location(resource_name, resource)
  18. else
  19. set_flash_message :notice, :inactive_signed_up, :reason => resource.inactive_message.to_s if is_navigational_format?
  20. expire_session_data_after_sign_in!
  21. respond_with resource, :location => after_inactive_sign_up_path_for(resource)
  22. end
  23. else
  24. clean_up_passwords(resource)
  25. respond_with_navigational(resource) { render_with_scope :new }
  26. end
  27. end
  28. # GET /resource/edit
  29. def edit
  30. render_with_scope :edit
  31. end
  32. # PUT /resource
  33. # We need to use a copy of the resource because we don't want to change
  34. # the current user in place.
  35. def update
  36. self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)
  37. if resource.update_with_password(params[resource_name])
  38. set_flash_message :notice, :updated if is_navigational_format?
  39. sign_in resource_name, resource, :bypass => true
  40. respond_with resource, :location => after_update_path_for(resource)
  41. else
  42. clean_up_passwords(resource)
  43. respond_with_navigational(resource){ render_with_scope :edit }
  44. end
  45. end
  46. # DELETE /resource
  47. def destroy
  48. resource.destroy
  49. Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)
  50. set_flash_message :notice, :destroyed if is_navigational_format?
  51. respond_with_navigational(resource){ redirect_to after_sign_out_path_for(resource_name) }
  52. end
  53. # GET /resource/cancel
  54. # Forces the session data which is usually expired after sign
  55. # in to be expired now. This is useful if the user wants to
  56. # cancel oauth signing in/up in the middle of the process,
  57. # removing all OAuth session data.
  58. def cancel
  59. expire_session_data_after_sign_in!
  60. redirect_to new_registration_path(resource_name)
  61. end
  62. protected
  63. # Build a devise resource passing in the session. Useful to move
  64. # temporary session data to the newly created user.
  65. def build_resource(hash=nil)
  66. hash ||= params[resource_name] || {}
  67. self.resource = resource_class.new_with_session(hash, session)
  68. end
  69. # The path used after sign up. You need to overwrite this method
  70. # in your own RegistrationsController.
  71. def after_sign_up_path_for(resource)
  72. after_sign_in_path_for(resource)
  73. end
  74. # Overwrite redirect_for_sign_in so it takes uses after_sign_up_path_for.
  75. def redirect_location(scope, resource) #:nodoc:
  76. stored_location_for(scope) || after_sign_up_path_for(resource)
  77. end
  78. # The path used after sign up for inactive accounts. You need to overwrite
  79. # this method in your own RegistrationsController.
  80. def after_inactive_sign_up_path_for(resource)
  81. root_path
  82. end
  83. # The default url to be used after updating a resource. You need to overwrite
  84. # this method in your own RegistrationsController.
  85. def after_update_path_for(resource)
  86. if defined?(super)
  87. ActiveSupport::Deprecation.warn "Defining after_update_path_for in ApplicationController " <<
  88. "is deprecated. Please add a RegistrationsController to your application and define it there."
  89. super
  90. else
  91. after_sign_in_path_for(resource)
  92. end
  93. end
  94. # Authenticates the current scope and gets the current resource from the session.
  95. def authenticate_scope!
  96. send(:"authenticate_#{resource_name}!", true)
  97. self.resource = send(:"current_#{resource_name}")
  98. end
  99. end