devise-jdguyot 1.2.rc

Sign up to get free protection for your applications and to get access to all the features.
Files changed (185) hide show
  1. data/.gitignore +10 -0
  2. data/CHANGELOG.rdoc +532 -0
  3. data/Gemfile +29 -0
  4. data/Gemfile.lock +152 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.rdoc +353 -0
  7. data/Rakefile +36 -0
  8. data/TODO +4 -0
  9. data/app/controllers/devise/confirmations_controller.rb +33 -0
  10. data/app/controllers/devise/omniauth_callbacks_controller.rb +26 -0
  11. data/app/controllers/devise/passwords_controller.rb +41 -0
  12. data/app/controllers/devise/registrations_controller.rb +110 -0
  13. data/app/controllers/devise/sessions_controller.rb +25 -0
  14. data/app/controllers/devise/unlocks_controller.rb +34 -0
  15. data/app/helpers/devise_helper.rb +19 -0
  16. data/app/mailers/devise/mailer.rb +88 -0
  17. data/app/views/devise/confirmations/new.html.erb +12 -0
  18. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  19. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  20. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  21. data/app/views/devise/passwords/edit.html.erb +16 -0
  22. data/app/views/devise/passwords/new.html.erb +12 -0
  23. data/app/views/devise/registrations/edit.html.erb +25 -0
  24. data/app/views/devise/registrations/new.html.erb +18 -0
  25. data/app/views/devise/sessions/new.html.erb +17 -0
  26. data/app/views/devise/shared/_links.erb +25 -0
  27. data/app/views/devise/unlocks/new.html.erb +12 -0
  28. data/config/locales/en.yml +46 -0
  29. data/devise.gemspec +25 -0
  30. data/lib/devise/controllers/helpers.rb +227 -0
  31. data/lib/devise/controllers/internal_helpers.rb +119 -0
  32. data/lib/devise/controllers/scoped_views.rb +33 -0
  33. data/lib/devise/controllers/url_helpers.rb +39 -0
  34. data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
  35. data/lib/devise/encryptors/base.rb +20 -0
  36. data/lib/devise/encryptors/clearance_sha1.rb +17 -0
  37. data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
  38. data/lib/devise/encryptors/sha1.rb +25 -0
  39. data/lib/devise/encryptors/sha512.rb +25 -0
  40. data/lib/devise/failure_app.rb +132 -0
  41. data/lib/devise/hooks/activatable.rb +11 -0
  42. data/lib/devise/hooks/forgetable.rb +12 -0
  43. data/lib/devise/hooks/rememberable.rb +48 -0
  44. data/lib/devise/hooks/timeoutable.rb +22 -0
  45. data/lib/devise/hooks/trackable.rb +9 -0
  46. data/lib/devise/mapping.rb +110 -0
  47. data/lib/devise/models/authenticatable.rb +146 -0
  48. data/lib/devise/models/confirmable.rb +160 -0
  49. data/lib/devise/models/database_authenticatable.rb +100 -0
  50. data/lib/devise/models/encryptable.rb +72 -0
  51. data/lib/devise/models/lockable.rb +169 -0
  52. data/lib/devise/models/omniauthable.rb +23 -0
  53. data/lib/devise/models/recoverable.rb +123 -0
  54. data/lib/devise/models/registerable.rb +21 -0
  55. data/lib/devise/models/rememberable.rb +130 -0
  56. data/lib/devise/models/timeoutable.rb +43 -0
  57. data/lib/devise/models/token_authenticatable.rb +72 -0
  58. data/lib/devise/models/trackable.rb +30 -0
  59. data/lib/devise/models/validatable.rb +65 -0
  60. data/lib/devise/models.rb +68 -0
  61. data/lib/devise/modules.rb +30 -0
  62. data/lib/devise/omniauth/config.rb +30 -0
  63. data/lib/devise/omniauth/test_helpers.rb +57 -0
  64. data/lib/devise/omniauth/url_helpers.rb +29 -0
  65. data/lib/devise/omniauth.rb +47 -0
  66. data/lib/devise/orm/active_record.rb +38 -0
  67. data/lib/devise/orm/mongoid.rb +31 -0
  68. data/lib/devise/path_checker.rb +18 -0
  69. data/lib/devise/rails/routes.rb +292 -0
  70. data/lib/devise/rails/warden_compat.rb +125 -0
  71. data/lib/devise/rails.rb +50 -0
  72. data/lib/devise/schema.rb +97 -0
  73. data/lib/devise/strategies/authenticatable.rb +150 -0
  74. data/lib/devise/strategies/base.rb +15 -0
  75. data/lib/devise/strategies/database_authenticatable.rb +21 -0
  76. data/lib/devise/strategies/rememberable.rb +51 -0
  77. data/lib/devise/strategies/token_authenticatable.rb +53 -0
  78. data/lib/devise/test_helpers.rb +100 -0
  79. data/lib/devise/version.rb +3 -0
  80. data/lib/devise.rb +381 -0
  81. data/lib/generators/active_record/devise_generator.rb +28 -0
  82. data/lib/generators/active_record/templates/migration.rb +31 -0
  83. data/lib/generators/devise/devise_generator.rb +17 -0
  84. data/lib/generators/devise/install_generator.rb +24 -0
  85. data/lib/generators/devise/orm_helpers.rb +23 -0
  86. data/lib/generators/devise/views_generator.rb +106 -0
  87. data/lib/generators/mongoid/devise_generator.rb +17 -0
  88. data/lib/generators/templates/README +25 -0
  89. data/lib/generators/templates/devise.rb +186 -0
  90. data/test/controllers/helpers_test.rb +237 -0
  91. data/test/controllers/internal_helpers_test.rb +72 -0
  92. data/test/controllers/url_helpers_test.rb +59 -0
  93. data/test/devise_test.rb +65 -0
  94. data/test/encryptors_test.rb +30 -0
  95. data/test/failure_app_test.rb +187 -0
  96. data/test/generators/active_record_generator_test.rb +24 -0
  97. data/test/generators/install_generator_test.rb +13 -0
  98. data/test/generators/mongoid_generator_test.rb +22 -0
  99. data/test/generators/views_generator_test.rb +35 -0
  100. data/test/indifferent_hash.rb +33 -0
  101. data/test/integration/authenticatable_test.rb +447 -0
  102. data/test/integration/confirmable_test.rb +104 -0
  103. data/test/integration/database_authenticatable_test.rb +60 -0
  104. data/test/integration/http_authenticatable_test.rb +74 -0
  105. data/test/integration/lockable_test.rb +109 -0
  106. data/test/integration/omniauthable_test.rb +107 -0
  107. data/test/integration/recoverable_test.rb +160 -0
  108. data/test/integration/registerable_test.rb +179 -0
  109. data/test/integration/rememberable_test.rb +180 -0
  110. data/test/integration/timeoutable_test.rb +89 -0
  111. data/test/integration/token_authenticatable_test.rb +99 -0
  112. data/test/integration/trackable_test.rb +64 -0
  113. data/test/mailers/confirmation_instructions_test.rb +84 -0
  114. data/test/mailers/reset_password_instructions_test.rb +72 -0
  115. data/test/mailers/unlock_instructions_test.rb +66 -0
  116. data/test/mapping_test.rb +119 -0
  117. data/test/models/confirmable_test.rb +221 -0
  118. data/test/models/database_authenticatable_test.rb +98 -0
  119. data/test/models/encryptable_test.rb +65 -0
  120. data/test/models/lockable_test.rb +204 -0
  121. data/test/models/recoverable_test.rb +190 -0
  122. data/test/models/rememberable_test.rb +279 -0
  123. data/test/models/timeoutable_test.rb +28 -0
  124. data/test/models/token_authenticatable_test.rb +37 -0
  125. data/test/models/trackable_test.rb +5 -0
  126. data/test/models/validatable_test.rb +99 -0
  127. data/test/models_test.rb +84 -0
  128. data/test/omniauth/url_helpers_test.rb +47 -0
  129. data/test/orm/active_record.rb +9 -0
  130. data/test/orm/mongoid.rb +11 -0
  131. data/test/rails_app/Rakefile +10 -0
  132. data/test/rails_app/app/active_record/admin.rb +6 -0
  133. data/test/rails_app/app/active_record/shim.rb +2 -0
  134. data/test/rails_app/app/active_record/user.rb +8 -0
  135. data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
  136. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  137. data/test/rails_app/app/controllers/application_controller.rb +8 -0
  138. data/test/rails_app/app/controllers/home_controller.rb +16 -0
  139. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  140. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  141. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +7 -0
  142. data/test/rails_app/app/controllers/users_controller.rb +18 -0
  143. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  144. data/test/rails_app/app/mongoid/admin.rb +9 -0
  145. data/test/rails_app/app/mongoid/shim.rb +29 -0
  146. data/test/rails_app/app/mongoid/user.rb +10 -0
  147. data/test/rails_app/app/views/admins/index.html.erb +1 -0
  148. data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
  149. data/test/rails_app/app/views/home/index.html.erb +1 -0
  150. data/test/rails_app/app/views/home/private.html.erb +1 -0
  151. data/test/rails_app/app/views/layouts/application.html.erb +24 -0
  152. data/test/rails_app/app/views/users/index.html.erb +1 -0
  153. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
  154. data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
  155. data/test/rails_app/config/application.rb +40 -0
  156. data/test/rails_app/config/boot.rb +13 -0
  157. data/test/rails_app/config/database.yml +18 -0
  158. data/test/rails_app/config/environment.rb +5 -0
  159. data/test/rails_app/config/environments/development.rb +19 -0
  160. data/test/rails_app/config/environments/production.rb +33 -0
  161. data/test/rails_app/config/environments/test.rb +33 -0
  162. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  163. data/test/rails_app/config/initializers/devise.rb +176 -0
  164. data/test/rails_app/config/initializers/inflections.rb +2 -0
  165. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  166. data/test/rails_app/config/routes.rb +55 -0
  167. data/test/rails_app/config.ru +4 -0
  168. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +31 -0
  169. data/test/rails_app/db/schema.rb +52 -0
  170. data/test/rails_app/lib/shared_admin.rb +9 -0
  171. data/test/rails_app/lib/shared_user.rb +23 -0
  172. data/test/rails_app/public/404.html +26 -0
  173. data/test/rails_app/public/422.html +26 -0
  174. data/test/rails_app/public/500.html +26 -0
  175. data/test/rails_app/public/favicon.ico +0 -0
  176. data/test/rails_app/script/rails +10 -0
  177. data/test/routes_test.rb +179 -0
  178. data/test/support/assertions.rb +24 -0
  179. data/test/support/helpers.rb +60 -0
  180. data/test/support/integration.rb +88 -0
  181. data/test/support/locale/en.yml +4 -0
  182. data/test/support/webrat/integrations/rails.rb +24 -0
  183. data/test/test_helper.rb +29 -0
  184. data/test/test_helpers_test.rb +118 -0
  185. metadata +388 -0
