gonow-devise 1.2.rc

Sign up to get free protection for your applications and to get access to all the features.
Files changed (163) hide show
  1. data/CHANGELOG.rdoc +506 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +335 -0
  4. data/app/controllers/devise/confirmations_controller.rb +33 -0
  5. data/app/controllers/devise/oauth_callbacks_controller.rb +4 -0
  6. data/app/controllers/devise/passwords_controller.rb +41 -0
  7. data/app/controllers/devise/registrations_controller.rb +109 -0
  8. data/app/controllers/devise/sessions_controller.rb +24 -0
  9. data/app/controllers/devise/unlocks_controller.rb +34 -0
  10. data/app/helpers/devise_helper.rb +19 -0
  11. data/app/mailers/devise/mailer.rb +88 -0
  12. data/app/views/devise/confirmations/new.html.erb +12 -0
  13. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  14. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  15. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  16. data/app/views/devise/passwords/edit.html.erb +16 -0
  17. data/app/views/devise/passwords/new.html.erb +12 -0
  18. data/app/views/devise/registrations/edit.html.erb +25 -0
  19. data/app/views/devise/registrations/new.html.erb +18 -0
  20. data/app/views/devise/sessions/new.html.erb +17 -0
  21. data/app/views/devise/shared/_links.erb +25 -0
  22. data/app/views/devise/unlocks/new.html.erb +12 -0
  23. data/config/locales/en.yml +46 -0
  24. data/lib/devise.rb +372 -0
  25. data/lib/devise/controllers/helpers.rb +228 -0
  26. data/lib/devise/controllers/internal_helpers.rb +113 -0
  27. data/lib/devise/controllers/scoped_views.rb +33 -0
  28. data/lib/devise/controllers/url_helpers.rb +39 -0
  29. data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
  30. data/lib/devise/encryptors/base.rb +20 -0
  31. data/lib/devise/encryptors/clearance_sha1.rb +17 -0
  32. data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
  33. data/lib/devise/encryptors/sha1.rb +25 -0
  34. data/lib/devise/encryptors/sha512.rb +25 -0
  35. data/lib/devise/failure_app.rb +126 -0
  36. data/lib/devise/hooks/activatable.rb +11 -0
  37. data/lib/devise/hooks/forgetable.rb +12 -0
  38. data/lib/devise/hooks/rememberable.rb +45 -0
  39. data/lib/devise/hooks/timeoutable.rb +22 -0
  40. data/lib/devise/hooks/trackable.rb +9 -0
  41. data/lib/devise/mapping.rb +105 -0
  42. data/lib/devise/models.rb +66 -0
  43. data/lib/devise/models/authenticatable.rb +143 -0
  44. data/lib/devise/models/confirmable.rb +160 -0
  45. data/lib/devise/models/database_authenticatable.rb +94 -0
  46. data/lib/devise/models/encryptable.rb +65 -0
  47. data/lib/devise/models/lockable.rb +168 -0
  48. data/lib/devise/models/oauthable.rb +49 -0
  49. data/lib/devise/models/recoverable.rb +83 -0
  50. data/lib/devise/models/registerable.rb +21 -0
  51. data/lib/devise/models/rememberable.rb +122 -0
  52. data/lib/devise/models/timeoutable.rb +43 -0
  53. data/lib/devise/models/token_authenticatable.rb +72 -0
  54. data/lib/devise/models/trackable.rb +30 -0
  55. data/lib/devise/models/validatable.rb +64 -0
  56. data/lib/devise/modules.rb +30 -0
  57. data/lib/devise/oauth.rb +41 -0
  58. data/lib/devise/oauth/config.rb +33 -0
  59. data/lib/devise/oauth/helpers.rb +18 -0
  60. data/lib/devise/oauth/internal_helpers.rb +182 -0
  61. data/lib/devise/oauth/test_helpers.rb +29 -0
  62. data/lib/devise/oauth/url_helpers.rb +35 -0
  63. data/lib/devise/orm/active_record.rb +38 -0
  64. data/lib/devise/orm/mongoid.rb +31 -0
  65. data/lib/devise/path_checker.rb +18 -0
  66. data/lib/devise/rails.rb +68 -0
  67. data/lib/devise/rails/routes.rb +260 -0
  68. data/lib/devise/rails/warden_compat.rb +41 -0
  69. data/lib/devise/schema.rb +96 -0
  70. data/lib/devise/strategies/authenticatable.rb +150 -0
  71. data/lib/devise/strategies/base.rb +15 -0
  72. data/lib/devise/strategies/database_authenticatable.rb +21 -0
  73. data/lib/devise/strategies/rememberable.rb +51 -0
  74. data/lib/devise/strategies/token_authenticatable.rb +53 -0
  75. data/lib/devise/test_helpers.rb +100 -0
  76. data/lib/devise/version.rb +3 -0
  77. data/lib/generators/active_record/devise_generator.rb +28 -0
  78. data/lib/generators/active_record/templates/migration.rb +30 -0
  79. data/lib/generators/devise/devise_generator.rb +17 -0
  80. data/lib/generators/devise/install_generator.rb +24 -0
  81. data/lib/generators/devise/orm_helpers.rb +24 -0
  82. data/lib/generators/devise/views_generator.rb +63 -0
  83. data/lib/generators/mongoid/devise_generator.rb +17 -0
  84. data/lib/generators/templates/README +25 -0
  85. data/lib/generators/templates/devise.rb +168 -0
  86. data/test/controllers/helpers_test.rb +205 -0
  87. data/test/controllers/internal_helpers_test.rb +56 -0
  88. data/test/controllers/url_helpers_test.rb +59 -0
  89. data/test/devise_test.rb +65 -0
  90. data/test/encryptors_test.rb +30 -0
  91. data/test/failure_app_test.rb +148 -0
  92. data/test/generators/generators_test_helper.rb +4 -0
  93. data/test/generators/install_generator_test.rb +14 -0
  94. data/test/generators/views_generator_test.rb +37 -0
  95. data/test/integration/authenticatable_test.rb +424 -0
  96. data/test/integration/confirmable_test.rb +104 -0
  97. data/test/integration/database_authenticatable_test.rb +38 -0
  98. data/test/integration/http_authenticatable_test.rb +64 -0
  99. data/test/integration/lockable_test.rb +109 -0
  100. data/test/integration/oauthable_test.rb +244 -0
  101. data/test/integration/recoverable_test.rb +134 -0
  102. data/test/integration/registerable_test.rb +180 -0
  103. data/test/integration/rememberable_test.rb +179 -0
  104. data/test/integration/timeoutable_test.rb +89 -0
  105. data/test/integration/token_authenticatable_test.rb +99 -0
  106. data/test/integration/trackable_test.rb +64 -0
  107. data/test/mailers/confirmation_instructions_test.rb +84 -0
  108. data/test/mailers/reset_password_instructions_test.rb +72 -0
  109. data/test/mailers/unlock_instructions_test.rb +66 -0
  110. data/test/mapping_test.rb +95 -0
  111. data/test/models/confirmable_test.rb +221 -0
  112. data/test/models/database_authenticatable_test.rb +82 -0
  113. data/test/models/encryptable_test.rb +65 -0
  114. data/test/models/lockable_test.rb +204 -0
  115. data/test/models/oauthable_test.rb +21 -0
  116. data/test/models/recoverable_test.rb +155 -0
  117. data/test/models/rememberable_test.rb +271 -0
  118. data/test/models/timeoutable_test.rb +28 -0
  119. data/test/models/token_authenticatable_test.rb +37 -0
  120. data/test/models/trackable_test.rb +5 -0
  121. data/test/models/validatable_test.rb +99 -0
  122. data/test/models_test.rb +77 -0
  123. data/test/oauth/config_test.rb +44 -0
  124. data/test/oauth/url_helpers_test.rb +47 -0
  125. data/test/orm/active_record.rb +9 -0
  126. data/test/orm/mongoid.rb +10 -0
  127. data/test/rails_app/app/active_record/admin.rb +6 -0
  128. data/test/rails_app/app/active_record/shim.rb +2 -0
  129. data/test/rails_app/app/active_record/user.rb +8 -0
  130. data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
  131. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  132. data/test/rails_app/app/controllers/application_controller.rb +9 -0
  133. data/test/rails_app/app/controllers/home_controller.rb +12 -0
  134. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  135. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  136. data/test/rails_app/app/controllers/users_controller.rb +18 -0
  137. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  138. data/test/rails_app/app/mongoid/admin.rb +9 -0
  139. data/test/rails_app/app/mongoid/shim.rb +24 -0
  140. data/test/rails_app/app/mongoid/user.rb +10 -0
  141. data/test/rails_app/config/application.rb +35 -0
  142. data/test/rails_app/config/boot.rb +13 -0
  143. data/test/rails_app/config/environment.rb +5 -0
  144. data/test/rails_app/config/environments/development.rb +19 -0
  145. data/test/rails_app/config/environments/production.rb +33 -0
  146. data/test/rails_app/config/environments/test.rb +33 -0
  147. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  148. data/test/rails_app/config/initializers/devise.rb +172 -0
  149. data/test/rails_app/config/initializers/inflections.rb +2 -0
  150. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  151. data/test/rails_app/config/routes.rb +54 -0
  152. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +31 -0
  153. data/test/rails_app/db/schema.rb +52 -0
  154. data/test/rails_app/lib/shared_admin.rb +9 -0
  155. data/test/rails_app/lib/shared_user.rb +48 -0
  156. data/test/routes_test.rb +189 -0
  157. data/test/support/assertions.rb +24 -0
  158. data/test/support/helpers.rb +60 -0
  159. data/test/support/integration.rb +88 -0
  160. data/test/support/webrat/integrations/rails.rb +24 -0
  161. data/test/test_helper.rb +23 -0
  162. data/test/test_helpers_test.rb +101 -0
  163. metadata +357 -0
