devise 3.2.1 → 4.4.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of devise might be problematic. Click here for more details.

Files changed (254) hide show
  1. checksums.yaml +7 -0
  2. data/.travis.yml +58 -10
  3. data/CHANGELOG.md +199 -979
  4. data/CODE_OF_CONDUCT.md +22 -0
  5. data/CONTRIBUTING.md +73 -8
  6. data/Gemfile +19 -11
  7. data/Gemfile.lock +152 -119
  8. data/ISSUE_TEMPLATE.md +19 -0
  9. data/MIT-LICENSE +1 -1
  10. data/README.md +347 -93
  11. data/Rakefile +4 -2
  12. data/app/controllers/devise/confirmations_controller.rb +11 -5
  13. data/app/controllers/devise/omniauth_callbacks_controller.rb +12 -6
  14. data/app/controllers/devise/passwords_controller.rb +20 -8
  15. data/app/controllers/devise/registrations_controller.rb +34 -19
  16. data/app/controllers/devise/sessions_controller.rb +47 -17
  17. data/app/controllers/devise/unlocks_controller.rb +9 -4
  18. data/app/controllers/devise_controller.rb +67 -31
  19. data/app/helpers/devise_helper.rb +4 -2
  20. data/app/mailers/devise/mailer.rb +10 -0
  21. data/app/views/devise/confirmations/new.html.erb +8 -4
  22. data/app/views/devise/mailer/confirmation_instructions.html.erb +1 -1
  23. data/app/views/devise/mailer/email_changed.html.erb +7 -0
  24. data/app/views/devise/mailer/password_change.html.erb +3 -0
  25. data/app/views/devise/mailer/reset_password_instructions.html.erb +1 -1
  26. data/app/views/devise/mailer/unlock_instructions.html.erb +1 -1
  27. data/app/views/devise/passwords/edit.html.erb +15 -6
  28. data/app/views/devise/passwords/new.html.erb +8 -4
  29. data/app/views/devise/registrations/edit.html.erb +28 -14
  30. data/app/views/devise/registrations/new.html.erb +19 -8
  31. data/app/views/devise/sessions/new.html.erb +17 -8
  32. data/app/views/devise/shared/{_links.erb → _links.html.erb} +2 -2
  33. data/app/views/devise/unlocks/new.html.erb +8 -4
  34. data/bin/test +13 -0
  35. data/config/locales/en.yml +22 -17
  36. data/devise.gemspec +7 -6
  37. data/gemfiles/Gemfile.rails-4.1-stable +32 -0
  38. data/gemfiles/Gemfile.rails-4.1-stable.lock +171 -0
  39. data/gemfiles/Gemfile.rails-4.2-stable +32 -0
  40. data/gemfiles/Gemfile.rails-4.2-stable.lock +192 -0
  41. data/gemfiles/Gemfile.rails-5.0-stable +33 -0
  42. data/gemfiles/Gemfile.rails-5.0-stable.lock +192 -0
  43. data/gemfiles/Gemfile.rails-5.2-rc1 +26 -0
  44. data/gemfiles/Gemfile.rails-5.2-rc1.lock +201 -0
  45. data/guides/bug_report_templates/integration_test.rb +106 -0
  46. data/lib/devise.rb +107 -84
  47. data/lib/devise/controllers/helpers.rb +111 -31
  48. data/lib/devise/controllers/rememberable.rb +15 -6
  49. data/lib/devise/controllers/scoped_views.rb +3 -1
  50. data/lib/devise/controllers/sign_in_out.rb +39 -26
  51. data/lib/devise/controllers/store_location.rb +31 -2
  52. data/lib/devise/controllers/url_helpers.rb +9 -7
  53. data/lib/devise/delegator.rb +2 -0
  54. data/lib/devise/encryptor.rb +24 -0
  55. data/lib/devise/failure_app.rb +98 -39
  56. data/lib/devise/hooks/activatable.rb +7 -6
  57. data/lib/devise/hooks/csrf_cleaner.rb +5 -1
  58. data/lib/devise/hooks/forgetable.rb +2 -0
  59. data/lib/devise/hooks/lockable.rb +7 -2
  60. data/lib/devise/hooks/proxy.rb +4 -2
  61. data/lib/devise/hooks/rememberable.rb +4 -2
  62. data/lib/devise/hooks/timeoutable.rb +16 -9
  63. data/lib/devise/hooks/trackable.rb +3 -1
  64. data/lib/devise/mailers/helpers.rb +15 -12
  65. data/lib/devise/mapping.rb +8 -2
  66. data/lib/devise/models.rb +3 -1
  67. data/lib/devise/models/authenticatable.rb +63 -36
  68. data/lib/devise/models/confirmable.rb +121 -41
  69. data/lib/devise/models/database_authenticatable.rb +66 -23
  70. data/lib/devise/models/lockable.rb +30 -17
  71. data/lib/devise/models/omniauthable.rb +3 -1
  72. data/lib/devise/models/recoverable.rb +62 -26
  73. data/lib/devise/models/registerable.rb +2 -0
  74. data/lib/devise/models/rememberable.rb +62 -33
  75. data/lib/devise/models/timeoutable.rb +4 -8
  76. data/lib/devise/models/trackable.rb +12 -3
  77. data/lib/devise/models/validatable.rb +16 -9
  78. data/lib/devise/modules.rb +12 -10
  79. data/lib/devise/omniauth.rb +2 -0
  80. data/lib/devise/omniauth/config.rb +2 -0
  81. data/lib/devise/omniauth/url_helpers.rb +14 -5
  82. data/lib/devise/orm/active_record.rb +5 -1
  83. data/lib/devise/orm/mongoid.rb +6 -2
  84. data/lib/devise/parameter_filter.rb +2 -0
  85. data/lib/devise/parameter_sanitizer.rb +131 -69
  86. data/lib/devise/rails.rb +10 -13
  87. data/lib/devise/rails/routes.rb +147 -116
  88. data/lib/devise/rails/warden_compat.rb +3 -10
  89. data/lib/devise/secret_key_finder.rb +25 -0
  90. data/lib/devise/strategies/authenticatable.rb +20 -9
  91. data/lib/devise/strategies/base.rb +3 -1
  92. data/lib/devise/strategies/database_authenticatable.rb +8 -5
  93. data/lib/devise/strategies/rememberable.rb +15 -3
  94. data/lib/devise/test/controller_helpers.rb +165 -0
  95. data/lib/devise/test/integration_helpers.rb +63 -0
  96. data/lib/devise/test_helpers.rb +7 -124
  97. data/lib/devise/time_inflector.rb +4 -2
  98. data/lib/devise/token_generator.rb +3 -41
  99. data/lib/devise/version.rb +3 -1
  100. data/lib/generators/active_record/devise_generator.rb +47 -10
  101. data/lib/generators/active_record/templates/migration.rb +9 -7
  102. data/lib/generators/active_record/templates/migration_existing.rb +9 -7
  103. data/lib/generators/devise/controllers_generator.rb +46 -0
  104. data/lib/generators/devise/devise_generator.rb +9 -5
  105. data/lib/generators/devise/install_generator.rb +22 -0
  106. data/lib/generators/devise/orm_helpers.rb +8 -19
  107. data/lib/generators/devise/views_generator.rb +51 -28
  108. data/lib/generators/mongoid/devise_generator.rb +22 -19
  109. data/lib/generators/templates/README +5 -12
  110. data/lib/generators/templates/controllers/README +14 -0
  111. data/lib/generators/templates/controllers/confirmations_controller.rb +30 -0
  112. data/lib/generators/templates/controllers/omniauth_callbacks_controller.rb +30 -0
  113. data/lib/generators/templates/controllers/passwords_controller.rb +34 -0
  114. data/lib/generators/templates/controllers/registrations_controller.rb +62 -0
  115. data/lib/generators/templates/controllers/sessions_controller.rb +27 -0
  116. data/lib/generators/templates/controllers/unlocks_controller.rb +30 -0
  117. data/lib/generators/templates/devise.rb +64 -35
  118. data/lib/generators/templates/markerb/confirmation_instructions.markerb +1 -1
  119. data/lib/generators/templates/markerb/email_changed.markerb +7 -0
  120. data/lib/generators/templates/markerb/password_change.markerb +3 -0
  121. data/lib/generators/templates/markerb/reset_password_instructions.markerb +1 -1
  122. data/lib/generators/templates/markerb/unlock_instructions.markerb +1 -1
  123. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +2 -2
  124. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +4 -4
  125. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +2 -2
  126. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +6 -6
  127. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +4 -4
  128. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +6 -6
  129. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +2 -2
  130. data/test/controllers/custom_registrations_controller_test.rb +42 -0
  131. data/test/controllers/custom_strategy_test.rb +10 -6
  132. data/test/controllers/helper_methods_test.rb +24 -0
  133. data/test/controllers/helpers_test.rb +88 -40
  134. data/test/controllers/inherited_controller_i18n_messages_test.rb +53 -0
  135. data/test/controllers/internal_helpers_test.rb +31 -22
  136. data/test/controllers/load_hooks_controller_test.rb +21 -0
  137. data/test/controllers/passwords_controller_test.rb +8 -5
  138. data/test/controllers/sessions_controller_test.rb +42 -33
  139. data/test/controllers/url_helpers_test.rb +13 -5
  140. data/test/delegator_test.rb +3 -1
  141. data/test/devise_test.rb +34 -19
  142. data/test/failure_app_test.rb +150 -42
  143. data/test/generators/active_record_generator_test.rb +58 -31
  144. data/test/generators/controllers_generator_test.rb +50 -0
  145. data/test/generators/devise_generator_test.rb +4 -2
  146. data/test/generators/install_generator_test.rb +16 -3
  147. data/test/generators/mongoid_generator_test.rb +5 -3
  148. data/test/generators/views_generator_test.rb +40 -2
  149. data/test/helpers/devise_helper_test.rb +20 -20
  150. data/test/integration/authenticatable_test.rb +134 -141
  151. data/test/integration/confirmable_test.rb +109 -67
  152. data/test/integration/database_authenticatable_test.rb +36 -23
  153. data/test/integration/http_authenticatable_test.rb +29 -20
  154. data/test/integration/lockable_test.rb +52 -49
  155. data/test/integration/mounted_engine_test.rb +38 -0
  156. data/test/integration/omniauthable_test.rb +30 -15
  157. data/test/integration/recoverable_test.rb +76 -61
  158. data/test/integration/registerable_test.rb +107 -91
  159. data/test/integration/rememberable_test.rb +82 -30
  160. data/test/integration/timeoutable_test.rb +48 -40
  161. data/test/integration/trackable_test.rb +15 -8
  162. data/test/mailers/confirmation_instructions_test.rb +16 -14
  163. data/test/mailers/email_changed_test.rb +132 -0
  164. data/test/mailers/mailer_test.rb +20 -0
  165. data/test/mailers/reset_password_instructions_test.rb +13 -11
  166. data/test/mailers/unlock_instructions_test.rb +12 -10
  167. data/test/mapping_test.rb +15 -6
  168. data/test/models/authenticatable_test.rb +15 -3
  169. data/test/models/confirmable_test.rb +190 -95
  170. data/test/models/database_authenticatable_test.rb +75 -41
  171. data/test/models/lockable_test.rb +115 -61
  172. data/test/models/omniauthable_test.rb +3 -1
  173. data/test/models/recoverable_test.rb +116 -37
  174. data/test/models/registerable_test.rb +3 -1
  175. data/test/models/rememberable_test.rb +95 -94
  176. data/test/models/serializable_test.rb +19 -8
  177. data/test/models/timeoutable_test.rb +10 -8
  178. data/test/models/trackable_test.rb +50 -1
  179. data/test/models/validatable_test.rb +24 -30
  180. data/test/models_test.rb +19 -8
  181. data/test/omniauth/config_test.rb +15 -11
  182. data/test/omniauth/url_helpers_test.rb +8 -9
  183. data/test/orm/active_record.rb +16 -2
  184. data/test/orm/mongoid.rb +4 -2
  185. data/test/parameter_sanitizer_test.rb +53 -57
  186. data/test/rails_app/app/active_record/admin.rb +2 -0
  187. data/test/rails_app/app/active_record/shim.rb +3 -1
  188. data/test/rails_app/app/active_record/user.rb +14 -0
  189. data/test/rails_app/app/active_record/user_on_engine.rb +9 -0
  190. data/test/rails_app/app/active_record/user_on_main_app.rb +9 -0
  191. data/test/rails_app/app/active_record/user_with_validations.rb +12 -0
  192. data/test/rails_app/app/active_record/user_without_email.rb +10 -0
  193. data/test/rails_app/app/controllers/admins/sessions_controller.rb +3 -1
  194. data/test/rails_app/app/controllers/admins_controller.rb +3 -6
  195. data/test/rails_app/app/controllers/application_controller.rb +7 -3
  196. data/test/rails_app/app/controllers/application_with_fake_engine.rb +32 -0
  197. data/test/rails_app/app/controllers/custom/registrations_controller.rb +33 -0
  198. data/test/rails_app/app/controllers/home_controller.rb +7 -1
  199. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +3 -1
  200. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +3 -1
  201. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +7 -5
  202. data/test/rails_app/app/controllers/users_controller.rb +8 -6
  203. data/test/rails_app/app/helpers/application_helper.rb +2 -0
  204. data/test/rails_app/app/mailers/users/from_proc_mailer.rb +5 -0
  205. data/test/rails_app/app/mailers/users/mailer.rb +3 -10
  206. data/test/rails_app/app/mailers/users/reply_to_mailer.rb +6 -0
  207. data/test/rails_app/app/mongoid/admin.rb +13 -11
  208. data/test/rails_app/app/mongoid/shim.rb +4 -2
  209. data/test/rails_app/app/mongoid/user.rb +30 -19
  210. data/test/rails_app/app/mongoid/user_on_engine.rb +41 -0
  211. data/test/rails_app/app/mongoid/user_on_main_app.rb +41 -0
  212. data/test/rails_app/app/mongoid/user_with_validations.rb +37 -0
  213. data/test/rails_app/app/mongoid/user_without_email.rb +35 -0
  214. data/test/rails_app/app/views/admins/sessions/new.html.erb +1 -1
  215. data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -1
  216. data/test/rails_app/app/views/home/index.html.erb +1 -1
  217. data/test/rails_app/app/views/home/join.html.erb +1 -1
  218. data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -1
  219. data/test/rails_app/app/views/layouts/application.html.erb +1 -1
  220. data/test/rails_app/config/application.rb +13 -5
  221. data/test/rails_app/config/boot.rb +17 -4
  222. data/test/rails_app/config/environment.rb +2 -0
  223. data/test/rails_app/config/environments/development.rb +2 -0
  224. data/test/rails_app/config/environments/production.rb +10 -2
  225. data/test/rails_app/config/environments/test.rb +14 -3
  226. data/test/rails_app/config/initializers/backtrace_silencers.rb +2 -0
  227. data/test/rails_app/config/initializers/devise.rb +22 -21
  228. data/test/rails_app/config/initializers/inflections.rb +2 -0
  229. data/test/rails_app/config/initializers/secret_token.rb +3 -6
  230. data/test/rails_app/config/initializers/session_store.rb +2 -0
  231. data/test/rails_app/config/routes.rb +67 -43
  232. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +16 -10
  233. data/test/rails_app/db/schema.rb +2 -0
  234. data/test/rails_app/lib/shared_admin.rb +10 -4
  235. data/test/rails_app/lib/shared_user.rb +4 -1
  236. data/test/rails_app/lib/shared_user_without_email.rb +28 -0
  237. data/test/rails_app/lib/shared_user_without_omniauth.rb +15 -0
  238. data/test/rails_test.rb +11 -0
  239. data/test/routes_test.rb +92 -61
  240. data/test/secret_key_finder_test.rb +97 -0
  241. data/test/support/action_controller/record_identifier.rb +12 -0
  242. data/test/support/assertions.rb +4 -14
  243. data/test/support/helpers.rb +23 -10
  244. data/test/support/http_method_compatibility.rb +53 -0
  245. data/test/support/integration.rb +19 -16
  246. data/test/support/mongoid.yml +6 -0
  247. data/test/support/webrat/integrations/rails.rb +11 -0
  248. data/test/{test_helpers_test.rb → test/controller_helpers_test.rb} +60 -40
  249. data/test/test/integration_helpers_test.rb +34 -0
  250. data/test/test_helper.rb +9 -0
  251. data/test/test_models.rb +8 -6
  252. metadata +123 -53
  253. data/gemfiles/Gemfile.rails-3.2.x +0 -31
  254. data/gemfiles/Gemfile.rails-3.2.x.lock +0 -159
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'devise/rails/routes'
2
4
  require 'devise/rails/warden_compat'
