upstream-devise 2.1.0.rc

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 (215) hide show
  1. data/.gitignore +10 -0
  2. data/.travis.yml +15 -0
  3. data/CHANGELOG.rdoc +846 -0
  4. data/Gemfile +35 -0
  5. data/Gemfile.lock +165 -0
  6. data/MIT-LICENSE +20 -0
  7. data/README.md +384 -0
  8. data/Rakefile +34 -0
  9. data/app/controllers/devise/confirmations_controller.rb +43 -0
  10. data/app/controllers/devise/omniauth_callbacks_controller.rb +24 -0
  11. data/app/controllers/devise/passwords_controller.rb +47 -0
  12. data/app/controllers/devise/registrations_controller.rb +107 -0
  13. data/app/controllers/devise/sessions_controller.rb +49 -0
  14. data/app/controllers/devise/unlocks_controller.rb +44 -0
  15. data/app/controllers/devise_controller.rb +177 -0
  16. data/app/helpers/devise_helper.rb +25 -0
  17. data/app/mailers/devise/mailer.rb +15 -0
  18. data/app/views/devise/_links.erb +3 -0
  19. data/app/views/devise/confirmations/new.html.erb +12 -0
  20. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  21. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  22. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  23. data/app/views/devise/passwords/edit.html.erb +16 -0
  24. data/app/views/devise/passwords/new.html.erb +12 -0
  25. data/app/views/devise/registrations/edit.html.erb +25 -0
  26. data/app/views/devise/registrations/new.html.erb +18 -0
  27. data/app/views/devise/sessions/new.html.erb +17 -0
  28. data/app/views/devise/shared/_links.erb +25 -0
  29. data/app/views/devise/unlocks/new.html.erb +12 -0
  30. data/config/locales/en.yml +57 -0
  31. data/devise.gemspec +25 -0
  32. data/gemfiles/Gemfile.rails-3.1.x +35 -0
  33. data/gemfiles/Gemfile.rails-3.1.x.lock +167 -0
  34. data/lib/devise.rb +455 -0
  35. data/lib/devise/controllers/helpers.rb +269 -0
  36. data/lib/devise/controllers/rememberable.rb +52 -0
  37. data/lib/devise/controllers/scoped_views.rb +17 -0
  38. data/lib/devise/controllers/url_helpers.rb +67 -0
  39. data/lib/devise/delegator.rb +17 -0
  40. data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
  41. data/lib/devise/encryptors/base.rb +24 -0
  42. data/lib/devise/encryptors/clearance_sha1.rb +17 -0
  43. data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
  44. data/lib/devise/encryptors/sha1.rb +25 -0
  45. data/lib/devise/encryptors/sha512.rb +25 -0
  46. data/lib/devise/failure_app.rb +185 -0
  47. data/lib/devise/hooks/activatable.rb +11 -0
  48. data/lib/devise/hooks/forgetable.rb +9 -0
  49. data/lib/devise/hooks/lockable.rb +7 -0
  50. data/lib/devise/hooks/rememberable.rb +6 -0
  51. data/lib/devise/hooks/timeoutable.rb +22 -0
  52. data/lib/devise/hooks/trackable.rb +9 -0
  53. data/lib/devise/mailers/helpers.rb +86 -0
  54. data/lib/devise/mapping.rb +172 -0
  55. data/lib/devise/models.rb +123 -0
  56. data/lib/devise/models/authenticatable.rb +231 -0
  57. data/lib/devise/models/confirmable.rb +242 -0
  58. data/lib/devise/models/database_authenticatable.rb +126 -0
  59. data/lib/devise/models/encryptable.rb +86 -0
  60. data/lib/devise/models/lockable.rb +185 -0
  61. data/lib/devise/models/omniauthable.rb +27 -0
  62. data/lib/devise/models/recoverable.rb +140 -0
  63. data/lib/devise/models/registerable.rb +25 -0
  64. data/lib/devise/models/rememberable.rb +125 -0
  65. data/lib/devise/models/timeoutable.rb +49 -0
  66. data/lib/devise/models/token_authenticatable.rb +77 -0
  67. data/lib/devise/models/trackable.rb +35 -0
  68. data/lib/devise/models/validatable.rb +66 -0
  69. data/lib/devise/modules.rb +30 -0
  70. data/lib/devise/omniauth.rb +28 -0
  71. data/lib/devise/omniauth/config.rb +45 -0
  72. data/lib/devise/omniauth/url_helpers.rb +33 -0
  73. data/lib/devise/orm/active_record.rb +3 -0
  74. data/lib/devise/orm/mongoid.rb +3 -0
  75. data/lib/devise/param_filter.rb +41 -0
  76. data/lib/devise/rails.rb +54 -0
  77. data/lib/devise/rails/routes.rb +412 -0
  78. data/lib/devise/rails/warden_compat.rb +43 -0
  79. data/lib/devise/strategies/authenticatable.rb +165 -0
  80. data/lib/devise/strategies/base.rb +15 -0
  81. data/lib/devise/strategies/database_authenticatable.rb +21 -0
  82. data/lib/devise/strategies/rememberable.rb +53 -0
  83. data/lib/devise/strategies/token_authenticatable.rb +57 -0
  84. data/lib/devise/test_helpers.rb +130 -0
  85. data/lib/devise/version.rb +3 -0
  86. data/lib/generators/active_record/devise_generator.rb +78 -0
  87. data/lib/generators/active_record/templates/migration.rb +19 -0
  88. data/lib/generators/active_record/templates/migration_existing.rb +26 -0
  89. data/lib/generators/devise/devise_generator.rb +24 -0
  90. data/lib/generators/devise/install_generator.rb +24 -0
  91. data/lib/generators/devise/orm_helpers.rb +32 -0
  92. data/lib/generators/devise/views_generator.rb +110 -0
  93. data/lib/generators/mongoid/devise_generator.rb +60 -0
  94. data/lib/generators/templates/README +31 -0
  95. data/lib/generators/templates/devise.rb +216 -0
  96. data/lib/generators/templates/markerb/confirmation_instructions.markerb +5 -0
  97. data/lib/generators/templates/markerb/reset_password_instructions.markerb +8 -0
  98. data/lib/generators/templates/markerb/unlock_instructions.markerb +7 -0
  99. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +15 -0
  100. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
  101. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
  102. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +22 -0
  103. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
  104. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
  105. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +15 -0
  106. data/test/controllers/custom_strategy_test.rb +62 -0
  107. data/test/controllers/helpers_test.rb +254 -0
  108. data/test/controllers/internal_helpers_test.rb +97 -0
  109. data/test/controllers/sessions_controller_test.rb +36 -0
  110. data/test/controllers/url_helpers_test.rb +59 -0
  111. data/test/delegator_test.rb +19 -0
  112. data/test/devise_test.rb +72 -0
  113. data/test/encryptors_test.rb +30 -0
  114. data/test/failure_app_test.rb +211 -0
  115. data/test/generators/active_record_generator_test.rb +69 -0
  116. data/test/generators/devise_generator_test.rb +39 -0
  117. data/test/generators/install_generator_test.rb +13 -0
  118. data/test/generators/mongoid_generator_test.rb +23 -0
  119. data/test/generators/views_generator_test.rb +52 -0
  120. data/test/helpers/devise_helper_test.rb +51 -0
  121. data/test/indifferent_hash.rb +33 -0
  122. data/test/integration/authenticatable_test.rb +587 -0
  123. data/test/integration/confirmable_test.rb +255 -0
  124. data/test/integration/database_authenticatable_test.rb +82 -0
  125. data/test/integration/http_authenticatable_test.rb +97 -0
  126. data/test/integration/lockable_test.rb +224 -0
  127. data/test/integration/omniauthable_test.rb +133 -0
  128. data/test/integration/recoverable_test.rb +300 -0
  129. data/test/integration/registerable_test.rb +324 -0
  130. data/test/integration/rememberable_test.rb +158 -0
  131. data/test/integration/timeoutable_test.rb +114 -0
  132. data/test/integration/token_authenticatable_test.rb +161 -0
  133. data/test/integration/trackable_test.rb +92 -0
  134. data/test/mailers/confirmation_instructions_test.rb +95 -0
  135. data/test/mailers/reset_password_instructions_test.rb +83 -0
  136. data/test/mailers/unlock_instructions_test.rb +77 -0
  137. data/test/mapping_test.rb +127 -0
  138. data/test/models/authenticatable_test.rb +7 -0
  139. data/test/models/confirmable_test.rb +357 -0
  140. data/test/models/database_authenticatable_test.rb +189 -0
  141. data/test/models/encryptable_test.rb +73 -0
  142. data/test/models/lockable_test.rb +263 -0
  143. data/test/models/omniauthable_test.rb +7 -0
  144. data/test/models/recoverable_test.rb +205 -0
  145. data/test/models/registerable_test.rb +7 -0
  146. data/test/models/rememberable_test.rb +174 -0
  147. data/test/models/serializable_test.rb +48 -0
  148. data/test/models/timeoutable_test.rb +46 -0
  149. data/test/models/token_authenticatable_test.rb +55 -0
  150. data/test/models/trackable_test.rb +13 -0
  151. data/test/models/validatable_test.rb +117 -0
  152. data/test/models_test.rb +179 -0
  153. data/test/omniauth/config_test.rb +57 -0
  154. data/test/omniauth/url_helpers_test.rb +58 -0
  155. data/test/orm/active_record.rb +9 -0
  156. data/test/orm/mongoid.rb +14 -0
  157. data/test/rails_app/Rakefile +10 -0
  158. data/test/rails_app/app/active_record/admin.rb +6 -0
  159. data/test/rails_app/app/active_record/shim.rb +2 -0
  160. data/test/rails_app/app/active_record/user.rb +6 -0
  161. data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
  162. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  163. data/test/rails_app/app/controllers/application_controller.rb +8 -0
  164. data/test/rails_app/app/controllers/home_controller.rb +25 -0
  165. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  166. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  167. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +14 -0
  168. data/test/rails_app/app/controllers/users_controller.rb +23 -0
  169. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  170. data/test/rails_app/app/mailers/users/mailer.rb +3 -0
  171. data/test/rails_app/app/mongoid/admin.rb +30 -0
  172. data/test/rails_app/app/mongoid/shim.rb +24 -0
  173. data/test/rails_app/app/mongoid/user.rb +45 -0
  174. data/test/rails_app/app/views/admins/index.html.erb +1 -0
  175. data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
  176. data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -0
  177. data/test/rails_app/app/views/home/index.html.erb +1 -0
  178. data/test/rails_app/app/views/home/join.html.erb +1 -0
  179. data/test/rails_app/app/views/home/private.html.erb +1 -0
  180. data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -0
  181. data/test/rails_app/app/views/layouts/application.html.erb +24 -0
  182. data/test/rails_app/app/views/users/index.html.erb +1 -0
  183. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
  184. data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
  185. data/test/rails_app/config.ru +4 -0
  186. data/test/rails_app/config/application.rb +41 -0
  187. data/test/rails_app/config/boot.rb +8 -0
  188. data/test/rails_app/config/database.yml +18 -0
  189. data/test/rails_app/config/environment.rb +5 -0
  190. data/test/rails_app/config/environments/development.rb +18 -0
  191. data/test/rails_app/config/environments/production.rb +33 -0
  192. data/test/rails_app/config/environments/test.rb +33 -0
  193. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  194. data/test/rails_app/config/initializers/devise.rb +186 -0
  195. data/test/rails_app/config/initializers/inflections.rb +2 -0
  196. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  197. data/test/rails_app/config/routes.rb +90 -0
  198. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +77 -0
  199. data/test/rails_app/db/schema.rb +52 -0
  200. data/test/rails_app/lib/shared_admin.rb +14 -0
  201. data/test/rails_app/lib/shared_user.rb +26 -0
  202. data/test/rails_app/public/404.html +26 -0
  203. data/test/rails_app/public/422.html +26 -0
  204. data/test/rails_app/public/500.html +26 -0
  205. data/test/rails_app/public/favicon.ico +0 -0
  206. data/test/rails_app/script/rails +10 -0
  207. data/test/routes_test.rb +248 -0
  208. data/test/support/assertions.rb +42 -0
  209. data/test/support/helpers.rb +91 -0
  210. data/test/support/integration.rb +90 -0
  211. data/test/support/locale/en.yml +4 -0
  212. data/test/support/webrat/integrations/rails.rb +24 -0
  213. data/test/test_helper.rb +27 -0
  214. data/test/test_helpers_test.rb +134 -0
  215. metadata +451 -0
