rmello-devise 2.1.0.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 (208) hide show
  1. data/.gitignore +12 -0
  2. data/.travis.yml +15 -0
  3. data/CHANGELOG.rdoc +845 -0
  4. data/Gemfile +35 -0
  5. data/Gemfile.lock +165 -0
  6. data/MIT-LICENSE +20 -0
  7. data/README.md +383 -0
  8. data/Rakefile +34 -0
  9. data/app/controllers/devise/confirmations_controller.rb +43 -0
  10. data/app/controllers/devise/omniauth_callbacks_controller.rb +24 -0
  11. data/app/controllers/devise/passwords_controller.rb +47 -0
  12. data/app/controllers/devise/registrations_controller.rb +107 -0
  13. data/app/controllers/devise/sessions_controller.rb +49 -0
  14. data/app/controllers/devise/unlocks_controller.rb +44 -0
  15. data/app/controllers/devise_controller.rb +184 -0
  16. data/app/helpers/devise_helper.rb +25 -0
  17. data/app/mailers/devise/mailer.rb +15 -0
  18. data/app/views/devise/_links.erb +3 -0
  19. data/app/views/devise/confirmations/new.html.erb +12 -0
  20. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  21. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  22. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  23. data/app/views/devise/passwords/edit.html.erb +16 -0
  24. data/app/views/devise/passwords/new.html.erb +12 -0
  25. data/app/views/devise/registrations/edit.html.erb +25 -0
  26. data/app/views/devise/registrations/new.html.erb +18 -0
  27. data/app/views/devise/sessions/new.html.erb +17 -0
  28. data/app/views/devise/shared/_links.erb +25 -0
  29. data/app/views/devise/unlocks/new.html.erb +12 -0
  30. data/config/locales/en.yml +57 -0
  31. data/devise.gemspec +25 -0
  32. data/gemfiles/Gemfile.rails-3.1.x +35 -0
  33. data/gemfiles/Gemfile.rails-3.1.x.lock +167 -0
  34. data/lib/devise.rb +440 -0
  35. data/lib/devise/controllers/helpers.rb +269 -0
  36. data/lib/devise/controllers/rememberable.rb +52 -0
  37. data/lib/devise/controllers/scoped_views.rb +17 -0
  38. data/lib/devise/controllers/url_helpers.rb +67 -0
  39. data/lib/devise/delegator.rb +16 -0
  40. data/lib/devise/failure_app.rb +187 -0
  41. data/lib/devise/hooks/activatable.rb +11 -0
  42. data/lib/devise/hooks/forgetable.rb +9 -0
  43. data/lib/devise/hooks/lockable.rb +7 -0
  44. data/lib/devise/hooks/rememberable.rb +6 -0
  45. data/lib/devise/hooks/timeoutable.rb +22 -0
  46. data/lib/devise/hooks/trackable.rb +9 -0
  47. data/lib/devise/mailers/helpers.rb +86 -0
  48. data/lib/devise/mapping.rb +172 -0
  49. data/lib/devise/models.rb +128 -0
  50. data/lib/devise/models/authenticatable.rb +231 -0
  51. data/lib/devise/models/confirmable.rb +268 -0
  52. data/lib/devise/models/database_authenticatable.rb +126 -0
  53. data/lib/devise/models/lockable.rb +185 -0
  54. data/lib/devise/models/omniauthable.rb +27 -0
  55. data/lib/devise/models/recoverable.rb +140 -0
  56. data/lib/devise/models/registerable.rb +25 -0
  57. data/lib/devise/models/rememberable.rb +125 -0
  58. data/lib/devise/models/timeoutable.rb +49 -0
  59. data/lib/devise/models/token_authenticatable.rb +77 -0
  60. data/lib/devise/models/trackable.rb +35 -0
  61. data/lib/devise/models/validatable.rb +66 -0
  62. data/lib/devise/modules.rb +29 -0
  63. data/lib/devise/omniauth.rb +28 -0
  64. data/lib/devise/omniauth/config.rb +45 -0
  65. data/lib/devise/omniauth/url_helpers.rb +33 -0
  66. data/lib/devise/orm/active_record.rb +3 -0
  67. data/lib/devise/orm/mongoid.rb +3 -0
  68. data/lib/devise/param_filter.rb +41 -0
  69. data/lib/devise/rails.rb +54 -0
  70. data/lib/devise/rails/routes.rb +426 -0
  71. data/lib/devise/rails/warden_compat.rb +43 -0
  72. data/lib/devise/strategies/authenticatable.rb +176 -0
  73. data/lib/devise/strategies/base.rb +15 -0
  74. data/lib/devise/strategies/database_authenticatable.rb +20 -0
  75. data/lib/devise/strategies/rememberable.rb +55 -0
  76. data/lib/devise/strategies/token_authenticatable.rb +56 -0
  77. data/lib/devise/test_helpers.rb +130 -0
  78. data/lib/devise/version.rb +3 -0
  79. data/lib/generators/active_record/devise_generator.rb +75 -0
  80. data/lib/generators/active_record/templates/migration.rb +19 -0
  81. data/lib/generators/active_record/templates/migration_existing.rb +26 -0
  82. data/lib/generators/devise/devise_generator.rb +24 -0
  83. data/lib/generators/devise/install_generator.rb +24 -0
  84. data/lib/generators/devise/orm_helpers.rb +32 -0
  85. data/lib/generators/devise/views_generator.rb +110 -0
  86. data/lib/generators/mongoid/devise_generator.rb +57 -0
  87. data/lib/generators/templates/README +31 -0
  88. data/lib/generators/templates/devise.rb +216 -0
  89. data/lib/generators/templates/markerb/confirmation_instructions.markerb +5 -0
  90. data/lib/generators/templates/markerb/reset_password_instructions.markerb +8 -0
  91. data/lib/generators/templates/markerb/unlock_instructions.markerb +7 -0
  92. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +15 -0
  93. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
  94. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
  95. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +22 -0
  96. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
  97. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
  98. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +15 -0
  99. data/test/controllers/custom_strategy_test.rb +62 -0
  100. data/test/controllers/helpers_test.rb +254 -0
  101. data/test/controllers/internal_helpers_test.rb +104 -0
  102. data/test/controllers/sessions_controller_test.rb +43 -0
  103. data/test/controllers/url_helpers_test.rb +59 -0
  104. data/test/delegator_test.rb +19 -0
  105. data/test/devise_test.rb +72 -0
  106. data/test/failure_app_test.rb +221 -0
  107. data/test/generators/active_record_generator_test.rb +69 -0
  108. data/test/generators/devise_generator_test.rb +39 -0
  109. data/test/generators/install_generator_test.rb +13 -0
  110. data/test/generators/mongoid_generator_test.rb +23 -0
  111. data/test/generators/views_generator_test.rb +52 -0
  112. data/test/helpers/devise_helper_test.rb +51 -0
  113. data/test/indifferent_hash.rb +33 -0
  114. data/test/integration/authenticatable_test.rb +587 -0
  115. data/test/integration/confirmable_test.rb +255 -0
  116. data/test/integration/database_authenticatable_test.rb +82 -0
  117. data/test/integration/http_authenticatable_test.rb +97 -0
  118. data/test/integration/lockable_test.rb +224 -0
  119. data/test/integration/omniauthable_test.rb +133 -0
  120. data/test/integration/recoverable_test.rb +300 -0
  121. data/test/integration/registerable_test.rb +324 -0
  122. data/test/integration/rememberable_test.rb +158 -0
  123. data/test/integration/timeoutable_test.rb +114 -0
  124. data/test/integration/token_authenticatable_test.rb +161 -0
  125. data/test/integration/trackable_test.rb +92 -0
  126. data/test/mailers/confirmation_instructions_test.rb +95 -0
  127. data/test/mailers/reset_password_instructions_test.rb +83 -0
  128. data/test/mailers/unlock_instructions_test.rb +77 -0
  129. data/test/mapping_test.rb +127 -0
  130. data/test/models/authenticatable_test.rb +7 -0
  131. data/test/models/confirmable_test.rb +377 -0
  132. data/test/models/database_authenticatable_test.rb +189 -0
  133. data/test/models/lockable_test.rb +263 -0
  134. data/test/models/omniauthable_test.rb +7 -0
  135. data/test/models/recoverable_test.rb +205 -0
  136. data/test/models/registerable_test.rb +7 -0
  137. data/test/models/rememberable_test.rb +174 -0
  138. data/test/models/serializable_test.rb +48 -0
  139. data/test/models/timeoutable_test.rb +46 -0
  140. data/test/models/token_authenticatable_test.rb +55 -0
  141. data/test/models/trackable_test.rb +13 -0
  142. data/test/models/validatable_test.rb +117 -0
  143. data/test/models_test.rb +179 -0
  144. data/test/omniauth/config_test.rb +57 -0
  145. data/test/omniauth/url_helpers_test.rb +58 -0
  146. data/test/orm/active_record.rb +9 -0
  147. data/test/orm/mongoid.rb +14 -0
  148. data/test/rails_app/Rakefile +10 -0
  149. data/test/rails_app/app/active_record/admin.rb +6 -0
  150. data/test/rails_app/app/active_record/mobile_user.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 +6 -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 +3 -0
  163. data/test/rails_app/app/mongoid/admin.rb +27 -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 +93 -0
  190. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +88 -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_mobile_user.rb +13 -0
  194. data/test/rails_app/lib/shared_user.rb +26 -0
  195. data/test/rails_app/public/404.html +26 -0
  196. data/test/rails_app/public/422.html +26 -0
  197. data/test/rails_app/public/500.html +26 -0
  198. data/test/rails_app/public/favicon.ico +0 -0
  199. data/test/rails_app/script/rails +10 -0
  200. data/test/routes_test.rb +248 -0
  201. data/test/support/assertions.rb +40 -0
  202. data/test/support/helpers.rb +97 -0
  203. data/test/support/integration.rb +90 -0
  204. data/test/support/locale/en.yml +4 -0
  205. data/test/support/webrat/integrations/rails.rb +24 -0
  206. data/test/test_helper.rb +27 -0
  207. data/test/test_helpers_test.rb +134 -0
  208. metadata +425 -0