3
5
 
@@ -11,13 +13,15 @@ module Devise
11
13
  end
12
14
 
13
15
  # Force routes to be loaded if we are doing any eager load.
14
- config.before_eager_load { |app| app.reload_routes! }
16
+ config.before_eager_load do |app|
17
+ app.reload_routes! if Devise.reload_routes
18
+ end
15
19
 
16
20
  initializer "devise.url_helpers" do
17
21
  Devise.include_helpers(Devise::Controllers)
18
22
  end
19
23
 
20
- initializer "devise.omniauth" do |app|
24
+ initializer "devise.omniauth", after: :load_config_initializers, before: :build_middleware_stack do |app|
21
25
  Devise.omniauth_configs.each do |provider, config|
22
26
  app.middleware.use config.strategy_class, *config.args do |strategy|
23
27
  config.strategy = strategy
@@ -29,22 +33,15 @@ module Devise
29
33
  end
30
34
  end
31
35
 
32
- initializer "devise.secret_key" do
36
+ initializer "devise.secret_key" do |app|
37
+ Devise.secret_key ||= Devise::SecretKeyFinder.new(app).find
38
+
33
39
  Devise.token_generator ||=
34
40
  if secret_key = Devise.secret_key
35
41
  Devise::TokenGenerator.new(
36
- Devise::CachingKeyGenerator.new(Devise::KeyGenerator.new(secret_key))
42
+ ActiveSupport::CachingKeyGenerator.new(ActiveSupport::KeyGenerator.new(secret_key))
37
43
  )
