loyal_devise 2.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +10 -0
- data/.travis.yml +15 -0
- data/CHANGELOG.rdoc +881 -0
- data/CONTRIBUTING.md +12 -0
- data/Gemfile +31 -0
- data/Gemfile.lock +154 -0
- data/MIT-LICENSE +20 -0
- data/README.md +388 -0
- data/Rakefile +34 -0
- data/app/controllers/devise/confirmations_controller.rb +44 -0
- data/app/controllers/devise/omniauth_callbacks_controller.rb +31 -0
- data/app/controllers/devise/passwords_controller.rb +57 -0
- data/app/controllers/devise/registrations_controller.rb +120 -0
- data/app/controllers/devise/sessions_controller.rb +51 -0
- data/app/controllers/devise/unlocks_controller.rb +45 -0
- data/app/controllers/devise_controller.rb +193 -0
- data/app/helpers/devise_helper.rb +26 -0
- data/app/mailers/devise/mailer.rb +16 -0
- data/app/views/devise/_links.erb +3 -0
- data/app/views/devise/confirmations/new.html.erb +12 -0
- data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
- data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
- data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
- data/app/views/devise/passwords/edit.html.erb +16 -0
- data/app/views/devise/passwords/new.html.erb +12 -0
- data/app/views/devise/registrations/edit.html.erb +25 -0
- data/app/views/devise/registrations/new.html.erb +18 -0
- data/app/views/devise/sessions/new.html.erb +17 -0
- data/app/views/devise/shared/_links.erb +25 -0
- data/app/views/devise/unlocks/new.html.erb +12 -0
- data/config/locales/en.yml +59 -0
- data/devise.gemspec +26 -0
- data/gemfiles/Gemfile.rails-3.1.x +35 -0
- data/gemfiles/Gemfile.rails-3.1.x.lock +167 -0
- data/lib/devise/controllers/helpers.rb +273 -0
- data/lib/devise/controllers/rememberable.rb +53 -0
- data/lib/devise/controllers/scoped_views.rb +18 -0
- data/lib/devise/controllers/url_helpers.rb +68 -0
- data/lib/devise/delegator.rb +17 -0
- data/lib/devise/failure_app.rb +188 -0
- data/lib/devise/hooks/activatable.rb +12 -0
- data/lib/devise/hooks/forgetable.rb +10 -0
- data/lib/devise/hooks/lockable.rb +8 -0
- data/lib/devise/hooks/rememberable.rb +7 -0
- data/lib/devise/hooks/timeoutable.rb +26 -0
- data/lib/devise/hooks/trackable.rb +10 -0
- data/lib/devise/mailers/helpers.rb +92 -0
- data/lib/devise/mapping.rb +173 -0
- data/lib/devise/models/authenticatable.rb +269 -0
- data/lib/devise/models/confirmable.rb +271 -0
- data/lib/devise/models/database_authenticatable.rb +127 -0
- data/lib/devise/models/lockable.rb +194 -0
- data/lib/devise/models/omniauthable.rb +28 -0
- data/lib/devise/models/recoverable.rb +141 -0
- data/lib/devise/models/registerable.rb +26 -0
- data/lib/devise/models/rememberable.rb +126 -0
- data/lib/devise/models/timeoutable.rb +50 -0
- data/lib/devise/models/token_authenticatable.rb +90 -0
- data/lib/devise/models/trackable.rb +36 -0
- data/lib/devise/models/validatable.rb +67 -0
- data/lib/devise/models.rb +129 -0
- data/lib/devise/modules.rb +30 -0
- data/lib/devise/omniauth/config.rb +46 -0
- data/lib/devise/omniauth/url_helpers.rb +19 -0
- data/lib/devise/omniauth.rb +29 -0
- data/lib/devise/orm/active_record.rb +4 -0
- data/lib/devise/orm/mongoid.rb +4 -0
- data/lib/devise/param_filter.rb +42 -0
- data/lib/devise/rails/routes.rb +447 -0
- data/lib/devise/rails/warden_compat.rb +44 -0
- data/lib/devise/rails.rb +55 -0
- data/lib/devise/strategies/authenticatable.rb +177 -0
- data/lib/devise/strategies/base.rb +21 -0
- data/lib/devise/strategies/database_authenticatable.rb +21 -0
- data/lib/devise/strategies/rememberable.rb +56 -0
- data/lib/devise/strategies/token_authenticatable.rb +57 -0
- data/lib/devise/test_helpers.rb +132 -0
- data/lib/devise/time_inflector.rb +15 -0
- data/lib/devise/version.rb +4 -0
- data/lib/devise.rb +445 -0
- data/lib/generators/active_record/devise_generator.rb +80 -0
- data/lib/generators/active_record/templates/migration.rb +20 -0
- data/lib/generators/active_record/templates/migration_existing.rb +27 -0
- data/lib/generators/devise/devise_generator.rb +25 -0
- data/lib/generators/devise/install_generator.rb +25 -0
- data/lib/generators/devise/orm_helpers.rb +33 -0
- data/lib/generators/devise/views_generator.rb +117 -0
- data/lib/generators/mongoid/devise_generator.rb +58 -0
- data/lib/generators/templates/README +35 -0
- data/lib/generators/templates/devise.rb +241 -0
- data/lib/generators/templates/markerb/confirmation_instructions.markerb +5 -0
- data/lib/generators/templates/markerb/reset_password_instructions.markerb +8 -0
- data/lib/generators/templates/markerb/unlock_instructions.markerb +7 -0
- data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +15 -0
- data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
- data/lib/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
- data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +22 -0
- data/lib/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
- data/lib/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
- data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +15 -0
- data/test/controllers/custom_strategy_test.rb +63 -0
- data/test/controllers/helpers_test.rb +254 -0
- data/test/controllers/internal_helpers_test.rb +111 -0
- data/test/controllers/sessions_controller_test.rb +58 -0
- data/test/controllers/url_helpers_test.rb +60 -0
- data/test/delegator_test.rb +20 -0
- data/test/devise_test.rb +73 -0
- data/test/failure_app_test.rb +222 -0
- data/test/generators/active_record_generator_test.rb +76 -0
- data/test/generators/devise_generator_test.rb +40 -0
- data/test/generators/install_generator_test.rb +14 -0
- data/test/generators/mongoid_generator_test.rb +24 -0
- data/test/generators/views_generator_test.rb +53 -0
- data/test/helpers/devise_helper_test.rb +52 -0
- data/test/indifferent_hash.rb +34 -0
- data/test/integration/authenticatable_test.rb +634 -0
- data/test/integration/confirmable_test.rb +299 -0
- data/test/integration/database_authenticatable_test.rb +83 -0
- data/test/integration/http_authenticatable_test.rb +98 -0
- data/test/integration/lockable_test.rb +243 -0
- data/test/integration/omniauthable_test.rb +134 -0
- data/test/integration/recoverable_test.rb +307 -0
- data/test/integration/registerable_test.rb +346 -0
- data/test/integration/rememberable_test.rb +159 -0
- data/test/integration/timeoutable_test.rb +141 -0
- data/test/integration/token_authenticatable_test.rb +162 -0
- data/test/integration/trackable_test.rb +93 -0
- data/test/mailers/confirmation_instructions_test.rb +103 -0
- data/test/mailers/reset_password_instructions_test.rb +84 -0
- data/test/mailers/unlock_instructions_test.rb +78 -0
- data/test/mapping_test.rb +128 -0
- data/test/models/authenticatable_test.rb +8 -0
- data/test/models/confirmable_test.rb +392 -0
- data/test/models/database_authenticatable_test.rb +190 -0
- data/test/models/lockable_test.rb +274 -0
- data/test/models/omniauthable_test.rb +8 -0
- data/test/models/recoverable_test.rb +206 -0
- data/test/models/registerable_test.rb +8 -0
- data/test/models/rememberable_test.rb +175 -0
- data/test/models/serializable_test.rb +49 -0
- data/test/models/timeoutable_test.rb +47 -0
- data/test/models/token_authenticatable_test.rb +56 -0
- data/test/models/trackable_test.rb +14 -0
- data/test/models/validatable_test.rb +117 -0
- data/test/models_test.rb +180 -0
- data/test/omniauth/config_test.rb +58 -0
- data/test/omniauth/url_helpers_test.rb +52 -0
- data/test/orm/active_record.rb +10 -0
- data/test/orm/mongoid.rb +15 -0
- data/test/rails_app/Rakefile +10 -0
- data/test/rails_app/app/active_record/admin.rb +7 -0
- data/test/rails_app/app/active_record/shim.rb +3 -0
- data/test/rails_app/app/active_record/user.rb +7 -0
- data/test/rails_app/app/controllers/admins/sessions_controller.rb +7 -0
- data/test/rails_app/app/controllers/admins_controller.rb +12 -0
- data/test/rails_app/app/controllers/application_controller.rb +9 -0
- data/test/rails_app/app/controllers/home_controller.rb +26 -0
- data/test/rails_app/app/controllers/publisher/registrations_controller.rb +3 -0
- data/test/rails_app/app/controllers/publisher/sessions_controller.rb +3 -0
- data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +15 -0
- data/test/rails_app/app/controllers/users_controller.rb +24 -0
- data/test/rails_app/app/helpers/application_helper.rb +4 -0
- data/test/rails_app/app/mailers/users/mailer.rb +9 -0
- data/test/rails_app/app/mongoid/admin.rb +28 -0
- data/test/rails_app/app/mongoid/shim.rb +25 -0
- data/test/rails_app/app/mongoid/user.rb +43 -0
- data/test/rails_app/app/views/admins/index.html.erb +1 -0
- data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
- data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -0
- data/test/rails_app/app/views/home/index.html.erb +1 -0
- data/test/rails_app/app/views/home/join.html.erb +1 -0
- data/test/rails_app/app/views/home/private.html.erb +1 -0
- data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -0
- data/test/rails_app/app/views/layouts/application.html.erb +24 -0
- data/test/rails_app/app/views/users/index.html.erb +1 -0
- data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
- data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
- data/test/rails_app/config/application.rb +42 -0
- data/test/rails_app/config/boot.rb +9 -0
- data/test/rails_app/config/database.yml +18 -0
- data/test/rails_app/config/environment.rb +6 -0
- data/test/rails_app/config/environments/development.rb +19 -0
- data/test/rails_app/config/environments/production.rb +34 -0
- data/test/rails_app/config/environments/test.rb +34 -0
- data/test/rails_app/config/initializers/backtrace_silencers.rb +8 -0
- data/test/rails_app/config/initializers/devise.rb +179 -0
- data/test/rails_app/config/initializers/inflections.rb +3 -0
- data/test/rails_app/config/initializers/secret_token.rb +3 -0
- data/test/rails_app/config/routes.rb +101 -0
- data/test/rails_app/config.ru +4 -0
- data/test/rails_app/db/migrate/20100401102949_create_tables.rb +75 -0
- data/test/rails_app/db/schema.rb +53 -0
- data/test/rails_app/lib/shared_admin.rb +15 -0
- data/test/rails_app/lib/shared_user.rb +27 -0
- data/test/rails_app/public/404.html +26 -0
- data/test/rails_app/public/422.html +26 -0
- data/test/rails_app/public/500.html +26 -0
- data/test/rails_app/public/favicon.ico +0 -0
- data/test/rails_app/script/rails +10 -0
- data/test/routes_test.rb +249 -0
- data/test/support/assertions.rb +41 -0
- data/test/support/helpers.rb +92 -0
- data/test/support/integration.rb +93 -0
- data/test/support/locale/en.yml +4 -0
- data/test/support/webrat/integrations/rails.rb +25 -0
- data/test/test_helper.rb +28 -0
- data/test/test_helpers_test.rb +152 -0
- metadata +407 -0
@@ -0,0 +1,269 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'devise/hooks/activatable'
|
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
|
+
# * +skip_session_storage+: By default Devise will store the user in session.
|
29
|
+
# You can skip storage for http and token auth by appending values to array:
|
30
|
+
# :skip_session_storage => [:token_auth] or :skip_session_storage => [:http_auth, :token_auth],
|
31
|
+
# by default is set to :skip_session_storage => [:http_auth].
|
32
|
+
#
|
33
|
+
# == active_for_authentication?
|
34
|
+
#
|
35
|
+
# After authenticating a user and in each request, Devise checks if your model is active by
|
36
|
+
# calling model.active_for_authentication?. This method is overwriten by other devise modules. For instance,
|
37
|
+
# :confirmable overwrites .active_for_authentication? to only return true if your model was confirmed.
|
38
|
+
#
|
39
|
+
# You overwrite this method yourself, but if you do, don't forget to call super:
|
40
|
+
#
|
41
|
+
# def active_for_authentication?
|
42
|
+
# super && special_condition_is_valid?
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
# Whenever active_for_authentication? returns false, Devise asks the reason why your model is inactive using
|
46
|
+
# the inactive_message method. You can overwrite it as well:
|
47
|
+
#
|
48
|
+
# def inactive_message
|
49
|
+
# special_condition_is_valid? ? super : :special_condition_is_not_valid
|
50
|
+
# end
|
51
|
+
#
|
52
|
+
module Authenticatable
|
53
|
+
extend ActiveSupport::Concern
|
54
|
+
|
55
|
+
BLACKLIST_FOR_SERIALIZATION = [:encrypted_password, :reset_password_token, :reset_password_sent_at,
|
56
|
+
:remember_created_at, :sign_in_count, :current_sign_in_at, :last_sign_in_at, :current_sign_in_ip,
|
57
|
+
:last_sign_in_ip, :password_salt, :confirmation_token, :confirmed_at, :confirmation_sent_at,
|
58
|
+
:remember_token, :unconfirmed_email, :failed_attempts, :unlock_token, :locked_at, :authentication_token]
|
59
|
+
|
60
|
+
included do
|
61
|
+
class_attribute :devise_modules, :instance_writer => false
|
62
|
+
self.devise_modules ||= []
|
63
|
+
|
64
|
+
before_validation :downcase_keys
|
65
|
+
before_validation :strip_whitespace
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.required_fields(klass)
|
69
|
+
[]
|
70
|
+
end
|
71
|
+
|
72
|
+
# Check if the current object is valid for authentication. This method and
|
73
|
+
# find_for_authentication are the methods used in a Warden::Strategy to check
|
74
|
+
# if a model should be signed in or not.
|
75
|
+
#
|
76
|
+
# However, you should not overwrite this method, you should overwrite active_for_authentication?
|
77
|
+
# and inactive_message instead.
|
78
|
+
def valid_for_authentication?
|
79
|
+
block_given? ? yield : true
|
80
|
+
end
|
81
|
+
|
82
|
+
def unauthenticated_message
|
83
|
+
:invalid
|
84
|
+
end
|
85
|
+
|
86
|
+
def active_for_authentication?
|
87
|
+
true
|
88
|
+
end
|
89
|
+
|
90
|
+
def inactive_message
|
91
|
+
:inactive
|
92
|
+
end
|
93
|
+
|
94
|
+
def authenticatable_salt
|
95
|
+
end
|
96
|
+
|
97
|
+
def headers_for(name)
|
98
|
+
{}
|
99
|
+
end
|
100
|
+
|
101
|
+
array = %w(serializable_hash)
|
102
|
+
# to_xml does not call serializable_hash on 3.1
|
103
|
+
array << "to_xml" if Rails::VERSION::STRING[0,3] == "3.1"
|
104
|
+
|
105
|
+
array.each do |method|
|
106
|
+
class_eval <<-RUBY, __FILE__, __LINE__
|
107
|
+
# Redefine to_xml and serializable_hash in models for more secure defaults.
|
108
|
+
# By default, it removes from the serializable model all attributes that
|
109
|
+
# are *not* accessible. You can remove this default by using :force_except
|
110
|
+
# and passing a new list of attributes you want to exempt. All attributes
|
111
|
+
# given to :except will simply add names to exempt to Devise internal list.
|
112
|
+
def #{method}(options=nil)
|
113
|
+
options ||= {}
|
114
|
+
options[:except] = Array(options[:except])
|
115
|
+
|
116
|
+
if options[:force_except]
|
117
|
+
options[:except].concat Array(options[:force_except])
|
118
|
+
else
|
119
|
+
options[:except].concat BLACKLIST_FOR_SERIALIZATION
|
120
|
+
end
|
121
|
+
super(options)
|
122
|
+
end
|
123
|
+
RUBY
|
124
|
+
end
|
125
|
+
|
126
|
+
protected
|
127
|
+
|
128
|
+
def devise_mailer
|
129
|
+
Devise.mailer
|
130
|
+
end
|
131
|
+
|
132
|
+
# This is an internal method called every time Devise needs
|
133
|
+
# to send a notification/mail. This can be overriden if you
|
134
|
+
# need to customize the e-mail delivery logic. For instance,
|
135
|
+
# if you are using a queue to deliver e-mails (delayed job,
|
136
|
+
# sidekiq, resque, etc), you must add the delivery to the queue
|
137
|
+
# just after the transaction was committed. To achieve this,
|
138
|
+
# you can override send_devise_notification to store the
|
139
|
+
# deliveries until the after_commit callback is triggered:
|
140
|
+
#
|
141
|
+
# class User
|
142
|
+
# devise :database_authenticatable, :confirmable
|
143
|
+
#
|
144
|
+
# after_commit :send_pending_notifications
|
145
|
+
#
|
146
|
+
# protected
|
147
|
+
#
|
148
|
+
# def send_devise_notification(notification)
|
149
|
+
# pending_notifications << notification
|
150
|
+
# end
|
151
|
+
#
|
152
|
+
# def send_pending_notifications
|
153
|
+
# pending_notifications.each do |n|
|
154
|
+
# devise_mailer.send(n, self).deliver
|
155
|
+
# end
|
156
|
+
# end
|
157
|
+
#
|
158
|
+
# def pending_notifications
|
159
|
+
# @pending_notifications ||= []
|
160
|
+
# end
|
161
|
+
# end
|
162
|
+
#
|
163
|
+
def send_devise_notification(notification)
|
164
|
+
devise_mailer.send(notification, self).deliver
|
165
|
+
end
|
166
|
+
|
167
|
+
def downcase_keys
|
168
|
+
self.class.case_insensitive_keys.each { |k| self[k].try(:downcase!) }
|
169
|
+
end
|
170
|
+
|
171
|
+
def strip_whitespace
|
172
|
+
self.class.strip_whitespace_keys.each { |k| self[k].try(:strip!) }
|
173
|
+
end
|
174
|
+
|
175
|
+
module ClassMethods
|
176
|
+
Devise::Models.config(self, :authentication_keys, :request_keys, :strip_whitespace_keys,
|
177
|
+
:case_insensitive_keys, :http_authenticatable, :params_authenticatable, :skip_session_storage)
|
178
|
+
|
179
|
+
def serialize_into_session(record)
|
180
|
+
[record.to_key, record.authenticatable_salt]
|
181
|
+
end
|
182
|
+
|
183
|
+
def serialize_from_session(key, salt)
|
184
|
+
record = to_adapter.get(key)
|
185
|
+
record if record && record.authenticatable_salt == salt
|
186
|
+
end
|
187
|
+
|
188
|
+
def params_authenticatable?(strategy)
|
189
|
+
params_authenticatable.is_a?(Array) ?
|
190
|
+
params_authenticatable.include?(strategy) : params_authenticatable
|
191
|
+
end
|
192
|
+
|
193
|
+
def http_authenticatable?(strategy)
|
194
|
+
http_authenticatable.is_a?(Array) ?
|
195
|
+
http_authenticatable.include?(strategy) : http_authenticatable
|
196
|
+
end
|
197
|
+
|
198
|
+
# Find first record based on conditions given (ie by the sign in form).
|
199
|
+
# This method is always called during an authentication process but
|
200
|
+
# it may be wrapped as well. For instance, database authenticatable
|
201
|
+
# provides a `find_for_database_authentication` that wraps a call to
|
202
|
+
# this method. This allows you to customize both database authenticatable
|
203
|
+
# or the whole authenticate stack by customize `find_for_authentication.`
|
204
|
+
#
|
205
|
+
# Overwrite to add customized conditions, create a join, or maybe use a
|
206
|
+
# namedscope to filter records while authenticating.
|
207
|
+
# Example:
|
208
|
+
#
|
209
|
+
# def self.find_for_authentication(conditions={})
|
210
|
+
# conditions[:active] = true
|
211
|
+
# super
|
212
|
+
# end
|
213
|
+
#
|
214
|
+
# Finally, notice that Devise also queries for users in other scenarios
|
215
|
+
# besides authentication, for example when retrieving an user to send
|
216
|
+
# an e-mail for password reset. In such cases, find_for_authentication
|
217
|
+
# is not called.
|
218
|
+
def find_for_authentication(conditions)
|
219
|
+
find_first_by_auth_conditions(conditions)
|
220
|
+
end
|
221
|
+
|
222
|
+
def find_first_by_auth_conditions(conditions)
|
223
|
+
to_adapter.find_first devise_param_filter.filter(conditions)
|
224
|
+
end
|
225
|
+
|
226
|
+
# Find an initialize a record setting an error if it can't be found.
|
227
|
+
def find_or_initialize_with_error_by(attribute, value, error=:invalid) #:nodoc:
|
228
|
+
find_or_initialize_with_errors([attribute], { attribute => value }, error)
|
229
|
+
end
|
230
|
+
|
231
|
+
# Find an initialize a group of attributes based on a list of required attributes.
|
232
|
+
def find_or_initialize_with_errors(required_attributes, attributes, error=:invalid) #:nodoc:
|
233
|
+
attributes = attributes.slice(*required_attributes)
|
234
|
+
attributes.delete_if { |key, value| value.blank? }
|
235
|
+
|
236
|
+
if attributes.size == required_attributes.size
|
237
|
+
record = find_first_by_auth_conditions(attributes)
|
238
|
+
end
|
239
|
+
|
240
|
+
unless record
|
241
|
+
record = new
|
242
|
+
|
243
|
+
required_attributes.each do |key|
|
244
|
+
value = attributes[key]
|
245
|
+
record.send("#{key}=", value)
|
246
|
+
record.errors.add(key, value.present? ? error : :blank)
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
record
|
251
|
+
end
|
252
|
+
|
253
|
+
protected
|
254
|
+
|
255
|
+
def devise_param_filter
|
256
|
+
@devise_param_filter ||= Devise::ParamFilter.new(case_insensitive_keys, strip_whitespace_keys)
|
257
|
+
end
|
258
|
+
|
259
|
+
# Generate a token by looping and ensuring does not already exist.
|
260
|
+
def generate_token(column)
|
261
|
+
loop do
|
262
|
+
token = Devise.friendly_token
|
263
|
+
break token unless to_adapter.find_first({ column => token })
|
264
|
+
end
|
265
|
+
end
|
266
|
+
end
|
267
|
+
end
|
268
|
+
end
|
269
|
+
end
|
@@ -0,0 +1,271 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module Devise
|
3
|
+
module Models
|
4
|
+
# Confirmable is responsible to verify if an account is already confirmed to
|
5
|
+
# sign in, and to send emails with confirmation instructions.
|
6
|
+
# Confirmation instructions are sent to the user email after creating a
|
7
|
+
# record and when manually requested by a new confirmation instruction request.
|
8
|
+
#
|
9
|
+
# == Options
|
10
|
+
#
|
11
|
+
# Confirmable adds the following options to devise_for:
|
12
|
+
#
|
13
|
+
# * +allow_unconfirmed_access_for+: the time you want to allow the user to access his account
|
14
|
+
# before confirming it. After this period, the user access is denied. You can
|
15
|
+
# use this to let your user access some features of your application without
|
16
|
+
# confirming the account, but blocking it after a certain period (ie 7 days).
|
17
|
+
# By default allow_unconfirmed_access_for is zero, it means users always have to confirm to sign in.
|
18
|
+
# * +reconfirmable+: requires any email changes to be confirmed (exactly the same way as
|
19
|
+
# initial account confirmation) to be applied. Requires additional unconfirmed_email
|
20
|
+
# db field to be setup (t.reconfirmable in migrations). Until confirmed new email is
|
21
|
+
# stored in unconfirmed email column, and copied to email column on successful
|
22
|
+
# confirmation.
|
23
|
+
# * +confirm_within+: the time before a sent confirmation token becomes invalid.
|
24
|
+
# You can use this to force the user to confirm within a set period of time.
|
25
|
+
#
|
26
|
+
# == Examples
|
27
|
+
#
|
28
|
+
# User.find(1).confirm! # returns true unless it's already confirmed
|
29
|
+
# User.find(1).confirmed? # true/false
|
30
|
+
# User.find(1).send_confirmation_instructions # manually send instructions
|
31
|
+
#
|
32
|
+
module Confirmable
|
33
|
+
extend ActiveSupport::Concern
|
34
|
+
include ActionView::Helpers::DateHelper
|
35
|
+
|
36
|
+
included do
|
37
|
+
before_create :generate_confirmation_token, :if => :confirmation_required?
|
38
|
+
after_create :send_on_create_confirmation_instructions, :if => :confirmation_required?
|
39
|
+
before_update :postpone_email_change_until_confirmation, :if => :postpone_email_change?
|
40
|
+
after_update :send_confirmation_instructions, :if => :reconfirmation_required?
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.required_fields(klass)
|
44
|
+
required_methods = [:confirmation_token, :confirmed_at, :confirmation_sent_at]
|
45
|
+
required_methods << :unconfirmed_email if klass.reconfirmable
|
46
|
+
required_methods
|
47
|
+
end
|
48
|
+
|
49
|
+
# Confirm a user by setting it's confirmed_at to actual time. If the user
|
50
|
+
# is already confirmed, add an error to email field. If the user is invalid
|
51
|
+
# add errors
|
52
|
+
def confirm!
|
53
|
+
pending_any_confirmation do
|
54
|
+
if confirmation_period_expired?
|
55
|
+
self.errors.add(:email, :confirmation_period_expired,
|
56
|
+
:period => Devise::TimeInflector.time_ago_in_words(self.class.confirm_within.ago))
|
57
|
+
return false
|
58
|
+
end
|
59
|
+
|
60
|
+
self.confirmation_token = nil
|
61
|
+
self.confirmed_at = Time.now.utc
|
62
|
+
|
63
|
+
if self.class.reconfirmable && unconfirmed_email.present?
|
64
|
+
skip_reconfirmation!
|
65
|
+
self.email = unconfirmed_email
|
66
|
+
self.unconfirmed_email = nil
|
67
|
+
|
68
|
+
# We need to validate in such cases to enforce e-mail uniqueness
|
69
|
+
save(:validate => true)
|
70
|
+
else
|
71
|
+
save(:validate => false)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# Verifies whether a user is confirmed or not
|
77
|
+
def confirmed?
|
78
|
+
!!confirmed_at
|
79
|
+
end
|
80
|
+
|
81
|
+
def pending_reconfirmation?
|
82
|
+
self.class.reconfirmable && unconfirmed_email.present?
|
83
|
+
end
|
84
|
+
|
85
|
+
# Send confirmation instructions by email
|
86
|
+
def send_confirmation_instructions
|
87
|
+
self.confirmation_token = nil if reconfirmation_required?
|
88
|
+
@reconfirmation_required = false
|
89
|
+
|
90
|
+
generate_confirmation_token! if self.confirmation_token.blank?
|
91
|
+
send_devise_notification(:confirmation_instructions)
|
92
|
+
end
|
93
|
+
|
94
|
+
# Resend confirmation token. This method does not need to generate a new token.
|
95
|
+
def resend_confirmation_token
|
96
|
+
pending_any_confirmation do
|
97
|
+
self.confirmation_token = nil if confirmation_period_expired?
|
98
|
+
send_confirmation_instructions
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
# Overwrites active_for_authentication? for confirmation
|
103
|
+
# by verifying whether a user is active to sign in or not. If the user
|
104
|
+
# is already confirmed, it should never be blocked. Otherwise we need to
|
105
|
+
# calculate if the confirm time has not expired for this user.
|
106
|
+
def active_for_authentication?
|
107
|
+
super && (!confirmation_required? || confirmed? || confirmation_period_valid?)
|
108
|
+
end
|
109
|
+
|
110
|
+
# The message to be shown if the account is inactive.
|
111
|
+
def inactive_message
|
112
|
+
!confirmed? ? :unconfirmed : super
|
113
|
+
end
|
114
|
+
|
115
|
+
# If you don't want confirmation to be sent on create, neither a code
|
116
|
+
# to be generated, call skip_confirmation!
|
117
|
+
def skip_confirmation!
|
118
|
+
self.confirmed_at = Time.now.utc
|
119
|
+
end
|
120
|
+
|
121
|
+
# If you don't want reconfirmation to be sent, neither a code
|
122
|
+
# to be generated, call skip_reconfirmation!
|
123
|
+
def skip_reconfirmation!
|
124
|
+
@bypass_postpone = true
|
125
|
+
end
|
126
|
+
|
127
|
+
def headers_for(action)
|
128
|
+
headers = super
|
129
|
+
if action == :confirmation_instructions && pending_reconfirmation?
|
130
|
+
headers[:to] = unconfirmed_email
|
131
|
+
end
|
132
|
+
headers
|
133
|
+
end
|
134
|
+
|
135
|
+
protected
|
136
|
+
|
137
|
+
# A callback method used to deliver confirmation
|
138
|
+
# instructions on creation. This can be overriden
|
139
|
+
# in models to map to a nice sign up e-mail.
|
140
|
+
def send_on_create_confirmation_instructions
|
141
|
+
send_devise_notification(:confirmation_instructions)
|
142
|
+
end
|
143
|
+
|
144
|
+
# Callback to overwrite if confirmation is required or not.
|
145
|
+
def confirmation_required?
|
146
|
+
!confirmed?
|
147
|
+
end
|
148
|
+
|
149
|
+
# Checks if the confirmation for the user is within the limit time.
|
150
|
+
# We do this by calculating if the difference between today and the
|
151
|
+
# confirmation sent date does not exceed the confirm in time configured.
|
152
|
+
# Confirm_within is a model configuration, must always be an integer value.
|
153
|
+
#
|
154
|
+
# Example:
|
155
|
+
#
|
156
|
+
# # allow_unconfirmed_access_for = 1.day and confirmation_sent_at = today
|
157
|
+
# confirmation_period_valid? # returns true
|
158
|
+
#
|
159
|
+
# # allow_unconfirmed_access_for = 5.days and confirmation_sent_at = 4.days.ago
|
160
|
+
# confirmation_period_valid? # returns true
|
161
|
+
#
|
162
|
+
# # allow_unconfirmed_access_for = 5.days and confirmation_sent_at = 5.days.ago
|
163
|
+
# confirmation_period_valid? # returns false
|
164
|
+
#
|
165
|
+
# # allow_unconfirmed_access_for = 0.days
|
166
|
+
# confirmation_period_valid? # will always return false
|
167
|
+
#
|
168
|
+
def confirmation_period_valid?
|
169
|
+
confirmation_sent_at && confirmation_sent_at.utc >= self.class.allow_unconfirmed_access_for.ago
|
170
|
+
end
|
171
|
+
|
172
|
+
# Checks if the user confirmation happens before the token becomes invalid
|
173
|
+
# Examples:
|
174
|
+
#
|
175
|
+
# # confirm_within = 3.days and confirmation_sent_at = 2.days.ago
|
176
|
+
# confirmation_period_expired? # returns false
|
177
|
+
#
|
178
|
+
# # confirm_within = 3.days and confirmation_sent_at = 4.days.ago
|
179
|
+
# confirmation_period_expired? # returns true
|
180
|
+
#
|
181
|
+
# # confirm_within = nil
|
182
|
+
# confirmation_period_expired? # will always return false
|
183
|
+
#
|
184
|
+
def confirmation_period_expired?
|
185
|
+
self.class.confirm_within && (Time.now > self.confirmation_sent_at + self.class.confirm_within )
|
186
|
+
end
|
187
|
+
|
188
|
+
# Checks whether the record requires any confirmation.
|
189
|
+
def pending_any_confirmation
|
190
|
+
if (!confirmed? || pending_reconfirmation?)
|
191
|
+
yield
|
192
|
+
else
|
193
|
+
self.errors.add(:email, :already_confirmed)
|
194
|
+
false
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
# Generates a new random token for confirmation, and stores the time
|
199
|
+
# this token is being generated
|
200
|
+
def generate_confirmation_token
|
201
|
+
self.confirmation_token = self.class.confirmation_token
|
202
|
+
self.confirmation_sent_at = Time.now.utc
|
203
|
+
end
|
204
|
+
|
205
|
+
def generate_confirmation_token!
|
206
|
+
generate_confirmation_token && save(:validate => false)
|
207
|
+
end
|
208
|
+
|
209
|
+
def after_password_reset
|
210
|
+
super
|
211
|
+
confirm! unless confirmed?
|
212
|
+
end
|
213
|
+
|
214
|
+
def postpone_email_change_until_confirmation
|
215
|
+
@reconfirmation_required = true
|
216
|
+
self.unconfirmed_email = self.email
|
217
|
+
self.email = self.email_was
|
218
|
+
end
|
219
|
+
|
220
|
+
def postpone_email_change?
|
221
|
+
postpone = self.class.reconfirmable && email_changed? && !@bypass_postpone
|
222
|
+
@bypass_postpone = nil
|
223
|
+
postpone
|
224
|
+
end
|
225
|
+
|
226
|
+
def reconfirmation_required?
|
227
|
+
self.class.reconfirmable && @reconfirmation_required
|
228
|
+
end
|
229
|
+
|
230
|
+
module ClassMethods
|
231
|
+
# Attempt to find a user by its email. If a record is found, send new
|
232
|
+
# confirmation instructions to it. If not, try searching for a user by unconfirmed_email
|
233
|
+
# field. If no user is found, returns a new user with an email not found error.
|
234
|
+
# Options must contain the user email
|
235
|
+
def send_confirmation_instructions(attributes={})
|
236
|
+
confirmable = find_by_unconfirmed_email_with_errors(attributes) if reconfirmable
|
237
|
+
unless confirmable.try(:persisted?)
|
238
|
+
confirmable = find_or_initialize_with_errors(confirmation_keys, attributes, :not_found)
|
239
|
+
end
|
240
|
+
confirmable.resend_confirmation_token if confirmable.persisted?
|
241
|
+
confirmable
|
242
|
+
end
|
243
|
+
|
244
|
+
# Find a user by its confirmation token and try to confirm it.
|
245
|
+
# If no user is found, returns a new user with an error.
|
246
|
+
# If the user is already confirmed, create an error for the user
|
247
|
+
# Options must have the confirmation_token
|
248
|
+
def confirm_by_token(confirmation_token)
|
249
|
+
confirmable = find_or_initialize_with_error_by(:confirmation_token, confirmation_token)
|
250
|
+
confirmable.confirm! if confirmable.persisted?
|
251
|
+
confirmable
|
252
|
+
end
|
253
|
+
|
254
|
+
# Generate a token checking if one does not already exist in the database.
|
255
|
+
def confirmation_token
|
256
|
+
generate_token(:confirmation_token)
|
257
|
+
end
|
258
|
+
|
259
|
+
# Find a record for confirmation by unconfirmed email field
|
260
|
+
def find_by_unconfirmed_email_with_errors(attributes = {})
|
261
|
+
unconfirmed_required_attributes = confirmation_keys.map { |k| k == :email ? :unconfirmed_email : k }
|
262
|
+
unconfirmed_attributes = attributes.symbolize_keys
|
263
|
+
unconfirmed_attributes[:unconfirmed_email] = unconfirmed_attributes.delete(:email)
|
264
|
+
find_or_initialize_with_errors(unconfirmed_required_attributes, unconfirmed_attributes, :not_found)
|
265
|
+
end
|
266
|
+
|
267
|
+
Devise::Models.config(self, :allow_unconfirmed_access_for, :confirmation_keys, :reconfirmable, :confirm_within)
|
268
|
+
end
|
269
|
+
end
|
270
|
+
end
|
271
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'devise/strategies/database_authenticatable'
|
3
|
+
require 'bcrypt'
|
4
|
+
|
5
|
+
module Devise
|
6
|
+
module Models
|
7
|
+
# Authenticatable Module, responsible for encrypting password and validating
|
8
|
+
# authenticity of a user while signing in.
|
9
|
+
#
|
10
|
+
# == Options
|
11
|
+
#
|
12
|
+
# DatabaseAuthenticable adds the following options to devise_for:
|
13
|
+
#
|
14
|
+
# * +pepper+: a random string used to provide a more secure hash. Use
|
15
|
+
# `rake secret` to generate new keys.
|
16
|
+
#
|
17
|
+
# * +stretches+: the cost given to bcrypt.
|
18
|
+
#
|
19
|
+
# == Examples
|
20
|
+
#
|
21
|
+
# User.find(1).valid_password?('password123') # returns true/false
|
22
|
+
#
|
23
|
+
module DatabaseAuthenticatable
|
24
|
+
extend ActiveSupport::Concern
|
25
|
+
|
26
|
+
included do
|
27
|
+
attr_reader :password, :current_password
|
28
|
+
attr_accessor :password_confirmation
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.required_fields(klass)
|
32
|
+
[:encrypted_password] + klass.authentication_keys
|
33
|
+
end
|
34
|
+
|
35
|
+
# Generates password encryption based on the given value.
|
36
|
+
def password=(new_password)
|
37
|
+
@password = new_password
|
38
|
+
self.encrypted_password = password_digest(@password) if @password.present?
|
39
|
+
end
|
40
|
+
|
41
|
+
# Verifies whether an password (ie from sign in) is the user password.
|
42
|
+
def valid_password?(password)
|
43
|
+
return false if encrypted_password.blank?
|
44
|
+
bcrypt = ::BCrypt::Password.new(encrypted_password)
|
45
|
+
password = ::BCrypt::Engine.hash_secret("#{password}#{self.class.pepper}", bcrypt.salt)
|
46
|
+
Devise.secure_compare(password, encrypted_password)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Set password and password confirmation to nil
|
50
|
+
def clean_up_passwords
|
51
|
+
self.password = self.password_confirmation = nil
|
52
|
+
end
|
53
|
+
|
54
|
+
# Update record attributes when :current_password matches, otherwise returns
|
55
|
+
# error on :current_password. It also automatically rejects :password and
|
56
|
+
# :password_confirmation if they are blank.
|
57
|
+
def update_with_password(params, *options)
|
58
|
+
current_password = params.delete(:current_password)
|
59
|
+
|
60
|
+
if params[:password].blank?
|
61
|
+
params.delete(:password)
|
62
|
+
params.delete(:password_confirmation) if params[:password_confirmation].blank?
|
63
|
+
end
|
64
|
+
|
65
|
+
result = if valid_password?(current_password)
|
66
|
+
update_attributes(params, *options)
|
67
|
+
else
|
68
|
+
self.assign_attributes(params, *options)
|
69
|
+
self.valid?
|
70
|
+
self.errors.add(:current_password, current_password.blank? ? :blank : :invalid)
|
71
|
+
false
|
72
|
+
end
|
73
|
+
|
74
|
+
clean_up_passwords
|
75
|
+
result
|
76
|
+
end
|
77
|
+
|
78
|
+
# Updates record attributes without asking for the current password.
|
79
|
+
# Never allows to change the current password. If you are using this
|
80
|
+
# method, you should probably override this method to protect other
|
81
|
+
# attributes you would not like to be updated without a password.
|
82
|
+
#
|
83
|
+
# Example:
|
84
|
+
#
|
85
|
+
# def update_without_password(params={})
|
86
|
+
# params.delete(:email)
|
87
|
+
# super(params)
|
88
|
+
# end
|
89
|
+
#
|
90
|
+
def update_without_password(params, *options)
|
91
|
+
params.delete(:password)
|
92
|
+
params.delete(:password_confirmation)
|
93
|
+
|
94
|
+
result = update_attributes(params, *options)
|
95
|
+
clean_up_passwords
|
96
|
+
result
|
97
|
+
end
|
98
|
+
|
99
|
+
def after_database_authentication
|
100
|
+
end
|
101
|
+
|
102
|
+
# A reliable way to expose the salt regardless of the implementation.
|
103
|
+
def authenticatable_salt
|
104
|
+
encrypted_password[0,29] if encrypted_password
|
105
|
+
end
|
106
|
+
|
107
|
+
protected
|
108
|
+
|
109
|
+
# Digests the password using bcrypt.
|
110
|
+
def password_digest(password)
|
111
|
+
::BCrypt::Password.create("#{password}#{self.class.pepper}", :cost => self.class.stretches).to_s
|
112
|
+
end
|
113
|
+
|
114
|
+
module ClassMethods
|
115
|
+
Devise::Models.config(self, :pepper, :stretches)
|
116
|
+
|
117
|
+
# We assume this method already gets the sanitized values from the
|
118
|
+
# DatabaseAuthenticatable strategy. If you are using this method on
|
119
|
+
# your own, be sure to sanitize the conditions hash to only include
|
120
|
+
# the proper fields.
|
121
|
+
def find_for_database_authentication(conditions)
|
122
|
+
find_for_authentication(conditions)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|