@@ -0,0 +1,33 @@
1
+ module Devise
2
+ module OmniAuth
3
+ module UrlHelpers
4
+ def self.define_helpers(mapping)
5
+ return unless mapping.omniauthable?
6
+
7
+ class_eval <<-URL_HELPERS, __FILE__, __LINE__ + 1
8
+ def #{mapping.name}_omniauth_authorize_path(provider, params = {})
9
+ if Devise.omniauth_configs[provider.to_sym]
10
+ script_name = request.env["SCRIPT_NAME"]
11
+
12
+ path = "\#{script_name}/#{mapping.path}/auth/\#{provider}\".squeeze("/")
13
+ path << '?' + params.to_param if params.present?
14
+ path
15
+ else
16
+ raise ArgumentError, "Could not find omniauth provider \#{provider.inspect}"
17
+ end
18
+ end
19
+ URL_HELPERS
20
+ end
21
+
22
+ def omniauth_authorize_path(resource_or_scope, *args)
23
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
24
+ send("#{scope}_omniauth_authorize_path", *args)
25
+ end
26
+
27
+ def omniauth_callback_path(resource_or_scope, *args)
28
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
29
+ send("#{scope}_omniauth_callback_path", *args)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ require 'orm_adapter/adapters/active_record'
2
+
3
+ ActiveRecord::Base.extend Devise::Models
@@ -0,0 +1,3 @@
1
+ require 'orm_adapter/adapters/mongoid'
2
+
3
+ Mongoid::Document::ClassMethods.send :include, Devise::Models
@@ -0,0 +1,41 @@
1
+ module Devise
2
+ class ParamFilter
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
+ @case_insensitive_keys.each do |k|
12
+ value = conditions[k]
13
+ next unless value.respond_to?(:downcase)
14
+ conditions[k] = value.downcase
15
+ end
16
+
17
+ @strip_whitespace_keys.each do |k|
18
+ value = conditions[k]
19
+ next unless value.respond_to?(:strip)
20
+ conditions[k] = value.strip
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
+ # Determine which values should be transformed to string or passed as-is to the query builder underneath
37
+ def param_requires_string_conversion?(value)
38
+ [Fixnum, TrueClass, FalseClass, Regexp].none? {|clz| value.is_a? clz }
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,54 @@
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.mongoid_version_warning" do
33
+ if defined?(Mongoid)
34
+ require 'mongoid/version'
35
+ if Mongoid::VERSION.to_f < 2.1
36
+ puts "\n[DEVISE] Please note that Mongoid versions prior to 2.1 handle dirty model " \
37
+ "object attributes in such a way that the Devise `validatable` module will not apply " \
38
+ "its usual uniqueness and format validations for the email field. It is recommended " \
39
+ "that you upgrade to Mongoid 2.1+ for this and other fixes, but if for some reason you " \
40
+ "are unable to do so, you should add these validations manually.\n"
41
+ end
42
+ end
43
+ end
44
+
45
+ initializer "devise.fix_routes_proxy_missing_respond_to_bug" do
46
+ # We can get rid of this once we support only Rails > 3.2
47
+ ActionDispatch::Routing::RoutesProxy.class_eval do
48
+ def respond_to?(method, include_private = false)
49
+ super || routes.url_helpers.respond_to?(method)
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,412 @@
1
+ require "active_support/core_ext/object/try"
2
+
3
+ module ActionDispatch::Routing
4
+ class RouteSet #:nodoc:
5
+ # Ensure Devise modules are included only after loading routes, because we
6
+ # need devise_for mappings already declared to create filters and helpers.
7
+ def finalize_with_devise!
8
+ result = finalize_without_devise!
9
+
10
+ @devise_finalized ||= begin
11
+ if Devise.router_name.nil? && defined?(@devise_finalized) && self != Rails.application.try(:routes)
12
+ warn "[DEVISE] We have detected that you are using devise_for inside engine routes. " \
13
+ "In this case, you probably want to set Devise.router_name = MOUNT_POINT, where " \
14
+ "MOUNT_POINT is a symbol representing where this engine will be mounted at. For " \
15
+ "now Devise will default the mount point to :main_app. You can explicitly set it" \
16
+ " to :main_app as well in case you want to keep the current behavior."
17
+ end
18
+
19
+ Devise.configure_warden!
20
+ Devise.regenerate_helpers!
21
+ true
22
+ end
23
+
24
+ result
25
+ end
26
+ alias_method_chain :finalize!, :devise
27
+ end
28
+
29
+ class Mapper
30
+ # Includes devise_for method for routes. This method is responsible to
31
+ # generate all needed routes for devise, based on what modules you have
32
+ # defined in your model.
33
+ #
34
+ # ==== Examples
35
+ #
36
+ # Let's say you have an User model configured to use authenticatable,
37
+ # confirmable and recoverable modules. After creating this inside your routes:
38
+ #
39
+ # devise_for :users
40
+ #
41
+ # This method is going to look inside your User model and create the
42
+ # needed routes:
43
+ #
44
+ # # Session routes for Authenticatable (default)
45
+ # new_user_session GET /users/sign_in {:controller=>"devise/sessions", :action=>"new"}
46
+ # user_session POST /users/sign_in {:controller=>"devise/sessions", :action=>"create"}
47
+ # destroy_user_session GET /users/sign_out {:controller=>"devise/sessions", :action=>"destroy"}
48
+ #
49
+ # # Password routes for Recoverable, if User model has :recoverable configured
50
+ # new_user_password GET /users/password/new(.:format) {:controller=>"devise/passwords", :action=>"new"}
51
+ # edit_user_password GET /users/password/edit(.:format) {:controller=>"devise/passwords", :action=>"edit"}
52
+ # user_password PUT /users/password(.:format) {:controller=>"devise/passwords", :action=>"update"}
53
+ # POST /users/password(.:format) {:controller=>"devise/passwords", :action=>"create"}
54
+ #
55
+ # # Confirmation routes for Confirmable, if User model has :confirmable configured
56
+ # new_user_confirmation GET /users/confirmation/new(.:format) {:controller=>"devise/confirmations", :action=>"new"}
57
+ # user_confirmation GET /users/confirmation(.:format) {:controller=>"devise/confirmations", :action=>"show"}
58
+ # POST /users/confirmation(.:format) {:controller=>"devise/confirmations", :action=>"create"}
59
+ #
60
+ # ==== Options
61
+ #
62
+ # You can configure your routes with some options:
63
+ #
64
+ # * :class_name => setup a different class to be looked up by devise, if it cannot be
65
+ # properly found by the route name.
66
+ #
67
+ # devise_for :users, :class_name => 'Account'
68
+ #
69
+ # * :path => allows you to setup path name that will be used, as rails routes does.
70
+ # The following route configuration would setup your route as /accounts instead of /users:
71
+ #
72
+ # devise_for :users, :path => 'accounts'
73
+ #
74
+ # * :singular => setup the singular name for the given resource. This is used as the instance variable
75
+ # name in controller, as the name in routes and the scope given to warden.
76
+ #
77
+ # devise_for :users, :singular => :user
78
+ #
79
+ # * :path_names => configure different path names to overwrite defaults :sign_in, :sign_out, :sign_up,
80
+ # :password, :confirmation, :unlock.
81
+ #
82
+ # devise_for :users, :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification' }
83
+ #
84
+ # * :controllers => the controller which should be used. All routes by default points to Devise controllers.
85
+ # However, if you want them to point to custom controller, you should do:
86
+ #
87
+ # devise_for :users, :controllers => { :sessions => "users/sessions" }
88
+ #
89
+ # * :failure_app => a rack app which is invoked whenever there is a failure. Strings representing a given
90
+ # are also allowed as parameter.
91
+ #
92
+ # * :sign_out_via => the HTTP method(s) accepted for the :sign_out action (default: :get),
93
+ # if you wish to restrict this to accept only :post or :delete requests you should do:
94
+ #
95
+ # devise_for :users, :sign_out_via => [ :post, :delete ]
96
+ #
97
+ # You need to make sure that your sign_out controls trigger a request with a matching HTTP method.
98
+ #
99
+ # * :module => the namespace to find controllers (default: "devise", thus
100
+ # accessing devise/sessions, devise/registrations, and so on). If you want
101
+ # to namespace all at once, use module:
102
+ #
103
+ # devise_for :users, :module => "users"
104
+ #
105
+ # Notice that whenever you use namespace in the router DSL, it automatically sets the module.
106
+ # So the following setup:
107
+ #
108
+ # namespace :publisher do
109
+ # devise_for :account
110
+ # end
111
+ #
112
+ # Will use publisher/sessions controller instead of devise/sessions controller. You can revert
113
+ # this by providing the :module option to devise_for.
114
+ #
115
+ # Also pay attention that when you use a namespace it will affect all the helpers and methods for controllers
116
+ # and views. For example, using the above setup you'll end with following methods:
117
+ # current_publisher_account, authenticate_publisher_account!, publisher_account_signed_in, etc.
118
+ #
119
+ # * :skip => tell which controller you want to skip routes from being created:
120
+ #
121
+ # devise_for :users, :skip => :sessions
122
+ #
123
+ # * :only => the opposite of :skip, tell which controllers only to generate routes to:
124
+ #
125
+ # devise_for :users, :only => :sessions
126
+ #
127
+ # * :skip_helpers => skip generating Devise url helpers like new_session_path(@user).
128
+ # This is useful to avoid conflicts with previous routes and is false by default.
129
+ # It accepts true as option, meaning it will skip all the helpers for the controllers
130
+ # given in :skip but it also accepts specific helpers to be skipped:
131
+ #
132
+ # devise_for :users, :skip => [:registrations, :confirmations], :skip_helpers => true
133
+ # devise_for :users, :skip_helpers => [:registrations, :confirmations]
134
+ #
135
+ # * :format => include "(.:format)" in the generated routes? true by default, set to false to disable:
136
+ #
137
+ # devise_for :users, :format => false
138
+ #
139
+ # * :constraints => works the same as Rails' constraints
140
+ #
141
+ # * :defaults => works the same as Rails' defaults
142
+ #
143
+ # ==== Scoping
144
+ #
145
+ # Following Rails 3 routes DSL, you can nest devise_for calls inside a scope:
146
+ #
147
+ # scope "/my" do
148
+ # devise_for :users
149
+ # end
150
+ #
151
+ # However, since Devise uses the request path to retrieve the current user,
152
+ # this has one caveat: If you are using a dynamic segment, like so ...
153
+ #
154
+ # scope ":locale" do
155
+ # devise_for :users
156
+ # end
157
+ #
158
+ # you are required to configure default_url_options in your
159
+ # ApplicationController class, so Devise can pick it:
160
+ #
161
+ # class ApplicationController < ActionController::Base
162
+ # def self.default_url_options
163
+ # { :locale => I18n.locale }
164
+ # end
165
+ # end
166
+ #
167
+ # ==== Adding custom actions to override controllers
168
+ #
169
+ # You can pass a block to devise_for that will add any routes defined in the block to Devise's
170
+ # list of known actions. This is important if you add a custom action to a controller that
171
+ # overrides an out of the box Devise controller.
172
+ # For example:
173
+ #
174
+ # class RegistrationsController < Devise::RegistrationsController
175
+ # def update
176
+ # # do something different here
177
+ # end
178
+ #
179
+ # def deactivate
180
+ # # not a standard action
181
+ # # deactivate code here
182
+ # end
183
+ # end
184
+ #
185
+ # In order to get Devise to recognize the deactivate action, your devise_for entry should look like this,
186
+ #
187
+ # devise_scope :owner do
188
+ # post "deactivate", :to => "registrations#deactivate", :as => "deactivate_registration"
189
+ # end
190
+ #
191
+ def devise_for(*resources)
192
+ @devise_finalized = false
193
+ options = resources.extract_options!
194
+
195
+ options[:as] ||= @scope[:as] if @scope[:as].present?
196
+ options[:module] ||= @scope[:module] if @scope[:module].present?
197
+ options[:path_prefix] ||= @scope[:path] if @scope[:path].present?
198
+ options[:path_names] = (@scope[:path_names] || {}).merge(options[:path_names] || {})
199
+ options[:constraints] = (@scope[:constraints] || {}).merge(options[:constraints] || {})
200
+ options[:defaults] = (@scope[:defaults] || {}).merge(options[:defaults] || {})
201
+ options[:options] = @scope[:options] || {}
202
+ options[:options][:format] = false if options[:format] == false
203
+
204
+ resources.map!(&:to_sym)
205
+
206
+ resources.each do |resource|
207
+ mapping = Devise.add_mapping(resource, options)
208
+
209
+ begin
210
+ raise_no_devise_method_error!(mapping.class_name) unless mapping.to.respond_to?(:devise)
211
+ rescue NameError => e
212
+ raise unless mapping.class_name == resource.to_s.classify
213
+ warn "[WARNING] You provided devise_for #{resource.inspect} but there is " <<
214
+ "no model #{mapping.class_name} defined in your application"
215
+ next
216
+ rescue NoMethodError => e
217
+ raise unless e.message.include?("undefined method `devise'")
218
+ raise_no_devise_method_error!(mapping.class_name)
219
+ end
220
+
221
+ routes = mapping.used_routes
222
+
223
+ devise_scope mapping.name do
224
+ if block_given?
225
+ ActiveSupport::Deprecation.warn "Passing a block to devise_for is deprecated. " \
226
+ "Please remove the block from devise_for (only the block, the call to " \
227
+ "devise_for must still exist) and call devise_scope :#{mapping.name} do ... end " \
228
+ "with the block instead", caller
229
+ yield
230
+ end
231
+
232
+ with_devise_exclusive_scope mapping.fullpath, mapping.name, options do
233
+ routes.each { |mod| send("devise_#{mod}", mapping, mapping.controllers) }
234
+ end
235
+ end
236
+ end
237
+ end
238
+
239
+ # Allow you to add authentication request from the router:
240
+ #
241
+ # authenticate do
242
+ # resources :post
243
+ # end
244
+ #
245
+ # authenticate(:admin) do
246
+ # resources :users
247
+ # end
248
+ #
249
+ def authenticate(scope=nil)
250
+ constraint = lambda do |request|
251
+ request.env["warden"].authenticate!(:scope => scope)
252
+ end
253
+
254
+ constraints(constraint) do
255
+ yield
256
+ end
257
+ end
258
+
259
+ # Allow you to route based on whether a scope is authenticated. You
260
+ # can optionally specify which scope.
261
+ #
262
+ # authenticated :admin do
263
+ # root :to => 'admin/dashboard#show'
264
+ # end
265
+ #
266
+ # authenticated do
267
+ # root :to => 'dashboard#show'
268
+ # end
269
+ #
270
+ # root :to => 'landing#show'
271
+ #
272
+ def authenticated(scope=nil)
273
+ constraint = lambda do |request|
274
+ request.env["warden"].authenticate? :scope => scope
275
+ end
276
+
277
+ constraints(constraint) do
278
+ yield
279
+ end
280
+ end
281
+
282
+ # Allow you to route based on whether a scope is *not* authenticated.
283
+ # You can optionally specify which scope.
284
+ #
285
+ # unauthenticated do
286
+ # as :user do
287
+ # root :to => 'devise/registrations#new'
288
+ # end
289
+ # end
290
+ #
291
+ # root :to => 'dashboard#show'
292
+ #
293
+ def unauthenticated(scope=nil)
294
+ constraint = lambda do |request|
295
+ not request.env["warden"].authenticate? :scope => scope
296
+ end
297
+
298
+ constraints(constraint) do
299
+ yield
300
+ end
301
+ end
302
+
303
+ # Sets the devise scope to be used in the controller. If you have custom routes,
304
+ # you are required to call this method (also aliased as :as) in order to specify
305
+ # to which controller it is targetted.
306
+ #
307
+ # as :user do
308
+ # get "sign_in", :to => "devise/sessions#new"
309
+ # end
310
+ #
311
+ # Notice you cannot have two scopes mapping to the same URL. And remember, if
312
+ # you try to access a devise controller without specifying a scope, it will
313
+ # raise ActionNotFound error.
314
+ #
315
+ # Also be aware of that 'devise_scope' and 'as' use the singular form of the
316
+ # noun where other devise route commands expect the plural form. This would be a
317
+ # good and working example.
318
+ #
319
+ # devise_scope :user do
320
+ # match "/some/route" => "some_devise_controller"
321
+ # end
322
+ # devise_for :users
323
+ #
324
+ # Notice and be aware of the differences above between :user and :users
325
+ def devise_scope(scope)
326
+ constraint = lambda do |request|
327
+ request.env["devise.mapping"] = Devise.mappings[scope]
328
+ true
329
+ end
330
+
331
+ constraints(constraint) do
332
+ yield
333
+ end
334
+ end
335
+ alias :as :devise_scope
336
+
337
+ protected
338
+
339
+ def devise_session(mapping, controllers) #:nodoc:
340
+ resource :session, :only => [], :controller => controllers[:sessions], :path => "" do
341
+ get :new, :path => mapping.path_names[:sign_in], :as => "new"
342
+ post :create, :path => mapping.path_names[:sign_in]
343
+ match :destroy, :path => mapping.path_names[:sign_out], :as => "destroy", :via => mapping.sign_out_via
344
+ end
345
+ end
346
+
347
+ def devise_password(mapping, controllers) #:nodoc:
348
+ resource :password, :only => [:new, :create, :edit, :update],
349
+ :path => mapping.path_names[:password], :controller => controllers[:passwords]
350
+ end
351
+
352
+ def devise_confirmation(mapping, controllers) #:nodoc:
353
+ resource :confirmation, :only => [:new, :create, :show],
354
+ :path => mapping.path_names[:confirmation], :controller => controllers[:confirmations]
355
+ end
356
+
357
+ def devise_unlock(mapping, controllers) #:nodoc:
358
+ if mapping.to.unlock_strategy_enabled?(:email)
359
+ resource :unlock, :only => [:new, :create, :show],
360
+ :path => mapping.path_names[:unlock], :controller => controllers[:unlocks]
361
+ end
362
+ end
363
+
364
+ def devise_registration(mapping, controllers) #:nodoc:
365
+ path_names = {
366
+ :new => mapping.path_names[:sign_up],
367
+ :cancel => mapping.path_names[:cancel]
368
+ }
369
+
370
+ resource :registration, :only => [:new, :create, :edit, :update, :destroy], :path => mapping.path_names[:registration],
371
+ :path_names => path_names, :controller => controllers[:registrations] do
372
+ get :cancel
373
+ end
374
+ end
375
+
376
+ def devise_omniauth_callback(mapping, controllers) #:nodoc:
377
+ path, @scope[:path] = @scope[:path], nil
378
+ path_prefix = "/#{mapping.path}/auth".squeeze("/")
379
+
380
+ if ::OmniAuth.config.path_prefix && ::OmniAuth.config.path_prefix != path_prefix
381
+ raise "Wrong OmniAuth configuration. If you are getting this exception, it means that either:\n\n" \
382
+ "1) You are manually setting OmniAuth.config.path_prefix and it doesn't match the Devise one\n" \
383
+ "2) You are setting :omniauthable in more than one model\n" \
384
+ "3) You changed your Devise routes/OmniAuth setting and haven't restarted your server"
385
+ else
386
+ ::OmniAuth.config.path_prefix = path_prefix
387
+ end
388
+
389
+ match "#{path_prefix}/:action/callback", :constraints => { :action => Regexp.union(mapping.to.omniauth_providers.map(&:to_s)) },
390
+ :to => controllers[:omniauth_callbacks], :as => :omniauth_callback
391
+ ensure
392
+ @scope[:path] = path
393
+ end
394
+
395
+ def with_devise_exclusive_scope(new_path, new_as, options) #:nodoc:
396
+ old_as, old_path, old_module, old_constraints, old_defaults, old_options =
397
+ *@scope.values_at(:as, :path, :module, :constraints, :defaults, :options)
398
+ @scope[:as], @scope[:path], @scope[:module], @scope[:constraints], @scope[:defaults], @scope[:options] =
399
+ new_as, new_path, nil, *options.values_at(:constraints, :defaults, :options)
400
+ yield
401
+ ensure
402
+ @scope[:as], @scope[:path], @scope[:module], @scope[:constraints], @scope[:defaults], @scope[:options] =
403
+ old_as, old_path, old_module, old_constraints, old_defaults, old_options
404
+ end
405
+
406
+ def raise_no_devise_method_error!(klass) #:nodoc:
407
+ raise "#{klass} does not respond to 'devise' method. This usually means you haven't " \
408
+ "loaded your ORM file or it's being loaded too late. To fix it, be sure to require 'devise/orm/YOUR_ORM' " \
409
+ "inside 'config/initializers/devise.rb' or before your application definition in 'config/application.rb'"
410
+ end
411
+ end
412
+ end