38
44
  end
39
45
  end
40
-
41
- initializer "devise.fix_routes_proxy_missing_respond_to_bug" do
42
- # Deprecate: Remove once we move to Rails 4 only.
43
- ActionDispatch::Routing::RoutesProxy.class_eval do
44
- def respond_to?(method, include_private = false)
45
- super || routes.url_helpers.respond_to?(method)
46
- end
47
- end
48
- end
49
46
  end
50
47
  end
@@ -1,13 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/core_ext/object/try"
2
4
  require "active_support/core_ext/hash/slice"
3
5
 
4
- module ActionDispatch::Routing
5
- class RouteSet #:nodoc:
6
- # Ensure Devise modules are included only after loading routes, because we
7
- # need devise_for mappings already declared to create filters and helpers.
8
- def finalize_with_devise!
9
- result = finalize_without_devise!
10
-
6
+ module Devise
7
+ module RouteSet
8
+ def finalize!
9
+ result = super
11
10
  @devise_finalized ||= begin
12
11
  if Devise.router_name.nil? && defined?(@devise_finalized) && self != Rails.application.try(:routes)
13
12
  warn "[DEVISE] We have detected that you are using devise_for inside engine routes. " \
@@ -21,10 +20,16 @@ module ActionDispatch::Routing
21
20
  Devise.regenerate_helpers!
