af-devise 2.1.2

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