devise 2.1.2 → 3.5.10

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