22
21
  true
23
22
  end
24
-
25
23
  result
26
24
  end
27
- alias_method_chain :finalize!, :devise
25
+ end
26
+ end
27
+
28
+ module ActionDispatch::Routing
29
+ class RouteSet #:nodoc:
30
+ # Ensure Devise modules are included only after loading routes, because we
31
+ # need devise_for mappings already declared to create filters and helpers.
32
+ prepend Devise::RouteSet
28
33
  end
29
34
 
30
35
  class Mapper
@@ -43,20 +48,20 @@ module ActionDispatch::Routing
43
48
  # needed routes:
44
49
  #
45
50
  # # Session routes for Authenticatable (default)
46
- # new_user_session GET /users/sign_in {:controller=>"devise/sessions", :action=>"new"}
47
- # user_session POST /users/sign_in {:controller=>"devise/sessions", :action=>"create"}
48
- # destroy_user_session DELETE /users/sign_out {:controller=>"devise/sessions", :action=>"destroy"}
51
+ # new_user_session GET /users/sign_in {controller:"devise/sessions", action:"new"}
52
+ # user_session POST /users/sign_in {controller:"devise/sessions", action:"create"}
53
+ # destroy_user_session DELETE /users/sign_out {controller:"devise/sessions", action:"destroy"}
49
54
  #
