devise 3.0.0 → 4.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (242) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +351 -0
  3. data/MIT-LICENSE +2 -1
  4. data/README.md +422 -130
  5. data/app/controllers/devise/confirmations_controller.rb +17 -6
  6. data/app/controllers/devise/omniauth_callbacks_controller.rb +12 -6
  7. data/app/controllers/devise/passwords_controller.rb +23 -8
  8. data/app/controllers/devise/registrations_controller.rb +70 -28
  9. data/app/controllers/devise/sessions_controller.rb +49 -17
  10. data/app/controllers/devise/unlocks_controller.rb +11 -4
  11. data/app/controllers/devise_controller.rb +74 -34
  12. data/app/helpers/devise_helper.rb +23 -18
  13. data/app/mailers/devise/mailer.rb +25 -10
  14. data/app/views/devise/confirmations/new.html.erb +9 -5
  15. data/app/views/devise/mailer/confirmation_instructions.html.erb +1 -1
  16. data/app/views/devise/mailer/email_changed.html.erb +7 -0
  17. data/app/views/devise/mailer/password_change.html.erb +3 -0
  18. data/app/views/devise/mailer/reset_password_instructions.html.erb +1 -1
  19. data/app/views/devise/mailer/unlock_instructions.html.erb +1 -1
  20. data/app/views/devise/passwords/edit.html.erb +16 -7
  21. data/app/views/devise/passwords/new.html.erb +9 -5
  22. data/app/views/devise/registrations/edit.html.erb +29 -15
  23. data/app/views/devise/registrations/new.html.erb +20 -9
  24. data/app/views/devise/sessions/new.html.erb +19 -10
  25. data/app/views/devise/shared/_error_messages.html.erb +15 -0
  26. data/app/views/devise/shared/{_links.erb → _links.html.erb} +10 -10
  27. data/app/views/devise/unlocks/new.html.erb +9 -5
  28. data/config/locales/en.yml +26 -20
  29. data/lib/devise/controllers/helpers.rb +122 -125
  30. data/lib/devise/controllers/rememberable.rb +14 -14
  31. data/lib/devise/controllers/scoped_views.rb +3 -1
  32. data/lib/devise/controllers/sign_in_out.rb +121 -0
  33. data/lib/devise/controllers/store_location.rb +76 -0
  34. data/lib/devise/controllers/url_helpers.rb +10 -8
  35. data/lib/devise/delegator.rb +2 -0
  36. data/lib/devise/encryptor.rb +24 -0
  37. data/lib/devise/failure_app.rb +132 -42
  38. data/lib/devise/hooks/activatable.rb +7 -6
  39. data/lib/devise/hooks/csrf_cleaner.rb +9 -0
  40. data/lib/devise/hooks/forgetable.rb +3 -1
  41. data/lib/devise/hooks/lockable.rb +5 -3
  42. data/lib/devise/hooks/proxy.rb +23 -0
  43. data/lib/devise/hooks/rememberable.rb +7 -4
  44. data/lib/devise/hooks/timeoutable.rb +18 -8
  45. data/lib/devise/hooks/trackable.rb +3 -1
  46. data/lib/devise/mailers/helpers.rb +15 -18
  47. data/lib/devise/mapping.rb +9 -3
  48. data/lib/devise/models/authenticatable.rb +102 -80
  49. data/lib/devise/models/confirmable.rb +154 -72
  50. data/lib/devise/models/database_authenticatable.rb +125 -25
  51. data/lib/devise/models/lockable.rb +50 -29
  52. data/lib/devise/models/omniauthable.rb +3 -1
  53. data/lib/devise/models/recoverable.rb +72 -50
  54. data/lib/devise/models/registerable.rb +4 -0
  55. data/lib/devise/models/rememberable.rb +65 -32
  56. data/lib/devise/models/timeoutable.rb +4 -8
  57. data/lib/devise/models/trackable.rb +20 -4
  58. data/lib/devise/models/validatable.rb +16 -9
  59. data/lib/devise/models.rb +6 -13
  60. data/lib/devise/modules.rb +12 -11
  61. data/lib/devise/omniauth/config.rb +2 -0
  62. data/lib/devise/omniauth/url_helpers.rb +14 -5
  63. data/lib/devise/omniauth.rb +4 -5
  64. data/lib/devise/orm/active_record.rb +5 -1
  65. data/lib/devise/orm/mongoid.rb +6 -2
  66. data/lib/devise/parameter_filter.rb +4 -0
  67. data/lib/devise/parameter_sanitizer.rb +144 -34
  68. data/lib/devise/rails/deprecated_constant_accessor.rb +39 -0
  69. data/lib/devise/rails/routes.rb +191 -127
  70. data/lib/devise/rails/warden_compat.rb +2 -1
  71. data/lib/devise/rails.rb +13 -20
  72. data/lib/devise/secret_key_finder.rb +27 -0
  73. data/lib/devise/strategies/authenticatable.rb +21 -22
  74. data/lib/devise/strategies/base.rb +3 -1
  75. data/lib/devise/strategies/database_authenticatable.rb +15 -4
  76. data/lib/devise/strategies/rememberable.rb +15 -3
  77. data/lib/devise/test/controller_helpers.rb +167 -0
  78. data/lib/devise/test/integration_helpers.rb +63 -0
  79. data/lib/devise/test_helpers.rb +7 -123
  80. data/lib/devise/time_inflector.rb +4 -2
  81. data/lib/devise/token_generator.rb +32 -0
  82. data/lib/devise/version.rb +3 -1
  83. data/lib/devise.rb +124 -78
  84. data/lib/generators/active_record/devise_generator.rb +64 -15
  85. data/lib/generators/active_record/templates/migration.rb +9 -8
  86. data/lib/generators/active_record/templates/migration_existing.rb +9 -8
  87. data/lib/generators/devise/controllers_generator.rb +46 -0
  88. data/lib/generators/devise/devise_generator.rb +10 -6
  89. data/lib/generators/devise/install_generator.rb +19 -1
  90. data/lib/generators/devise/orm_helpers.rb +17 -9
  91. data/lib/generators/devise/views_generator.rb +51 -28
  92. data/lib/generators/mongoid/devise_generator.rb +24 -24
  93. data/lib/generators/templates/README +13 -12
  94. data/lib/generators/templates/controllers/README +14 -0
  95. data/lib/generators/templates/controllers/confirmations_controller.rb +30 -0
  96. data/lib/generators/templates/controllers/omniauth_callbacks_controller.rb +30 -0
  97. data/lib/generators/templates/controllers/passwords_controller.rb +34 -0
  98. data/lib/generators/templates/controllers/registrations_controller.rb +62 -0
  99. data/lib/generators/templates/controllers/sessions_controller.rb +27 -0
  100. data/lib/generators/templates/controllers/unlocks_controller.rb +30 -0
  101. data/lib/generators/templates/devise.rb +118 -53
  102. data/lib/generators/templates/markerb/confirmation_instructions.markerb +1 -1
  103. data/lib/generators/templates/markerb/email_changed.markerb +7 -0
  104. data/lib/generators/templates/markerb/password_change.markerb +3 -0
  105. data/lib/generators/templates/markerb/reset_password_instructions.markerb +1 -1
  106. data/lib/generators/templates/markerb/unlock_instructions.markerb +1 -1
  107. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +6 -2
  108. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +12 -4
  109. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +5 -2
  110. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +14 -6
  111. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +12 -4
  112. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +11 -6
  113. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +5 -2
  114. metadata +73 -294
  115. data/.gitignore +0 -10
  116. data/.travis.yml +0 -20
  117. data/.yardopts +0 -9
  118. data/CHANGELOG.rdoc +0 -941
  119. data/CONTRIBUTING.md +0 -14
  120. data/Gemfile +0 -31
  121. data/Gemfile.lock +0 -159
  122. data/Rakefile +0 -35
  123. data/app/views/devise/_links.erb +0 -3
  124. data/devise.gemspec +0 -26
  125. data/devise.png +0 -0
  126. data/gemfiles/Gemfile.rails-3.2.x +0 -31
  127. data/gemfiles/Gemfile.rails-3.2.x.lock +0 -156
  128. data/lib/devise/models/token_authenticatable.rb +0 -89
  129. data/lib/devise/strategies/token_authenticatable.rb +0 -91
  130. data/test/controllers/custom_strategy_test.rb +0 -62
  131. data/test/controllers/helpers_test.rb +0 -253
  132. data/test/controllers/internal_helpers_test.rb +0 -120
  133. data/test/controllers/passwords_controller_test.rb +0 -32
  134. data/test/controllers/sessions_controller_test.rb +0 -99
  135. data/test/controllers/url_helpers_test.rb +0 -59
  136. data/test/delegator_test.rb +0 -19
  137. data/test/devise_test.rb +0 -83
  138. data/test/failure_app_test.rb +0 -221
  139. data/test/generators/active_record_generator_test.rb +0 -73
  140. data/test/generators/devise_generator_test.rb +0 -39
  141. data/test/generators/install_generator_test.rb +0 -13
  142. data/test/generators/mongoid_generator_test.rb +0 -23
  143. data/test/generators/views_generator_test.rb +0 -67
  144. data/test/helpers/devise_helper_test.rb +0 -51
  145. data/test/integration/authenticatable_test.rb +0 -699
  146. data/test/integration/confirmable_test.rb +0 -299
  147. data/test/integration/database_authenticatable_test.rb +0 -84
  148. data/test/integration/http_authenticatable_test.rb +0 -115
  149. data/test/integration/lockable_test.rb +0 -242
  150. data/test/integration/omniauthable_test.rb +0 -133
  151. data/test/integration/recoverable_test.rb +0 -335
  152. data/test/integration/registerable_test.rb +0 -349
  153. data/test/integration/rememberable_test.rb +0 -165
  154. data/test/integration/timeoutable_test.rb +0 -150
  155. data/test/integration/token_authenticatable_test.rb +0 -205
  156. data/test/integration/trackable_test.rb +0 -92
  157. data/test/mailers/confirmation_instructions_test.rb +0 -111
  158. data/test/mailers/reset_password_instructions_test.rb +0 -92
  159. data/test/mailers/unlock_instructions_test.rb +0 -87
  160. data/test/mapping_test.rb +0 -127
  161. data/test/models/authenticatable_test.rb +0 -13
  162. data/test/models/confirmable_test.rb +0 -452
  163. data/test/models/database_authenticatable_test.rb +0 -226
  164. data/test/models/lockable_test.rb +0 -282
  165. data/test/models/omniauthable_test.rb +0 -7
  166. data/test/models/recoverable_test.rb +0 -222
  167. data/test/models/registerable_test.rb +0 -7
  168. data/test/models/rememberable_test.rb +0 -175
  169. data/test/models/serializable_test.rb +0 -49
  170. data/test/models/timeoutable_test.rb +0 -46
  171. data/test/models/token_authenticatable_test.rb +0 -55
  172. data/test/models/trackable_test.rb +0 -13
  173. data/test/models/validatable_test.rb +0 -127
  174. data/test/models_test.rb +0 -163
  175. data/test/omniauth/config_test.rb +0 -57
  176. data/test/omniauth/url_helpers_test.rb +0 -54
  177. data/test/orm/active_record.rb +0 -10
  178. data/test/orm/mongoid.rb +0 -13
  179. data/test/parameter_sanitizer_test.rb +0 -58
  180. data/test/rails_app/Rakefile +0 -6
  181. data/test/rails_app/app/active_record/admin.rb +0 -6
  182. data/test/rails_app/app/active_record/shim.rb +0 -2
  183. data/test/rails_app/app/active_record/user.rb +0 -6
  184. data/test/rails_app/app/controllers/admins/sessions_controller.rb +0 -6
  185. data/test/rails_app/app/controllers/admins_controller.rb +0 -11
  186. data/test/rails_app/app/controllers/application_controller.rb +0 -9
  187. data/test/rails_app/app/controllers/home_controller.rb +0 -25
  188. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +0 -2
  189. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +0 -2
  190. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +0 -14
  191. data/test/rails_app/app/controllers/users_controller.rb +0 -31
  192. data/test/rails_app/app/helpers/application_helper.rb +0 -3
  193. data/test/rails_app/app/mailers/users/mailer.rb +0 -12
  194. data/test/rails_app/app/mongoid/admin.rb +0 -29
  195. data/test/rails_app/app/mongoid/shim.rb +0 -23
  196. data/test/rails_app/app/mongoid/user.rb +0 -42
  197. data/test/rails_app/app/views/admins/index.html.erb +0 -1
  198. data/test/rails_app/app/views/admins/sessions/new.html.erb +0 -2
  199. data/test/rails_app/app/views/home/admin_dashboard.html.erb +0 -1
  200. data/test/rails_app/app/views/home/index.html.erb +0 -1
  201. data/test/rails_app/app/views/home/join.html.erb +0 -1
  202. data/test/rails_app/app/views/home/private.html.erb +0 -1
  203. data/test/rails_app/app/views/home/user_dashboard.html.erb +0 -1
  204. data/test/rails_app/app/views/layouts/application.html.erb +0 -24
  205. data/test/rails_app/app/views/users/edit_form.html.erb +0 -1
  206. data/test/rails_app/app/views/users/index.html.erb +0 -1
  207. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +0 -1
  208. data/test/rails_app/app/views/users/sessions/new.html.erb +0 -1
  209. data/test/rails_app/bin/bundle +0 -3
  210. data/test/rails_app/bin/rails +0 -4
  211. data/test/rails_app/bin/rake +0 -4
  212. data/test/rails_app/config/application.rb +0 -40
  213. data/test/rails_app/config/boot.rb +0 -8
  214. data/test/rails_app/config/database.yml +0 -18
  215. data/test/rails_app/config/environment.rb +0 -5
  216. data/test/rails_app/config/environments/development.rb +0 -34
  217. data/test/rails_app/config/environments/production.rb +0 -84
  218. data/test/rails_app/config/environments/test.rb +0 -36
  219. data/test/rails_app/config/initializers/backtrace_silencers.rb +0 -7
  220. data/test/rails_app/config/initializers/devise.rb +0 -178
  221. data/test/rails_app/config/initializers/inflections.rb +0 -2
  222. data/test/rails_app/config/initializers/secret_token.rb +0 -8
  223. data/test/rails_app/config/initializers/session_store.rb +0 -1
  224. data/test/rails_app/config/routes.rb +0 -104
  225. data/test/rails_app/config.ru +0 -4
  226. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +0 -74
  227. data/test/rails_app/db/schema.rb +0 -52
  228. data/test/rails_app/lib/shared_admin.rb +0 -14
  229. data/test/rails_app/lib/shared_user.rb +0 -25
  230. data/test/rails_app/public/404.html +0 -26
  231. data/test/rails_app/public/422.html +0 -26
  232. data/test/rails_app/public/500.html +0 -26
  233. data/test/rails_app/public/favicon.ico +0 -0
  234. data/test/routes_test.rb +0 -250
  235. data/test/support/assertions.rb +0 -40
  236. data/test/support/helpers.rb +0 -91
  237. data/test/support/integration.rb +0 -92
  238. data/test/support/locale/en.yml +0 -4
  239. data/test/support/webrat/integrations/rails.rb +0 -24
  240. data/test/test_helper.rb +0 -34
  241. data/test/test_helpers_test.rb +0 -151
  242. data/test/test_models.rb +0 -26
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Devise::ConfirmationsController < DeviseController
2
4
  # GET /resource/confirmation/new
