devise-bootstrap 0.0.1

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 (104) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/Gemfile +31 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +29 -0
  6. data/Rakefile +1 -0
  7. data/app/controllers/devise/confirmations_controller.rb +47 -0
  8. data/app/controllers/devise/omniauth_callbacks_controller.rb +30 -0
  9. data/app/controllers/devise/passwords_controller.rb +70 -0
  10. data/app/controllers/devise/registrations_controller.rb +137 -0
  11. data/app/controllers/devise/sessions_controller.rb +53 -0
  12. data/app/controllers/devise/unlocks_controller.rb +46 -0
  13. data/app/controllers/devise_controller.rb +176 -0
  14. data/app/helpers/devise_helper.rb +25 -0
  15. data/app/mailers/devise/mailer.rb +20 -0
  16. data/app/views/devise/confirmations/new.html.erb +12 -0
  17. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  18. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  19. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  20. data/app/views/devise/passwords/edit.html.erb +16 -0
  21. data/app/views/devise/passwords/new.html.erb +12 -0
  22. data/app/views/devise/registrations/edit.html.erb +29 -0
  23. data/app/views/devise/registrations/new.html.erb +18 -0
  24. data/app/views/devise/sessions/new.html.erb +17 -0
  25. data/app/views/devise/shared/_links.erb +25 -0
  26. data/app/views/devise/unlocks/new.html.erb +12 -0
  27. data/config/locales/en.yml +59 -0
  28. data/devise-bootstrap.gemspec +30 -0
  29. data/gemfiles/Gemfile.rails-3.2-stable +29 -0
  30. data/gemfiles/Gemfile.rails-4.0-stable +29 -0
  31. data/gemfiles/Gemfile.rails-head +29 -0
  32. data/lib/devise/bootstrap.rb +7 -0
  33. data/lib/devise/bootstrap/version.rb +5 -0
  34. data/lib/devise/devise.rb +491 -0
  35. data/lib/devise/devise/controllers/helpers.rb +213 -0
  36. data/lib/devise/devise/controllers/rememberable.rb +47 -0
  37. data/lib/devise/devise/controllers/scoped_views.rb +17 -0
  38. data/lib/devise/devise/controllers/sign_in_out.rb +103 -0
  39. data/lib/devise/devise/controllers/store_location.rb +50 -0
  40. data/lib/devise/devise/controllers/url_helpers.rb +67 -0
  41. data/lib/devise/devise/delegator.rb +16 -0
  42. data/lib/devise/devise/failure_app.rb +205 -0
  43. data/lib/devise/devise/hooks/activatable.rb +11 -0
  44. data/lib/devise/devise/hooks/csrf_cleaner.rb +5 -0
  45. data/lib/devise/devise/hooks/forgetable.rb +9 -0
  46. data/lib/devise/devise/hooks/lockable.rb +7 -0
  47. data/lib/devise/devise/hooks/proxy.rb +21 -0
  48. data/lib/devise/devise/hooks/rememberable.rb +7 -0
  49. data/lib/devise/devise/hooks/timeoutable.rb +28 -0
  50. data/lib/devise/devise/hooks/trackable.rb +9 -0
  51. data/lib/devise/devise/mailers/helpers.rb +90 -0
  52. data/lib/devise/devise/mapping.rb +172 -0
  53. data/lib/devise/devise/models.rb +119 -0
  54. data/lib/devise/devise/models/authenticatable.rb +284 -0
  55. data/lib/devise/devise/models/confirmable.rb +295 -0
  56. data/lib/devise/devise/models/database_authenticatable.rb +164 -0
  57. data/lib/devise/devise/models/lockable.rb +196 -0
  58. data/lib/devise/devise/models/omniauthable.rb +27 -0
  59. data/lib/devise/devise/models/recoverable.rb +131 -0
  60. data/lib/devise/devise/models/registerable.rb +25 -0
  61. data/lib/devise/devise/models/rememberable.rb +129 -0
  62. data/lib/devise/devise/models/timeoutable.rb +49 -0
  63. data/lib/devise/devise/models/trackable.rb +35 -0
  64. data/lib/devise/devise/models/validatable.rb +66 -0
  65. data/lib/devise/devise/modules.rb +28 -0
  66. data/lib/devise/devise/omniauth.rb +28 -0
  67. data/lib/devise/devise/omniauth/config.rb +45 -0
  68. data/lib/devise/devise/omniauth/url_helpers.rb +18 -0
  69. data/lib/devise/devise/orm/active_record.rb +3 -0
  70. data/lib/devise/devise/orm/mongoid.rb +3 -0
  71. data/lib/devise/devise/parameter_filter.rb +40 -0
  72. data/lib/devise/devise/parameter_sanitizer.rb +99 -0
  73. data/lib/devise/devise/rails.rb +56 -0
  74. data/lib/devise/devise/rails/routes.rb +496 -0
  75. data/lib/devise/devise/rails/warden_compat.rb +22 -0
  76. data/lib/devise/devise/strategies/authenticatable.rb +167 -0
  77. data/lib/devise/devise/strategies/base.rb +20 -0
  78. data/lib/devise/devise/strategies/database_authenticatable.rb +23 -0
  79. data/lib/devise/devise/strategies/rememberable.rb +55 -0
  80. data/lib/devise/devise/test_helpers.rb +132 -0
  81. data/lib/devise/devise/time_inflector.rb +14 -0
  82. data/lib/devise/devise/token_generator.rb +70 -0
  83. data/lib/devise/devise/version.rb +3 -0
  84. data/lib/devise/generators/active_record/devise_generator.rb +73 -0
  85. data/lib/devise/generators/active_record/templates/migration.rb +18 -0
  86. data/lib/devise/generators/active_record/templates/migration_existing.rb +25 -0
  87. data/lib/devise/generators/devise/devise_generator.rb +26 -0
  88. data/lib/devise/generators/devise/install_generator.rb +29 -0
  89. data/lib/devise/generators/devise/orm_helpers.rb +51 -0
  90. data/lib/devise/generators/devise/views_generator.rb +135 -0
  91. data/lib/devise/generators/mongoid/devise_generator.rb +55 -0
  92. data/lib/devise/generators/templates/README +35 -0
  93. data/lib/devise/generators/templates/devise.rb +260 -0
  94. data/lib/devise/generators/templates/markerb/confirmation_instructions.markerb +5 -0
  95. data/lib/devise/generators/templates/markerb/reset_password_instructions.markerb +8 -0
  96. data/lib/devise/generators/templates/markerb/unlock_instructions.markerb +7 -0
  97. data/lib/devise/generators/templates/simple_form_for/confirmations/new.html.erb +16 -0
  98. data/lib/devise/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
  99. data/lib/devise/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
  100. data/lib/devise/generators/templates/simple_form_for/registrations/edit.html.erb +27 -0
  101. data/lib/devise/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
  102. data/lib/devise/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
  103. data/lib/devise/generators/templates/simple_form_for/unlocks/new.html.erb +16 -0
  104. metadata +250 -0
