devise-jdguyot 1.2.rc

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 (185) hide show
  1. data/.gitignore +10 -0
  2. data/CHANGELOG.rdoc +532 -0
  3. data/Gemfile +29 -0
  4. data/Gemfile.lock +152 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.rdoc +353 -0
  7. data/Rakefile +36 -0
  8. data/TODO +4 -0
  9. data/app/controllers/devise/confirmations_controller.rb +33 -0
  10. data/app/controllers/devise/omniauth_callbacks_controller.rb +26 -0
  11. data/app/controllers/devise/passwords_controller.rb +41 -0
  12. data/app/controllers/devise/registrations_controller.rb +110 -0
  13. data/app/controllers/devise/sessions_controller.rb +25 -0
  14. data/app/controllers/devise/unlocks_controller.rb +34 -0
  15. data/app/helpers/devise_helper.rb +19 -0
  16. data/app/mailers/devise/mailer.rb +88 -0
  17. data/app/views/devise/confirmations/new.html.erb +12 -0
  18. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  19. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  20. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  21. data/app/views/devise/passwords/edit.html.erb +16 -0
  22. data/app/views/devise/passwords/new.html.erb +12 -0
  23. data/app/views/devise/registrations/edit.html.erb +25 -0
  24. data/app/views/devise/registrations/new.html.erb +18 -0
  25. data/app/views/devise/sessions/new.html.erb +17 -0
  26. data/app/views/devise/shared/_links.erb +25 -0
  27. data/app/views/devise/unlocks/new.html.erb +12 -0
  28. data/config/locales/en.yml +46 -0
  29. data/devise.gemspec +25 -0
  30. data/lib/devise/controllers/helpers.rb +227 -0
  31. data/lib/devise/controllers/internal_helpers.rb +119 -0
  32. data/lib/devise/controllers/scoped_views.rb +33 -0
  33. data/lib/devise/controllers/url_helpers.rb +39 -0
  34. data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
  35. data/lib/devise/encryptors/base.rb +20 -0
  36. data/lib/devise/encryptors/clearance_sha1.rb +17 -0
  37. data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
  38. data/lib/devise/encryptors/sha1.rb +25 -0
  39. data/lib/devise/encryptors/sha512.rb +25 -0
  40. data/lib/devise/failure_app.rb +132 -0
  41. data/lib/devise/hooks/activatable.rb +11 -0
  42. data/lib/devise/hooks/forgetable.rb +12 -0
  43. data/lib/devise/hooks/rememberable.rb +48 -0
  44. data/lib/devise/hooks/timeoutable.rb +22 -0
  45. data/lib/devise/hooks/trackable.rb +9 -0
  46. data/lib/devise/mapping.rb +110 -0
  47. data/lib/devise/models/authenticatable.rb +146 -0
  48. data/lib/devise/models/confirmable.rb +160 -0
  49. data/lib/devise/models/database_authenticatable.rb +100 -0
  50. data/lib/devise/models/encryptable.rb +72 -0
  51. data/lib/devise/models/lockable.rb +169 -0
  52. data/lib/devise/models/omniauthable.rb +23 -0
  53. data/lib/devise/models/recoverable.rb +123 -0
  54. data/lib/devise/models/registerable.rb +21 -0
  55. data/lib/devise/models/rememberable.rb +130 -0
  56. data/lib/devise/models/timeoutable.rb +43 -0
  57. data/lib/devise/models/token_authenticatable.rb +72 -0
  58. data/lib/devise/models/trackable.rb +30 -0
  59. data/lib/devise/models/validatable.rb +65 -0
  60. data/lib/devise/models.rb +68 -0
  61. data/lib/devise/modules.rb +30 -0
  62. data/lib/devise/omniauth/config.rb +30 -0
  63. data/lib/devise/omniauth/test_helpers.rb +57 -0
  64. data/lib/devise/omniauth/url_helpers.rb +29 -0
  65. data/lib/devise/omniauth.rb +47 -0
  66. data/lib/devise/orm/active_record.rb +38 -0
  67. data/lib/devise/orm/mongoid.rb +31 -0
  68. data/lib/devise/path_checker.rb +18 -0
  69. data/lib/devise/rails/routes.rb +292 -0
  70. data/lib/devise/rails/warden_compat.rb +125 -0
  71. data/lib/devise/rails.rb +50 -0
  72. data/lib/devise/schema.rb +97 -0
  73. data/lib/devise/strategies/authenticatable.rb +150 -0
  74. data/lib/devise/strategies/base.rb +15 -0
  75. data/lib/devise/strategies/database_authenticatable.rb +21 -0
  76. data/lib/devise/strategies/rememberable.rb +51 -0
  77. data/lib/devise/strategies/token_authenticatable.rb +53 -0
  78. data/lib/devise/test_helpers.rb +100 -0
  79. data/lib/devise/version.rb +3 -0
  80. data/lib/devise.rb +381 -0
  81. data/lib/generators/active_record/devise_generator.rb +28 -0
  82. data/lib/generators/active_record/templates/migration.rb +31 -0
  83. data/lib/generators/devise/devise_generator.rb +17 -0
  84. data/lib/generators/devise/install_generator.rb +24 -0
  85. data/lib/generators/devise/orm_helpers.rb +23 -0
  86. data/lib/generators/devise/views_generator.rb +106 -0
  87. data/lib/generators/mongoid/devise_generator.rb +17 -0
  88. data/lib/generators/templates/README +25 -0
  89. data/lib/generators/templates/devise.rb +186 -0
  90. data/test/controllers/helpers_test.rb +237 -0
  91. data/test/controllers/internal_helpers_test.rb +72 -0
  92. data/test/controllers/url_helpers_test.rb +59 -0
  93. data/test/devise_test.rb +65 -0
  94. data/test/encryptors_test.rb +30 -0
  95. data/test/failure_app_test.rb +187 -0
  96. data/test/generators/active_record_generator_test.rb +24 -0
  97. data/test/generators/install_generator_test.rb +13 -0
  98. data/test/generators/mongoid_generator_test.rb +22 -0
  99. data/test/generators/views_generator_test.rb +35 -0
  100. data/test/indifferent_hash.rb +33 -0
  101. data/test/integration/authenticatable_test.rb +447 -0
  102. data/test/integration/confirmable_test.rb +104 -0
  103. data/test/integration/database_authenticatable_test.rb +60 -0
  104. data/test/integration/http_authenticatable_test.rb +74 -0
  105. data/test/integration/lockable_test.rb +109 -0
  106. data/test/integration/omniauthable_test.rb +107 -0
  107. data/test/integration/recoverable_test.rb +160 -0
  108. data/test/integration/registerable_test.rb +179 -0
  109. data/test/integration/rememberable_test.rb +180 -0
  110. data/test/integration/timeoutable_test.rb +89 -0
  111. data/test/integration/token_authenticatable_test.rb +99 -0
  112. data/test/integration/trackable_test.rb +64 -0
  113. data/test/mailers/confirmation_instructions_test.rb +84 -0
  114. data/test/mailers/reset_password_instructions_test.rb +72 -0
  115. data/test/mailers/unlock_instructions_test.rb +66 -0
  116. data/test/mapping_test.rb +119 -0
  117. data/test/models/confirmable_test.rb +221 -0
  118. data/test/models/database_authenticatable_test.rb +98 -0
  119. data/test/models/encryptable_test.rb +65 -0
  120. data/test/models/lockable_test.rb +204 -0
  121. data/test/models/recoverable_test.rb +190 -0
  122. data/test/models/rememberable_test.rb +279 -0
  123. data/test/models/timeoutable_test.rb +28 -0
  124. data/test/models/token_authenticatable_test.rb +37 -0
  125. data/test/models/trackable_test.rb +5 -0
  126. data/test/models/validatable_test.rb +99 -0
  127. data/test/models_test.rb +84 -0
  128. data/test/omniauth/url_helpers_test.rb +47 -0
  129. data/test/orm/active_record.rb +9 -0
  130. data/test/orm/mongoid.rb +11 -0
  131. data/test/rails_app/Rakefile +10 -0
  132. data/test/rails_app/app/active_record/admin.rb +6 -0
  133. data/test/rails_app/app/active_record/shim.rb +2 -0
  134. data/test/rails_app/app/active_record/user.rb +8 -0
  135. data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
  136. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  137. data/test/rails_app/app/controllers/application_controller.rb +8 -0
  138. data/test/rails_app/app/controllers/home_controller.rb +16 -0
  139. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  140. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  141. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +7 -0
  142. data/test/rails_app/app/controllers/users_controller.rb +18 -0
  143. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  144. data/test/rails_app/app/mongoid/admin.rb +9 -0
  145. data/test/rails_app/app/mongoid/shim.rb +29 -0
  146. data/test/rails_app/app/mongoid/user.rb +10 -0
  147. data/test/rails_app/app/views/admins/index.html.erb +1 -0
  148. data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
  149. data/test/rails_app/app/views/home/index.html.erb +1 -0
  150. data/test/rails_app/app/views/home/private.html.erb +1 -0
  151. data/test/rails_app/app/views/layouts/application.html.erb +24 -0
  152. data/test/rails_app/app/views/users/index.html.erb +1 -0
  153. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
  154. data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
  155. data/test/rails_app/config/application.rb +40 -0
  156. data/test/rails_app/config/boot.rb +13 -0
  157. data/test/rails_app/config/database.yml +18 -0
  158. data/test/rails_app/config/environment.rb +5 -0
  159. data/test/rails_app/config/environments/development.rb +19 -0
  160. data/test/rails_app/config/environments/production.rb +33 -0
  161. data/test/rails_app/config/environments/test.rb +33 -0
  162. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  163. data/test/rails_app/config/initializers/devise.rb +176 -0
  164. data/test/rails_app/config/initializers/inflections.rb +2 -0
  165. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  166. data/test/rails_app/config/routes.rb +55 -0
  167. data/test/rails_app/config.ru +4 -0
  168. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +31 -0
  169. data/test/rails_app/db/schema.rb +52 -0
  170. data/test/rails_app/lib/shared_admin.rb +9 -0
  171. data/test/rails_app/lib/shared_user.rb +23 -0
  172. data/test/rails_app/public/404.html +26 -0
  173. data/test/rails_app/public/422.html +26 -0
  174. data/test/rails_app/public/500.html +26 -0
  175. data/test/rails_app/public/favicon.ico +0 -0
  176. data/test/rails_app/script/rails +10 -0
  177. data/test/routes_test.rb +179 -0
  178. data/test/support/assertions.rb +24 -0
  179. data/test/support/helpers.rb +60 -0
  180. data/test/support/integration.rb +88 -0
  181. data/test/support/locale/en.yml +4 -0
  182. data/test/support/webrat/integrations/rails.rb +24 -0
  183. data/test/test_helper.rb +29 -0
  184. data/test/test_helpers_test.rb +118 -0
  185. metadata +388 -0