3
5
  def new
@@ -7,9 +9,10 @@ class Devise::ConfirmationsController < DeviseController
7
9
  # POST /resource/confirmation
8
10
  def create
9
11
  self.resource = resource_class.send_confirmation_instructions(resource_params)
12
+ yield resource if block_given?
10
13
 
11
14
  if successfully_sent?(resource)
12
- respond_with({}, :location => after_resending_confirmation_instructions_path_for(resource_name))
15
+ respond_with({}, location: after_resending_confirmation_instructions_path_for(resource_name))
13
16
  else
14
17
  respond_with(resource)
15
18
  end
@@ -18,13 +21,13 @@ class Devise::ConfirmationsController < DeviseController
18
21
  # GET /resource/confirmation?confirmation_token=abcdef
19
22
  def show
20
23
  self.resource = resource_class.confirm_by_token(params[:confirmation_token])
24
+ yield resource if block_given?
21
25
 
22
26
  if resource.errors.empty?
23
- set_flash_message(:notice, :confirmed) if is_navigational_format?
24
- sign_in(resource_name, resource)
27
+ set_flash_message!(:notice, :confirmed)
25
28
  respond_with_navigational(resource){ redirect_to after_confirmation_path_for(resource_name, resource) }
26
29
  else
27
- respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render :new }
30
+ respond_with_navigational(resource.errors, status: :unprocessable_entity){ render :new }
28
31
  end