50
55
  # # Password routes for Recoverable, if User model has :recoverable configured
51
- # new_user_password GET /users/password/new(.:format) {:controller=>"devise/passwords", :action=>"new"}
52
- # edit_user_password GET /users/password/edit(.:format) {:controller=>"devise/passwords", :action=>"edit"}
53
- # user_password PUT /users/password(.:format) {:controller=>"devise/passwords", :action=>"update"}
54
- # POST /users/password(.:format) {:controller=>"devise/passwords", :action=>"create"}
56
+ # new_user_password GET /users/password/new(.:format) {controller:"devise/passwords", action:"new"}
57
+ # edit_user_password GET /users/password/edit(.:format) {controller:"devise/passwords", action:"edit"}
58
+ # user_password PUT /users/password(.:format) {controller:"devise/passwords", action:"update"}
59
+ # POST /users/password(.:format) {controller:"devise/passwords", action:"create"}
55
60
  #
56
61
  # # Confirmation routes for Confirmable, if User model has :confirmable configured
57
- # new_user_confirmation GET /users/confirmation/new(.:format) {:controller=>"devise/confirmations", :action=>"new"}
58
- # user_confirmation GET /users/confirmation(.:format) {:controller=>"devise/confirmations", :action=>"show"}
59
- # POST /users/confirmation(.:format) {:controller=>"devise/confirmations", :action=>"create"}
62
+ # new_user_confirmation GET /users/confirmation/new(.:format) {controller:"devise/confirmations", action:"new"}
63
+ # user_confirmation GET /users/confirmation(.:format) {controller:"devise/confirmations", action:"show"}
64
+ # POST /users/confirmation(.:format) {controller:"devise/confirmations", action:"create"}
60
65
  #
61
66
  # ==== Routes integration
62
67
  #
@@ -84,71 +89,91 @@ module ActionDispatch::Routing
84
89
  #
85
90
  # You can configure your routes with some options:
86
91
  #
87
- # * :class_name => setup a different class to be looked up by devise, if it cannot be
92
+ # * class_name: set up a different class to be looked up by devise, if it cannot be
88
93
  # properly found by the route name.
89
94
  #
90
- # devise_for :users, :class_name => 'Account'
95
+ # devise_for :users, class_name: 'Account'
96
+ #
97
+ # * path: allows you to set up path name that will be used, as rails routes does.
98
+ # The following route configuration would set up your route as /accounts instead of /users:
99
+ #
100
+ # devise_for :users, path: 'accounts'
101
+ #
102
+ # * singular: set up the singular name for the given resource. This is used as the helper methods
103
+ # names in controller ("authenticate_#{singular}!", "#{singular}_signed_in?", "current_#{singular}"
104
+ # and "#{singular}_session"), as the scope name in routes and as the scope given to warden.
91
105
  #
92
- # * :path => allows you to setup path name that will be used, as rails routes does.
93
- # The following route configuration would setup your route as /accounts instead of /users:
106
+ # devise_for :admins, singular: :manager
94
107
  #
95
- # devise_for :users, :path => 'accounts'
108
+ # devise_scope :manager do
109
+ # ...
110
+ # end
96
111
  #
97
- # * :singular => setup the singular name for the given resource. This is used as the instance variable
98
- # name in controller, as the name in routes and the scope given to warden.
112
+ # class ManagerController < ApplicationController
113
+ # before_action authenticate_manager!
99
114
  #
100
- # devise_for :users, :singular => :user
115
+ # def show
116
+ # @manager = current_manager
117
+ # ...
118
+ # end
119
+ # end
101
120
  #
102
- # * :path_names => configure different path names to overwrite defaults :sign_in, :sign_out, :sign_up,
121
+ # * path_names: configure different path names to overwrite defaults :sign_in, :sign_out, :sign_up,
103
122
  # :password, :confirmation, :unlock.
104
123
  #
105
- # devise_for :users, :path_names => { :sign_in => 'login', :sign_out => 'logout',
106
- # :password => 'secret', :confirmation => 'verification', registration: 'register }
124
+ # devise_for :users, path_names: {
125
+ # sign_in: 'login', sign_out: 'logout',
126
+ # password: 'secret', confirmation: 'verification',
127
+ # registration: 'register', edit: 'edit/profile'
128
+ # }
107
129
  #
108
- # * :controllers => the controller which should be used. All routes by default points to Devise controllers.
130
+ # * controllers: the controller which should be used. All routes by default points to Devise controllers.
109
131
  # However, if you want them to point to custom controller, you should do:
110
132
  #
111
- # devise_for :users, :controllers => { :sessions => "users/sessions" }
133
+ # devise_for :users, controllers: { sessions: "users/sessions" }
112
134
  #
113
- # * :failure_app => a rack app which is invoked whenever there is a failure. Strings representing a given
135
+ # * failure_app: a rack app which is invoked whenever there is a failure. Strings representing a given
114
136
  # are also allowed as parameter.
115
137
  #