@@ -0,0 +1,29 @@
1
+ module Devise
2
+ module Oauth
3
+ module TestHelpers #:nodoc:
4
+ def self.short_circuit_authorizers!
5
+ module_eval <<-ALIASES, __FILE__, __LINE__ + 1
6
+ def oauth_authorize_url(scope, provider)
7
+ oauth_callback_path(scope, provider, :code => "12345")
8
+ end
9
+ ALIASES
10
+
11
+ Devise.mappings.each_value do |m|
12
+ next unless m.oauthable?
13
+
14
+ module_eval <<-ALIASES, __FILE__, __LINE__ + 1
15
+ def #{m.name}_oauth_authorize_url(provider)
16
+ #{m.name}_oauth_callback_path(provider, :code => "12345")
17
+ end
18
+ ALIASES
19
+ end
20
+ end
21
+
22
+ def self.unshort_circuit_authorizers!
23
+ module_eval do
24
+ instance_methods.each { |m| remove_method(m) }
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,35 @@
1
+ module Devise
2
+ module Oauth
3
+ module UrlHelpers
4
+ def self.define_helpers(mapping)
5
+ return unless mapping.oauthable?
6
+
7
+ class_eval <<-URL_HELPERS, __FILE__, __LINE__ + 1
8
+ def #{mapping.name}_oauth_authorize_url(provider, options={})
9
+ if config = Devise.oauth_configs[provider.to_sym]
10
+ options[:redirect_uri] ||= #{mapping.name}_oauth_callback_url(provider.to_s)
11
+ config.authorize_url(options)
12
+ else
13
+ raise ArgumentError, "Could not find oauth provider \#{provider.inspect}"
14
+ end
15
+ end
16
+ URL_HELPERS
17
+ end
18
+
19
+ def oauth_authorize_url(resource_or_scope, *args)
20
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
21
+ send("#{scope}_oauth_authorize_url", *args)
22
+ end
23
+
24
+ def oauth_callback_url(resource_or_scope, *args)
25
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
26
+ send("#{scope}_oauth_callback_url", *args)
27
+ end
28
+
29
+ def oauth_callback_path(resource_or_scope, *args)
30
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
31
+ send("#{scope}_oauth_callback_path", *args)
32
+ end
33
+ end
34
+ end
35
+ 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,68 @@
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
+ # Skip eager load of controllers because it is handled by Devise
9
+ # to avoid loading unused controllers.
10
+ target = paths.is_a?(Hash) ? paths["app/controllers"] : paths.app.controllers
11
+ target.autoload!
12
+ target.skip_eager_load!
13
+
14
+ # Initialize Warden and copy its configurations.
15
+ config.app_middleware.use Warden::Manager do |config|
16
+ Devise.warden_config = config
17
+ end
18
+
19
+ # Force routes to be loaded if we are doing any eager load.
20
+ config.before_eager_load { |app| app.reload_routes! }
21
+
22
+ initializer "devise.add_filters" do |app|
23
+ app.config.filter_parameters += [:password, :password_confirmation]
24
+ app.config.filter_parameters.uniq
25
+ end
26
+
27
+ initializer "devise.url_helpers" do
28
+ Devise.include_helpers(Devise::Controllers)
29
+ end
30
+
31
+ initializer "devise.oauth_url_helpers" do
32
+ if Devise.oauth_providers.any?
33
+ Devise.include_helpers(Devise::Oauth)
34
+ end
35
+ end
36
+
37
+ initializer "devise.encryptor_check" do
38
+ case Devise.encryptor
39
+ when :bcrypt
40
+ puts "[DEVISE] From version 1.2, there is no need to set your encryptor to bcrypt " <<
41
+ "since encryptors are only enabled if you include :encryptable in your models. " <<
42
+ "With this change, we can integrate better with bcrypt and get rid of the " <<
43
+ "password_salt column (since bcrypt stores the salt with password). " <<
44
+ "Please comment config.encryptor in your initializer to get rid of this warning."
45
+ when nil
46
+ # Nothing to say
47
+ else
48
+ puts "[DEVISE] You are using #{Devise.encryptor} as encryptor. From version 1.2, " <<
49
+ "you need to explicitly add `devise :encryptable, :encryptor => #{Devise.encryptor.to_sym}` " <<
50
+ "to your models and comment the current value in the config/initializers/devise.rb"
51
+ end
52
+ end
53
+
54
+ # Check all available mappings and only load related controllers.
55
+ def eager_load!
56
+ mappings = Devise.mappings.values.map(&:modules).flatten.uniq
57
+ controllers = Devise::CONTROLLERS.values_at(*mappings)
58
+ path = paths.is_a?(Hash) ? paths["app/controllers"].first : paths.app.controllers.first
59
+ matcher = /\A#{Regexp.escape(path)}\/(.*)\.rb\Z/
60
+
61
+ Dir.glob("#{path}/devise/{#{controllers.join(',')}}_controller.rb").sort.each do |file|
62
+ require_dependency file.sub(matcher, '\1')
63
+ end
64
+
65
+ super
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,260 @@
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
+ def devise_for(*resources)
127
+ options = resources.extract_options!
128
+
129
+ options[:as] ||= @scope[:as] if @scope[:as].present?
130
+ options[:module] ||= @scope[:module] if @scope[:module].present?
131
+ options[:path_prefix] ||= @scope[:path] if @scope[:path].present?
132
+ options[:path_names] = (@scope[:path_names] || {}).merge(options[:path_names] || {})
133
+
134
+ resources.map!(&:to_sym)
135
+
136
+ resources.each do |resource|
137
+ mapping = Devise.add_mapping(resource, options)
138
+
139
+ begin
140
+ raise_no_devise_method_error!(mapping.class_name) unless mapping.to.respond_to?(:devise)
141
+ rescue NameError => e
142
+ raise unless mapping.class_name == resource.to_s.classify
143
+ warn "[WARNING] You provided devise_for #{resource.inspect} but there is " <<
144
+ "no model #{mapping.class_name} defined in your application"
145
+ next
146
+ rescue NoMethodError => e
147
+ raise unless e.message.include?("undefined method `devise'")
148
+ raise_no_devise_method_error!(mapping.class_name)
149
+ end
150
+
151
+ routes = mapping.routes
152
+ routes -= Array(options.delete(:skip)).map { |s| s.to_s.singularize.to_sym }
153
+
154
+ devise_scope mapping.name do
155
+ yield if block_given?
156
+ with_devise_exclusive_scope mapping.fullpath, mapping.name do
157
+ routes.each { |mod| send(:"devise_#{mod}", mapping, mapping.controllers) }
158
+ end
159
+ end
160
+ end
161
+ end
162
+
163
+ # Allow you to add authentication request from the router:
164
+ #
165
+ # authenticate(:user) do
166
+ # resources :post
167
+ # end
168
+ #
169
+ def authenticate(scope)
170
+ constraint = lambda do |request|
171
+ request.env["warden"].authenticate!(:scope => scope)
172
+ end
173
+
174
+ constraints(constraint) do
175
+ yield
176
+ end
177
+ end
178
+
179
+ # Sets the devise scope to be used in the controller. If you have custom routes,
180
+ # you are required to call this method (also aliased as :as) in order to specify
181
+ # to which controller it is targetted.
182
+ #
183
+ # as :user do
184
+ # get "sign_in", :to => "devise/sessions#new"
185
+ # end
186
+ #
187
+ # Notice you cannot have two scopes mapping to the same URL. And remember, if
188
+ # you try to access a devise controller without specifying a scope, it will
189
+ # raise ActionNotFound error.
190
+ def devise_scope(scope)
191
+ constraint = lambda do |request|
192
+ request.env["devise.mapping"] = Devise.mappings[scope]
193
+ true
194
+ end
195
+
196
+ constraints(constraint) do
197
+ yield
198
+ end
199
+ end
200
+ alias :as :devise_scope
201
+
202
+ protected
203
+
204
+ def devise_session(mapping, controllers) #:nodoc:
205
+ resource :session, :only => [], :controller => controllers[:sessions], :path => "" do
206
+ get :new, :path => mapping.path_names[:sign_in], :as => "new"
207
+ post :create, :path => mapping.path_names[:sign_in]
208
+ match :destroy, :path => mapping.path_names[:sign_out], :as => "destroy", :via => mapping.sign_out_via
209
+ end
210
+ end
211
+
212
+ def devise_password(mapping, controllers) #:nodoc:
213
+ resource :password, :only => [:new, :create, :edit, :update],
214
+ :path => mapping.path_names[:password], :controller => controllers[:passwords]
215
+ end
216
+
217
+ def devise_confirmation(mapping, controllers) #:nodoc:
218
+ resource :confirmation, :only => [:new, :create, :show],
219
+ :path => mapping.path_names[:confirmation], :controller => controllers[:confirmations]
220
+ end
221
+
222
+ def devise_unlock(mapping, controllers) #:nodoc:
223
+ if mapping.to.unlock_strategy_enabled?(:email)
224
+ resource :unlock, :only => [:new, :create, :show],
225
+ :path => mapping.path_names[:unlock], :controller => controllers[:unlocks]
226
+ end
227
+ end
228
+
229
+ def devise_registration(mapping, controllers) #:nodoc:
230
+ path_names = {
231
+ :new => mapping.path_names[:sign_up],
232
+ :cancel => mapping.path_names[:cancel]
233
+ }
234
+
235
+ resource :registration, :except => :show, :path => mapping.path_names[:registration],
236
+ :path_names => path_names, :controller => controllers[:registrations] do
237
+ get :cancel
238
+ end
239
+ end
240
+
241
+ def devise_oauth_callback(mapping, controllers) #:nodoc:
242
+ get "/oauth/:action/callback", :action => Regexp.union(mapping.to.oauth_providers.map(&:to_s)),
243
+ :to => controllers[:oauth_callbacks], :as => :oauth_callback
244
+ end
245
+
246
+ def with_devise_exclusive_scope(new_path, new_as) #:nodoc:
247
+ old_as, old_path, old_module = @scope[:as], @scope[:path], @scope[:module]
248
+ @scope[:as], @scope[:path], @scope[:module] = new_as, new_path, nil
249
+ yield
250
+ ensure
251
+ @scope[:as], @scope[:path], @scope[:module] = old_as, old_path, old_module
252
+ end
253
+
254
+ def raise_no_devise_method_error!(klass) #:nodoc:
255
+ raise "#{klass} does not respond to 'devise' method. This usually means you haven't " <<
256
+ "loaded your ORM file or it's being loaded too late. To fix it, be sure to require 'devise/orm/YOUR_ORM' " <<
257
+ "inside 'config/initializers/devise.rb' or before your application definition in 'config/application.rb'"
258
+ end
259
+ end
260
+ end