29
32
  end
30
33
 
@@ -32,11 +35,19 @@ class Devise::ConfirmationsController < DeviseController
32
35
 
33
36
  # The path used after resending confirmation instructions.
34
37
  def after_resending_confirmation_instructions_path_for(resource_name)
35
- new_session_path(resource_name) if is_navigational_format?
38
+ is_navigational_format? ? new_session_path(resource_name) : '/'
36
39
  end
37
40
 
38
41
  # The path used after confirmation.
39
42
  def after_confirmation_path_for(resource_name, resource)
40
- after_sign_in_path_for(resource)
43
+ if signed_in?(resource_name)
44
+ signed_in_root_path(resource)
45
+ else
46
+ new_session_path(resource_name)
47
+ end
48
+ end
49
+
50
+ def translation_scope
51
+ 'devise.confirmations'
41
52
  end
42
53
  end
@@ -1,30 +1,36 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Devise::OmniauthCallbacksController < DeviseController
2
- prepend_before_filter { request.env["devise.skip_timeout"] = true }
4
+ prepend_before_action { request.env["devise.skip_timeout"] = true }
3
5
 
4
6
  def passthru
5
- render :status => 404, :text => "Not found. Authentication passthru."
7
+ render status: 404, plain: "Not found. Authentication passthru."
6
8
  end