116
- # * :sign_out_via => the HTTP method(s) accepted for the :sign_out action (default: :get),
138
+ # * sign_out_via: the HTTP method(s) accepted for the :sign_out action (default: :get),
117
139
  # if you wish to restrict this to accept only :post or :delete requests you should do:
118
140
  #
119
- # devise_for :users, :sign_out_via => [ :post, :delete ]
141
+ # devise_for :users, sign_out_via: [:post, :delete]
120
142
  #
121
143
  # You need to make sure that your sign_out controls trigger a request with a matching HTTP method.
122
144
  #
123
- # * :module => the namespace to find controllers (default: "devise", thus
145
+ # * module: the namespace to find controllers (default: "devise", thus
124
146
  # accessing devise/sessions, devise/registrations, and so on). If you want
125
147
  # to namespace all at once, use module:
126
148
  #
127
- # devise_for :users, :module => "users"
149
+ # devise_for :users, module: "users"
128
150
  #
129
- # * :skip => tell which controller you want to skip routes from being created:
151
+ # * skip: tell which controller you want to skip routes from being created.
152
+ # It accepts :all as an option, meaning it will not generate any route at all:
130
153
  #
131
- # devise_for :users, :skip => :sessions
154
+ # devise_for :users, skip: :sessions
132
155
  #
133
- # * :only => the opposite of :skip, tell which controllers only to generate routes to:
156
+ # * only: the opposite of :skip, tell which controllers only to generate routes to:
134
157
  #
135
- # devise_for :users, :only => :sessions
158
+ # devise_for :users, only: :sessions
136
159
  #
137
- # * :skip_helpers => skip generating Devise url helpers like new_session_path(@user).
160
+ # * skip_helpers: skip generating Devise url helpers like new_session_path(@user).
138
161
  # This is useful to avoid conflicts with previous routes and is false by default.
139
162
  # It accepts true as option, meaning it will skip all the helpers for the controllers
140
163
  # given in :skip but it also accepts specific helpers to be skipped:
141
164
  #
142
- # devise_for :users, :skip => [:registrations, :confirmations], :skip_helpers => true
143
- # devise_for :users, :skip_helpers => [:registrations, :confirmations]
165
+ # devise_for :users, skip: [:registrations, :confirmations], skip_helpers: true
166
+ # devise_for :users, skip_helpers: [:registrations, :confirmations]
167
+ #
168
+ # * format: include "(.:format)" in the generated routes? true by default, set to false to disable:
144
169
  #
145
- # * :format => include "(.:format)" in the generated routes? true by default, set to false to disable:
170
+ # devise_for :users, format: false
146
171
  #
147
- # devise_for :users, :format => false
172
+ # * constraints: works the same as Rails' constraints
148
173
  #
149
- # * :constraints => works the same as Rails' constraints
174
+ # * defaults: works the same as Rails' defaults
150
175
  #
151
- # * :defaults => works the same as Rails' defaults
176
+ # * router_name: allows application level router name to be overwritten for the current scope
152
177
  #
153
178
  # ==== Scoping
154
179
  #
@@ -170,7 +195,7 @@ module ActionDispatch::Routing
170
195
  #
171
196
  # class ApplicationController < ActionController::Base
172
197
  # def self.default_url_options
173
- # { :locale => I18n.locale }
198
+ # { locale: I18n.locale }
174
199
  # end
175
200
  # end
176
201
  #
@@ -195,7 +220,7 @@ module ActionDispatch::Routing
195
220
  # In order to get Devise to recognize the deactivate action, your devise_scope entry should look like this:
196
221
  #
197
222
  # devise_scope :owner do
198
- # post "deactivate", :to => "registrations#deactivate", :as => "deactivate_registration"
223
+ # post "deactivate", to: "registrations#deactivate", as: "deactivate_registration"
199
224
  # end
200
225
  #
201
226
  def devise_for(*resources)
@@ -221,7 +246,7 @@ module ActionDispatch::Routing
221
246
  raise_no_devise_method_error!(mapping.class_name) unless mapping.to.respond_to?(:devise)
222
247
  rescue NameError => e
223
248
  raise unless mapping.class_name == resource.to_s.classify
224
- warn "[WARNING] You provided devise_for #{resource.inspect} but there is " <<
249
+ warn "[WARNING] You provided devise_for #{resource.inspect} but there is " \
225
250
  "no model #{mapping.class_name} defined in your application"
226
251
  next
227
252
  rescue NoMethodError => e
@@ -229,7 +254,14 @@ module ActionDispatch::Routing
229
254
  raise_no_devise_method_error!(mapping.class_name)
230
255
  end
231
256
 
232
- routes = mapping.used_routes
257
+ if options[:controllers] && options[:controllers][:omniauth_callbacks]
258
+ unless mapping.omniauthable?
259
+ raise ArgumentError, "Mapping omniauth_callbacks on a resource that is not omniauthable\n" \
260
+ "Please add `devise :omniauthable` to the `#{mapping.class_name}` model"
261
+ end
262
+ end
263
+
264
+ routes = mapping.used_routes
233
265
 
234
266
  devise_scope mapping.name do
235
267
  with_devise_exclusive_scope mapping.fullpath, mapping.name, options do
@@ -252,7 +284,7 @@ module ActionDispatch::Routing
252
284
  # end
