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,17 @@
1
+ require "digest/sha1"
2
+
3
+ module Devise
4
+ module Encryptors
5
+ # = ClearanceSha1
6
+ # Simulates Clearance's default encryption mechanism.
7
+ # Warning: it uses Devise's pepper to port the concept of REST_AUTH_SITE_KEY
8
+ # Warning: it uses Devise's stretches configuration to port the concept of REST_AUTH_DIGEST_STRETCHES
9
+ class ClearanceSha1 < Base
10
+ # Gererates a default password digest based on salt, pepper and the
11
+ # incoming password.
12
+ def self.digest(password, stretches, salt, pepper)
13
+ Digest::SHA1.hexdigest("--#{salt}--#{password}--")
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+ require "digest/sha1"
2
+
3
+ module Devise
4
+ module Encryptors
5
+ # = RestfulAuthenticationSha1
6
+ # Simulates Restful Authentication's default encryption mechanism.
7
+ # Warning: it uses Devise's pepper to port the concept of REST_AUTH_SITE_KEY
8
+ # Warning: it uses Devise's stretches configuration to port the concept of REST_AUTH_DIGEST_STRETCHES. Should be set to 10 in
9
+ # the initializer to simulate the default behavior.
10
+ class RestfulAuthenticationSha1 < Base
11
+
12
+ # Gererates a default password digest based on salt, pepper and the
13
+ # incoming password.
14
+ def self.digest(password, stretches, salt, pepper)
15
+ digest = pepper
16
+ stretches.times { digest = Digest::SHA1.hexdigest([digest, salt, password, pepper].flatten.join('--')) }
17
+ digest
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,25 @@
1
+ require "digest/sha1"
2
+
3
+ module Devise
4
+ module Encryptors
5
+ # = Sha1
6
+ # Uses the Sha1 hash algorithm to encrypt passwords.
7
+ class Sha1 < Base
8
+ # Gererates a default password digest based on stretches, salt, pepper and the
9
+ # incoming password.
10
+ def self.digest(password, stretches, salt, pepper)
11
+ digest = pepper
12
+ stretches.times { digest = self.secure_digest(salt, digest, password, pepper) }
13
+ digest
14
+ end
15
+
16
+ private
17
+
18
+ # Generate a SHA1 digest joining args. Generated token is something like
19
+ # --arg1--arg2--arg3--argN--
20
+ def self.secure_digest(*tokens)
21
+ ::Digest::SHA1.hexdigest('--' << tokens.flatten.join('--') << '--')
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ require "digest/sha2"
2
+
3
+ module Devise
4
+ module Encryptors
5
+ # = Sha512
6
+ # Uses the Sha512 hash algorithm to encrypt passwords.
7
+ class Sha512 < Base
8
+ # Gererates a default password digest based on salt, pepper and the
9
+ # incoming password.
10
+ def self.digest(password, stretches, salt, pepper)
11
+ digest = pepper
12
+ stretches.times { digest = self.secure_digest(salt, digest, password, pepper) }
13
+ digest
14
+ end
15
+
16
+ private
17
+
18
+ # Generate a Sha512 digest joining args. Generated token is something like
19
+ # --arg1--arg2--arg3--argN--
20
+ def self.secure_digest(*tokens)
21
+ ::Digest::SHA512.hexdigest('--' << tokens.flatten.join('--') << '--')
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,126 @@
1
+ require "action_controller/metal"
2
+
3
+ module Devise
4
+ # Failure application that will be called every time :warden is thrown from
5
+ # any strategy or hook. Responsible for redirect the user to the sign in
6
+ # page based on current scope and mapping. If no scope is given, redirect
7
+ # to the default_url.
8
+ class FailureApp < ActionController::Metal
9
+ include ActionController::RackDelegation
10
+ include ActionController::UrlFor
11
+ include ActionController::Redirecting
12
+ include Rails.application.routes.url_helpers
13
+
14
+ delegate :flash, :to => :request
15
+
16
+ def self.call(env)
17
+ action(:respond).call(env)
18
+ end
19
+
20
+ def self.default_url_options(*args)
21
+ ApplicationController.default_url_options(*args)
22
+ end
23
+
24
+ def respond
25
+ if http_auth?
26
+ http_auth
27
+ elsif warden_options[:recall]
28
+ recall
29
+ else
30
+ redirect
31
+ end
32
+ end
33
+
34
+ def http_auth
35
+ self.status = 401
36
+ self.headers["WWW-Authenticate"] = %(Basic realm=#{Devise.http_authentication_realm.inspect}) if http_auth_header?
37
+ self.content_type = request.format.to_s
38
+ self.response_body = http_auth_body
39
+ end
40
+
41
+ def recall
42
+ env["PATH_INFO"] = attempted_path
43
+ flash.now[:alert] = i18n_message(:invalid)
44
+ self.response = recall_app(warden_options[:recall]).call(env)
45
+ end
46
+
47
+ def redirect
48
+ store_location!
49
+ flash[:alert] = i18n_message
50
+ redirect_to redirect_url
51
+ end
52
+
53
+ protected
54
+
55
+ def i18n_message(default = nil)
56
+ message = warden.message || warden_options[:message] || default || :unauthenticated
57
+
58
+ if message.is_a?(Symbol)
59
+ I18n.t(:"#{scope}.#{message}", :resource_name => scope,
60
+ :scope => "devise.failure", :default => [message, message.to_s])
61
+ else
62
+ message.to_s
63
+ end
64
+ end
65
+
66
+ def redirect_url
67
+ send(:"new_#{scope}_session_path")
68
+ end
69
+
70
+ # Choose whether we should respond in a http authentication fashion,
71
+ # including 401 and optional headers.
72
+ #
73
+ # This method allows the user to explicitly disable http authentication
74
+ # on ajax requests in case they want to redirect on failures instead of
75
+ # handling the errors on their own. This is useful in case your ajax API
76
+ # is the same as your public API and uses a format like JSON (so you
77
+ # cannot mark JSON as a navigational format).
78
+ def http_auth?
79
+ if request.xhr?
80
+ Devise.http_authenticatable_on_xhr
81
+ else
82
+ !Devise.navigational_formats.include?(request.format.to_sym)
83
+ end
84
+ end
85
+
86
+ # It does not make sense to send authenticate headers in ajax requests
87
+ # or if the user disabled them.
88
+ def http_auth_header?
89
+ Devise.mappings[scope].to.http_authenticatable && !request.xhr?
90
+ end
91
+
92
+ def http_auth_body
93
+ method = :"to_#{request.format.to_sym}"
94
+ {}.respond_to?(method) ? { :error => i18n_message }.send(method) : i18n_message
95
+ end
96
+
97
+ def recall_app(app)
98
+ controller, action = app.split("#")
99
+ "#{controller.camelize}Controller".constantize.action(action)
100
+ end
101
+
102
+ def warden
103
+ env['warden']
104
+ end
105
+
106
+ def warden_options
107
+ env['warden.options']
108
+ end
109
+
110
+ def scope
111
+ @scope ||= warden_options[:scope] || Devise.default_scope
112
+ end
113
+
114
+ def attempted_path
115
+ warden_options[:attempted_path]
116
+ end
117
+
118
+ # Stores requested uri to redirect the user after signing in. We cannot use
119
+ # scoped session provided by warden here, since the user is not authenticated
120
+ # yet, but we still need to store the uri based on scope, so different scopes
121
+ # would never use the same uri to redirect.
122
+ def store_location!
123
+ session["#{scope}_return_to"] = attempted_path if request.get? && !http_auth?
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,11 @@
1
+ # Deny user access whenever his account is not active yet. All strategies that inherits from
2
+ # Devise::Strategies::Authenticatable and uses the validate already check if the user is active?
3
+ # before actively signing him in. However, we need this as hook to validate the user activity
4
+ # in each request and in case the user is using other strategies beside Devise ones.
5
+ Warden::Manager.after_set_user do |record, warden, options|
6
+ if record && record.respond_to?(:active?) && !record.active?
7
+ scope = options[:scope]
8
+ warden.logout(scope)
9
+ throw :warden, :scope => scope, :message => record.inactive_message
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ # Before logout hook to forget the user in the given scope, if it responds
2
+ # to forget_me! Also clear remember token to ensure the user won't be
3
+ # remembered again. Notice that we forget the user unless the record is frozen.
4
+ # This avoids forgetting deleted users.
5
+ Warden::Manager.before_logout do |record, warden, options|
6
+ if record.respond_to?(:forget_me!)
7
+ record.forget_me! unless record.frozen?
8
+ cookie_options = Rails.configuration.session_options.slice(:path, :domain, :secure)
9
+ cookie_options.merge!(record.cookie_options)
10
+ warden.cookies.delete("remember_#{options[:scope]}_token", cookie_options)
11
+ end
12
+ end
@@ -0,0 +1,45 @@
1
+ module Devise
2
+ module Hooks
3
+ # Overwrite success! in authentication strategies allowing users to be remembered.
4
+ # We choose to implement this as an strategy hook instead of a warden hook to allow a specific
5
+ # strategy (like token authenticatable or facebook authenticatable) to turn off remember_me?
6
+ # cookies.
7
+ module Rememberable #:nodoc:
8
+ def success!(resource)
9
+ super
10
+
11
+ if succeeded? && resource.respond_to?(:remember_me!) && remember_me?
12
+ resource.remember_me!(extend_remember_period?)
13
+ cookies.signed["remember_#{scope}_token"] = cookie_values(resource)
14
+ end
15
+ end
16
+
17
+ protected
18
+
19
+ def cookie_values(resource)
20
+ options = Rails.configuration.session_options.slice(:path, :domain, :secure)
21
+ options.merge!(resource.cookie_options)
22
+ options.merge!(
23
+ :value => resource.class.serialize_into_cookie(resource),
24
+ :expires => resource.remember_expires_at
25
+ )
26
+ options
27
+ end
28
+
29
+ def succeeded?
30
+ @result == :success
31
+ end
32
+
33
+ def extend_remember_period?
34
+ false
35
+ end
36
+
37
+ def remember_me?
38
+ valid_params? && Devise::TRUE_VALUES.include?(params_auth_hash[:remember_me])
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ Devise::Strategies::Authenticatable.send :include, Devise::Hooks::Rememberable
45
+
@@ -0,0 +1,22 @@
1
+ # Each time a record is set we check whether its session has already timed out
2
+ # or not, based on last request time. If so, the record is logged out and
3
+ # redirected to the sign in page. Also, each time the request comes and the
4
+ # record is set, we set the last request time inside it's scoped session to
5
+ # verify timeout in the following request.
6
+ Warden::Manager.after_set_user do |record, warden, options|
7
+ scope = options[:scope]
8
+
9
+ if record && record.respond_to?(:timedout?) && warden.authenticated?(scope)
10
+ last_request_at = warden.session(scope)['last_request_at']
11
+
12
+ if record.timedout?(last_request_at)
13
+ path_checker = Devise::PathChecker.new(warden.env, scope)
14
+ unless path_checker.signing_out?
15
+ warden.logout(scope)
16
+ throw :warden, :scope => scope, :message => :timeout
17
+ end
18
+ end
19
+
20
+ warden.session(scope)['last_request_at'] = Time.now.utc
21
+ end
22
+ end
@@ -0,0 +1,9 @@
1
+ # After each sign in, update sign in time, sign in count and sign in IP.
2
+ # This is only triggered when the user is explicitly set (with set_user)
3
+ # and on authentication. Retrieving the user from session (:fetch) does
4
+ # not trigger it.
5
+ Warden::Manager.after_set_user :except => :fetch do |record, warden, options|
6
+ if record.respond_to?(:update_tracked_fields!) && warden.authenticated?(options[:scope])
7
+ record.update_tracked_fields!(warden.request)
8
+ end
9
+ end
@@ -0,0 +1,105 @@
1
+ module Devise
2
+ # Responsible for handling devise mappings and routes configuration. Each
3
+ # resource configured by devise_for in routes is actually creating a mapping
4
+ # object. You can refer to devise_for in routes for usage options.
5
+ #
6
+ # The required value in devise_for is actually not used internally, but it's
7
+ # inflected to find all other values.
8
+ #
9
+ # map.devise_for :users
10
+ # mapping = Devise.mappings[:user]
11
+ #
12
+ # mapping.name #=> :user
13
+ # # is the scope used in controllers and warden, given in the route as :singular.
14
+ #
15
+ # mapping.as #=> "users"
16
+ # # how the mapping should be search in the path, given in the route as :as.
17
+ #
18
+ # mapping.to #=> User
19
+ # # is the class to be loaded from routes, given in the route as :class_name.
20
+ #
21
+ # mapping.modules #=> [:authenticatable]
22
+ # # is the modules included in the class
23
+ #
24
+ class Mapping #:nodoc:
25
+ attr_reader :singular, :plural, :path, :controllers, :path_names, :class_name, :sign_out_via
26
+ alias :name :singular
27
+
28
+ # Receives an object and find a scope for it. If a scope cannot be found,
29
+ # raises an error. If a symbol is given, it's considered to be the scope.
30
+ def self.find_scope!(duck)
31
+ case duck
32
+ when String, Symbol
33
+ return duck
34
+ when Class
35
+ Devise.mappings.each_value { |m| return m.name if duck <= m.to }
36
+ else
37
+ Devise.mappings.each_value { |m| return m.name if duck.is_a?(m.to) }
38
+ end
39
+
40
+ raise "Could not find a valid mapping for #{duck}"
41
+ end
42
+
43
+ def initialize(name, options) #:nodoc:
44
+ @plural = (options[:as] ? "#{options[:as]}_#{name}" : name).to_sym
45
+ @singular = (options[:singular] || @plural.to_s.singularize).to_sym
46
+
47
+ @class_name = (options[:class_name] || name.to_s.classify).to_s
48
+ @ref = ActiveSupport::Dependencies.ref(@class_name)
49
+
50
+ @path = (options[:path] || name).to_s
51
+ @path_prefix = options[:path_prefix]
52
+
53
+ mod = options[:module] || "devise"
54
+ @controllers = Hash.new { |h,k| h[k] = "#{mod}/#{k}" }
55
+ @controllers.merge!(options[:controllers] || {})
56
+
57
+ @path_names = Hash.new { |h,k| h[k] = k.to_s }
58
+ @path_names.merge!(:registration => "")
59
+ @path_names.merge!(options[:path_names] || {})
60
+
61
+ @sign_out_via = options[:sign_out_via] || Devise.sign_out_via
62
+ end
63
+
64
+ # Return modules for the mapping.
65
+ def modules
66
+ @modules ||= to.respond_to?(:devise_modules) ? to.devise_modules : []
67
+ end
68
+
69
+ # Gives the class the mapping points to.
70
+ def to
71
+ @ref.get
72
+ end
73
+
74
+ def strategies
75
+ @strategies ||= STRATEGIES.values_at(*self.modules).compact.uniq.reverse
76
+ end
77
+
78
+ def routes
79
+ @routes ||= ROUTES.values_at(*self.modules).compact.uniq
80
+ end
81
+
82
+ def authenticatable?
83
+ @authenticatable ||= self.modules.any? { |m| m.to_s =~ /authenticatable/ }
84
+ end
85
+
86
+ def fullpath
87
+ "#{@path_prefix}/#{@path}".squeeze("/")
88
+ end
89
+
90
+ # Create magic predicates for verifying what module is activated by this map.
91
+ # Example:
92
+ #
93
+ # def confirmable?
94
+ # self.modules.include?(:confirmable)
95
+ # end
96
+ #
97
+ def self.add_module(m)
98
+ class_eval <<-METHOD, __FILE__, __LINE__ + 1
99
+ def #{m}?
100
+ self.modules.include?(:#{m})
101
+ end
102
+ METHOD
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,66 @@
1
+ module Devise
2
+ module Models
3
+ # Creates configuration values for Devise and for the given module.
4
+ #
5
+ # Devise::Models.config(Devise::Authenticable, :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 += Devise::ALL & modules.map(&:to_sym).uniq
51
+
52
+ devise_modules_hook! do
53
+ devise_modules.each { |m| include Devise::Models.const_get(m.to_s.classify) }
54
+ options.each { |key, value| send(:"#{key}=", value) }
55
+ end
56
+ end
57
+
58
+ # The hook which is called inside devise. So your ORM can include devise
59
+ # compatibility stuff.
60
+ def devise_modules_hook!
61
+ yield
62
+ end
63
+ end
64
+ end
65
+
66
+ require 'devise/models/authenticatable'