7
9
 
8
10
  def failure
9
- set_flash_message :alert, :failure, :kind => OmniAuth::Utils.camelize(failed_strategy.name), :reason => failure_message
11
+ set_flash_message! :alert, :failure, kind: OmniAuth::Utils.camelize(failed_strategy.name), reason: failure_message
10
12
  redirect_to after_omniauth_failure_path_for(resource_name)
11
13
  end
12
14
 
13
15
  protected
14
16
 
15
17
  def failed_strategy
16
- env["omniauth.error.strategy"]
18
+ request.respond_to?(:get_header) ? request.get_header("omniauth.error.strategy") : request.env["omniauth.error.strategy"]
17
19
  end
18
20
 
19
21
  def failure_message
20
- exception = env["omniauth.error"]
22
+ exception = request.respond_to?(:get_header) ? request.get_header("omniauth.error") : request.env["omniauth.error"]
21
23
  error = exception.error_reason if exception.respond_to?(:error_reason)
22
24
  error ||= exception.error if exception.respond_to?(:error)
23
- error ||= env["omniauth.error.type"].to_s
25
+ error ||= (request.respond_to?(:get_header) ? request.get_header("omniauth.error.type") : request.env["omniauth.error.type"]).to_s
24
26
  error.to_s.humanize if error
25
27
  end
26
28
 
27
29
  def after_omniauth_failure_path_for(scope)
28
30
  new_session_path(scope)
29
31
  end
32
+
33
+ def translation_scope
34
+ 'devise.omniauth_callbacks'
35
+ end
30
36
  end
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Devise::PasswordsController < DeviseController
2
- prepend_before_filter :require_no_authentication
4
+ prepend_before_action :require_no_authentication
3
5
  # Render the #edit only if coming from a reset password email link
4
- append_before_filter :assert_reset_token_passed, :only => :edit
6
+ append_before_action :assert_reset_token_passed, only: :edit
5
7
 
6
8
  # GET /resource/password/new
7
9
  def new
@@ -11,9 +13,10 @@ class Devise::PasswordsController < DeviseController
11
13
  # POST /resource/password
12
14
  def create
13
15
  self.resource = resource_class.send_reset_password_instructions(resource_params)
16
+ yield resource if block_given?
14
17
 
15
18
  if successfully_sent?(resource)
16
- respond_with({}, :location => after_sending_reset_password_instructions_path_for(resource_name))
19
+ respond_with({}, location: after_sending_reset_password_instructions_path_for(resource_name))
17
20
  else
18
21
  respond_with(resource)
19
22
  end
@@ -22,27 +25,35 @@ class Devise::PasswordsController < DeviseController
22
25
  # GET /resource/password/edit?reset_password_token=abcdef
23
26
  def edit
24
27
  self.resource = resource_class.new
28
+ set_minimum_password_length
25
29
  resource.reset_password_token = params[:reset_password_token]
26
30
  end
27
31
 
28
32
  # PUT /resource/password
29
33
  def update
30
34
  self.resource = resource_class.reset_password_by_token(resource_params)
35
+ yield resource if block_given?
31
36
 
32
37
  if resource.errors.empty?
33
38
  resource.unlock_access! if unlockable?(resource)