253
285
  #
254
286
  # authenticate :user, lambda {|u| u.role == "admin"} do
255
- # root :to => "admin/dashboard#show", :as => :user_root
287
+ # root to: "admin/dashboard#show", as: :user_root
256
288
  # end
257
289
  #
258
290
  def authenticate(scope=nil, block=nil)
@@ -266,18 +298,18 @@ module ActionDispatch::Routing
266
298
  # a model and allows extra constraints to be done on the instance.
267
299
  #
268
300
  # authenticated :admin do
269
- # root :to => 'admin/dashboard#show', :as => :admin_root
301
+ # root to: 'admin/dashboard#show', as: :admin_root
270
302
  # end
271
303
  #
272
304
  # authenticated do
273
- # root :to => 'dashboard#show', :as => :authenticated_root
305
+ # root to: 'dashboard#show', as: :authenticated_root
274
306
  # end
275
307
  #
276
308
  # authenticated :user, lambda {|u| u.role == "admin"} do
277
- # root :to => "admin/dashboard#show", :as => :user_root
309
+ # root to: "admin/dashboard#show", as: :user_root
278
310
  # end
279
311
  #
280
- # root :to => 'landing#show'
312
+ # root to: 'landing#show'
281
313
  #
282
314
  def authenticated(scope=nil, block=nil)
283
315
  constraints_for(:authenticate?, scope, block) do
@@ -290,15 +322,15 @@ module ActionDispatch::Routing
290
322
  #
291
323
  # unauthenticated do
292
324
  # as :user do
293
- # root :to => 'devise/registrations#new'
325
+ # root to: 'devise/registrations#new'
294
326
  # end
295
327
  # end
296
328
  #
297
- # root :to => 'dashboard#show'
329
+ # root to: 'dashboard#show'
298
330
  #
299
331
  def unauthenticated(scope=nil)
300
332
  constraint = lambda do |request|
301
- not request.env["warden"].authenticate? :scope => scope
333
+ not request.env["warden"].authenticate? scope: scope
302
334
  end
303
335
 
304
336
  constraints(constraint) do
@@ -308,10 +340,10 @@ module ActionDispatch::Routing
308
340
 
309
341
  # Sets the devise scope to be used in the controller. If you have custom routes,
310
342
  # you are required to call this method (also aliased as :as) in order to specify
311
- # to which controller it is targetted.
343
+ # to which controller it is targeted.
312
344
  #
313
345
  # as :user do
314
- # get "sign_in", :to => "devise/sessions#new"
346
+ # get "sign_in", to: "devise/sessions#new"
315
347
  # end
316
348
  #
317
349
  # Notice you cannot have two scopes mapping to the same URL. And remember, if
@@ -343,41 +375,42 @@ module ActionDispatch::Routing
343
375
  protected
344
376
 
345
377
  def devise_session(mapping, controllers) #:nodoc:
346
- resource :session, :only => [], :controller => controllers[:sessions], :path => "" do
347
- get :new, :path => mapping.path_names[:sign_in], :as => "new"
348
- post :create, :path => mapping.path_names[:sign_in]
349
- match :destroy, :path => mapping.path_names[:sign_out], :as => "destroy", :via => mapping.sign_out_via
378
+ resource :session, only: [], controller: controllers[:sessions], path: "" do
379
+ get :new, path: mapping.path_names[:sign_in], as: "new"
380
+ post :create, path: mapping.path_names[:sign_in]
381
+ match :destroy, path: mapping.path_names[:sign_out], as: "destroy", via: mapping.sign_out_via
350
382
  end
351
383
  end
352
384
 
353
385
  def devise_password(mapping, controllers) #:nodoc:
354
- resource :password, :only => [:new, :create, :edit, :update],
355
- :path => mapping.path_names[:password], :controller => controllers[:passwords]
386
+ resource :password, only: [:new, :create, :edit, :update],
387
+ path: mapping.path_names[:password], controller: controllers[:passwords]
356
388
  end
357
389
 
358
390
  def devise_confirmation(mapping, controllers) #:nodoc:
359
- resource :confirmation, :only => [:new, :create, :show],
360
- :path => mapping.path_names[:confirmation], :controller => controllers[:confirmations]
391
+ resource :confirmation, only: [:new, :create, :show],
392
+ path: mapping.path_names[:confirmation], controller: controllers[:confirmations]
361
393
  end
362
394
 
363
395
  def devise_unlock(mapping, controllers) #:nodoc:
364
396
  if mapping.to.unlock_strategy_enabled?(:email)
365
- resource :unlock, :only => [:new, :create, :show],
366
- :path => mapping.path_names[:unlock], :controller => controllers[:unlocks]
397
+ resource :unlock, only: [:new, :create, :show],
398
+ path: mapping.path_names[:unlock], controller: controllers[:unlocks]
367
399
  end
368
400
  end
369
401
 
370
402
  def devise_registration(mapping, controllers) #:nodoc:
371
403
  path_names = {
372
- :new => mapping.path_names[:sign_up],
373
- :cancel => mapping.path_names[:cancel]
404
+ new: mapping.path_names[:sign_up],
405
+ edit: mapping.path_names[:edit],
406
+ cancel: mapping.path_names[:cancel]
374
407
  }