data/lib/devise.rb ADDED
@@ -0,0 +1,381 @@
1
+ require 'active_support/core_ext/numeric/time'
2
+ require 'active_support/dependencies'
3
+ require 'orm_adapter'
4
+ require 'set'
5
+
6
+ module Devise
7
+ autoload :FailureApp, 'devise/failure_app'
8
+ autoload :OmniAuth, 'devise/omniauth'
9
+ autoload :PathChecker, 'devise/path_checker'
10
+ autoload :Schema, 'devise/schema'
11
+ autoload :TestHelpers, 'devise/test_helpers'
12
+
13
+ module Controllers
14
+ autoload :Helpers, 'devise/controllers/helpers'
15
+ autoload :InternalHelpers, 'devise/controllers/internal_helpers'
16
+ autoload :ScopedViews, 'devise/controllers/scoped_views'
17
+ autoload :UrlHelpers, 'devise/controllers/url_helpers'
18
+ end
19
+
20
+ module Encryptors
21
+ autoload :Base, 'devise/encryptors/base'
22
+ autoload :AuthlogicSha512, 'devise/encryptors/authlogic_sha512'
23
+ autoload :ClearanceSha1, 'devise/encryptors/clearance_sha1'
24
+ autoload :RestfulAuthenticationSha1, 'devise/encryptors/restful_authentication_sha1'
25
+ autoload :Sha512, 'devise/encryptors/sha512'
26
+ autoload :Sha1, 'devise/encryptors/sha1'
27
+ end
28
+
29
+ module Strategies
30
+ autoload :Base, 'devise/strategies/base'
31
+ autoload :Authenticatable, 'devise/strategies/authenticatable'
32
+ end
33
+
34
+ # Constants which holds devise configuration for extensions. Those should
35
+ # not be modified by the "end user" (this is why they are constants).
36
+ ALL = []
37
+ CONTROLLERS = ActiveSupport::OrderedHash.new
38
+ ROUTES = ActiveSupport::OrderedHash.new
39
+ STRATEGIES = ActiveSupport::OrderedHash.new
40
+ URL_HELPERS = ActiveSupport::OrderedHash.new
41
+
42
+ # True values used to check params
43
+ TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE']
44
+
45
+ # Declare encryptors length which are used in migrations.
46
+ ENCRYPTORS_LENGTH = {
47
+ :sha1 => 40,
48
+ :sha512 => 128,
49
+ :clearance_sha1 => 40,
50
+ :restful_authentication_sha1 => 40,
51
+ :authlogic_sha512 => 128
52
+ }
53
+
54
+ # Custom domain for cookies. Not set by default
55
+ mattr_accessor :cookie_options
56
+ @@cookie_options = {}
57
+
58
+ # The number of times to encrypt password.
59
+ mattr_accessor :stretches
60
+ @@stretches = 10
61
+
62
+ # Keys used when authenticating a user.
63
+ mattr_accessor :authentication_keys
64
+ @@authentication_keys = [ :email ]
65
+
66
+ # Request keys used when authenticating a user.
67
+ mattr_accessor :request_keys
68
+ @@request_keys = []
69
+
70
+ # Keys that should be case-insensitive.
71
+ # Empty by default for backwards compaibility.
72
+ mattr_accessor :case_insensitive_keys
73
+ @@case_insensitive_keys = [ ]
74
+
75
+ # If http authentication is enabled by default.
76
+ mattr_accessor :http_authenticatable
77
+ @@http_authenticatable = false
78
+
79
+ # If http headers should be returned for ajax requests. True by default.
80
+ mattr_accessor :http_authenticatable_on_xhr
81
+ @@http_authenticatable_on_xhr = true
82
+
83
+ # If params authenticatable is enabled by default.
84
+ mattr_accessor :params_authenticatable
85
+ @@params_authenticatable = true
86
+
87
+ # The realm used in Http Basic Authentication.
88
+ mattr_accessor :http_authentication_realm
89
+ @@http_authentication_realm = "Application"
90
+
91
+ # Email regex used to validate email formats. Adapted from authlogic.
92
+ mattr_accessor :email_regexp
93
+ @@email_regexp = /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
94
+
95
+ # Range validation for password length
96
+ mattr_accessor :password_length
97
+ @@password_length = 6..20
98
+
99
+ # The time the user will be remembered without asking for credentials again.
100
+ mattr_accessor :remember_for
101
+ @@remember_for = 2.weeks
102
+
103
+ # If true, a valid remember token can be re-used between multiple browsers.
104
+ mattr_accessor :remember_across_browsers
105
+ @@remember_across_browsers = true
106
+
107
+ # If true, extends the user's remember period when remembered via cookie.
108
+ mattr_accessor :extend_remember_period
109
+ @@extend_remember_period = false
110
+
111
+ # If true, uses salt as remember token and does not create it in the database.
112
+ # By default is false for backwards compatibility.
113
+ mattr_accessor :use_salt_as_remember_token
114
+ @@use_salt_as_remember_token = false
115
+
116
+ # Time interval you can access your account before confirming your account.
117
+ mattr_accessor :confirm_within
118
+ @@confirm_within = 0.days
119
+
120
+ # Time interval to timeout the user session without activity.
121
+ mattr_accessor :timeout_in
122
+ @@timeout_in = 30.minutes
123
+
124
+ # Used to encrypt password. Please generate one with rake secret.
125
+ mattr_accessor :pepper
126
+ @@pepper = nil
127
+
128
+ # Used to define the password encryption algorithm.
129
+ mattr_accessor :encryptor
130
+ @@encryptor = nil
131
+
132
+ # Tells if devise should apply the schema in ORMs where devise declaration
133
+ # and schema belongs to the same class (as Datamapper and Mongoid).
134
+ mattr_accessor :apply_schema
135
+ @@apply_schema = true
136
+
137
+ # Scoped views. Since it relies on fallbacks to render default views, it's
138
+ # turned off by default.
139
+ mattr_accessor :scoped_views
140
+ @@scoped_views = false
141
+
142
+ # Defines which strategy can be used to lock an account.
143
+ # Values: :failed_attempts, :none
144
+ mattr_accessor :lock_strategy
145
+ @@lock_strategy = :failed_attempts
146
+
147
+ # Defines which key will be used when locking and unlocking an account
148
+ mattr_accessor :unlock_keys
149
+ @@unlock_keys = [ :email ]
150
+
151
+ # Defines which strategy can be used to unlock an account.
152
+ # Values: :email, :time, :both
153
+ mattr_accessor :unlock_strategy
154
+ @@unlock_strategy = :both
155
+
156
+ # Number of authentication tries before locking an account
157
+ mattr_accessor :maximum_attempts
158
+ @@maximum_attempts = 20
159
+
160
+ # Time interval to unlock the account if :time is defined as unlock_strategy.
161
+ mattr_accessor :unlock_in
162
+ @@unlock_in = 1.hour
163
+
164
+ # Defines which key will be used when recovering the password for an account
165
+ mattr_accessor :reset_password_keys
166
+ @@reset_password_keys = [ :email ]
167
+
168
+ # Time interval you can reset your password with a reset password key
169
+ mattr_accessor :reset_password_within
170
+ @@reset_password_within = 1.hour
171
+
172
+ # The default scope which is used by warden.
173
+ mattr_accessor :default_scope
174
+ @@default_scope = nil
175
+
176
+ # Address which sends Devise e-mails.
177
+ mattr_accessor :mailer_sender
178
+ @@mailer_sender = nil
179
+
180
+ # Authentication token params key name of choice. E.g. /users/sign_in?some_key=...
181
+ mattr_accessor :token_authentication_key
182
+ @@token_authentication_key = :auth_token
183
+
184
+ # If true, authentication through token does not store user in session
185
+ mattr_accessor :stateless_token
186
+ @@stateless_token = false
187
+
188
+ # Which formats should be treated as navigational.
189
+ mattr_accessor :navigational_formats
190
+ @@navigational_formats = [:"*/*", :html]
191
+
192
+ # When set to true, signing out an user signs out all other scopes.
193
+ mattr_accessor :sign_out_all_scopes
194
+ @@sign_out_all_scopes = true
195
+
196
+ # The default method used while signing out
197
+ mattr_accessor :sign_out_via
198
+ @@sign_out_via = :get
199
+
200
+ # PRIVATE CONFIGURATION
201
+
202
+ # Store scopes mappings.
203
+ mattr_reader :mappings
204
+ @@mappings = ActiveSupport::OrderedHash.new
205
+
206
+ # Omniauth configurations.
207
+ mattr_reader :omniauth_configs
208
+ @@omniauth_configs = ActiveSupport::OrderedHash.new
209
+
210
+ # Define a set of modules that are called when a mapping is added.
211
+ mattr_reader :helpers
212
+ @@helpers = Set.new
213
+ @@helpers << Devise::Controllers::Helpers
214
+
215
+ # Private methods to interface with Warden.
216
+ mattr_accessor :warden_config
217
+ @@warden_config = nil
218
+ @@warden_config_block = nil
219
+
220
+ # Default way to setup Devise. Run rails generate devise_install to create
221
+ # a fresh initializer with all configuration values.
222
+ def self.setup
223
+ yield self
224
+ end
225
+
226
+ def self.omniauth_providers
227
+ omniauth_configs.keys
228
+ end
229
+
230
+ def self.cookie_domain=(value)
231
+ ActiveSupport::Deprecation.warn "Devise.cookie_domain=(value) is deprecated. "
232
+ "Please use Devise.cookie_options = { :domain => value } instead."
233
+ self.cookie_options[:domain] = value
234
+ end
235
+
236
+ # Get the mailer class from the mailer reference object.
237
+ def self.mailer
238
+ @@mailer_ref.get
239
+ end
240
+
241
+ # Set the mailer reference object to access the mailer.
242
+ def self.mailer=(class_name)
243
+ @@mailer_ref = ActiveSupport::Dependencies.ref(class_name)
244
+ end
245
+ self.mailer = "Devise::Mailer"
246
+
247
+ # Small method that adds a mapping to Devise.
248
+ def self.add_mapping(resource, options)
249
+ mapping = Devise::Mapping.new(resource, options)
250
+ @@mappings[mapping.name] = mapping
251
+ @@default_scope ||= mapping.name
252
+ @@helpers.each { |h| h.define_helpers(mapping) }
253
+ mapping
254
+ end
255
+
256
+ # Make Devise aware of an 3rd party Devise-module (like invitable). For convenience.
257
+ #
258
+ # == Options:
259
+ #
260
+ # +model+ - String representing the load path to a custom *model* for this module (to autoload.)
261
+ # +controller+ - Symbol representing the name of an exisiting or custom *controller* for this module.
262
+ # +route+ - Symbol representing the named *route* helper for this module.
263
+ # +strategy+ - Symbol representing if this module got a custom *strategy*.
264
+ #
265
+ # All values, except :model, accept also a boolean and will have the same name as the given module
266
+ # name.
267
+ #
268
+ # == Examples:
269
+ #
270
+ # Devise.add_module(:party_module)
271
+ # Devise.add_module(:party_module, :strategy => true, :controller => :sessions)
272
+ # Devise.add_module(:party_module, :model => 'party_module/model')
273
+ #
274
+ def self.add_module(module_name, options = {})
275
+ ALL << module_name
276
+ options.assert_valid_keys(:strategy, :model, :controller, :route)
277
+
278
+ if strategy = options[:strategy]
279
+ STRATEGIES[module_name] = (strategy == true ? module_name : strategy)
280
+ end
281
+
282
+ if controller = options[:controller]
283
+ CONTROLLERS[module_name] = (controller == true ? module_name : controller)
284
+ end
285
+
286
+ if route = options[:route]
287
+ case route
288
+ when TrueClass
289
+ key, value = module_name, []
290
+ when Symbol
291
+ key, value = route, []
292
+ when Hash
293
+ key, value = route.keys.first, route.values.flatten
294
+ else
295
+ raise ArgumentError, ":route should be true, a Symbol or a Hash"
296
+ end
297
+
298
+ URL_HELPERS[key] ||= []
299
+ URL_HELPERS[key].concat(value)
300
+ URL_HELPERS[key].uniq!
301
+
302
+ ROUTES[module_name] = key
303
+ end
304
+
305
+ if options[:model]
306
+ path = (options[:model] == true ? "devise/models/#{module_name}" : options[:model])
307
+ Devise::Models.send(:autoload, module_name.to_s.camelize.to_sym, path)
308
+ end
309
+
310
+ Devise::Mapping.add_module module_name
311
+ end
312
+
313
+ # Sets warden configuration using a block that will be invoked on warden
314
+ # initialization.
315
+ #
316
+ # Devise.initialize do |config|
317
+ # config.confirm_within = 2.days
318
+ #
319
+ # config.warden do |manager|
320
+ # # Configure warden to use other strategies, like oauth.
321
+ # manager.oauth(:twitter)
322
+ # end
323
+ # end
324
+ def self.warden(&block)
325
+ @@warden_config_block = block
326
+ end
327
+
328
+ # Specify an omniauth provider.
329
+ #
330
+ # config.omniauth :github, APP_ID, APP_SECRET
331
+ #
332
+ def self.omniauth(provider, *args)
333
+ @@helpers << Devise::OmniAuth::UrlHelpers
334
+ @@omniauth_configs[provider] = Devise::OmniAuth::Config.new(provider, args)
335
+ end
336
+
337
+ # Include helpers in the given scope to AC and AV.
338
+ def self.include_helpers(scope)
339
+ ActiveSupport.on_load(:action_controller) do
340
+ include scope::Helpers if defined?(scope::Helpers)
341
+ include scope::UrlHelpers
342
+ end
343
+
344
+ ActiveSupport.on_load(:action_view) do
345
+ include scope::UrlHelpers
346
+ end
347
+ end
348
+
349
+ # Returns true if Rails version is bigger than 3.0.x
350
+ def self.rack_session?
351
+ Rails::VERSION::STRING[0,3] != "3.0"
352
+ end
353
+
354
+ # A method used internally to setup warden manager from the Rails initialize
355
+ # block.
356
+ def self.configure_warden! #:nodoc:
357
+ @@warden_configured ||= begin
358
+ warden_config.failure_app = Devise::FailureApp
359
+ warden_config.default_scope = Devise.default_scope
360
+ warden_config.intercept_401 = false
361
+
362
+ Devise.mappings.each_value do |mapping|
363
+ warden_config.scope_defaults mapping.name, :strategies => mapping.strategies
364
+ end
365
+
366
+ @@warden_config_block.try :call, Devise.warden_config
367
+ true
368
+ end
369
+ end
370
+
371
+ # Generate a friendly string randomically to be used as token.
372
+ def self.friendly_token
373
+ ActiveSupport::SecureRandom.base64(44).tr('+/=', 'xyz')
374
+ end
375
+ end
376
+
377
+ require 'warden'
378
+ require 'devise/mapping'
379
+ require 'devise/models'
380
+ require 'devise/modules'
381
+ require 'devise/rails'
@@ -0,0 +1,28 @@
1
+ require 'rails/generators/active_record'
2
+ require 'generators/devise/orm_helpers'
3
+
4
+ module ActiveRecord
5
+ module Generators
6
+ class DeviseGenerator < ActiveRecord::Generators::Base
7
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
8
+
9
+ include Devise::Generators::OrmHelpers
10
+ source_root File.expand_path("../templates", __FILE__)
11
+
12
+ def generate_model
13
+ invoke "active_record:model", [name], :migration => false unless model_exists? && behavior == :invoke
14
+ end
15
+
16
+ def copy_devise_migration
17
+ migration_template "migration.rb", "db/migrate/devise_create_#{table_name}"
18
+ end
19
+
20
+ def inject_devise_content
21
+ inject_into_class(model_path, class_name, model_contents + <<CONTENT) if model_exists?
22
+ # Setup accessible (or protected) attributes for your model
23
+ attr_accessible :email, :password, :password_confirmation, :remember_me
24
+ CONTENT
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,31 @@
1
+ class DeviseCreate<%= table_name.camelize %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table(:<%= table_name %>) do |t|
4
+ t.database_authenticatable :null => false
5
+ t.recoverable
6
+ t.rememberable
7
+ t.trackable
8
+
9
+ # t.encryptable
10
+ # t.confirmable
11
+ # t.lockable :lock_strategy => :<%= Devise.lock_strategy %>, :unlock_strategy => :<%= Devise.unlock_strategy %>
12
+ # t.token_authenticatable
13
+
14
+ <% for attribute in attributes -%>
15
+ t.<%= attribute.type %> :<%= attribute.name %>
16
+ <% end -%>
17
+
18
+ t.timestamps
19
+ end
20
+
21
+ add_index :<%= table_name %>, :email, :unique => true
22
+ add_index :<%= table_name %>, :reset_password_token, :unique => true
23
+ # add_index :<%= table_name %>, :confirmation_token, :unique => true
24
+ # add_index :<%= table_name %>, :unlock_token, :unique => true
25
+ # add_index :<%= table_name %>, :authentication_token, :unique => true
26
+ end
27
+
28
+ def self.down
29
+ drop_table :<%= table_name %>
30
+ end
31
+ end
@@ -0,0 +1,17 @@
1
+ module Devise
2
+ module Generators
3
+ class DeviseGenerator < Rails::Generators::NamedBase
4
+ namespace "devise"
5
+ source_root File.expand_path("../templates", __FILE__)
6
+
7
+ desc "Generates a model with the given NAME (if one does not exist) with devise " <<
8
+ "configuration plus a migration file and devise routes."
9
+
10
+ hook_for :orm
11
+
12
+ def add_devise_routes
13
+ route "devise_for :#{table_name}"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,24 @@
1
+ require 'active_support/secure_random'
2
+
3
+ module Devise
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ source_root File.expand_path("../../templates", __FILE__)
7
+
8
+ desc "Creates a Devise initializer and copy locale files to your application."
9
+ class_option :orm
10
+
11
+ def copy_initializer
12
+ template "devise.rb", "config/initializers/devise.rb"
13
+ end
14
+
15
+ def copy_locale
16
+ copy_file "../../../config/locales/en.yml", "config/locales/devise.en.yml"
17
+ end
18
+
19
+ def show_readme
20
+ readme "README" if behavior == :invoke
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ module Devise
2
+ module Generators
3
+ module OrmHelpers
4
+ def model_contents
5
+ <<-CONTENT
6
+ # Include default devise modules. Others available are:
7
+ # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
8
+ devise :database_authenticatable, :registerable,
9
+ :recoverable, :rememberable, :trackable, :validatable
10
+
11
+ CONTENT
12
+ end
13
+
14
+ def model_exists?
15
+ File.exists?(File.join(destination_root, model_path))
16
+ end
17
+
18
+ def model_path
19
+ @model_path ||= File.join("app", "models", "#{file_path}.rb")
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,106 @@
1
+ require 'tmpdir'
2
+
3
+ module Devise
4
+ module Generators
5
+ class ViewsGenerator < Rails::Generators::Base
6
+ source_root File.expand_path("../../../../app/views", __FILE__)
7
+ desc "Copies all Devise views to your application."
8
+
9
+ argument :scope, :required => false, :default => nil,
10
+ :desc => "The scope to copy views to"
11
+
12
+ class_option :template_engine, :type => :string, :aliases => "-t",
13
+ :desc => "Template engine for the views. Available options are 'erb', 'haml' and 'slim'."
14
+
15
+ def copy_views
16
+ case options[:template_engine].to_s
17
+ when "haml"
18
+ verify_haml_existence
19
+ verify_haml_version
20
+ create_and_copy_haml_views
21
+ when "slim"
22
+ verify_haml_existence
23
+ verify_haml_version
24
+ verify_haml2slim_existence
25
+ verify_haml2slim_version
26
+ create_and_copy_slim_views
27
+ else
28
+ directory "devise", "app/views/#{scope || :devise}"
29
+ end
30
+ end
31
+
32
+ protected
33
+
34
+ def verify_haml_existence
35
+ begin
36
+ require 'haml'
37
+ rescue LoadError
38
+ say "Haml is not installed, or it is not specified in your Gemfile."
39
+ exit
40
+ end
41
+ end
42
+
43
+ def verify_haml2slim_existence
44
+ begin
45
+ require 'haml2slim'
46
+ rescue LoadError
47
+ say "Haml2Slim is not installed, or it is not specified in your Gemfile."
48
+ exit
49
+ end
50
+ end
51
+
52
+ def verify_haml_version
53
+ unless Haml.version[:major] == 2 && Haml.version[:minor] >= 3 || Haml.version[:major] >= 3
54
+ say "To generate Haml or Slim templates, you need to have Haml 2.3 or above installed."
55
+ exit
56
+ end
57
+ end
58
+
59
+ def verify_haml2slim_version
60
+ unless Haml2Slim::VERSION.to_f >= '0.4.0'.to_f
61
+ say "To generate Slim templates, you need to have Haml2Slim 0.4.0 or above installed."
62
+ exit
63
+ end
64
+ end
65
+
66
+ def create_and_copy_haml_views
67
+ directory haml_tmp_root, "app/views/#{scope || :devise}"
68
+ FileUtils.rm_rf(haml_tmp_root)
69
+ end
70
+
71
+ def create_and_copy_slim_views
72
+ slim_tmp_root = Dir.mktmpdir("devise-slim.")
73
+ `haml2slim #{haml_tmp_root} #{slim_tmp_root}`
74
+
75
+ directory slim_tmp_root, "app/views/#{scope || :devise}"
76
+
77
+ FileUtils.rm_rf(haml_tmp_root)
78
+ FileUtils.rm_rf(slim_tmp_root)
79
+ end
80
+
81
+ private
82
+
83
+ def create_haml_views
84
+ @haml_tmp_root ||= begin
85
+ html_root = "#{self.class.source_root}/devise"
86
+ haml_tmp_root = Dir.mktmpdir("devise-haml.")
87
+
88
+ Dir["#{html_root}/**/*"].each do |path|
89
+ relative_path = path.sub(html_root, "")
90
+ source_path = (haml_tmp_root + relative_path).sub(/erb$/, "haml")
91
+
92
+ if File.directory?(path)
93
+ FileUtils.mkdir_p(source_path)
94
+ else
95
+ `html2haml -r #{path} #{source_path}`
96
+ end
97
+ end
98
+
99
+ haml_tmp_root
100
+ end
101
+ end
102
+
103
+ alias :haml_tmp_root :create_haml_views
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,17 @@
1
+ require 'generators/devise/orm_helpers'
2
+
3
+ module Mongoid
4
+ module Generators
5
+ class DeviseGenerator < Rails::Generators::NamedBase
6
+ include Devise::Generators::OrmHelpers
7
+
8
+ def generate_model
9
+ invoke "mongoid:model", [name] unless model_exists? && behavior == :invoke
10
+ end
11
+
12
+ def inject_devise_content
13
+ inject_into_file model_path, model_contents, :after => "include Mongoid::Document\n" if model_exists?
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,25 @@
1
+
2
+ ===============================================================================
3
+
4
+ Some setup you must do manually if you haven't yet:
5
+
6
+ 1. Setup default url options for your specific environment. Here is an
7
+ example of development environment:
8
+
9
+ config.action_mailer.default_url_options = { :host => 'localhost:3000' }
10
+
11
+ This is a required Rails configuration. In production it must be the
12
+ actual host of your application
13
+
14
+ 2. Ensure you have defined root_url to *something* in your config/routes.rb.
15
+ For example:
16
+
17
+ root :to => "home#index"
18
+
19
+ 3. Ensure you have flash messages in app/views/layouts/application.html.erb.
20
+ For example:
21
+
22
+ <p class="notice"><%= notice %></p>
23
+ <p class="alert"><%= alert %></p>
24
+
25
+ ===============================================================================