@@ -0,0 +1,68 @@
1
+ module Devise
2
+ module Models
3
+ # Creates configuration values for Devise and for the given module.
4
+ #
5
+ # Devise::Models.config(Devise::Authenticatable, :stretches, 10)
6
+ #
7
+ # The line above creates:
8
+ #
9
+ # 1) An accessor called Devise.stretches, which value is used by default;
10
+ #
11
+ # 2) Some class methods for your model Model.stretches and Model.stretches=
12
+ # which have higher priority than Devise.stretches;
13
+ #
14
+ # 3) And an instance method stretches.
15
+ #
16
+ # To add the class methods you need to have a module ClassMethods defined
17
+ # inside the given class.
18
+ #
19
+ def self.config(mod, *accessors) #:nodoc:
20
+ accessors.each do |accessor|
21
+ mod.class_eval <<-METHOD, __FILE__, __LINE__ + 1
22
+ def #{accessor}
23
+ if defined?(@#{accessor})
24
+ @#{accessor}
25
+ elsif superclass.respond_to?(:#{accessor})
26
+ superclass.#{accessor}
27
+ else
28
+ Devise.#{accessor}
29
+ end
30
+ end
31
+
32
+ def #{accessor}=(value)
33
+ @#{accessor} = value
34
+ end
35
+ METHOD
36
+ end
37
+ end
38
+
39
+ # Include the chosen devise modules in your model:
40
+ #
41
+ # devise :database_authenticatable, :confirmable, :recoverable
42
+ #
43
+ # You can also give any of the devise configuration values in form of a hash,
44
+ # with specific values for this model. Please check your Devise initializer
45
+ # for a complete description on those values.
46
+ #
47
+ def devise(*modules)
48
+ include Devise::Models::Authenticatable
49
+ options = modules.extract_options!
50
+ self.devise_modules += modules.map(&:to_sym).uniq.sort_by { |s|
51
+ Devise::ALL.index(s) || -1 # follow Devise::ALL order
52
+ }
53
+
54
+ devise_modules_hook! do
55
+ devise_modules.each { |m| include Devise::Models.const_get(m.to_s.classify) }
56
+ options.each { |key, value| send(:"#{key}=", value) }
57
+ end
58
+ end
59
+
60
+ # The hook which is called inside devise. So your ORM can include devise
61
+ # compatibility stuff.
62
+ def devise_modules_hook!
63
+ yield
64
+ end
65
+ end
66
+ end
67
+
68
+ require 'devise/models/authenticatable'
@@ -0,0 +1,30 @@
1
+ require 'active_support/core_ext/object/with_options'
2
+
3
+ Devise.with_options :model => true do |d|
4
+ # Strategies first
5
+ d.with_options :strategy => true do |s|
6
+ routes = [nil, :new, :destroy]
7
+ s.add_module :database_authenticatable, :controller => :sessions, :route => { :session => routes }
8
+ s.add_module :token_authenticatable, :controller => :sessions, :route => { :session => routes }
9
+ s.add_module :rememberable
10
+ end
11
+
12
+ # Other authentications
13
+ d.add_module :encryptable
14
+ d.add_module :omniauthable, :controller => :omniauth_callbacks, :route => :omniauth_callback
15
+
16
+ # Misc after
17
+ routes = [nil, :new, :edit]
18
+ d.add_module :recoverable, :controller => :passwords, :route => { :password => routes }
19
+ d.add_module :registerable, :controller => :registrations, :route => { :registration => (routes << :cancel) }
20
+ d.add_module :validatable
21
+
22
+ # The ones which can sign out after
23
+ routes = [nil, :new]
24
+ d.add_module :confirmable, :controller => :confirmations, :route => { :confirmation => routes }
25
+ d.add_module :lockable, :controller => :unlocks, :route => { :unlock => routes }
26
+ d.add_module :timeoutable
27
+
28
+ # Stats for last, so we make sure the user is really signed in
29
+ d.add_module :trackable
30
+ end
@@ -0,0 +1,30 @@
1
+ module Devise
2
+ module OmniAuth
3
+ class Config
4
+ attr_accessor :strategy
5
+ attr_reader :args
6
+
7
+ def initialize(provider, args)
8
+ @provider = provider
9
+ @args = args
10
+ @strategy = nil
11
+ end
12
+
13
+ def strategy_class
14
+ ::OmniAuth::Strategies.const_get("#{::OmniAuth::Utils.camelize(@provider.to_s)}")
15
+ end
16
+
17
+ def check_if_allow_stubs!
18
+ raise "OmniAuth strategy for #{@provider} does not allow stubs, only OAuth2 ones do." unless allow_stubs?
19
+ end
20
+
21
+ def allow_stubs?
22
+ defined?(::OmniAuth::Strategies::OAuth2) && strategy.is_a?(::OmniAuth::Strategies::OAuth2)
23
+ end
24
+
25
+ def build_connection(&block)
26
+ strategy.client.connection.build(&block)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,57 @@
1
+ module Devise
2
+ module OmniAuth
3
+ module TestHelpers
4
+ def self.test_mode!
5
+ Faraday.default_adapter = :test if defined?(Faraday)
6
+ ActiveSupport.on_load(:action_controller) { include Devise::OmniAuth::TestHelpers }
7
+ ActiveSupport.on_load(:action_view) { include Devise::OmniAuth::TestHelpers }
8
+ end
9
+
10
+ def self.stub!(provider, stubs=nil, &block)
11
+ raise "You either need to pass stubs as a block or as a parameter" unless block_given? || stubs
12
+
13
+ config = Devise.omniauth_configs[provider]
14
+ raise "Could not find configuration for #{provider.to_s} omniauth provider" unless config
15
+
16
+ config.check_if_allow_stubs!
17
+ stubs ||= Faraday::Adapter::Test::Stubs.new(&block)
18
+
19
+ config.build_connection do |b|
20
+ b.adapter :test, stubs
21
+ end
22
+ end
23
+
24
+ def self.reset_stubs!(*providers)
25
+ target = providers.any? ? Devise.omniauth_configs.slice(*providers) : Devise.omniauth_configs
26
+ target.each_value do |config|
27
+ next unless config.allow_stubs?
28
+ config.build_connection { |b| b.adapter Faraday.default_adapter }
29
+ end
30
+ end
31
+
32
+ def self.short_circuit_authorizers!
33
+ module_eval <<-ALIASES, __FILE__, __LINE__ + 1
34
+ def omniauth_authorize_path(*args)
35
+ omniauth_callback_path(*args)
36
+ end
37
+ ALIASES
38
+
39
+ Devise.mappings.each_value do |m|
40
+ next unless m.omniauthable?
41
+
42
+ module_eval <<-ALIASES, __FILE__, __LINE__ + 1
43
+ def #{m.name}_omniauth_authorize_path(provider, params = {})
44
+ #{m.name}_omniauth_callback_path(provider, params)
45
+ end
46
+ ALIASES
47
+ end
48
+ end
49
+
50
+ def self.unshort_circuit_authorizers!
51
+ module_eval do
52
+ instance_methods.each { |m| remove_method(m) }
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,29 @@
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
+ "/#{mapping.path}/auth/\#{provider}\#{'?'+params.to_param if params.present?}"
11
+ else
12
+ raise ArgumentError, "Could not find omniauth provider \#{provider.inspect}"
13
+ end
14
+ end
15
+ URL_HELPERS
16
+ end
17
+
18
+ def omniauth_authorize_path(resource_or_scope, *args)
19
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
20
+ send("#{scope}_omniauth_authorize_path", *args)
21
+ end
22
+
23
+ def omniauth_callback_path(resource_or_scope, *args)
24
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
25
+ send("#{scope}_omniauth_callback_path", *args)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,47 @@
1
+ begin
2
+ require "omniauth/core"
3
+ rescue LoadError => e
4
+ warn "Could not load 'omniauth/core'. Please ensure you have the oa-core gem installed and listed in your Gemfile."
5
+ raise
6
+ end
7
+
8
+ module OmniAuth
9
+ # TODO HAXES Backport to OmniAuth
10
+ module Strategy #:nodoc:
11
+ def initialize(app, name, *args)
12
+ @app = app
13
+ @name = name.to_sym
14
+ @options = args.last.is_a?(Hash) ? args.pop : {}
15
+ yield self if block_given?
16
+ end
17
+
18
+ def fail!(message_key, exception = nil)
19
+ self.env['omniauth.error'] = exception
20
+ self.env['omniauth.failure_key'] = message_key
21
+ self.env['omniauth.failed_strategy'] = self
22
+ OmniAuth.config.on_failure.call(self.env, message_key.to_sym)
23
+ end
24
+ end
25
+ end
26
+
27
+ # Clean up the default path_prefix. It will be automatically set by Devise.
28
+ OmniAuth.config.path_prefix = nil
29
+
30
+ OmniAuth.config.on_failure = Proc.new do |env, key|
31
+ env['devise.mapping'] = Devise::Mapping.find_by_path!(env['PATH_INFO'], :path)
32
+ controller_klass = "#{env['devise.mapping'].controllers[:omniauth_callbacks].camelize}Controller"
33
+ controller_klass.constantize.action(:failure).call(env)
34
+ end
35
+
36
+ module Devise
37
+ module OmniAuth
38
+ autoload :Config, "devise/omniauth/config"
39
+ autoload :UrlHelpers, "devise/omniauth/url_helpers"
40
+ autoload :TestHelpers, "devise/omniauth/test_helpers"
41
+
42
+ class << self
43
+ delegate :short_circuit_authorizers!, :unshort_circuit_authorizers!,
44
+ :test_mode!, :stub!, :reset_stubs!, :to => "Devise::OmniAuth::TestHelpers"
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,38 @@
1
+ require 'orm_adapter/adapters/active_record'
2
+
3
+ module Devise
4
+ module Orm
5
+ # This module contains some helpers and handle schema (migrations):
6
+ #
7
+ # create_table :accounts do |t|
8
+ # t.database_authenticatable
9
+ # t.confirmable
10
+ # t.recoverable
11
+ # t.rememberable
12
+ # t.trackable
13
+ # t.lockable
14
+ # t.timestamps
15
+ # end
16
+ #
17
+ # However this method does not add indexes. If you need them, here is the declaration:
18
+ #
19
+ # add_index "accounts", ["email"], :name => "email", :unique => true
20
+ # add_index "accounts", ["confirmation_token"], :name => "confirmation_token", :unique => true
21
+ # add_index "accounts", ["reset_password_token"], :name => "reset_password_token", :unique => true
22
+ #
23
+ module ActiveRecord
24
+ module Schema
25
+ include Devise::Schema
26
+
27
+ # Tell how to apply schema methods.
28
+ def apply_devise_schema(name, type, options={})
29
+ column name, type.to_s.downcase.to_sym, options
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ ActiveRecord::Base.extend Devise::Models
37
+ ActiveRecord::ConnectionAdapters::Table.send :include, Devise::Orm::ActiveRecord::Schema
38
+ ActiveRecord::ConnectionAdapters::TableDefinition.send :include, Devise::Orm::ActiveRecord::Schema
@@ -0,0 +1,31 @@
1
+ require 'orm_adapter/adapters/mongoid'
2
+
3
+ module Devise
4
+ module Orm
5
+ module Mongoid
6
+ module Hook
7
+ def devise_modules_hook!
8
+ extend Schema
9
+ yield
10
+ return unless Devise.apply_schema
11
+ devise_modules.each { |m| send(m) if respond_to?(m, true) }
12
+ end
13
+ end
14
+
15
+ module Schema
16
+ include Devise::Schema
17
+
18
+ # Tell how to apply schema methods
19
+ def apply_devise_schema(name, type, options={})
20
+ type = Time if type == DateTime
21
+ field name, { :type => type }.merge!(options)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ Mongoid::Document::ClassMethods.class_eval do
29
+ include Devise::Models
30
+ include Devise::Orm::Mongoid::Hook
31
+ end
@@ -0,0 +1,18 @@
1
+ module Devise
2
+ class PathChecker
3
+ include Rails.application.routes.url_helpers
4
+
5
+ def self.default_url_options(*args)
6
+ ApplicationController.default_url_options(*args)
7
+ end
8
+
9
+ def initialize(env, scope)
10
+ @current_path = "/#{env["SCRIPT_NAME"]}/#{env["PATH_INFO"]}".squeeze("/")
11
+ @scope = scope
12
+ end
13
+
14
+ def signing_out?
15
+ @current_path == send("destroy_#{@scope}_session_path")
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,292 @@
1
+ module ActionDispatch::Routing
2
+ class RouteSet #:nodoc:
3
+ # Ensure Devise modules are included only after loading routes, because we
4
+ # need devise_for mappings already declared to create filters and helpers.
5
+ def finalize_with_devise!
6
+ finalize_without_devise!
7
+ Devise.configure_warden!
8
+ end
9
+ alias_method_chain :finalize!, :devise
10
+ end
11
+
12
+ class Mapper
13
+ # Includes devise_for method for routes. This method is responsible to
14
+ # generate all needed routes for devise, based on what modules you have
15
+ # defined in your model.
16
+ #
17
+ # ==== Examples
18
+ #
19
+ # Let's say you have an User model configured to use authenticatable,
20
+ # confirmable and recoverable modules. After creating this inside your routes:
21
+ #
22
+ # devise_for :users
23
+ #
24
+ # This method is going to look inside your User model and create the
25
+ # needed routes:
26
+ #
27
+ # # Session routes for Authenticatable (default)
28
+ # new_user_session GET /users/sign_in {:controller=>"devise/sessions", :action=>"new"}
29
+ # user_session POST /users/sign_in {:controller=>"devise/sessions", :action=>"create"}
30
+ # destroy_user_session GET /users/sign_out {:controller=>"devise/sessions", :action=>"destroy"}
31
+ #
32
+ # # Password routes for Recoverable, if User model has :recoverable configured
33
+ # new_user_password GET /users/password/new(.:format) {:controller=>"devise/passwords", :action=>"new"}
34
+ # edit_user_password GET /users/password/edit(.:format) {:controller=>"devise/passwords", :action=>"edit"}
35
+ # user_password PUT /users/password(.:format) {:controller=>"devise/passwords", :action=>"update"}
36
+ # POST /users/password(.:format) {:controller=>"devise/passwords", :action=>"create"}
37
+ #
38
+ # # Confirmation routes for Confirmable, if User model has :confirmable configured
39
+ # new_user_confirmation GET /users/confirmation/new(.:format) {:controller=>"devise/confirmations", :action=>"new"}
40
+ # user_confirmation GET /users/confirmation(.:format) {:controller=>"devise/confirmations", :action=>"show"}
41
+ # POST /users/confirmation(.:format) {:controller=>"devise/confirmations", :action=>"create"}
42
+ #
43
+ # ==== Options
44
+ #
45
+ # You can configure your routes with some options:
46
+ #
47
+ # * :class_name => setup a different class to be looked up by devise,
48
+ # if it cannot be correctly find by the route name.
49
+ #
50
+ # devise_for :users, :class_name => 'Account'
51
+ #
52
+ # * :path => allows you to setup path name that will be used, as rails routes does.
53
+ # The following route configuration would setup your route as /accounts instead of /users:
54
+ #
55
+ # devise_for :users, :path => 'accounts'
56
+ #
57
+ # * :singular => setup the singular name for the given resource. This is used as the instance variable name in
58
+ # controller, as the name in routes and the scope given to warden.
59
+ #
60
+ # devise_for :users, :singular => :user
61
+ #
62
+ # * :path_names => configure different path names to overwrite defaults :sign_in, :sign_out, :sign_up,
63
+ # :password, :confirmation, :unlock.
64
+ #
65
+ # devise_for :users, :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification' }
66
+ #
67
+ # * :controllers => the controller which should be used. All routes by default points to Devise controllers.
68
+ # However, if you want them to point to custom controller, you should do:
69
+ #
70
+ # devise_for :users, :controllers => { :sessions => "users/sessions" }
71
+ #
72
+ # * :sign_out_via => the HTTP method(s) accepted for the :sign_out action (default: :get),
73
+ # if you wish to restrict this to accept only :post or :delete requests you should do:
74
+ #
75
+ # devise_for :users, :sign_out_via => [ :post, :delete ]
76
+ #
77
+ # You need to make sure that your sign_out controls trigger a request with a matching HTTP method.
78
+ #
79
+ # * :module => the namespace to find controlers. By default, devise will access devise/sessions,
80
+ # devise/registrations and so on. If you want to namespace all at once, use module:
81
+ #
82
+ # devise_for :users, :module => "users"
83
+ #
84
+ # Notice that whenever you use namespace in the router DSL, it automatically sets the module.
85
+ # So the following setup:
86
+ #
87
+ # namespace :publisher
88
+ # devise_for :account
89
+ # end
90
+ #
91
+ # Will use publisher/sessions controller instead of devise/sessions controller. You can revert
92
+ # this by providing the :module option to devise_for.
93
+ #
94
+ # Also pay attention that when you use a namespace it will affect all the helpers and methods for controllers
95
+ # and views. For example, using the above setup you'll end with following methods:
96
+ # current_publisher_account, authenticate_publisher_account!, pusblisher_account_signed_in, etc.
97
+ #
98
+ # * :skip => tell which controller you want to skip routes from being created:
99
+ #
100
+ # devise_for :users, :skip => :sessions
101
+ #
102
+ # ==== Scoping
103
+ #
104
+ # Following Rails 3 routes DSL, you can nest devise_for calls inside a scope:
105
+ #
106
+ # scope "/my" do
107
+ # devise_for :users
108
+ # end
109
+ #
110
+ # However, since Devise uses the request path to retrieve the current user, it has one caveats.
111
+ # If you are using a dynamic segment, as below:
112
+ #
113
+ # scope ":locale" do
114
+ # devise_for :users
115
+ # end
116
+ #
117
+ # You are required to configure default_url_options in your ApplicationController class level, so
118
+ # Devise can pick it:
119
+ #
120
+ # class ApplicationController < ActionController::Base
121
+ # def self.default_url_options
122
+ # { :locale => I18n.locale }
123
+ # end
124
+ # end
125
+ #
126
+ # ==== Adding custom actions to override controllers
127
+ #
128
+ # You can pass a block to devise_for that will add any routes defined in the block to Devise's
129
+ # list of known actions. This is important if you add a custom action to a controller that
130
+ # overrides an out of the box Devise controller.
131
+ # For example:
132
+ #
133
+ # class RegistrationsController < Devise::RegistrationsController
134
+ # def update
135
+ # # do something different here
136
+ # end
137
+ #
138
+ # def deactivate
139
+ # # not a standard action
140
+ # # deactivate code here
141
+ # end
142
+ # end
143
+ #
144
+ # In order to get Devise to recognize the deactivate action, your devise_for entry should look like this,
145
+ #
146
+ # devise_for :owners, :controllers => { :registrations => "registrations" } do
147
+ # post "deactivate", :to => "registrations#deactivate", :as => "deactivate_registration"
148
+ # end
149
+ #
150
+ def devise_for(*resources)
151
+ options = resources.extract_options!
152
+
153
+ options[:as] ||= @scope[:as] if @scope[:as].present?
154
+ options[:module] ||= @scope[:module] if @scope[:module].present?
155
+ options[:path_prefix] ||= @scope[:path] if @scope[:path].present?
156
+ options[:path_names] = (@scope[:path_names] || {}).merge(options[:path_names] || {})
157
+
158
+ resources.map!(&:to_sym)
159
+
160
+ resources.each do |resource|
161
+ mapping = Devise.add_mapping(resource, options)
162
+
163
+ begin
164
+ raise_no_devise_method_error!(mapping.class_name) unless mapping.to.respond_to?(:devise)
165
+ rescue NameError => e
166
+ raise unless mapping.class_name == resource.to_s.classify
167
+ warn "[WARNING] You provided devise_for #{resource.inspect} but there is " <<
168
+ "no model #{mapping.class_name} defined in your application"
169
+ next
170
+ rescue NoMethodError => e
171
+ raise unless e.message.include?("undefined method `devise'")
172
+ raise_no_devise_method_error!(mapping.class_name)
173
+ end
174
+
175
+ routes = mapping.routes
176
+ routes -= Array(options.delete(:skip)).map { |s| s.to_s.singularize.to_sym }
177
+
178
+ devise_scope mapping.name do
179
+ yield if block_given?
180
+ with_devise_exclusive_scope mapping.fullpath, mapping.name do
181
+ routes.each { |mod| send("devise_#{mod}", mapping, mapping.controllers) }
182
+ end
183
+ end
184
+ end
185
+ end
186
+
187
+ # Allow you to add authentication request from the router:
188
+ #
189
+ # authenticate(:user) do
190
+ # resources :post
191
+ # end
192
+ #
193
+ def authenticate(scope)
194
+ constraint = lambda do |request|
195
+ request.env["warden"].authenticate!(:scope => scope)
196
+ end
197
+
198
+ constraints(constraint) do
199
+ yield
200
+ end
201
+ end
202
+
203
+ # Sets the devise scope to be used in the controller. If you have custom routes,
204
+ # you are required to call this method (also aliased as :as) in order to specify
205
+ # to which controller it is targetted.
206
+ #
207
+ # as :user do
208
+ # get "sign_in", :to => "devise/sessions#new"
209
+ # end
210
+ #
211
+ # Notice you cannot have two scopes mapping to the same URL. And remember, if
212
+ # you try to access a devise controller without specifying a scope, it will
213
+ # raise ActionNotFound error.
214
+ def devise_scope(scope)
215
+ constraint = lambda do |request|
216
+ request.env["devise.mapping"] = Devise.mappings[scope]
217
+ true
218
+ end
219
+
220
+ constraints(constraint) do
221
+ yield
222
+ end
223
+ end
224
+ alias :as :devise_scope
225
+
226
+ protected
227
+
228
+ def devise_session(mapping, controllers) #:nodoc:
229
+ resource :session, :only => [], :controller => controllers[:sessions], :path => "" do
230
+ get :new, :path => mapping.path_names[:sign_in], :as => "new"
231
+ post :create, :path => mapping.path_names[:sign_in]
232
+ match :destroy, :path => mapping.path_names[:sign_out], :as => "destroy", :via => mapping.sign_out_via
233
+ end
234
+ end
235
+
236
+ def devise_password(mapping, controllers) #:nodoc:
237
+ resource :password, :only => [:new, :create, :edit, :update],
238
+ :path => mapping.path_names[:password], :controller => controllers[:passwords]
239
+ end
240
+
241
+ def devise_confirmation(mapping, controllers) #:nodoc:
242
+ resource :confirmation, :only => [:new, :create, :show],
243
+ :path => mapping.path_names[:confirmation], :controller => controllers[:confirmations]
244
+ end
245
+
246
+ def devise_unlock(mapping, controllers) #:nodoc:
247
+ if mapping.to.unlock_strategy_enabled?(:email)
248
+ resource :unlock, :only => [:new, :create, :show],
249
+ :path => mapping.path_names[:unlock], :controller => controllers[:unlocks]
250
+ end
251
+ end
252
+
253
+ def devise_registration(mapping, controllers) #:nodoc:
254
+ path_names = {
255
+ :new => mapping.path_names[:sign_up],
256
+ :cancel => mapping.path_names[:cancel]
257
+ }
258
+
259
+ resource :registration, :except => :show, :path => mapping.path_names[:registration],
260
+ :path_names => path_names, :controller => controllers[:registrations] do
261
+ get :cancel
262
+ end
263
+ end
264
+
265
+ def devise_omniauth_callback(mapping, controllers) #:nodoc:
266
+ path_prefix = "/#{mapping.path}/auth"
267
+
268
+ if ::OmniAuth.config.path_prefix && ::OmniAuth.config.path_prefix != path_prefix
269
+ warn "[DEVISE] You can only add :omniauthable behavior to one model."
270
+ else
271
+ ::OmniAuth.config.path_prefix = path_prefix
272
+ end
273
+
274
+ match "/auth/:action/callback", :action => Regexp.union(mapping.to.omniauth_providers.map(&:to_s)),
275
+ :to => controllers[:omniauth_callbacks], :as => :omniauth_callback
276
+ end
277
+
278
+ def with_devise_exclusive_scope(new_path, new_as) #:nodoc:
279
+ old_as, old_path, old_module = @scope[:as], @scope[:path], @scope[:module]
280
+ @scope[:as], @scope[:path], @scope[:module] = new_as, new_path, nil
281
+ yield
282
+ ensure
283
+ @scope[:as], @scope[:path], @scope[:module] = old_as, old_path, old_module
284
+ end
285
+
286
+ def raise_no_devise_method_error!(klass) #:nodoc:
287
+ raise "#{klass} does not respond to 'devise' method. This usually means you haven't " <<
288
+ "loaded your ORM file or it's being loaded too late. To fix it, be sure to require 'devise/orm/YOUR_ORM' " <<
289
+ "inside 'config/initializers/devise.rb' or before your application definition in 'config/application.rb'"
290
+ end
291
+ end
292
+ end