@@ -0,0 +1,40 @@
1
+ module Devise
2
+ class ParameterFilter
3
+ def initialize(case_insensitive_keys, strip_whitespace_keys)
4
+ @case_insensitive_keys = case_insensitive_keys || []
5
+ @strip_whitespace_keys = strip_whitespace_keys || []
6
+ end
7
+
8
+ def filter(conditions)
9
+ conditions = stringify_params(conditions.dup)
10
+
11
+ conditions.merge!(filtered_hash_by_method_for_given_keys(conditions.dup, :downcase, @case_insensitive_keys))
12
+ conditions.merge!(filtered_hash_by_method_for_given_keys(conditions.dup, :strip, @strip_whitespace_keys))
13
+
14
+ conditions
15
+ end
16
+
17
+ def filtered_hash_by_method_for_given_keys(conditions, method, condition_keys)
18
+ condition_keys.each do |k|
19
+ value = conditions[k]
20
+ conditions[k] = value.send(method) if value.respond_to?(method)
21
+ end
22
+
23
+ conditions
24
+ end
25
+
26
+ # Force keys to be string to avoid injection on mongoid related database.
27
+ def stringify_params(conditions)
28
+ return conditions unless conditions.is_a?(Hash)
29
+ conditions.each do |k, v|
30
+ conditions[k] = v.to_s if param_requires_string_conversion?(v)
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def param_requires_string_conversion?(value)
37
+ true
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,99 @@
1
+ module Devise
2
+ class BaseSanitizer
3
+ attr_reader :params, :resource_name, :resource_class
4
+
5
+ def initialize(resource_class, resource_name, params)
6
+ @resource_class = resource_class
7
+ @resource_name = resource_name
8
+ @params = params
9
+ @blocks = Hash.new
10
+ end
11
+
12
+ def for(kind, &block)
13
+ if block_given?
14
+ @blocks[kind] = block
15
+ else
16
+ default_for(kind)
17
+ end
18
+ end
19
+
20
+ def sanitize(kind)
21
+ if block = @blocks[kind]
22
+ block.call(default_params)
23
+ else
24
+ default_sanitize(kind)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def default_for(kind)
31
+ raise ArgumentError, "a block is expected in Devise base sanitizer"
32
+ end
33
+
34
+ def default_sanitize(kind)
35
+ default_params
36
+ end
37
+
38
+ def default_params
39
+ params.fetch(resource_name, {})
40
+ end
41
+ end
42
+
43
+ class ParameterSanitizer < BaseSanitizer
44
+ def initialize(*)
45
+ super
46
+ @permitted = Hash.new { |h,k| h[k] = attributes_for(k) }
47
+ end
48
+
49
+ def sign_in
50
+ permit self.for(:sign_in)
51
+ end
52
+
53
+ def sign_up
54
+ permit self.for(:sign_up)
55
+ end
56
+
57
+ def account_update
58
+ permit self.for(:account_update)
59
+ end
60
+
61
+ private
62
+
63
+ # TODO: We do need to flatten so it works with strong_parameters
64
+ # gem. We should drop it once we move to Rails 4 only support.
65
+ def permit(keys)
66
+ default_params.permit(*Array(keys))
67
+ end
68
+
69
+ # Change for(kind) to return the values in the @permitted
70
+ # hash, allowing the developer to customize at runtime.
71
+ def default_for(kind)
72
+ @permitted[kind] || raise("No sanitizer provided for #{kind}")
73
+ end
74
+
75
+ def default_sanitize(kind)
76
+ if respond_to?(kind, true)
77
+ send(kind)
78
+ else
79
+ raise NotImplementedError, "Devise doesn't know how to sanitize parameters for #{kind}"
80
+ end
81
+ end
82
+
83
+ def attributes_for(kind)
84
+ case kind
85
+ when :sign_in
86
+ auth_keys + [:password, :remember_me]
87
+ when :sign_up
88
+ auth_keys + [:password, :password_confirmation]
89
+ when :account_update
90
+ auth_keys + [:password, :password_confirmation, :current_password]
91
+ end
92
+ end
93
+
94
+ def auth_keys
95
+ @auth_keys ||= @resource_class.authentication_keys.respond_to?(:keys) ?
96
+ @resource_class.authentication_keys.keys : @resource_class.authentication_keys
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,56 @@
1
+ require 'devise/rails/routes'
2
+ require 'devise/rails/warden_compat'
3
+
4
+ module Devise
5
+ class Engine < ::Rails::Engine
6
+ config.devise = Devise
7
+
8
+ # Initialize Warden and copy its configurations.
9
+ config.app_middleware.use Warden::Manager do |config|
10
+ Devise.warden_config = config
11
+ end
12
+
13
+ # Force routes to be loaded if we are doing any eager load.
14
+ config.before_eager_load { |app| app.reload_routes! }
15
+
16
+ initializer "devise.url_helpers" do
17
+ Devise.include_helpers(Devise::Controllers)
18
+ end
19
+
20
+ initializer "devise.omniauth" do |app|
21
+ Devise.omniauth_configs.each do |provider, config|
22
+ app.middleware.use config.strategy_class, *config.args do |strategy|
23
+ config.strategy = strategy
24
+ end
25
+ end
26
+
27
+ if Devise.omniauth_configs.any?
28
+ Devise.include_helpers(Devise::OmniAuth)
29
+ end
30
+ end
31
+
32
+ initializer "devise.secret_key" do |app|
33
+ if app.respond_to?(:secrets)
34
+ Devise.secret_key ||= app.secrets.secret_key_base
35
+ elsif app.config.respond_to?(:secret_key_base)
36
+ Devise.secret_key ||= app.config.secret_key_base
37
+ end
38
+
39
+ Devise.token_generator ||=
40
+ if secret_key = Devise.secret_key
41
+ Devise::TokenGenerator.new(
42
+ Devise::CachingKeyGenerator.new(Devise::KeyGenerator.new(secret_key))
43
+ )
44
+ end
45
+ end
46
+
47
+ initializer "devise.fix_routes_proxy_missing_respond_to_bug" do
48
+ # Deprecate: Remove once we move to Rails 4 only.
49
+ ActionDispatch::Routing::RoutesProxy.class_eval do
50
+ def respond_to?(method, include_private = false)
51
+ super || routes.url_helpers.respond_to?(method)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,496 @@
1
+ require "active_support/core_ext/object/try"
2
+ require "active_support/core_ext/hash/slice"
3
+
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
+
11
+ @devise_finalized ||= begin
12
+ if Devise.router_name.nil? && defined?(@devise_finalized) && self != Rails.application.try(:routes)
13
+ warn "[DEVISE] We have detected that you are using devise_for inside engine routes. " \
14
+ "In this case, you probably want to set Devise.router_name = MOUNT_POINT, where " \
15
+ "MOUNT_POINT is a symbol representing where this engine will be mounted at. For " \
16
+ "now Devise will default the mount point to :main_app. You can explicitly set it" \
17
+ " to :main_app as well in case you want to keep the current behavior."
18
+ end
19
+
20
+ Devise.configure_warden!
21
+ Devise.regenerate_helpers!
22
+ true
23
+ end
24
+
25
+ result
26
+ end
27
+ alias_method_chain :finalize!, :devise
28
+ end
29
+
30
+ class Mapper
31
+ # Includes devise_for method for routes. This method is responsible to
32
+ # generate all needed routes for devise, based on what modules you have
33
+ # defined in your model.
34
+ #
35
+ # ==== Examples
36
+ #
37
+ # Let's say you have an User model configured to use authenticatable,
38
+ # confirmable and recoverable modules. After creating this inside your routes:
39
+ #
40
+ # devise_for :users
41
+ #
42
+ # This method is going to look inside your User model and create the
43
+ # needed routes:
44
+ #
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 DELETE /users/sign_out {controller:"devise/sessions", action:"destroy"}
49
+ #
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"}
55
+ #
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"}
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.
82
+ #
83
+ # ==== Options
84
+ #
85
+ # You can configure your routes with some options:
86
+ #
87
+ # * class_name: setup a different class to be looked up by devise, if it cannot be
88
+ # properly found by the route name.
89
+ #
90
+ # devise_for :users, class_name: 'Account'
91
+ #
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:
94
+ #
95
+ # devise_for :users, path: 'accounts'
96
+ #
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.
99
+ #
100
+ # devise_for :users, singular: :user
101
+ #
102
+ # * path_names: configure different path names to overwrite defaults :sign_in, :sign_out, :sign_up,
103
+ # :password, :confirmation, :unlock.
104
+ #
105
+ # devise_for :users, path_names: {
106
+ # sign_in: 'login', sign_out: 'logout',
107
+ # password: 'secret', confirmation: 'verification',
108
+ # registration: 'register', edit: 'edit/profile'
109
+ # }
110
+ #
111
+ # * controllers: the controller which should be used. All routes by default points to Devise controllers.
112
+ # However, if you want them to point to custom controller, you should do:
113
+ #
114
+ # devise_for :users, controllers: { sessions: "users/sessions" }
115
+ #
116
+ # * failure_app: a rack app which is invoked whenever there is a failure. Strings representing a given
117
+ # are also allowed as parameter.
118
+ #
119
+ # * sign_out_via: the HTTP method(s) accepted for the :sign_out action (default: :get),
120
+ # if you wish to restrict this to accept only :post or :delete requests you should do:
121
+ #
122
+ # devise_for :users, sign_out_via: [ :post, :delete ]
123
+ #
124
+ # You need to make sure that your sign_out controls trigger a request with a matching HTTP method.
125
+ #
126
+ # * module: the namespace to find controllers (default: "devise", thus
127
+ # accessing devise/sessions, devise/registrations, and so on). If you want
128
+ # to namespace all at once, use module:
129
+ #
130
+ # devise_for :users, module: "users"
131
+ #
132
+ # * skip: tell which controller you want to skip routes from being created:
133
+ #
134
+ # devise_for :users, skip: :sessions
135
+ #
136
+ # * only: the opposite of :skip, tell which controllers only to generate routes to:
137
+ #
138
+ # devise_for :users, only: :sessions
139
+ #
140
+ # * skip_helpers: skip generating Devise url helpers like new_session_path(@user).
141
+ # This is useful to avoid conflicts with previous routes and is false by default.
142
+ # It accepts true as option, meaning it will skip all the helpers for the controllers
143
+ # given in :skip but it also accepts specific helpers to be skipped:
144
+ #
145
+ # devise_for :users, skip: [:registrations, :confirmations], skip_helpers: true
146
+ # devise_for :users, skip_helpers: [:registrations, :confirmations]
147
+ #
148
+ # * format: include "(.:format)" in the generated routes? true by default, set to false to disable:
149
+ #
150
+ # devise_for :users, format: false
151
+ #
152
+ # * constraints: works the same as Rails' constraints
153
+ #
154
+ # * defaults: works the same as Rails' defaults
155
+ #
156
+ # ==== Scoping
157
+ #
158
+ # Following Rails 3 routes DSL, you can nest devise_for calls inside a scope:
159
+ #
160
+ # scope "/my" do
161
+ # devise_for :users
162
+ # end
163
+ #
164
+ # However, since Devise uses the request path to retrieve the current user,
165
+ # this has one caveat: If you are using a dynamic segment, like so ...
166
+ #
167
+ # scope ":locale" do
168
+ # devise_for :users
169
+ # end
170
+ #
171
+ # you are required to configure default_url_options in your
172
+ # ApplicationController class, so Devise can pick it:
173
+ #
174
+ # class ApplicationController < ActionController::Base
175
+ # def self.default_url_options
176
+ # { locale: I18n.locale }
177
+ # end
178
+ # end
179
+ #
180
+ # ==== Adding custom actions to override controllers
181
+ #
182
+ # You can pass a block to devise_for that will add any routes defined in the block to Devise's
183
+ # list of known actions. This is important if you add a custom action to a controller that
184
+ # overrides an out of the box Devise controller.
185
+ # For example:
186
+ #
187
+ # class RegistrationsController < Devise::RegistrationsController
188
+ # def update
189
+ # # do something different here
190
+ # end
191
+ #
192
+ # def deactivate
193
+ # # not a standard action
194
+ # # deactivate code here
195
+ # end
196
+ # end
197
+ #
198
+ # In order to get Devise to recognize the deactivate action, your devise_scope entry should look like this:
199
+ #
200
+ # devise_scope :owner do
201
+ # post "deactivate", to: "registrations#deactivate", as: "deactivate_registration"
202
+ # end
203
+ #
204
+ def devise_for(*resources)
205
+ @devise_finalized = false
206
+ raise_no_secret_key unless Devise.secret_key
207
+ options = resources.extract_options!
208
+
209
+ options[:as] ||= @scope[:as] if @scope[:as].present?
210
+ options[:module] ||= @scope[:module] if @scope[:module].present?
211
+ options[:path_prefix] ||= @scope[:path] if @scope[:path].present?
212
+ options[:path_names] = (@scope[:path_names] || {}).merge(options[:path_names] || {})
213
+ options[:constraints] = (@scope[:constraints] || {}).merge(options[:constraints] || {})
214
+ options[:defaults] = (@scope[:defaults] || {}).merge(options[:defaults] || {})
215
+ options[:options] = @scope[:options] || {}
216
+ options[:options][:format] = false if options[:format] == false
217
+
218
+ resources.map!(&:to_sym)
219
+
220
+ resources.each do |resource|
221
+ mapping = Devise.add_mapping(resource, options)
222
+
223
+ begin
224
+ raise_no_devise_method_error!(mapping.class_name) unless mapping.to.respond_to?(:devise)
225
+ rescue NameError => e
226
+ raise unless mapping.class_name == resource.to_s.classify
227
+ warn "[WARNING] You provided devise_for #{resource.inspect} but there is " <<
228
+ "no model #{mapping.class_name} defined in your application"
229
+ next
230
+ rescue NoMethodError => e
231
+ raise unless e.message.include?("undefined method `devise'")
232
+ raise_no_devise_method_error!(mapping.class_name)
233
+ end
234
+
235
+ if options[:controllers] && options[:controllers][:omniauth_callbacks]
236
+ unless mapping.omniauthable?
237
+ msg = "Mapping omniauth_callbacks on a resource that is not omniauthable\n"
238
+ msg << "Please add `devise :omniauthable` to the `#{mapping.class_name}` model"
239
+ raise msg
240
+ end
241
+ end
242
+
243
+ routes = mapping.used_routes
244
+
245
+ devise_scope mapping.name do
246
+ with_devise_exclusive_scope mapping.fullpath, mapping.name, options do
247
+ routes.each { |mod| send("devise_#{mod}", mapping, mapping.controllers) }
248
+ end
249
+ end
250
+ end
251
+ end
252
+
253
+ # Allow you to add authentication request from the router.
254
+ # Takes an optional scope and block to provide constraints
255
+ # on the model instance itself.
256
+ #
257
+ # authenticate do
258
+ # resources :post
259
+ # end
260
+ #
261
+ # authenticate(:admin) do
262
+ # resources :users
263
+ # end
264
+ #
265
+ # authenticate :user, lambda {|u| u.role == "admin"} do
266
+ # root to: "admin/dashboard#show", as: :user_root
267
+ # end
268
+ #
269
+ def authenticate(scope=nil, block=nil)
270
+ constraints_for(:authenticate!, scope, block) do
271
+ yield
272
+ end
273
+ end
274
+
275
+ # Allow you to route based on whether a scope is authenticated. You
276
+ # can optionally specify which scope and a block. The block accepts
277
+ # a model and allows extra constraints to be done on the instance.
278
+ #
279
+ # authenticated :admin do
280
+ # root to: 'admin/dashboard#show', as: :admin_root
281
+ # end
282
+ #
283
+ # authenticated do
284
+ # root to: 'dashboard#show', as: :authenticated_root
285
+ # end
286
+ #
287
+ # authenticated :user, lambda {|u| u.role == "admin"} do
288
+ # root to: "admin/dashboard#show", as: :user_root
289
+ # end
290
+ #
291
+ # root to: 'landing#show'
292
+ #
293
+ def authenticated(scope=nil, block=nil)
294
+ constraints_for(:authenticate?, scope, block) do
295
+ yield
296
+ end
297
+ end
298
+
299
+ # Allow you to route based on whether a scope is *not* authenticated.
300
+ # You can optionally specify which scope.
301
+ #
302
+ # unauthenticated do
303
+ # as :user do
304
+ # root to: 'devise/registrations#new'
305
+ # end
306
+ # end
307
+ #
308
+ # root to: 'dashboard#show'
309
+ #
310
+ def unauthenticated(scope=nil)
311
+ constraint = lambda do |request|
312
+ not request.env["warden"].authenticate? scope: scope
313
+ end
314
+
315
+ constraints(constraint) do
316
+ yield
317
+ end
318
+ end
319
+
320
+ # Sets the devise scope to be used in the controller. If you have custom routes,
321
+ # you are required to call this method (also aliased as :as) in order to specify
322
+ # to which controller it is targetted.
323
+ #
324
+ # as :user do
325
+ # get "sign_in", to: "devise/sessions#new"
326
+ # end
327
+ #
328
+ # Notice you cannot have two scopes mapping to the same URL. And remember, if
329
+ # you try to access a devise controller without specifying a scope, it will
330
+ # raise ActionNotFound error.
331
+ #
332
+ # Also be aware of that 'devise_scope' and 'as' use the singular form of the
333
+ # noun where other devise route commands expect the plural form. This would be a
334
+ # good and working example.
335
+ #
336
+ # devise_scope :user do
337
+ # get "/some/route" => "some_devise_controller"
338
+ # end
339
+ # devise_for :users
340
+ #
341
+ # Notice and be aware of the differences above between :user and :users
342
+ def devise_scope(scope)
343
+ constraint = lambda do |request|
344
+ request.env["devise.mapping"] = Devise.mappings[scope]
345
+ true
346
+ end
347
+
348
+ constraints(constraint) do
349
+ yield
350
+ end
351
+ end
352
+ alias :as :devise_scope
353
+
354
+ protected
355
+
356
+ def devise_session(mapping, controllers) #:nodoc:
357
+ resource :session, only: [], controller: controllers[:sessions], path: "" do
358
+ get :new, path: mapping.path_names[:sign_in], as: "new"
359
+ post :create, path: mapping.path_names[:sign_in]
360
+ match :destroy, path: mapping.path_names[:sign_out], as: "destroy", via: mapping.sign_out_via
361
+ end
362
+ end
363
+
364
+ def devise_password(mapping, controllers) #:nodoc:
365
+ resource :password, only: [:new, :create, :edit, :update],
366
+ path: mapping.path_names[:password], controller: controllers[:passwords]
367
+ end
368
+
369
+ def devise_confirmation(mapping, controllers) #:nodoc:
370
+ resource :confirmation, only: [:new, :create, :show],
371
+ path: mapping.path_names[:confirmation], controller: controllers[:confirmations]
372
+ end
373
+
374
+ def devise_unlock(mapping, controllers) #:nodoc:
375
+ if mapping.to.unlock_strategy_enabled?(:email)
376
+ resource :unlock, only: [:new, :create, :show],
377
+ path: mapping.path_names[:unlock], controller: controllers[:unlocks]
378
+ end
379
+ end
380
+
381
+ def devise_registration(mapping, controllers) #:nodoc:
382
+ path_names = {
383
+ new: mapping.path_names[:sign_up],
384
+ edit: mapping.path_names[:edit],
385
+ cancel: mapping.path_names[:cancel]
386
+ }
387
+
388
+ options = {
389
+ only: [:new, :create, :edit, :update, :destroy],
390
+ path: mapping.path_names[:registration],
391
+ path_names: path_names,
392
+ controller: controllers[:registrations]
393
+ }
394
+
395
+ resource :registration, options do
396
+ get :cancel
397
+ end
398
+ end
399
+
400
+ def devise_omniauth_callback(mapping, controllers) #:nodoc:
401
+ if mapping.fullpath =~ /:[a-zA-Z_]/
402
+ raise <<-ERROR
403
+ Devise does not support scoping omniauth callbacks under a dynamic segment
404
+ and you have set #{mapping.fullpath.inspect}. You can work around by passing
405
+ `skip: :omniauth_callbacks` and manually defining the routes. Here is an example:
406
+
407
+ match "/users/auth/:provider",
408
+ constraints: { provider: /google|facebook/ },
409
+ to: "devise/omniauth_callbacks#passthru",
410
+ as: :omniauth_authorize,
411
+ via: [:get, :post]
412
+
413
+ match "/users/auth/:action/callback",
414
+ constraints: { action: /google|facebook/ },
415
+ to: "devise/omniauth_callbacks",
416
+ as: :omniauth_callback,
417
+ via: [:get, :post]
418
+ ERROR
419
+ end
420
+
421
+ path, @scope[:path] = @scope[:path], nil
422
+ path_prefix = Devise.omniauth_path_prefix || "/#{mapping.fullpath}/auth".squeeze("/")
423
+
424
+ set_omniauth_path_prefix!(path_prefix)
425
+
426
+ providers = Regexp.union(mapping.to.omniauth_providers.map(&:to_s))
427
+
428
+ match "#{path_prefix}/:provider",
429
+ constraints: { provider: providers },
430
+ to: "#{controllers[:omniauth_callbacks]}#passthru",
431
+ as: :omniauth_authorize,
432
+ via: [:get, :post]
433
+
434
+ match "#{path_prefix}/:action/callback",
435
+ constraints: { action: providers },
436
+ to: controllers[:omniauth_callbacks],
437
+ as: :omniauth_callback,
438
+ via: [:get, :post]
439
+ ensure
440
+ @scope[:path] = path
441
+ end
442
+
443
+ DEVISE_SCOPE_KEYS = [:as, :path, :module, :constraints, :defaults, :options]
444
+
445
+ def with_devise_exclusive_scope(new_path, new_as, options) #:nodoc:
446
+ old = {}
447
+ DEVISE_SCOPE_KEYS.each { |k| old[k] = @scope[k] }
448
+
449
+ new = { as: new_as, path: new_path, module: nil }
450
+ new.merge!(options.slice(:constraints, :defaults, :options))
451
+
452
+ @scope.merge!(new)
453
+ yield
454
+ ensure
455
+ @scope.merge!(old)
456
+ end
457
+
458
+ def constraints_for(method_to_apply, scope=nil, block=nil)
459
+ constraint = lambda do |request|
460
+ request.env['warden'].send(method_to_apply, scope: scope) &&
461
+ (block.nil? || block.call(request.env["warden"].user(scope)))
462
+ end
463
+
464
+ constraints(constraint) do
465
+ yield
466
+ end
467
+ end
468
+
469
+ def set_omniauth_path_prefix!(path_prefix) #:nodoc:
470
+ if ::OmniAuth.config.path_prefix && ::OmniAuth.config.path_prefix != path_prefix
471
+ raise "Wrong OmniAuth configuration. If you are getting this exception, it means that either:\n\n" \
472
+ "1) You are manually setting OmniAuth.config.path_prefix and it doesn't match the Devise one\n" \
473
+ "2) You are setting :omniauthable in more than one model\n" \
474
+ "3) You changed your Devise routes/OmniAuth setting and haven't restarted your server"
475
+ else
476
+ ::OmniAuth.config.path_prefix = path_prefix
477
+ end
478
+ end
479
+
480
+ def raise_no_secret_key #:nodoc:
481
+ raise <<-ERROR
482
+ Devise.secret_key was not set. Please add the following to your Devise initializer:
483
+
484
+ config.secret_key = '#{SecureRandom.hex(64)}'
485
+
486
+ Please ensure you restarted your application after installing Devise or setting the key.
487
+ ERROR
488
+ end
489
+
490
+ def raise_no_devise_method_error!(klass) #:nodoc:
491
+ raise "#{klass} does not respond to 'devise' method. This usually means you haven't " \
492
+ "loaded your ORM file or it's being loaded too late. To fix it, be sure to require 'devise/orm/YOUR_ORM' " \
493
+ "inside 'config/initializers/devise.rb' or before your application definition in 'config/application.rb'"
494
+ end
495
+ end
496
+ end