cloudfoundry-devise 1.5.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (210) hide show
  1. data/.gitignore +12 -0
  2. data/.travis.yml +13 -0
  3. data/CHANGELOG.rdoc +755 -0
  4. data/Gemfile +35 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.rdoc +366 -0
  7. data/Rakefile +34 -0
  8. data/app/controllers/devise/confirmations_controller.rb +46 -0
  9. data/app/controllers/devise/omniauth_callbacks_controller.rb +26 -0
  10. data/app/controllers/devise/passwords_controller.rb +50 -0
  11. data/app/controllers/devise/registrations_controller.rb +114 -0
  12. data/app/controllers/devise/sessions_controller.rb +49 -0
  13. data/app/controllers/devise/unlocks_controller.rb +34 -0
  14. data/app/helpers/devise_helper.rb +25 -0
  15. data/app/mailers/devise/mailer.rb +15 -0
  16. data/app/views/devise/confirmations/new.html.erb +12 -0
  17. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  18. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  19. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  20. data/app/views/devise/passwords/edit.html.erb +16 -0
  21. data/app/views/devise/passwords/new.html.erb +12 -0
  22. data/app/views/devise/registrations/edit.html.erb +25 -0
  23. data/app/views/devise/registrations/new.html.erb +18 -0
  24. data/app/views/devise/sessions/new.html.erb +17 -0
  25. data/app/views/devise/shared/_links.erb +25 -0
  26. data/app/views/devise/unlocks/new.html.erb +12 -0
  27. data/cloudfoundry-devise.gemspec +25 -0
  28. data/config/locales/en.yml +59 -0
  29. data/lib/devise.rb +453 -0
  30. data/lib/devise/controllers/helpers.rb +260 -0
  31. data/lib/devise/controllers/internal_helpers.rb +161 -0
  32. data/lib/devise/controllers/rememberable.rb +52 -0
  33. data/lib/devise/controllers/scoped_views.rb +33 -0
  34. data/lib/devise/controllers/shared_helpers.rb +26 -0
  35. data/lib/devise/controllers/url_helpers.rb +53 -0
  36. data/lib/devise/delegator.rb +16 -0
  37. data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
  38. data/lib/devise/encryptors/base.rb +20 -0
  39. data/lib/devise/encryptors/clearance_sha1.rb +17 -0
  40. data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
  41. data/lib/devise/encryptors/sha1.rb +25 -0
  42. data/lib/devise/encryptors/sha512.rb +25 -0
  43. data/lib/devise/failure_app.rb +149 -0
  44. data/lib/devise/hooks/activatable.rb +11 -0
  45. data/lib/devise/hooks/forgetable.rb +9 -0
  46. data/lib/devise/hooks/rememberable.rb +6 -0
  47. data/lib/devise/hooks/timeoutable.rb +24 -0
  48. data/lib/devise/hooks/trackable.rb +9 -0
  49. data/lib/devise/mailers/helpers.rb +86 -0
  50. data/lib/devise/mapping.rb +175 -0
  51. data/lib/devise/models.rb +91 -0
  52. data/lib/devise/models/authenticatable.rb +181 -0
  53. data/lib/devise/models/confirmable.rb +220 -0
  54. data/lib/devise/models/database_authenticatable.rb +122 -0
  55. data/lib/devise/models/encryptable.rb +72 -0
  56. data/lib/devise/models/lockable.rb +169 -0
  57. data/lib/devise/models/omniauthable.rb +23 -0
  58. data/lib/devise/models/recoverable.rb +136 -0
  59. data/lib/devise/models/registerable.rb +21 -0
  60. data/lib/devise/models/rememberable.rb +114 -0
  61. data/lib/devise/models/serializable.rb +43 -0
  62. data/lib/devise/models/timeoutable.rb +45 -0
  63. data/lib/devise/models/token_authenticatable.rb +72 -0
  64. data/lib/devise/models/trackable.rb +30 -0
  65. data/lib/devise/models/validatable.rb +62 -0
  66. data/lib/devise/modules.rb +30 -0
  67. data/lib/devise/omniauth.rb +28 -0
  68. data/lib/devise/omniauth/config.rb +45 -0
  69. data/lib/devise/omniauth/url_helpers.rb +33 -0
  70. data/lib/devise/orm/active_record.rb +44 -0
  71. data/lib/devise/orm/mongoid.rb +31 -0
  72. data/lib/devise/param_filter.rb +41 -0
  73. data/lib/devise/path_checker.rb +18 -0
  74. data/lib/devise/rails.rb +73 -0
  75. data/lib/devise/rails/routes.rb +385 -0
  76. data/lib/devise/rails/warden_compat.rb +120 -0
  77. data/lib/devise/schema.rb +109 -0
  78. data/lib/devise/strategies/authenticatable.rb +155 -0
  79. data/lib/devise/strategies/base.rb +15 -0
  80. data/lib/devise/strategies/database_authenticatable.rb +21 -0
  81. data/lib/devise/strategies/rememberable.rb +53 -0
  82. data/lib/devise/strategies/token_authenticatable.rb +57 -0
  83. data/lib/devise/test_helpers.rb +90 -0
  84. data/lib/devise/version.rb +3 -0
  85. data/lib/generators/active_record/devise_generator.rb +71 -0
  86. data/lib/generators/active_record/templates/migration.rb +29 -0
  87. data/lib/generators/active_record/templates/migration_existing.rb +26 -0
  88. data/lib/generators/devise/devise_generator.rb +22 -0
  89. data/lib/generators/devise/install_generator.rb +24 -0
  90. data/lib/generators/devise/orm_helpers.rb +31 -0
  91. data/lib/generators/devise/views_generator.rb +98 -0
  92. data/lib/generators/mongoid/devise_generator.rb +60 -0
  93. data/lib/generators/templates/README +32 -0
  94. data/lib/generators/templates/devise.rb +215 -0
  95. data/lib/generators/templates/markerb/confirmation_instructions.markerb +5 -0
  96. data/lib/generators/templates/markerb/reset_password_instructions.markerb +8 -0
  97. data/lib/generators/templates/markerb/unlock_instructions.markerb +7 -0
  98. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +15 -0
  99. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
  100. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
  101. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +22 -0
  102. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
  103. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
  104. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +15 -0
  105. data/test/controllers/helpers_test.rb +254 -0
  106. data/test/controllers/internal_helpers_test.rb +96 -0
  107. data/test/controllers/sessions_controller_test.rb +16 -0
  108. data/test/controllers/url_helpers_test.rb +59 -0
  109. data/test/delegator_test.rb +19 -0
  110. data/test/devise_test.rb +72 -0
  111. data/test/encryptors_test.rb +30 -0
  112. data/test/failure_app_test.rb +207 -0
  113. data/test/generators/active_record_generator_test.rb +47 -0
  114. data/test/generators/devise_generator_test.rb +39 -0
  115. data/test/generators/install_generator_test.rb +13 -0
  116. data/test/generators/mongoid_generator_test.rb +23 -0
  117. data/test/generators/views_generator_test.rb +52 -0
  118. data/test/helpers/devise_helper_test.rb +51 -0
  119. data/test/indifferent_hash.rb +33 -0
  120. data/test/integration/authenticatable_test.rb +590 -0
  121. data/test/integration/confirmable_test.rb +262 -0
  122. data/test/integration/database_authenticatable_test.rb +82 -0
  123. data/test/integration/http_authenticatable_test.rb +82 -0
  124. data/test/integration/lockable_test.rb +212 -0
  125. data/test/integration/omniauthable_test.rb +133 -0
  126. data/test/integration/recoverable_test.rb +287 -0
  127. data/test/integration/registerable_test.rb +335 -0
  128. data/test/integration/rememberable_test.rb +158 -0
  129. data/test/integration/timeoutable_test.rb +98 -0
  130. data/test/integration/token_authenticatable_test.rb +148 -0
  131. data/test/integration/trackable_test.rb +92 -0
  132. data/test/mailers/confirmation_instructions_test.rb +95 -0
  133. data/test/mailers/reset_password_instructions_test.rb +83 -0
  134. data/test/mailers/unlock_instructions_test.rb +77 -0
  135. data/test/mapping_test.rb +128 -0
  136. data/test/models/confirmable_test.rb +334 -0
  137. data/test/models/database_authenticatable_test.rb +167 -0
  138. data/test/models/encryptable_test.rb +67 -0
  139. data/test/models/lockable_test.rb +225 -0
  140. data/test/models/recoverable_test.rb +198 -0
  141. data/test/models/rememberable_test.rb +168 -0
  142. data/test/models/serializable_test.rb +38 -0
  143. data/test/models/timeoutable_test.rb +42 -0
  144. data/test/models/token_authenticatable_test.rb +49 -0
  145. data/test/models/trackable_test.rb +5 -0
  146. data/test/models/validatable_test.rb +113 -0
  147. data/test/models_test.rb +109 -0
  148. data/test/omniauth/config_test.rb +57 -0
  149. data/test/omniauth/url_helpers_test.rb +58 -0
  150. data/test/orm/active_record.rb +9 -0
  151. data/test/orm/mongoid.rb +14 -0
  152. data/test/rails_app/Rakefile +10 -0
  153. data/test/rails_app/app/active_record/admin.rb +6 -0
  154. data/test/rails_app/app/active_record/shim.rb +2 -0
  155. data/test/rails_app/app/active_record/user.rb +6 -0
  156. data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
  157. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  158. data/test/rails_app/app/controllers/application_controller.rb +8 -0
  159. data/test/rails_app/app/controllers/home_controller.rb +25 -0
  160. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  161. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  162. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +14 -0
  163. data/test/rails_app/app/controllers/users_controller.rb +23 -0
  164. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  165. data/test/rails_app/app/mailers/users/mailer.rb +3 -0
  166. data/test/rails_app/app/mongoid/admin.rb +24 -0
  167. data/test/rails_app/app/mongoid/shim.rb +24 -0
  168. data/test/rails_app/app/mongoid/user.rb +45 -0
  169. data/test/rails_app/app/views/admins/index.html.erb +1 -0
  170. data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
  171. data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -0
  172. data/test/rails_app/app/views/home/index.html.erb +1 -0
  173. data/test/rails_app/app/views/home/join.html.erb +1 -0
  174. data/test/rails_app/app/views/home/private.html.erb +1 -0
  175. data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -0
  176. data/test/rails_app/app/views/layouts/application.html.erb +24 -0
  177. data/test/rails_app/app/views/users/index.html.erb +1 -0
  178. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
  179. data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
  180. data/test/rails_app/config.ru +4 -0
  181. data/test/rails_app/config/application.rb +41 -0
  182. data/test/rails_app/config/boot.rb +8 -0
  183. data/test/rails_app/config/database.yml +18 -0
  184. data/test/rails_app/config/environment.rb +5 -0
  185. data/test/rails_app/config/environments/development.rb +18 -0
  186. data/test/rails_app/config/environments/production.rb +33 -0
  187. data/test/rails_app/config/environments/test.rb +33 -0
  188. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  189. data/test/rails_app/config/initializers/devise.rb +197 -0
  190. data/test/rails_app/config/initializers/inflections.rb +2 -0
  191. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  192. data/test/rails_app/config/routes.rb +87 -0
  193. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +71 -0
  194. data/test/rails_app/db/schema.rb +52 -0
  195. data/test/rails_app/lib/shared_admin.rb +10 -0
  196. data/test/rails_app/lib/shared_user.rb +26 -0
  197. data/test/rails_app/public/404.html +26 -0
  198. data/test/rails_app/public/422.html +26 -0
  199. data/test/rails_app/public/500.html +26 -0
  200. data/test/rails_app/public/favicon.ico +0 -0
  201. data/test/rails_app/script/rails +10 -0
  202. data/test/routes_test.rb +240 -0
  203. data/test/support/assertions.rb +27 -0
  204. data/test/support/helpers.rb +109 -0
  205. data/test/support/integration.rb +88 -0
  206. data/test/support/locale/en.yml +4 -0
  207. data/test/support/webrat/integrations/rails.rb +24 -0
  208. data/test/test_helper.rb +27 -0
  209. data/test/test_helpers_test.rb +134 -0
  210. metadata +295 -0