34
- flash_message = resource.active_for_authentication? ? :updated : :updated_not_active
35
- set_flash_message(:notice, flash_message) if is_navigational_format?
36
- sign_in(resource_name, resource)
37
- respond_with resource, :location => after_resetting_password_path_for(resource)
39
+ if Devise.sign_in_after_reset_password
40
+ flash_message = resource.active_for_authentication? ? :updated : :updated_not_active
41
+ set_flash_message!(:notice, flash_message)
42
+ resource.after_database_authentication
43
+ sign_in(resource_name, resource)
44
+ else
45
+ set_flash_message!(:notice, :updated_not_active)
46
+ end
47
+ respond_with resource, location: after_resetting_password_path_for(resource)
38
48
  else
49
+ set_minimum_password_length
39
50
  respond_with resource
40
51
  end
41
52
  end
42
53
 
43
54
  protected
44
55
  def after_resetting_password_path_for(resource)
45
- after_sign_in_path_for(resource)
56
+ Devise.sign_in_after_reset_password ? after_sign_in_path_for(resource) : new_session_path(resource_name)
46
57
  end
47
58
 
48
59
  # The path used after sending reset password instructions
@@ -65,4 +76,8 @@ class Devise::PasswordsController < DeviseController
65
76
  resource.respond_to?(:unlock_strategy_enabled?) &&
66
77
  resource.unlock_strategy_enabled?(:email)
67
78
  end
79
+
80
+ def translation_scope
81
+ 'devise.passwords'
82
+ end
68
83
  end
@@ -1,29 +1,36 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Devise::RegistrationsController < DeviseController
2
- prepend_before_filter :require_no_authentication, :only => [ :new, :create, :cancel ]
3
- prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy]
4
+ prepend_before_action :require_no_authentication, only: [:new, :create, :cancel]
5
+ prepend_before_action :authenticate_scope!, only: [:edit, :update, :destroy]
6
+ prepend_before_action :set_minimum_password_length, only: [:new, :edit]
4
7
 
5
8
  # GET /resource/sign_up
6
9
  def new
7
- build_resource({})
8
- respond_with self.resource
10
+ build_resource
11
+ yield resource if block_given?
12
+ respond_with resource
9
13
  end
10
14
 
11
15
  # POST /resource
12
16
  def create
13
17
  build_resource(sign_up_params)
14
18
 
15
- if resource.save
19
+ resource.save
20
+ yield resource if block_given?
21
+ if resource.persisted?
16
22
  if resource.active_for_authentication?
17
- set_flash_message :notice, :signed_up if is_navigational_format?
23
+ set_flash_message! :notice, :signed_up
18
24
  sign_up(resource_name, resource)
19
- respond_with resource, :location => after_sign_up_path_for(resource)
25
+ respond_with resource, location: after_sign_up_path_for(resource)
20
26
  else
21
- set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
22
- expire_session_data_after_sign_in!
23
- respond_with resource, :location => after_inactive_sign_up_path_for(resource)
27
+ set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}"
28
+ expire_data_after_sign_in!
29
+ respond_with resource, location: after_inactive_sign_up_path_for(resource)
24
30
  end
25
31
  else
26
32
  clean_up_passwords resource
33
+ set_minimum_password_length
27
34
  respond_with resource
28
35
  end
29
36
  end
@@ -40,16 +47,16 @@ class Devise::RegistrationsController < DeviseController
40
47
  self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)
41
48
  prev_unconfirmed_email = resource.unconfirmed_email if resource.respond_to?(:unconfirmed_email)
42
49
 
43
- if resource.update_with_password(account_update_params)
44
- if is_navigational_format?
45
- flash_key = update_needs_confirmation?(resource, prev_unconfirmed_email) ?
46
- :update_needs_confirmation : :updated
47
- set_flash_message :notice, flash_key
48
- end
49
- sign_in resource_name, resource, :bypass => true
50
- respond_with resource, :location => after_update_path_for(resource)
50
+ resource_updated = update_resource(resource, account_update_params)
51
+ yield resource if block_given?
52
+ if resource_updated
53
+ set_flash_message_for_update(resource, prev_unconfirmed_email)
54
+ bypass_sign_in resource, scope: resource_name if sign_in_after_change_password?
55
+
56
+ respond_with resource, location: after_update_path_for(resource)
51
57
  else
52
58
  clean_up_passwords resource
59
+ set_minimum_password_length
53
60
  respond_with resource
54
61
  end
55
62
  end
@@ -58,7 +65,8 @@ class Devise::RegistrationsController < DeviseController
58
65
  def destroy
59
66
  resource.destroy
60
67
  Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)
61
- set_flash_message :notice, :destroyed if is_navigational_format?
68
+ set_flash_message! :notice, :destroyed
69
+ yield resource if block_given?
62
70
  respond_with_navigational(resource){ redirect_to after_sign_out_path_for(resource_name) }
63
71
  end
64
72
 
@@ -68,7 +76,7 @@ class Devise::RegistrationsController < DeviseController
68
76
  # cancel oauth signing in/up in the middle of the process,
69
77
  # removing all OAuth session data.
70
78
  def cancel
71
- expire_session_data_after_sign_in!
79
+ expire_data_after_sign_in!
72
80
  redirect_to new_registration_path(resource_name)
73
81
  end
74
82
 
@@ -80,10 +88,16 @@ class Devise::RegistrationsController < DeviseController
80
88
  previous != resource.unconfirmed_email
81
89
  end
82
90
 
91
+ # By default we want to require a password checks on update.
92
+ # You can overwrite this method in your own RegistrationsController.
93
+ def update_resource(resource, params)
94
+ resource.update_with_password(params)
95
+ end
96
+
83
97
  # Build a devise resource passing in the session. Useful to move