@@ -0,0 +1,268 @@
1
+ module Devise
2
+ module Models
3
+ # Confirmable is responsible to verify if an account is already confirmed to
4
+ # sign in, and to send emails with confirmation instructions.
5
+ # Confirmation instructions are sent to the user email after creating a
6
+ # record and when manually requested by a new confirmation instruction request.
7
+ #
8
+ # == Options
9
+ #
10
+ # Confirmable adds the following options to devise_for:
11
+ #
12
+ # * +allow_unconfirmed_access_for+: the time you want to allow the user to access his account
13
+ # before confirming it. After this period, the user access is denied. You can
14
+ # use this to let your user access some features of your application without
15
+ # confirming the account, but blocking it after a certain period (ie 7 days).
16
+ # By default allow_unconfirmed_access_for is zero, it means users always have to confirm to sign in.
17
+ # * +reconfirmable+: requires changes to specific attribute (email by default) to be confirmed
18
+ # (exactly the same way as initial account confirmation) to be applied.
19
+ # Requires additional attribute (unconfirmed_email by default) db field to be setup
20
+ # (t.reconfirmable in migrations).
21
+ # Until confirmed new email is stored in unconfirmed email column, and copied to email
22
+ # column on successful confirmation.
23
+ #
24
+ # == Examples
25
+ #
26
+ # User.find(1).confirm! # returns true unless it's already confirmed
27
+ # User.find(1).confirmed? # true/false
28
+ # User.find(1).send_confirmation_instructions # manually send instructions
29
+ #
30
+ module Confirmable
31
+ extend ActiveSupport::Concern
32
+
33
+ included do
34
+ before_create :generate_confirmation_token, :if => :confirmation_required?
35
+ after_create :send_on_create_confirmation_instructions, :if => :confirmation_required?
36
+ before_update :postpone_confirmable_attribute_change_until_confirmation, :if => :postpone_confirmable_attribute_change?
37
+ after_update :send_confirmation_instructions, :if => :reconfirmation_required?
38
+ end
39
+
40
+ def self.required_fields(klass)
41
+ required_methods = [:confirmation_token, :confirmed_at, :confirmation_sent_at]
42
+ required_methods << klass.unconfirmed_attribute if klass.reconfirmable
43
+ required_methods
44
+ end
45
+
46
+ # Confirm a user by setting it's confirmed_at to actual time. If the user
47
+ # is already confirmed, add an error to the proper field. If the user is invalid
48
+ # add errors
49
+ def confirm!
50
+ pending_any_confirmation do
51
+ self.confirmation_token = nil
52
+ self.confirmed_at = Time.now.utc
53
+
54
+ if self.class.reconfirmable && unconfirmed_attribute.present?
55
+ skip_reconfirmation!
56
+ self.confirmable_attribute = self.unconfirmed_attribute
57
+ self.unconfirmed_attribute = nil
58
+
59
+ # We need to validate in such cases to enforce e-mail uniqueness
60
+ save(:validate => true)
61
+ else
62
+ save(:validate => false)
63
+ end
64
+ end
65
+ end
66
+
67
+ # Defines confirmable/unconfirmed attribute accessors
68
+ def confirmable_attribute
69
+ send(self.class.confirmable_attribute)
70
+ end
71
+
72
+ def confirmable_attribute=(_confirmable_attribute)
73
+ send(self.class.confirmable_attribute.to_s + '=', _confirmable_attribute)
74
+ end
75
+
76
+ def confirmable_attribute_was
77
+ send(self.class.confirmable_attribute.to_s + '_was')
78
+ end
79
+
80
+ def unconfirmed_attribute
81
+ send(self.class.unconfirmed_attribute)
82
+ end
83
+
84
+ def unconfirmed_attribute=(_unconfirmed_attribute)
85
+ send(self.class.unconfirmed_attribute.to_s + '=', _unconfirmed_attribute)
86
+ end
87
+
88
+ def confirmable_attribute_changed?
89
+ send(self.class.confirmable_attribute.to_s + '_changed?')
90
+ end
91
+
92
+ # Verifies whether a user is confirmed or not
93
+ def confirmed?
94
+ !!confirmed_at
95
+ end
96
+
97
+ def pending_reconfirmation?
98
+ self.class.reconfirmable && unconfirmed_attribute.present?
99
+ end
100
+
101
+ # Send confirmation instructions by email
102
+ def send_confirmation_instructions
103
+ self.confirmation_token = nil if reconfirmation_required?
104
+ @reconfirmation_required = false
105
+
106
+ generate_confirmation_token! if self.confirmation_token.blank?
107
+ self.devise_mailer.confirmation_instructions(self).deliver
108
+ end
109
+
110
+ # Resend confirmation token. This method does not need to generate a new token.
111
+ def resend_confirmation_token
112
+ pending_any_confirmation { send_confirmation_instructions }
113
+ end
114
+
115
+ # Overwrites active_for_authentication? for confirmation
116
+ # by verifying whether a user is active to sign in or not. If the user
117
+ # is already confirmed, it should never be blocked. Otherwise we need to
118
+ # calculate if the confirm time has not expired for this user.
119
+ def active_for_authentication?
120
+ super && (!confirmation_required? || confirmed? || confirmation_period_valid?)
121
+ end
122
+
123
+ # The message to be shown if the account is inactive.
124
+ def inactive_message
125
+ !confirmed? ? :unconfirmed : super
126
+ end
127
+
128
+ # If you don't want confirmation to be sent on create, neither a code
129
+ # to be generated, call skip_confirmation!
130
+ def skip_confirmation!
131
+ self.confirmed_at = Time.now.utc
132
+ end
133
+
134
+ # If you don't want reconfirmation to be sent, neither a code
135
+ # to be generated, call skip_reconfirmation!
136
+ def skip_reconfirmation!
137
+ @bypass_postpone = true
138
+ end
139
+
140
+ def headers_for(action)
141
+ headers = super
142
+ if action == :confirmation_instructions && pending_reconfirmation?
143
+ headers[:to] = unconfirmed_attribute
144
+ end
145
+ headers
146
+ end
147
+
148
+ protected
149
+
150
+ # A callback method used to deliver confirmation
151
+ # instructions on creation. This can be overriden
152
+ # in models to map to a nice sign up e-mail.
153
+ def send_on_create_confirmation_instructions
154
+ self.devise_mailer.confirmation_instructions(self).deliver
155
+ end
156
+
157
+ # Callback to overwrite if confirmation is required or not.
158
+ def confirmation_required?
159
+ !confirmed?
160
+ end
161
+
162
+ # Checks if the confirmation for the user is within the limit time.
163
+ # We do this by calculating if the difference between today and the
164
+ # confirmation sent date does not exceed the confirm in time configured.
165
+ # Confirm_within is a model configuration, must always be an integer value.
166
+ #
167
+ # Example:
168
+ #
169
+ # # allow_unconfirmed_access_for = 1.day and confirmation_sent_at = today
170
+ # confirmation_period_valid? # returns true
171
+ #
172
+ # # allow_unconfirmed_access_for = 5.days and confirmation_sent_at = 4.days.ago
173
+ # confirmation_period_valid? # returns true
174
+ #
175
+ # # allow_unconfirmed_access_for = 5.days and confirmation_sent_at = 5.days.ago
176
+ # confirmation_period_valid? # returns false
177
+ #
178
+ # # allow_unconfirmed_access_for = 0.days
179
+ # confirmation_period_valid? # will always return false
180
+ #
181
+ def confirmation_period_valid?
182
+ confirmation_sent_at && confirmation_sent_at.utc >= self.class.allow_unconfirmed_access_for.ago
183
+ end
184
+
185
+ # Checks whether the record requires any confirmation.
186
+ def pending_any_confirmation
187
+ if !confirmed? || pending_reconfirmation?
188
+ yield
189
+ else
190
+ self.errors.add(self.class.confirmable_attribute, :already_confirmed)
191
+ false
192
+ end
193
+ end
194
+
195
+ # Generates a new random token for confirmation, and stores the time
196
+ # this token is being generated
197
+ def generate_confirmation_token
198
+ self.confirmation_token = self.class.confirmation_token
199
+ self.confirmation_sent_at = Time.now.utc
200
+ end
201
+
202
+ def generate_confirmation_token!
203
+ generate_confirmation_token && save(:validate => false)
204
+ end
205
+
206
+ def after_password_reset
207
+ super
208
+ confirm! unless confirmed?
209
+ end
210
+
211
+ def postpone_confirmable_attribute_change_until_confirmation
212
+ @reconfirmation_required = true
213
+ self.unconfirmed_attribute = self.confirmable_attribute
214
+ self.confirmable_attribute = self.confirmable_attribute_was
215
+ end
216
+
217
+ def postpone_confirmable_attribute_change?
218
+ postpone = self.class.reconfirmable && confirmable_attribute_changed? && !@bypass_postpone
219
+ @bypass_postpone = nil
220
+ postpone
221
+ end
222
+
223
+ def reconfirmation_required?
224
+ self.class.reconfirmable && @reconfirmation_required
225
+ end
226
+
227
+ module ClassMethods
228
+ # Attempt to find a user by its attributes. If a record is found, send new
229
+ # confirmation instructions to it. If not, try searching for a user by unconfirmed_attribute
230
+ # field. If no user is found, returns a new user with a not found error.
231
+ # Options must contain the confirmable_attribute
232
+ def send_confirmation_instructions(attributes={})
233
+ confirmable = find_by_unconfirmed_attribute_with_errors(attributes) if reconfirmable
234
+ unless confirmable.try(:persisted?)
235
+ confirmable = find_or_initialize_with_errors(confirmation_keys, attributes, :not_found)
236
+ end
237
+ confirmable.resend_confirmation_token if confirmable.persisted?
238
+ confirmable
239
+ end
240
+
241
+ # Find a user by its confirmation token and try to confirm it.
242
+ # If no user is found, returns a new user with an error.
243
+ # If the user is already confirmed, create an error for the user
244
+ # Options must have the confirmation_token
245
+ def confirm_by_token(confirmation_token)
246
+ confirmable = find_or_initialize_with_error_by(:confirmation_token, confirmation_token)
247
+ confirmable.confirm! if confirmable.persisted?
248
+ confirmable
249
+ end
250
+
251
+ # Generate a token checking if one does not already exist in the database.
252
+ def confirmation_token
253
+ generate_token(:confirmation_token)
254
+ end
255
+
256
+ # Find a record for confirmation by unconfirmed attribute field
257
+ def find_by_unconfirmed_attribute_with_errors(attributes = {})
258
+ unconfirmed_required_attributes = confirmation_keys.map { |k| k == confirmable_attribute ? unconfirmed_attribute : k }
259
+ unconfirmed_attributes = attributes.symbolize_keys
260
+ unconfirmed_attributes[unconfirmed_attribute] = unconfirmed_attributes.delete(confirmable_attribute)
261
+ find_or_initialize_with_errors(unconfirmed_required_attributes, unconfirmed_attributes, :not_found)
262
+ end
263
+
264
+ Devise::Models.config(self, :allow_unconfirmed_access_for, :confirmation_keys, :confirmable_attribute, :unconfirmed_attribute, :reconfirmable)
265
+ end
266
+ end
267
+ end
268
+ end
@@ -0,0 +1,126 @@
1
+ require 'devise/strategies/database_authenticatable'
2
+ require 'bcrypt'
3
+
4
+ module Devise
5
+ module Models
6
+ # Authenticatable Module, responsible for encrypting password and validating
7
+ # authenticity of a user while signing in.
8
+ #
9
+ # == Options
10
+ #
11
+ # DatabaseAuthenticable adds the following options to devise_for:
12
+ #
13
+ # * +pepper+: a random string used to provide a more secure hash. Use
14
+ # `rake secret` to generate new keys.
15
+ #
16
+ # * +stretches+: the cost given to bcrypt.
17
+ #
18
+ # == Examples
19
+ #
20
+ # User.find(1).valid_password?('password123') # returns true/false
21
+ #
22
+ module DatabaseAuthenticatable
23
+ extend ActiveSupport::Concern
24
+
25
+ included do
26
+ attr_reader :password, :current_password
27
+ attr_accessor :password_confirmation
28
+ end
29
+
30
+ def self.required_fields(klass)
31
+ [:encrypted_password] + klass.authentication_keys
32
+ end
33
+
34
+ # Generates password encryption based on the given value.
35
+ def password=(new_password)
36
+ @password = new_password
37
+ self.encrypted_password = password_digest(@password) if @password.present?
38
+ end
39
+
40
+ # Verifies whether an password (ie from sign in) is the user password.
41
+ def valid_password?(password)
42
+ return false if encrypted_password.blank?
43
+ bcrypt = ::BCrypt::Password.new(encrypted_password)
44
+ password = ::BCrypt::Engine.hash_secret("#{password}#{self.class.pepper}", bcrypt.salt)
45
+ Devise.secure_compare(password, encrypted_password)
46
+ end
47
+
48
+ # Set password and password confirmation to nil
49
+ def clean_up_passwords
50
+ self.password = self.password_confirmation = nil
51
+ end
52
+
53
+ # Update record attributes when :current_password matches, otherwise returns
54
+ # error on :current_password. It also automatically rejects :password and
55
+ # :password_confirmation if they are blank.
56
+ def update_with_password(params, *options)
57
+ current_password = params.delete(:current_password)
58
+
59
+ if params[:password].blank?
60
+ params.delete(:password)
61
+ params.delete(:password_confirmation) if params[:password_confirmation].blank?
62
+ end
63
+
64
+ result = if valid_password?(current_password)
65
+ update_attributes(params, *options)
66
+ else
67
+ self.attributes = params
68
+ self.valid?
69
+ self.errors.add(:current_password, current_password.blank? ? :blank : :invalid)
70
+ false
71
+ end
72
+
73
+ clean_up_passwords
74
+ result
75
+ end
76
+
77
+ # Updates record attributes without asking for the current password.
78
+ # Never allows to change the current password. If you are using this
79
+ # method, you should probably override this method to protect other
80
+ # attributes you would not like to be updated without a password.
81
+ #
82
+ # Example:
83
+ #
84
+ # def update_without_password(params={})
85
+ # params.delete(:email)
86
+ # super(params)
87
+ # end
88
+ #
89
+ def update_without_password(params, *options)
90
+ params.delete(:password)
91
+ params.delete(:password_confirmation)
92
+
93
+ result = update_attributes(params, *options)
94
+ clean_up_passwords
95
+ result
96
+ end
97
+
98
+ def after_database_authentication
99
+ end
100
+
101
+ # A reliable way to expose the salt regardless of the implementation.
102
+ def authenticatable_salt
103
+ encrypted_password[0,29] if encrypted_password
104
+ end
105
+
106
+ protected
107
+
108
+ # Digests the password using bcrypt.
109
+ def password_digest(password)
110
+ ::BCrypt::Password.create("#{password}#{self.class.pepper}", :cost => self.class.stretches).to_s
111
+ end
112
+
113
+ module ClassMethods
114
+ Devise::Models.config(self, :pepper, :stretches)
115
+
116
+ # We assume this method already gets the sanitized values from the
117
+ # DatabaseAuthenticatable strategy. If you are using this method on
118
+ # your own, be sure to sanitize the conditions hash to only include
119
+ # the proper fields.
120
+ def find_for_database_authentication(conditions)
121
+ find_for_authentication(conditions)
122
+ end
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,185 @@
1
+ require "devise/hooks/lockable"
2
+
3
+ module Devise
4
+ module Models
5
+ # Handles blocking a user access after a certain number of attempts.
6
+ # Lockable accepts two different strategies to unlock a user after it's
7
+ # blocked: email and time. The former will send an email to the user when
8
+ # the lock happens, containing a link to unlock its account. The second
9
+ # will unlock the user automatically after some configured time (ie 2.hours).
10
+ # It's also possible to setup lockable to use both email and time strategies.
11
+ #
12
+ # == Options
13
+ #
14
+ # Lockable adds the following options to +devise+:
15
+ #
16
+ # * +maximum_attempts+: how many attempts should be accepted before blocking the user.
17
+ # * +lock_strategy+: lock the user account by :failed_attempts or :none.
18
+ # * +unlock_strategy+: unlock the user account by :time, :email, :both or :none.
19
+ # * +unlock_in+: the time you want to lock the user after to lock happens. Only available when unlock_strategy is :time or :both.
20
+ # * +unlock_keys+: the keys you want to use when locking and unlocking an account
21
+ #
22
+ module Lockable
23
+ extend ActiveSupport::Concern
24
+
25
+ delegate :lock_strategy_enabled?, :unlock_strategy_enabled?, :to => "self.class"
26
+
27
+ def self.required_fields(klass)
28
+ attributes = []
29
+ attributes << :failed_attempts if klass.lock_strategy_enabled?(:failed_attempts)
30
+ attributes << :unlock_at if klass.unlock_strategy_enabled?(:time)
31
+ attributes << :unlock_token if klass.unlock_strategy_enabled?(:email)
32
+
33
+ attributes
34
+ end
35
+
36
+ # Lock a user setting its locked_at to actual time.
37
+ def lock_access!
38
+ self.locked_at = Time.now.utc
39
+
40
+ if unlock_strategy_enabled?(:email)
41
+ generate_unlock_token
42
+ send_unlock_instructions
43
+ end
44
+
45
+ save(:validate => false)
46
+ end
47
+
48
+ # Unlock a user by cleaning locked_at and failed_attempts.
49
+ def unlock_access!
50
+ self.locked_at = nil
51
+ self.failed_attempts = 0 if respond_to?(:failed_attempts=)
52
+ self.unlock_token = nil if respond_to?(:unlock_token=)
53
+ save(:validate => false)
54
+ end
55
+
56
+ # Verifies whether a user is locked or not.
57
+ def access_locked?
58
+ locked_at && !lock_expired?
59
+ end
60
+
61
+ # Send unlock instructions by email
62
+ def send_unlock_instructions
63
+ self.devise_mailer.unlock_instructions(self).deliver
64
+ end
65
+
66
+ # Resend the unlock instructions if the user is locked.
67
+ def resend_unlock_token
68
+ if_access_locked { send_unlock_instructions }
69
+ end
70
+
71
+ # Overwrites active_for_authentication? from Devise::Models::Activatable for locking purposes
72
+ # by verifying whether a user is active to sign in or not based on locked?
73
+ def active_for_authentication?
74
+ super && !access_locked?
75
+ end
76
+
77
+ # Overwrites invalid_message from Devise::Models::Authenticatable to define
78
+ # the correct reason for blocking the sign in.
79
+ def inactive_message
80
+ access_locked? ? :locked : super
81
+ end
82
+
83
+ # Overwrites valid_for_authentication? from Devise::Models::Authenticatable
84
+ # for verifying whether a user is allowed to sign in or not. If the user
85
+ # is locked, it should never be allowed.
86
+ def valid_for_authentication?
87
+ return super unless persisted? && lock_strategy_enabled?(:failed_attempts)
88
+
89
+ # Unlock the user if the lock is expired, no matter
90
+ # if the user can login or not (wrong password, etc)
91
+ unlock_access! if lock_expired?
92
+
93
+ if super && !access_locked?
94
+ true
95
+ else
96
+ self.failed_attempts ||= 0
97
+ self.failed_attempts += 1
98
+ if attempts_exceeded?
99
+ lock_access! unless access_locked?
100
+ else
101
+ save(:validate => false)
102
+ end
103
+ false
104
+ end
105
+ end
106
+
107
+ def unauthenticated_message
108
+ if lock_strategy_enabled?(:failed_attempts) && attempts_exceeded?
109
+ :locked
110
+ else
111
+ super
112
+ end
113
+ end
114
+
115
+ protected
116
+
117
+ def attempts_exceeded?
118
+ self.failed_attempts > self.class.maximum_attempts
119
+ end
120
+
121
+ # Generates unlock token
122
+ def generate_unlock_token
123
+ self.unlock_token = self.class.unlock_token
124
+ end
125
+
126
+ # Tells if the lock is expired if :time unlock strategy is active
127
+ def lock_expired?
128
+ if unlock_strategy_enabled?(:time)
129
+ locked_at && locked_at < self.class.unlock_in.ago
130
+ else
131
+ false
132
+ end
133
+ end
134
+
135
+ # Checks whether the record is locked or not, yielding to the block
136
+ # if it's locked, otherwise adds an error to email.
137
+ def if_access_locked
138
+ if access_locked?
139
+ yield
140
+ else
141
+ self.errors.add(:email, :not_locked)
142
+ false
143
+ end
144
+ end
145
+
146
+ module ClassMethods
147
+ # Attempt to find a user by its email. If a record is found, send new
148
+ # unlock instructions to it. If not user is found, returns a new user
149
+ # with an email not found error.
150
+ # Options must contain the user email
151
+ def send_unlock_instructions(attributes={})
152
+ lockable = find_or_initialize_with_errors(unlock_keys, attributes, :not_found)
153
+ lockable.resend_unlock_token if lockable.persisted?
154
+ lockable
155
+ end
156
+
157
+ # Find a user by its unlock token and try to unlock it.
158
+ # If no user is found, returns a new user with an error.
159
+ # If the user is not locked, creates an error for the user
160
+ # Options must have the unlock_token
161
+ def unlock_access_by_token(unlock_token)
162
+ lockable = find_or_initialize_with_error_by(:unlock_token, unlock_token)
163
+ lockable.unlock_access! if lockable.persisted?
164
+ lockable
165
+ end
166
+
167
+ # Is the unlock enabled for the given unlock strategy?
168
+ def unlock_strategy_enabled?(strategy)
169
+ [:both, strategy].include?(self.unlock_strategy)
170
+ end
171
+
172
+ # Is the lock enabled for the given lock strategy?
173
+ def lock_strategy_enabled?(strategy)
174
+ self.lock_strategy == strategy
175
+ end
176
+
177
+ def unlock_token
178
+ Devise.friendly_token
179
+ end
180
+
181
+ Devise::Models.config(self, :maximum_attempts, :lock_strategy, :unlock_strategy, :unlock_in, :unlock_keys)
182
+ end
183
+ end
184
+ end
185
+ end