@@ -0,0 +1,24 @@
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 its 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) && options[:store] != false
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
+ unless warden.request.env['devise.skip_trackable']
21
+ warden.session(scope)['last_request_at'] = Time.now.utc
22
+ end
23
+ end
24
+ 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]) && !warden.request.env['devise.skip_trackable']
7
+ record.update_tracked_fields!(warden.request)
8
+ end
9
+ end
@@ -0,0 +1,86 @@
1
+ module Devise
2
+ module Mailers
3
+ module Helpers
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ include Devise::Controllers::ScopedViews
8
+ attr_reader :scope_name, :resource
9
+ end
10
+
11
+ protected
12
+
13
+ # Configure default email options
14
+ def devise_mail(record, action)
15
+ initialize_from_record(record)
16
+ mail headers_for(action)
17
+ end
18
+
19
+ def initialize_from_record(record)
20
+ @scope_name = Devise::Mapping.find_scope!(record)
21
+ @resource = instance_variable_set("@#{devise_mapping.name}", record)
22
+ end
23
+
24
+ def devise_mapping
25
+ @devise_mapping ||= Devise.mappings[scope_name]
26
+ end
27
+
28
+ def headers_for(action)
29
+ headers = {
30
+ :subject => translate(devise_mapping, action),
31
+ :from => mailer_sender(devise_mapping),
32
+ :to => resource.email,
33
+ :template_path => template_paths
34
+ }
35
+
36
+ if resource.respond_to?(:headers_for)
37
+ headers.merge!(resource.headers_for(action))
38
+ end
39
+
40
+ unless headers.key?(:reply_to)
41
+ headers[:reply_to] = headers[:from]
42
+ end
43
+
44
+ headers
45
+ end
46
+
47
+ def mailer_sender(mapping)
48
+ if default_params[:from].present?
49
+ default_params[:from]
50
+ elsif Devise.mailer_sender.is_a?(Proc)
51
+ Devise.mailer_sender.call(mapping.name)
52
+ else
53
+ Devise.mailer_sender
54
+ end
55
+ end
56
+
57
+ def template_paths
58
+ template_path = [self.class.mailer_name]
59
+ template_path.unshift "#{@devise_mapping.scoped_path}/mailer" if self.class.scoped_views?
60
+ template_path
61
+ end
62
+
63
+ # Setup a subject doing an I18n lookup. At first, it attemps to set a subject
64
+ # based on the current mapping:
65
+ #
66
+ # en:
67
+ # devise:
68
+ # mailer:
69
+ # confirmation_instructions:
70
+ # user_subject: '...'
71
+ #
72
+ # If one does not exist, it fallbacks to ActionMailer default:
73
+ #
74
+ # en:
75
+ # devise:
76
+ # mailer:
77
+ # confirmation_instructions:
78
+ # subject: '...'
79
+ #
80
+ def translate(mapping, key)
81
+ I18n.t(:"#{mapping.name}_subject", :scope => [:devise, :mailer, key],
82
+ :default => [:subject, key.to_s.humanize])
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,175 @@
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, :scoped_path, :path, :controllers, :path_names,
26
+ :class_name, :sign_out_via, :format, :used_routes, :used_helpers,
27
+ :constraints, :defaults, :failure_app
28
+
29
+ alias :name :singular
30
+
31
+ # Receives an object and find a scope for it. If a scope cannot be found,
32
+ # raises an error. If a symbol is given, it's considered to be the scope.
33
+ def self.find_scope!(duck)
34
+ case duck
35
+ when String, Symbol
36
+ return duck
37
+ when Class
38
+ Devise.mappings.each_value { |m| return m.name if duck <= m.to }
39
+ else
40
+ Devise.mappings.each_value { |m| return m.name if duck.is_a?(m.to) }
41
+ end
42
+
43
+ raise "Could not find a valid mapping for #{duck.inspect}"
44
+ end
45
+
46
+ def self.find_by_path!(path, path_type=:fullpath)
47
+ Devise.mappings.each_value { |m| return m if path.include?(m.send(path_type)) }
48
+ raise "Could not find a valid mapping for path #{path.inspect}"
49
+ end
50
+
51
+ def initialize(name, options) #:nodoc:
52
+ @scoped_path = options[:as] ? "#{options[:as]}/#{name}" : name.to_s
53
+ @singular = (options[:singular] || @scoped_path.tr('/', '_').singularize).to_sym
54
+
55
+ @class_name = (options[:class_name] || name.to_s.classify).to_s
56
+ @klass = Devise.ref(@class_name)
57
+
58
+ @path = (options[:path] || name).to_s
59
+ @path_prefix = options[:path_prefix]
60
+
61
+ @sign_out_via = options[:sign_out_via] || Devise.sign_out_via
62
+ @format = options[:format]
63
+
64
+ default_failure_app(options)
65
+ default_controllers(options)
66
+ default_path_names(options)
67
+ default_constraints(options)
68
+ default_defaults(options)
69
+ default_used_route(options)
70
+ default_used_helpers(options)
71
+ end
72
+
73
+ # Return modules for the mapping.
74
+ def modules
75
+ @modules ||= to.respond_to?(:devise_modules) ? to.devise_modules : []
76
+ end
77
+
78
+ # Gives the class the mapping points to.
79
+ def to
80
+ @klass.get
81
+ end
82
+
83
+ def strategies
84
+ @strategies ||= STRATEGIES.values_at(*self.modules).compact.uniq.reverse
85
+ end
86
+
87
+ def no_input_strategies
88
+ self.strategies & Devise::NO_INPUT
89
+ end
90
+
91
+ def routes
92
+ @routes ||= ROUTES.values_at(*self.modules).compact.uniq
93
+ end
94
+
95
+ def authenticatable?
96
+ @authenticatable ||= self.modules.any? { |m| m.to_s =~ /authenticatable/ }
97
+ end
98
+
99
+ def fullpath
100
+ "/#{@path_prefix}/#{@path}".squeeze("/")
101
+ end
102
+
103
+ # Create magic predicates for verifying what module is activated by this map.
104
+ # Example:
105
+ #
106
+ # def confirmable?
107
+ # self.modules.include?(:confirmable)
108
+ # end
109
+ #
110
+ def self.add_module(m)
111
+ class_eval <<-METHOD, __FILE__, __LINE__ + 1
112
+ def #{m}?
113
+ self.modules.include?(:#{m})
114
+ end
115
+ METHOD
116
+ end
117
+
118
+ private
119
+
120
+ def default_failure_app(options)
121
+ @failure_app = options[:failure_app] || Devise::FailureApp
122
+ if @failure_app.is_a?(String)
123
+ ref = Devise.ref(@failure_app)
124
+ @failure_app = lambda { |env| ref.get.call(env) }
125
+ end
126
+ end
127
+
128
+ def default_controllers(options)
129
+ mod = options[:module] || "devise"
130
+ @controllers = Hash.new { |h,k| h[k] = "#{mod}/#{k}" }
131
+ @controllers.merge!(options[:controllers]) if options[:controllers]
132
+ @controllers.each { |k,v| @controllers[k] = v.to_s }
133
+ end
134
+
135
+ def default_path_names(options)
136
+ @path_names = Hash.new { |h,k| h[k] = k.to_s }
137
+ @path_names[:registration] = ""
138
+ @path_names.merge!(options[:path_names]) if options[:path_names]
139
+ end
140
+
141
+ def default_constraints(options)
142
+ @constraints = Hash.new
143
+ @constraints.merge!(options[:constraints]) if options[:constraints]
144
+ end
145
+
146
+ def default_defaults(options)
147
+ @defaults = Hash.new
148
+ @defaults.merge!(options[:defaults]) if options[:defaults]
149
+ end
150
+
151
+ def default_used_route(options)
152
+ singularizer = lambda { |s| s.to_s.singularize.to_sym }
153
+
154
+ if options.has_key?(:only)
155
+ @used_routes = self.routes & Array(options[:only]).map(&singularizer)
156
+ elsif options[:skip] == :all
157
+ @used_routes = []
158
+ else
159
+ @used_routes = self.routes - Array(options[:skip]).map(&singularizer)
160
+ end
161
+ end
162
+
163
+ def default_used_helpers(options)
164
+ singularizer = lambda { |s| s.to_s.singularize.to_sym }
165
+
166
+ if options[:skip_helpers] == true
167
+ @used_helpers = @used_routes
168
+ elsif skip = options[:skip_helpers]
169
+ @used_helpers = self.routes - Array(skip).map(&singularizer)
170
+ else
171
+ @used_helpers = self.routes
172
+ end
173
+ end
174
+ end
175
+ end
@@ -0,0 +1,91 @@
1
+ module Devise
2
+ module Models
3
+ # Creates configuration values for Devise and for the given module.
4
+ #
5
+ # Devise::Models.config(Devise::Authenticatable, :stretches, 10)
6
+ #
7
+ # The line above creates:
8
+ #
9
+ # 1) An accessor called Devise.stretches, which value is used by default;
10
+ #
11
+ # 2) Some class methods for your model Model.stretches and Model.stretches=
12
+ # which have higher priority than Devise.stretches;
13
+ #
14
+ # 3) And an instance method stretches.
15
+ #
16
+ # To add the class methods you need to have a module ClassMethods defined
17
+ # inside the given class.
18
+ #
19
+ def self.config(mod, *accessors) #:nodoc:
20
+ (class << mod; self; end).send :attr_accessor, :available_configs
21
+ mod.available_configs = accessors
22
+
23
+ accessors.each do |accessor|
24
+ mod.class_eval <<-METHOD, __FILE__, __LINE__ + 1
25
+ def #{accessor}
26
+ if defined?(@#{accessor})
27
+ @#{accessor}
28
+ elsif superclass.respond_to?(:#{accessor})
29
+ superclass.#{accessor}
30
+ else
31
+ Devise.#{accessor}
32
+ end
33
+ end
34
+
35
+ def #{accessor}=(value)
36
+ @#{accessor} = value
37
+ end
38
+ METHOD
39
+ end
40
+ end
41
+
42
+ # Include the chosen devise modules in your model:
43
+ #
44
+ # devise :database_authenticatable, :confirmable, :recoverable
45
+ #
46
+ # You can also give any of the devise configuration values in form of a hash,
47
+ # with specific values for this model. Please check your Devise initializer
48
+ # for a complete description on those values.
49
+ #
50
+ def devise(*modules)
51
+ include Devise::Models::Authenticatable
52
+ options = modules.extract_options!.dup
53
+
54
+ selected_modules = modules.map(&:to_sym).uniq.sort_by do |s|
55
+ Devise::ALL.index(s) || -1 # follow Devise::ALL order
56
+ end
57
+
58
+ devise_modules_hook! do
59
+ selected_modules.each do |m|
60
+ mod = Devise::Models.const_get(m.to_s.classify)
61
+
62
+ if mod.const_defined?("ClassMethods")
63
+ class_mod = mod.const_get("ClassMethods")
64
+ extend class_mod
65
+
66
+ if class_mod.respond_to?(:available_configs)
67
+ available_configs = class_mod.available_configs
68
+ available_configs.each do |config|
69
+ next unless options.key?(config)
70
+ send(:"#{config}=", options.delete(config))
71
+ end
72
+ end
73
+ end
74
+
75
+ include mod
76
+ end
77
+
78
+ self.devise_modules |= selected_modules
79
+ options.each { |key, value| send(:"#{key}=", value) }
80
+ end
81
+ end
82
+
83
+ # The hook which is called inside devise. So your ORM can include devise
84
+ # compatibility stuff.
85
+ def devise_modules_hook!
86
+ yield
87
+ end
88
+ end
89
+ end
90
+
91
+ require 'devise/models/authenticatable'
@@ -0,0 +1,181 @@
1
+ require 'devise/hooks/activatable'
2
+ require 'devise/models/serializable'
3
+
4
+ module Devise
5
+ module Models
6
+ # Authenticatable module. Holds common settings for authentication.
7
+ #
8
+ # == Options
9
+ #
10
+ # Authenticatable adds the following options to devise_for:
11
+ #
12
+ # * +authentication_keys+: parameters used for authentication. By default [:email].
13
+ #
14
+ # * +request_keys+: parameters from the request object used for authentication.
15
+ # By specifying a symbol (which should be a request method), it will automatically be
16
+ # passed to find_for_authentication method and considered in your model lookup.
17
+ #
18
+ # For instance, if you set :request_keys to [:subdomain], :subdomain will be considered
19
+ # as key on authentication. This can also be a hash where the value is a boolean expliciting
20
+ # if the value is required or not.
21
+ #
22
+ # * +http_authenticatable+: if this model allows http authentication. By default true.
23
+ # It also accepts an array specifying the strategies that should allow http.
24
+ #
25
+ # * +params_authenticatable+: if this model allows authentication through request params. By default true.
26
+ # It also accepts an array specifying the strategies that should allow params authentication.
27
+ #
28
+ # == active_for_authentication?
29
+ #
30
+ # After authenticating a user and in each request, Devise checks if your model is active by
31
+ # calling model.active_for_authentication?. This method is overwriten by other devise modules. For instance,
32
+ # :confirmable overwrites .active_for_authentication? to only return true if your model was confirmed.
33
+ #
34
+ # You overwrite this method yourself, but if you do, don't forget to call super:
35
+ #
36
+ # def active_for_authentication?
37
+ # super && special_condition_is_valid?
38
+ # end
39
+ #
40
+ # Whenever active_for_authentication? returns false, Devise asks the reason why your model is inactive using
41
+ # the inactive_message method. You can overwrite it as well:
42
+ #
43
+ # def inactive_message
44
+ # special_condition_is_valid? ? super : :special_condition_is_not_valid
45
+ # end
46
+ #
47
+ module Authenticatable
48
+ extend ActiveSupport::Concern
49
+
50
+ include Devise::Models::Serializable
51
+
52
+ included do
53
+ class_attribute :devise_modules, :instance_writer => false
54
+ self.devise_modules ||= []
55
+
56
+ before_validation :downcase_keys
57
+ before_validation :strip_whitespace
58
+ end
59
+
60
+ # Check if the current object is valid for authentication. This method and
61
+ # find_for_authentication are the methods used in a Warden::Strategy to check
62
+ # if a model should be signed in or not.
63
+ #
64
+ # However, you should not overwrite this method, you should overwrite active_for_authentication?
65
+ # and inactive_message instead.
66
+ def valid_for_authentication?
67
+ block_given? ? yield : true
68
+ end
69
+
70
+ def active_for_authentication?
71
+ true
72
+ end
73
+
74
+ def inactive_message
75
+ :inactive
76
+ end
77
+
78
+ def authenticatable_salt
79
+ end
80
+
81
+ def devise_mailer
82
+ Devise.mailer
83
+ end
84
+
85
+ def headers_for(name)
86
+ {}
87
+ end
88
+
89
+ def downcase_keys
90
+ (self.class.case_insensitive_keys || []).each { |k| self[k].try(:downcase!) }
91
+ end
92
+
93
+ def strip_whitespace
94
+ (self.class.strip_whitespace_keys || []).each { |k| self[k].try(:strip!) }
95
+ end
96
+
97
+ module ClassMethods
98
+ Devise::Models.config(self, :authentication_keys, :request_keys, :strip_whitespace_keys,
99
+ :case_insensitive_keys, :http_authenticatable, :params_authenticatable)
100
+
101
+ def serialize_into_session(record)
102
+ [record.to_key, record.authenticatable_salt]
103
+ end
104
+
105
+ def serialize_from_session(key, salt)
106
+ record = to_adapter.get(key)
107
+ record if record && record.authenticatable_salt == salt
108
+ end
109
+
110
+ def params_authenticatable?(strategy)
111
+ params_authenticatable.is_a?(Array) ?
112
+ params_authenticatable.include?(strategy) : params_authenticatable
113
+ end
114
+
115
+ def http_authenticatable?(strategy)
116
+ http_authenticatable.is_a?(Array) ?
117
+ http_authenticatable.include?(strategy) : http_authenticatable
118
+ end
119
+
120
+ # Find first record based on conditions given (ie by the sign in form).
121
+ # Overwrite to add customized conditions, create a join, or maybe use a
122
+ # namedscope to filter records while authenticating.
123
+ # Example:
124
+ #
125
+ # def self.find_for_authentication(conditions={})
126
+ # conditions[:active] = true
127
+ # super
128
+ # end
129
+ #
130
+ def find_for_authentication(conditions)
131
+ find_first_by_auth_conditions(conditions)
132
+ end
133
+
134
+ def find_first_by_auth_conditions(conditions)
135
+ to_adapter.find_first devise_param_filter.filter(conditions)
136
+ end
137
+
138
+ # Find an initialize a record setting an error if it can't be found.
139
+ def find_or_initialize_with_error_by(attribute, value, error=:invalid) #:nodoc:
140
+ find_or_initialize_with_errors([attribute], { attribute => value }, error)
141
+ end
142
+
143
+ # Find an initialize a group of attributes based on a list of required attributes.
144
+ def find_or_initialize_with_errors(required_attributes, attributes, error=:invalid) #:nodoc:
145
+ attributes = attributes.slice(*required_attributes)
146
+ attributes.delete_if { |key, value| value.blank? }
147
+
148
+ if attributes.size == required_attributes.size
149
+ record = find_first_by_auth_conditions(attributes)
150
+ end
151
+
152
+ unless record
153
+ record = new
154
+
155
+ required_attributes.each do |key|
156
+ value = attributes[key]
157
+ record.send("#{key}=", value)
158
+ record.errors.add(key, value.present? ? error : :blank)
159
+ end
160
+ end
161
+
162
+ record
163
+ end
164
+
165
+ protected
166
+
167
+ def devise_param_filter
168
+ @devise_param_filter ||= Devise::ParamFilter.new(case_insensitive_keys, strip_whitespace_keys)
169
+ end
170
+
171
+ # Generate a token by looping and ensuring does not already exist.
172
+ def generate_token(column)
173
+ loop do
174
+ token = Devise.friendly_token
175
+ break token unless to_adapter.find_first({ column => token })
176
+ end
177
+ end
178
+ end
179
+ end
180
+ end
181
+ end