84
98
  # temporary session data to the newly created user.
85
- def build_resource(hash=nil)
86
- self.resource = resource_class.new_with_session(hash || {}, session)
99
+ def build_resource(hash = {})
100
+ self.resource = resource_class.new_with_session(hash, session)
87
101
  end
88
102
 
89
103
  # Signs in a user on sign up. You can overwrite this method in your own
@@ -95,32 +109,60 @@ class Devise::RegistrationsController < DeviseController
95
109
  # The path used after sign up. You need to overwrite this method
96
110
  # in your own RegistrationsController.
97
111
  def after_sign_up_path_for(resource)
98
- after_sign_in_path_for(resource)
112
+ after_sign_in_path_for(resource) if is_navigational_format?
99
113
  end
100
114
 
101
115
  # The path used after sign up for inactive accounts. You need to overwrite
102
116
  # this method in your own RegistrationsController.
103
117
  def after_inactive_sign_up_path_for(resource)
104
- respond_to?(:root_path) ? root_path : "/"
118
+ scope = Devise::Mapping.find_scope!(resource)
119
+ router_name = Devise.mappings[scope].router_name
120
+ context = router_name ? send(router_name) : self
121
+ context.respond_to?(:root_path) ? context.root_path : "/"
105
122
  end
106
123
 
107
124
  # The default url to be used after updating a resource. You need to overwrite
108
125
  # this method in your own RegistrationsController.
109
126
  def after_update_path_for(resource)
110
- signed_in_root_path(resource)
127
+ sign_in_after_change_password? ? signed_in_root_path(resource) : new_session_path(resource_name)
111
128
  end
112
129
 
113
130
  # Authenticates the current scope and gets the current resource from the session.
114
131
  def authenticate_scope!
115
- send(:"authenticate_#{resource_name}!", :force => true)
132
+ send(:"authenticate_#{resource_name}!", force: true)
116
133
  self.resource = send(:"current_#{resource_name}")
117
134
  end
118
135
 
119
136
  def sign_up_params
120
- devise_parameter_sanitizer.for(:sign_up)
137
+ devise_parameter_sanitizer.sanitize(:sign_up)
121
138
  end
122
139
 
123
140
  def account_update_params
124
- devise_parameter_sanitizer.for(:account_update)
141
+ devise_parameter_sanitizer.sanitize(:account_update)
142
+ end
143
+
144
+ def translation_scope
145
+ 'devise.registrations'
146
+ end
147
+
148
+ private
149
+
150
+ def set_flash_message_for_update(resource, prev_unconfirmed_email)
151
+ return unless is_flashing_format?
152
+
153
+ flash_key = if update_needs_confirmation?(resource, prev_unconfirmed_email)
154
+ :update_needs_confirmation
155
+ elsif sign_in_after_change_password?
156
+ :updated
157
+ else
158
+ :updated_but_not_signed_in
159
+ end
160
+ set_flash_message :notice, flash_key
161
+ end
162
+
163
+ def sign_in_after_change_password?
164
+ return true if account_update_params[:password].blank?
165
+
166
+ Devise.sign_in_after_change_password
125
167
  end
126
168
  end
@@ -1,51 +1,83 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Devise::SessionsController < DeviseController
2
- prepend_before_filter :require_no_authentication, :only => [ :new, :create ]
3
- prepend_before_filter :allow_params_authentication!, :only => :create
4
- prepend_before_filter { request.env["devise.skip_timeout"] = true }
4
+ prepend_before_action :require_no_authentication, only: [:new, :create]
5
+ prepend_before_action :allow_params_authentication!, only: :create
6
+ prepend_before_action :verify_signed_out_user, only: :destroy
7
+ prepend_before_action(only: [:create, :destroy]) { request.env["devise.skip_timeout"] = true }
5
8
 
6
9
  # GET /resource/sign_in
7
10
  def new
8
11
  self.resource = resource_class.new(sign_in_params)
9
12
  clean_up_passwords(resource)
13
+ yield resource if block_given?
10
14
  respond_with(resource, serialize_options(resource))
11
15
  end
12
16
 
13
17
  # POST /resource/sign_in
14
18
  def create
15
19
  self.resource = warden.authenticate!(auth_options)
16
- set_flash_message(:notice, :signed_in) if is_navigational_format?
20
+ set_flash_message!(:notice, :signed_in)
17
21
  sign_in(resource_name, resource)
18
- respond_with resource, :location => after_sign_in_path_for(resource)
22
+ yield resource if block_given?
23
+ respond_with resource, location: after_sign_in_path_for(resource)
19
24
  end
20
25
 
21
26
  # DELETE /resource/sign_out
22
27
  def destroy
23
- redirect_path = after_sign_out_path_for(resource_name)
24
28
  signed_out = (Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name))
25
- set_flash_message :notice, :signed_out if signed_out && is_navigational_format?
26
-
27
- # We actually need to hardcode this as Rails default responder doesn't
28
- # support returning empty response on GET request
29
- respond_to do |format|
30
- format.all { head :no_content }
31
- format.any(*navigational_formats) { redirect_to redirect_path }
32
- end
29
+ set_flash_message! :notice, :signed_out if signed_out
30
+ yield if block_given?
31
+ respond_to_on_destroy
33
32
  end
34
33
 
35
34
  protected
36
35
 
37
36
  def sign_in_params