375
408
 
376
409
  options = {
377
- :only => [:new, :create, :edit, :update, :destroy],
378
- :path => mapping.path_names[:registration],
379
- :path_names => path_names,
380
- :controller => controllers[:registrations]
410
+ only: [:new, :create, :edit, :update, :destroy],
411
+ path: mapping.path_names[:registration],
412
+ path_names: path_names,
413
+ controller: controllers[:registrations]
381
414
  }
382
415
 
383
416
  resource :registration, options do
@@ -388,64 +421,62 @@ module ActionDispatch::Routing
388
421
  def devise_omniauth_callback(mapping, controllers) #:nodoc:
389
422
  if mapping.fullpath =~ /:[a-zA-Z_]/
390
423
  raise <<-ERROR
391
- Devise does not support scoping omniauth callbacks under a dynamic segment
424
+ Devise does not support scoping OmniAuth callbacks under a dynamic segment
392
425
  and you have set #{mapping.fullpath.inspect}. You can work around by passing
393
- `skip: :omniauth_callbacks` and manually defining the routes. Here is an example:
394
-
395
- match "/users/auth/:provider",
396
- :constraints => { :provider => /\Agoogle|facebook\z/ },
397
- :to => "devise/omniauth_callbacks#passthru",
398
- :as => :omniauth_authorize,
399
- :via => [:get, :post]
400
-
401
- match "/users/auth/:action/callback",
402
- :constraints => { :action => /\Agoogle|facebook\z/ },
403
- :to => "devise/omniauth_callbacks",
404
- :as => :omniauth_callback,
405
- :via => [:get, :post]
426
+ `skip: :omniauth_callbacks` to the `devise_for` call and extract omniauth
427
+ options to another `devise_for` call outside the scope. Here is an example:
428
+
429
+ devise_for :users, only: :omniauth_callbacks, controllers: {omniauth_callbacks: 'users/omniauth_callbacks'}
430
+
431
+ scope '/(:locale)', locale: /ru|en/ do
432
+ devise_for :users, skip: :omniauth_callbacks
433
+ end
406
434
  ERROR
407
435
  end
408
-
409
- path, @scope[:path] = @scope[:path], nil
436
+ current_scope = @scope.dup
437
+ if @scope.respond_to? :new
438
+ @scope = @scope.new path: nil
439
+ else
440
+ @scope[:path] = nil
441
+ end
410
442
  path_prefix = Devise.omniauth_path_prefix || "/#{mapping.fullpath}/auth".squeeze("/")
411
443
 
412
444
  set_omniauth_path_prefix!(path_prefix)
413
445
 
414
- providers = Regexp.union(mapping.to.omniauth_providers.map(&:to_s))
415
-
416
- match "#{path_prefix}/:provider",
417
- :constraints => { :provider => providers },
418
- :to => "#{controllers[:omniauth_callbacks]}#passthru",
419
- :as => :omniauth_authorize,
420
- :via => [:get, :post]
446
+ mapping.to.omniauth_providers.each do |provider|
447
+ match "#{path_prefix}/#{provider}",
448
+ to: "#{controllers[:omniauth_callbacks]}#passthru",
449
+ as: "#{provider}_omniauth_authorize",
450
+ via: [:get, :post]
421
451
 
422
- match "#{path_prefix}/:action/callback",
423
- :constraints => { :action => providers },
424
- :to => controllers[:omniauth_callbacks],
425
- :as => :omniauth_callback,
426
- :via => [:get, :post]
452
+ match "#{path_prefix}/#{provider}/callback",
453
+ to: "#{controllers[:omniauth_callbacks]}##{provider}",
454
+ as: "#{provider}_omniauth_callback",
455
+ via: [:get, :post]
456
+ end
427
457
  ensure
428
- @scope[:path] = path
458
+ @scope = current_scope
429
459
  end
430
460
 
431
- DEVISE_SCOPE_KEYS = [:as, :path, :module, :constraints, :defaults, :options]
432
-
433
461
  def with_devise_exclusive_scope(new_path, new_as, options) #:nodoc:
434
- old = {}
435
- DEVISE_SCOPE_KEYS.each { |k| old[k] = @scope[k] }
462
+ current_scope = @scope.dup
436
463
 
437
- new = { :as => new_as, :path => new_path, :module => nil }
438
- new.merge!(options.slice(:constraints, :defaults, :options))
464
+ exclusive = { as: new_as, path: new_path, module: nil }
465
+ exclusive.merge!(options.slice(:constraints, :defaults, :options))
439
466
 
440
- @scope.merge!(new)
467
+ if @scope.respond_to? :new
468
+ @scope = @scope.new exclusive
469
+ else
470
+ exclusive.each_pair { |key, value| @scope[key] = value }
471
+ end
441
472
  yield
442
473
  ensure
443
- @scope.merge!(old)
474
+ @scope = current_scope
444
475
  end
445
476
 
446
477
  def constraints_for(method_to_apply, scope=nil, block=nil)
447
478
  constraint = lambda do |request|
448
- request.env['warden'].send(method_to_apply, :scope => scope) &&
479
+ request.env['warden'].send(method_to_apply, scope: scope) &&
449
480
  (block.nil? || block.call(request.env["warden"].user(scope)))
450
481
  end
451
482