38
- devise_parameter_sanitizer.for(:sign_in)
37
+ devise_parameter_sanitizer.sanitize(:sign_in)
39
38
  end
40
39
 
41
40
  def serialize_options(resource)
42
41
  methods = resource_class.authentication_keys.dup
43
42
  methods = methods.keys if methods.is_a?(Hash)
44
43
  methods << :password if resource.respond_to?(:password)
45
- { :methods => methods, :only => [:password] }
44
+ { methods: methods, only: [:password] }
46
45
  end
47
46
 
48
47
  def auth_options
49
- { :scope => resource_name, :recall => "#{controller_path}#new" }
48
+ { scope: resource_name, recall: "#{controller_path}#new" }
49
+ end
50
+
51
+ def translation_scope
52
+ 'devise.sessions'
53
+ end
54
+
55
+ private
56
+
57
+ # Check if there is no signed in user before doing the sign out.
58
+ #
59
+ # If there is no signed in user, it will set the flash message and redirect
60
+ # to the after_sign_out path.
61
+ def verify_signed_out_user
62
+ if all_signed_out?
63
+ set_flash_message! :notice, :already_signed_out
64
+
65
+ respond_to_on_destroy
66
+ end
67
+ end
68
+
69
+ def all_signed_out?
70
+ users = Devise.mappings.keys.map { |s| warden.user(scope: s, run_callbacks: false) }
71
+
72
+ users.all?(&:blank?)
73
+ end
74
+
75
+ def respond_to_on_destroy
76
+ # We actually need to hardcode this as Rails default responder doesn't
77
+ # support returning empty response on GET request
78
+ respond_to do |format|
79
+ format.all { head :no_content }
80
+ format.any(*navigational_formats) { redirect_to after_sign_out_path_for(resource_name) }
81
+ end
50
82
  end
51
83
  end
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Devise::UnlocksController < DeviseController
2
- prepend_before_filter :require_no_authentication
4
+ prepend_before_action :require_no_authentication
3
5
 
4
6
  # GET /resource/unlock/new
5
7
  def new
@@ -9,9 +11,10 @@ class Devise::UnlocksController < DeviseController
9
11
  # POST /resource/unlock
10
12
  def create
11
13
  self.resource = resource_class.send_unlock_instructions(resource_params)
14
+ yield resource if block_given?
12
15
 
13
16
  if successfully_sent?(resource)
14
- respond_with({}, :location => after_sending_unlock_instructions_path_for(resource))
17
+ respond_with({}, location: after_sending_unlock_instructions_path_for(resource))
15
18
  else
16
19
  respond_with(resource)
17
20
  end
@@ -20,12 +23,13 @@ class Devise::UnlocksController < DeviseController
20
23
  # GET /resource/unlock?unlock_token=abcdef
21
24
  def show
22
25
  self.resource = resource_class.unlock_access_by_token(params[:unlock_token])
26
+ yield resource if block_given?
23
27
 
24
28
  if resource.errors.empty?
25
- set_flash_message :notice, :unlocked if is_navigational_format?
29
+ set_flash_message! :notice, :unlocked
26
30
  respond_with_navigational(resource){ redirect_to after_unlock_path_for(resource) }
27
31
  else
28
- respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render :new }
32
+ respond_with_navigational(resource.errors, status: :unprocessable_entity){ render :new }
29
33
  end
30
34
  end
31
35
 
@@ -41,4 +45,7 @@ class Devise::UnlocksController < DeviseController
41
45
  new_session_path(resource) if is_navigational_format?
42
46
  end
43
47
 
48
+ def translation_scope
49
+ 'devise.unlocks'
50
+ end
44
51
  end
@@ -1,17 +1,39 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # All Devise controllers are inherited from here.
2
4
  class DeviseController < Devise.parent_controller.constantize
3
5
  include Devise::Controllers::ScopedViews
4
6
 
5
- helper DeviseHelper
7
+ if respond_to?(:helper)
8
+ helper DeviseHelper
9
+ end
6
10
 
7
- helpers = %w(resource scope_name resource_name signed_in_resource
8
- resource_class resource_params devise_mapping)
9
- hide_action *helpers
10
- helper_method *helpers
11
+ if respond_to?(:helper_method)
12
+ helpers = %w(resource scope_name resource_name signed_in_resource
13
+ resource_class resource_params devise_mapping)
14
+ helper_method(*helpers)
15
+ end
11
16
 
12
- prepend_before_filter :assert_is_devise_resource!
17
+ prepend_before_action :assert_is_devise_resource!
13
18
  respond_to :html if mimes_for_respond_to.empty?
14
19
 
20
+ # Override prefixes to consider the scoped view.
21
+ # Notice we need to check for the request due to a bug in
22
+ # Action Controller tests that forces _prefixes to be
23
+ # loaded before even having a request object.
24
+ #
25
+ # This method should be public as it is in ActionPack
26
+ # itself. Changing its visibility may break other gems.
27
+ def _prefixes #:nodoc:
28
+ @_prefixes ||= if self.class.scoped_views? && request && devise_mapping
29
+ ["#{devise_mapping.scoped_path}/#{controller_name}"] + super
30
+ else
31
+ super
32
+ end
33
+ end
34
+
35
+ protected
36
+
15
37
  # Gets the actual resource stored in the instance variable
16
38
  def resource
17
39
  instance_variable_get(:"@#{resource_name}")
@@ -30,7 +52,7 @@ class DeviseController < Devise.parent_controller.constantize
30
52
 
31
53
  # Returns a signed in resource from session (if one exists)
32
54
  def signed_in_resource
33
- warden.authenticate(:scope => resource_name)
55
+ warden.authenticate(scope: resource_name)
34
56
  end
35
57
 
36
58
  # Attempt to find the mapped route for devise based on request path
@@ -38,22 +60,6 @@ class DeviseController < Devise.parent_controller.constantize
38
60
  @devise_mapping ||= request.env["devise.mapping"]
39
61
  end
40
62
 
41
- # Override prefixes to consider the scoped view.
42
- # Notice we need to check for the request due to a bug in
43
- # Action Controller tests that forces _prefixes to be
44
- # loaded before even having a request object.
45
- def _prefixes #:nodoc:
46
- @_prefixes ||= if self.class.scoped_views? && request && devise_mapping
47
- super.unshift("#{devise_mapping.scoped_path}/#{controller_name}")
48
- else
49
- super
50
- end
51
- end
52
-
53
- hide_action :_prefixes
54
-
55
- protected
56
-
57
63
  # Checks whether it's a devise mapped resource or not.
58
64
  def assert_is_devise_resource! #:nodoc:
59
65
  unknown_action! <<-MESSAGE unless devise_mapping
@@ -89,24 +95,24 @@ MESSAGE
89
95
  instance_variable_set(:"@#{resource_name}", new_resource)
90
96
  end
91
97
 
92
- # Helper for use in before_filters where no authentication is required.
98
+ # Helper for use in before_actions where no authentication is required.
93
99
  #
94
100
  # Example:
95
- # before_filter :require_no_authentication, :only => :new
101
+ # before_action :require_no_authentication, only: :new
96
102
  def require_no_authentication
97
103
  assert_is_devise_resource!
98
104
  return unless is_navigational_format?
99
105
  no_input = devise_mapping.no_input_strategies
100
106
 
101
107
  authenticated = if no_input.present?
102
- args = no_input.dup.push :scope => resource_name
108
+ args = no_input.dup.push scope: resource_name
103
109
  warden.authenticate?(*args)
104
110
  else
105
111
  warden.authenticated?(resource_name)
106
112
  end
107
113
 
108
114
  if authenticated && resource = warden.user(resource_name)
109
- flash[:alert] = I18n.t("devise.failure.already_authenticated")
115
+ set_flash_message(:alert, 'already_authenticated', scope: 'devise.failure')
110
116
  redirect_to after_sign_in_path_for(resource)
111
117
  end
112
118
  end
@@ -123,14 +129,17 @@ MESSAGE
123
129
  end
124
130
 
125
131
  if notice
126
- set_flash_message :notice, notice if is_navigational_format?
132
+ set_flash_message! :notice, notice
127
133
  true
128
134
  end
129
135
  end
130
136
 
131
137
  # Sets the flash message with :key, using I18n. By default you are able
132
- # to setup your messages using specific resource scope, and if no one is
133
- # found we look to default scope.
138
+ # to set up your messages using specific resource scope, and if no message is
139
+ # found we look to the default scope. Set the "now" options key to a true
140
+ # value to populate the flash.now hash in lieu of the default flash hash (so
141
+ # the flash message will be available to the current action instead of the
142
+ # next action).
134
143
  # Example (i18n locale file):
135
144
  #
136
145
  # en:
@@ -144,16 +153,45 @@ MESSAGE
144
153
  # available.
145
154
  def set_flash_message(key, kind, options = {})
146
155
  message = find_message(kind, options)
147
- flash[key] = message if message.present?
156
+ if options[:now]
157
+ flash.now[key] = message if message.present?
158
+ else
159
+ flash[key] = message if message.present?
160
+ end
161
+ end
162
+
163
+ # Sets flash message if is_flashing_format? equals true
164
+ def set_flash_message!(key, kind, options = {})
165
+ if is_flashing_format?
166
+ set_flash_message(key, kind, options)
167
+ end
168
+ end
169
+
170
+ # Sets minimum password length to show to user
171
+ def set_minimum_password_length
172
+ if devise_mapping.validatable?
173
+ @minimum_password_length = resource_class.password_length.min
174
+ end
175
+ end
176
+
177
+ def devise_i18n_options(options)
178
+ options
148
179
  end
149
180
 
150
181
  # Get message for given
151
182
  def find_message(kind, options = {})
152
- options[:scope] = "devise.#{controller_name}"
183
+ options[:scope] ||= translation_scope
153
184
  options[:default] = Array(options[:default]).unshift(kind.to_sym)
154
185
  options[:resource_name] = resource_name
155
- options = devise_i18n_options(options) if respond_to?(:devise_i18n_options, true)
156
- I18n.t("#{options[:resource_name]}.#{kind}", options)
186
+ options = devise_i18n_options(options)
187
+ I18n.t("#{options[:resource_name]}.#{kind}", **options)
188
+ end
189
+
190
+ # Controllers inheriting DeviseController are advised to override this
191
+ # method so that other controllers inheriting from them would use
192
+ # existing translations.
193
+ def translation_scope
194
+ "devise.#{controller_name}"
157
195
  end
158
196
 
159
197
  def clean_up_passwords(object)
@@ -169,4 +207,6 @@ MESSAGE
169
207
  def resource_params
170
208
  params.fetch(resource_name, {})
171
209
  end
210
+
211
+ ActiveSupport.run_load_hooks(:devise_controller, self)
172
212
  end