upstream-devise 2.1.0.rc
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 +846 -0
- data/Gemfile +35 -0
- data/Gemfile.lock +165 -0
- data/MIT-LICENSE +20 -0
- data/README.md +384 -0
- data/Rakefile +34 -0
- data/app/controllers/devise/confirmations_controller.rb +43 -0
- data/app/controllers/devise/omniauth_callbacks_controller.rb +24 -0
- data/app/controllers/devise/passwords_controller.rb +47 -0
- data/app/controllers/devise/registrations_controller.rb +107 -0
- data/app/controllers/devise/sessions_controller.rb +49 -0
- data/app/controllers/devise/unlocks_controller.rb +44 -0
- data/app/controllers/devise_controller.rb +177 -0
- data/app/helpers/devise_helper.rb +25 -0
- data/app/mailers/devise/mailer.rb +15 -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 +57 -0
- data/devise.gemspec +25 -0
- data/gemfiles/Gemfile.rails-3.1.x +35 -0
- data/gemfiles/Gemfile.rails-3.1.x.lock +167 -0
- data/lib/devise.rb +455 -0
- data/lib/devise/controllers/helpers.rb +269 -0
- data/lib/devise/controllers/rememberable.rb +52 -0
- data/lib/devise/controllers/scoped_views.rb +17 -0
- data/lib/devise/controllers/url_helpers.rb +67 -0
- data/lib/devise/delegator.rb +17 -0
- data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
- data/lib/devise/encryptors/base.rb +24 -0
- data/lib/devise/encryptors/clearance_sha1.rb +17 -0
- data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
- data/lib/devise/encryptors/sha1.rb +25 -0
- data/lib/devise/encryptors/sha512.rb +25 -0
- data/lib/devise/failure_app.rb +185 -0
- data/lib/devise/hooks/activatable.rb +11 -0
- data/lib/devise/hooks/forgetable.rb +9 -0
- data/lib/devise/hooks/lockable.rb +7 -0
- data/lib/devise/hooks/rememberable.rb +6 -0
- data/lib/devise/hooks/timeoutable.rb +22 -0
- data/lib/devise/hooks/trackable.rb +9 -0
- data/lib/devise/mailers/helpers.rb +86 -0
- data/lib/devise/mapping.rb +172 -0
- data/lib/devise/models.rb +123 -0
- data/lib/devise/models/authenticatable.rb +231 -0
- data/lib/devise/models/confirmable.rb +242 -0
- data/lib/devise/models/database_authenticatable.rb +126 -0
- data/lib/devise/models/encryptable.rb +86 -0
- data/lib/devise/models/lockable.rb +185 -0
- data/lib/devise/models/omniauthable.rb +27 -0
- data/lib/devise/models/recoverable.rb +140 -0
- data/lib/devise/models/registerable.rb +25 -0
- data/lib/devise/models/rememberable.rb +125 -0
- data/lib/devise/models/timeoutable.rb +49 -0
- data/lib/devise/models/token_authenticatable.rb +77 -0
- data/lib/devise/models/trackable.rb +35 -0
- data/lib/devise/models/validatable.rb +66 -0
- data/lib/devise/modules.rb +30 -0
- data/lib/devise/omniauth.rb +28 -0
- data/lib/devise/omniauth/config.rb +45 -0
- data/lib/devise/omniauth/url_helpers.rb +33 -0
- data/lib/devise/orm/active_record.rb +3 -0
- data/lib/devise/orm/mongoid.rb +3 -0
- data/lib/devise/param_filter.rb +41 -0
- data/lib/devise/rails.rb +54 -0
- data/lib/devise/rails/routes.rb +412 -0
- data/lib/devise/rails/warden_compat.rb +43 -0
- data/lib/devise/strategies/authenticatable.rb +165 -0
- data/lib/devise/strategies/base.rb +15 -0
- data/lib/devise/strategies/database_authenticatable.rb +21 -0
- data/lib/devise/strategies/rememberable.rb +53 -0
- data/lib/devise/strategies/token_authenticatable.rb +57 -0
- data/lib/devise/test_helpers.rb +130 -0
- data/lib/devise/version.rb +3 -0
- data/lib/generators/active_record/devise_generator.rb +78 -0
- data/lib/generators/active_record/templates/migration.rb +19 -0
- data/lib/generators/active_record/templates/migration_existing.rb +26 -0
- data/lib/generators/devise/devise_generator.rb +24 -0
- data/lib/generators/devise/install_generator.rb +24 -0
- data/lib/generators/devise/orm_helpers.rb +32 -0
- data/lib/generators/devise/views_generator.rb +110 -0
- data/lib/generators/mongoid/devise_generator.rb +60 -0
- data/lib/generators/templates/README +31 -0
- data/lib/generators/templates/devise.rb +216 -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 +62 -0
- data/test/controllers/helpers_test.rb +254 -0
- data/test/controllers/internal_helpers_test.rb +97 -0
- data/test/controllers/sessions_controller_test.rb +36 -0
- data/test/controllers/url_helpers_test.rb +59 -0
- data/test/delegator_test.rb +19 -0
- data/test/devise_test.rb +72 -0
- data/test/encryptors_test.rb +30 -0
- data/test/failure_app_test.rb +211 -0
- data/test/generators/active_record_generator_test.rb +69 -0
- data/test/generators/devise_generator_test.rb +39 -0
- data/test/generators/install_generator_test.rb +13 -0
- data/test/generators/mongoid_generator_test.rb +23 -0
- data/test/generators/views_generator_test.rb +52 -0
- data/test/helpers/devise_helper_test.rb +51 -0
- data/test/indifferent_hash.rb +33 -0
- data/test/integration/authenticatable_test.rb +587 -0
- data/test/integration/confirmable_test.rb +255 -0
- data/test/integration/database_authenticatable_test.rb +82 -0
- data/test/integration/http_authenticatable_test.rb +97 -0
- data/test/integration/lockable_test.rb +224 -0
- data/test/integration/omniauthable_test.rb +133 -0
- data/test/integration/recoverable_test.rb +300 -0
- data/test/integration/registerable_test.rb +324 -0
- data/test/integration/rememberable_test.rb +158 -0
- data/test/integration/timeoutable_test.rb +114 -0
- data/test/integration/token_authenticatable_test.rb +161 -0
- data/test/integration/trackable_test.rb +92 -0
- data/test/mailers/confirmation_instructions_test.rb +95 -0
- data/test/mailers/reset_password_instructions_test.rb +83 -0
- data/test/mailers/unlock_instructions_test.rb +77 -0
- data/test/mapping_test.rb +127 -0
- data/test/models/authenticatable_test.rb +7 -0
- data/test/models/confirmable_test.rb +357 -0
- data/test/models/database_authenticatable_test.rb +189 -0
- data/test/models/encryptable_test.rb +73 -0
- data/test/models/lockable_test.rb +263 -0
- data/test/models/omniauthable_test.rb +7 -0
- data/test/models/recoverable_test.rb +205 -0
- data/test/models/registerable_test.rb +7 -0
- data/test/models/rememberable_test.rb +174 -0
- data/test/models/serializable_test.rb +48 -0
- data/test/models/timeoutable_test.rb +46 -0
- data/test/models/token_authenticatable_test.rb +55 -0
- data/test/models/trackable_test.rb +13 -0
- data/test/models/validatable_test.rb +117 -0
- data/test/models_test.rb +179 -0
- data/test/omniauth/config_test.rb +57 -0
- data/test/omniauth/url_helpers_test.rb +58 -0
- data/test/orm/active_record.rb +9 -0
- data/test/orm/mongoid.rb +14 -0
- data/test/rails_app/Rakefile +10 -0
- data/test/rails_app/app/active_record/admin.rb +6 -0
- data/test/rails_app/app/active_record/shim.rb +2 -0
- data/test/rails_app/app/active_record/user.rb +6 -0
- data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
- data/test/rails_app/app/controllers/admins_controller.rb +6 -0
- data/test/rails_app/app/controllers/application_controller.rb +8 -0
- data/test/rails_app/app/controllers/home_controller.rb +25 -0
- data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
- data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
- data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +14 -0
- data/test/rails_app/app/controllers/users_controller.rb +23 -0
- data/test/rails_app/app/helpers/application_helper.rb +3 -0
- data/test/rails_app/app/mailers/users/mailer.rb +3 -0
- data/test/rails_app/app/mongoid/admin.rb +30 -0
- data/test/rails_app/app/mongoid/shim.rb +24 -0
- data/test/rails_app/app/mongoid/user.rb +45 -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.ru +4 -0
- data/test/rails_app/config/application.rb +41 -0
- data/test/rails_app/config/boot.rb +8 -0
- data/test/rails_app/config/database.yml +18 -0
- data/test/rails_app/config/environment.rb +5 -0
- data/test/rails_app/config/environments/development.rb +18 -0
- data/test/rails_app/config/environments/production.rb +33 -0
- data/test/rails_app/config/environments/test.rb +33 -0
- data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
- data/test/rails_app/config/initializers/devise.rb +186 -0
- data/test/rails_app/config/initializers/inflections.rb +2 -0
- data/test/rails_app/config/initializers/secret_token.rb +2 -0
- data/test/rails_app/config/routes.rb +90 -0
- data/test/rails_app/db/migrate/20100401102949_create_tables.rb +77 -0
- data/test/rails_app/db/schema.rb +52 -0
- data/test/rails_app/lib/shared_admin.rb +14 -0
- data/test/rails_app/lib/shared_user.rb +26 -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 +248 -0
- data/test/support/assertions.rb +42 -0
- data/test/support/helpers.rb +91 -0
- data/test/support/integration.rb +90 -0
- data/test/support/locale/en.yml +4 -0
- data/test/support/webrat/integrations/rails.rb +24 -0
- data/test/test_helper.rb +27 -0
- data/test/test_helpers_test.rb +134 -0
- metadata +451 -0
@@ -0,0 +1,269 @@
|
|
1
|
+
module Devise
|
2
|
+
module Controllers
|
3
|
+
# Those helpers are convenience methods added to ApplicationController.
|
4
|
+
module Helpers
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
helper_method :warden, :signed_in?, :devise_controller?
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def log_process_action(payload)
|
13
|
+
payload[:status] ||= 401 unless payload[:exception]
|
14
|
+
super
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Define authentication filters and accessor helpers based on mappings.
|
19
|
+
# These filters should be used inside the controllers as before_filters,
|
20
|
+
# so you can control the scope of the user who should be signed in to
|
21
|
+
# access that specific controller/action.
|
22
|
+
# Example:
|
23
|
+
#
|
24
|
+
# Roles:
|
25
|
+
# User
|
26
|
+
# Admin
|
27
|
+
#
|
28
|
+
# Generated methods:
|
29
|
+
# authenticate_user! # Signs user in or redirect
|
30
|
+
# authenticate_admin! # Signs admin in or redirect
|
31
|
+
# user_signed_in? # Checks whether there is a user signed in or not
|
32
|
+
# admin_signed_in? # Checks whether there is an admin signed in or not
|
33
|
+
# current_user # Current signed in user
|
34
|
+
# current_admin # Current signed in admin
|
35
|
+
# user_session # Session data available only to the user scope
|
36
|
+
# admin_session # Session data available only to the admin scope
|
37
|
+
#
|
38
|
+
# Use:
|
39
|
+
# before_filter :authenticate_user! # Tell devise to use :user map
|
40
|
+
# before_filter :authenticate_admin! # Tell devise to use :admin map
|
41
|
+
#
|
42
|
+
def self.define_helpers(mapping) #:nodoc:
|
43
|
+
mapping = mapping.name
|
44
|
+
|
45
|
+
class_eval <<-METHODS, __FILE__, __LINE__ + 1
|
46
|
+
def authenticate_#{mapping}!(opts={})
|
47
|
+
opts[:scope] = :#{mapping}
|
48
|
+
warden.authenticate!(opts) if !devise_controller? || opts.delete(:force)
|
49
|
+
end
|
50
|
+
|
51
|
+
def #{mapping}_signed_in?
|
52
|
+
!!current_#{mapping}
|
53
|
+
end
|
54
|
+
|
55
|
+
def current_#{mapping}
|
56
|
+
@current_#{mapping} ||= warden.authenticate(:scope => :#{mapping})
|
57
|
+
end
|
58
|
+
|
59
|
+
def #{mapping}_session
|
60
|
+
current_#{mapping} && warden.session(:#{mapping})
|
61
|
+
end
|
62
|
+
METHODS
|
63
|
+
|
64
|
+
ActiveSupport.on_load(:action_controller) do
|
65
|
+
helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# The main accessor for the warden proxy instance
|
70
|
+
def warden
|
71
|
+
request.env['warden']
|
72
|
+
end
|
73
|
+
|
74
|
+
# Return true if it's a devise_controller. false to all controllers unless
|
75
|
+
# the controllers defined inside devise. Useful if you want to apply a before
|
76
|
+
# filter to all controllers, except the ones in devise:
|
77
|
+
#
|
78
|
+
# before_filter :my_filter, :unless => :devise_controller?
|
79
|
+
def devise_controller?
|
80
|
+
is_a?(DeviseController)
|
81
|
+
end
|
82
|
+
|
83
|
+
# Tell warden that params authentication is allowed for that specific page.
|
84
|
+
def allow_params_authentication!
|
85
|
+
request.env["devise.allow_params_authentication"] = true
|
86
|
+
end
|
87
|
+
|
88
|
+
# Return true if the given scope is signed in session. If no scope given, return
|
89
|
+
# true if any scope is signed in. Does not run authentication hooks.
|
90
|
+
def signed_in?(scope=nil)
|
91
|
+
[ scope || Devise.mappings.keys ].flatten.any? do |scope|
|
92
|
+
warden.authenticate?(:scope => scope)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
# Sign in a user that already was authenticated. This helper is useful for logging
|
97
|
+
# users in after sign up.
|
98
|
+
#
|
99
|
+
# All options given to sign_in is passed forward to the set_user method in warden.
|
100
|
+
# The only exception is the :bypass option, which bypass warden callbacks and stores
|
101
|
+
# the user straight in session. This option is useful in cases the user is already
|
102
|
+
# signed in, but we want to refresh the credentials in session.
|
103
|
+
#
|
104
|
+
# Examples:
|
105
|
+
#
|
106
|
+
# sign_in :user, @user # sign_in(scope, resource)
|
107
|
+
# sign_in @user # sign_in(resource)
|
108
|
+
# sign_in @user, :event => :authentication # sign_in(resource, options)
|
109
|
+
# sign_in @user, :bypass => true # sign_in(resource, options)
|
110
|
+
#
|
111
|
+
def sign_in(resource_or_scope, *args)
|
112
|
+
options = args.extract_options!
|
113
|
+
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
114
|
+
resource = args.last || resource_or_scope
|
115
|
+
|
116
|
+
expire_session_data_after_sign_in!
|
117
|
+
|
118
|
+
if options[:bypass]
|
119
|
+
warden.session_serializer.store(resource, scope)
|
120
|
+
elsif warden.user(scope) == resource && !options.delete(:force)
|
121
|
+
# Do nothing. User already signed in and we are not forcing it.
|
122
|
+
true
|
123
|
+
else
|
124
|
+
warden.set_user(resource, options.merge!(:scope => scope))
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
# Sign out a given user or scope. This helper is useful for signing out a user
|
129
|
+
# after deleting accounts. Returns true if there was a logout and false if there is no user logged in
|
130
|
+
# on the referred scope
|
131
|
+
#
|
132
|
+
# Examples:
|
133
|
+
#
|
134
|
+
# sign_out :user # sign_out(scope)
|
135
|
+
# sign_out @user # sign_out(resource)
|
136
|
+
#
|
137
|
+
def sign_out(resource_or_scope=nil)
|
138
|
+
return sign_out_all_scopes unless resource_or_scope
|
139
|
+
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
140
|
+
user = warden.user(:scope => scope, :run_callbacks => false) # If there is no user
|
141
|
+
|
142
|
+
warden.raw_session.inspect # Without this inspect here. The session does not clear.
|
143
|
+
warden.logout(scope)
|
144
|
+
instance_variable_set(:"@current_#{scope}", nil)
|
145
|
+
|
146
|
+
!!user
|
147
|
+
end
|
148
|
+
|
149
|
+
# Sign out all active users or scopes. This helper is useful for signing out all roles
|
150
|
+
# in one click. This signs out ALL scopes in warden. Returns true if there was at least one logout
|
151
|
+
# and false if there was no user logged in on all scopes.
|
152
|
+
def sign_out_all_scopes
|
153
|
+
users = Devise.mappings.keys.map { |s| warden.user(:scope => s, :run_callbacks => false) }
|
154
|
+
|
155
|
+
warden.raw_session.inspect
|
156
|
+
warden.logout
|
157
|
+
expire_devise_cached_variables!
|
158
|
+
|
159
|
+
users.any?
|
160
|
+
end
|
161
|
+
|
162
|
+
# Returns and delete the url stored in the session for the given scope. Useful
|
163
|
+
# for giving redirect backs after sign up:
|
164
|
+
#
|
165
|
+
# Example:
|
166
|
+
#
|
167
|
+
# redirect_to stored_location_for(:user) || root_path
|
168
|
+
#
|
169
|
+
def stored_location_for(resource_or_scope)
|
170
|
+
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
171
|
+
session.delete("#{scope}_return_to")
|
172
|
+
end
|
173
|
+
|
174
|
+
# The scope root url to be used when he's signed in. By default, it first
|
175
|
+
# tries to find a resource_root_path, otherwise it uses the root_path.
|
176
|
+
def signed_in_root_path(resource_or_scope)
|
177
|
+
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
178
|
+
home_path = "#{scope}_root_path"
|
179
|
+
if respond_to?(home_path, true)
|
180
|
+
send(home_path)
|
181
|
+
elsif respond_to?(:root_path)
|
182
|
+
root_path
|
183
|
+
else
|
184
|
+
"/"
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
# The default url to be used after signing in. This is used by all Devise
|
189
|
+
# controllers and you can overwrite it in your ApplicationController to
|
190
|
+
# provide a custom hook for a custom resource.
|
191
|
+
#
|
192
|
+
# By default, it first tries to find a valid resource_return_to key in the
|
193
|
+
# session, then it fallbacks to resource_root_path, otherwise it uses the
|
194
|
+
# root path. For a user scope, you can define the default url in
|
195
|
+
# the following way:
|
196
|
+
#
|
197
|
+
# map.user_root '/users', :controller => 'users' # creates user_root_path
|
198
|
+
#
|
199
|
+
# map.namespace :user do |user|
|
200
|
+
# user.root :controller => 'users' # creates user_root_path
|
201
|
+
# end
|
202
|
+
#
|
203
|
+
# If the resource root path is not defined, root_path is used. However,
|
204
|
+
# if this default is not enough, you can customize it, for example:
|
205
|
+
#
|
206
|
+
# def after_sign_in_path_for(resource)
|
207
|
+
# stored_location_for(resource) ||
|
208
|
+
# if resource.is_a?(User) && resource.can_publish?
|
209
|
+
# publisher_url
|
210
|
+
# else
|
211
|
+
# super
|
212
|
+
# end
|
213
|
+
# end
|
214
|
+
#
|
215
|
+
def after_sign_in_path_for(resource_or_scope)
|
216
|
+
stored_location_for(resource_or_scope) || signed_in_root_path(resource_or_scope)
|
217
|
+
end
|
218
|
+
|
219
|
+
# Method used by sessions controller to sign out a user. You can overwrite
|
220
|
+
# it in your ApplicationController to provide a custom hook for a custom
|
221
|
+
# scope. Notice that differently from +after_sign_in_path_for+ this method
|
222
|
+
# receives a symbol with the scope, and not the resource.
|
223
|
+
#
|
224
|
+
# By default it is the root_path.
|
225
|
+
def after_sign_out_path_for(resource_or_scope)
|
226
|
+
respond_to?(:root_path) ? root_path : "/"
|
227
|
+
end
|
228
|
+
|
229
|
+
# Sign in a user and tries to redirect first to the stored location and
|
230
|
+
# then to the url specified by after_sign_in_path_for. It accepts the same
|
231
|
+
# parameters as the sign_in method.
|
232
|
+
def sign_in_and_redirect(resource_or_scope, *args)
|
233
|
+
options = args.extract_options!
|
234
|
+
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
235
|
+
resource = args.last || resource_or_scope
|
236
|
+
sign_in(scope, resource, options)
|
237
|
+
redirect_to after_sign_in_path_for(resource)
|
238
|
+
end
|
239
|
+
|
240
|
+
def expire_session_data_after_sign_in!
|
241
|
+
session.keys.grep(/^devise\./).each { |k| session.delete(k) }
|
242
|
+
end
|
243
|
+
|
244
|
+
# Sign out a user and tries to redirect to the url specified by
|
245
|
+
# after_sign_out_path_for.
|
246
|
+
def sign_out_and_redirect(resource_or_scope)
|
247
|
+
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
248
|
+
redirect_path = after_sign_out_path_for(scope)
|
249
|
+
Devise.sign_out_all_scopes ? sign_out : sign_out(scope)
|
250
|
+
redirect_to redirect_path
|
251
|
+
end
|
252
|
+
|
253
|
+
# Overwrite Rails' handle unverified request to sign out all scopes,
|
254
|
+
# clear run strategies and remove cached variables.
|
255
|
+
def handle_unverified_request
|
256
|
+
sign_out_all_scopes
|
257
|
+
warden.clear_strategies_cache!
|
258
|
+
expire_devise_cached_variables!
|
259
|
+
super # call the default behaviour which resets the session
|
260
|
+
end
|
261
|
+
|
262
|
+
private
|
263
|
+
|
264
|
+
def expire_devise_cached_variables!
|
265
|
+
Devise.mappings.each { |_,m| instance_variable_set("@current_#{m.name}", nil) }
|
266
|
+
end
|
267
|
+
end
|
268
|
+
end
|
269
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Devise
|
2
|
+
module Controllers
|
3
|
+
# A module that may be optionally included in a controller in order
|
4
|
+
# to provide remember me behavior.
|
5
|
+
module Rememberable
|
6
|
+
# Return default cookie values retrieved from session options.
|
7
|
+
def self.cookie_values
|
8
|
+
Rails.configuration.session_options.slice(:path, :domain, :secure)
|
9
|
+
end
|
10
|
+
|
11
|
+
# A small warden proxy so we can remember and forget uses from hooks.
|
12
|
+
class Proxy #:nodoc:
|
13
|
+
include Devise::Controllers::Rememberable
|
14
|
+
|
15
|
+
delegate :cookies, :env, :to => :@warden
|
16
|
+
|
17
|
+
def initialize(warden)
|
18
|
+
@warden = warden
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Remembers the given resource by setting up a cookie
|
23
|
+
def remember_me(resource)
|
24
|
+
scope = Devise::Mapping.find_scope!(resource)
|
25
|
+
resource.remember_me!(resource.extend_remember_period)
|
26
|
+
cookies.signed["remember_#{scope}_token"] = remember_cookie_values(resource)
|
27
|
+
end
|
28
|
+
|
29
|
+
# Forgets the given resource by deleting a cookie
|
30
|
+
def forget_me(resource)
|
31
|
+
scope = Devise::Mapping.find_scope!(resource)
|
32
|
+
resource.forget_me!
|
33
|
+
cookies.delete("remember_#{scope}_token", forget_cookie_values(resource))
|
34
|
+
end
|
35
|
+
|
36
|
+
protected
|
37
|
+
|
38
|
+
def forget_cookie_values(resource)
|
39
|
+
Devise::Controllers::Rememberable.cookie_values.merge!(resource.rememberable_options)
|
40
|
+
end
|
41
|
+
|
42
|
+
def remember_cookie_values(resource)
|
43
|
+
options = { :httponly => true }
|
44
|
+
options.merge!(forget_cookie_values(resource))
|
45
|
+
options.merge!(
|
46
|
+
:value => resource.class.serialize_into_cookie(resource),
|
47
|
+
:expires => resource.remember_expires_at
|
48
|
+
)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Devise
|
2
|
+
module Controllers
|
3
|
+
module ScopedViews
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def scoped_views?
|
8
|
+
defined?(@scoped_views) ? @scoped_views : Devise.scoped_views
|
9
|
+
end
|
10
|
+
|
11
|
+
def scoped_views=(value)
|
12
|
+
@scoped_views = value
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module Devise
|
2
|
+
module Controllers
|
3
|
+
# Create url helpers to be used with resource/scope configuration. Acts as
|
4
|
+
# proxies to the generated routes created by devise.
|
5
|
+
# Resource param can be a string or symbol, a class, or an instance object.
|
6
|
+
# Example using a :user resource:
|
7
|
+
#
|
8
|
+
# new_session_path(:user) => new_user_session_path
|
9
|
+
# session_path(:user) => user_session_path
|
10
|
+
# destroy_session_path(:user) => destroy_user_session_path
|
11
|
+
#
|
12
|
+
# new_password_path(:user) => new_user_password_path
|
13
|
+
# password_path(:user) => user_password_path
|
14
|
+
# edit_password_path(:user) => edit_user_password_path
|
15
|
+
#
|
16
|
+
# new_confirmation_path(:user) => new_user_confirmation_path
|
17
|
+
# confirmation_path(:user) => user_confirmation_path
|
18
|
+
#
|
19
|
+
# Those helpers are included by default to ActionController::Base.
|
20
|
+
#
|
21
|
+
# In case you want to add such helpers to another class, you can do
|
22
|
+
# that as long as this new class includes both url_helpers and
|
23
|
+
# mounted_helpers. Example:
|
24
|
+
#
|
25
|
+
# include Rails.application.routes.url_helpers
|
26
|
+
# include Rails.application.routes.mounted_helpers
|
27
|
+
#
|
28
|
+
module UrlHelpers
|
29
|
+
def self.remove_helpers!
|
30
|
+
self.instance_methods.map(&:to_s).grep(/_(url|path)$/).each do |method|
|
31
|
+
remove_method method
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.generate_helpers!(routes=nil)
|
36
|
+
routes ||= begin
|
37
|
+
mappings = Devise.mappings.values.map(&:used_helpers).flatten.uniq
|
38
|
+
Devise::URL_HELPERS.slice(*mappings)
|
39
|
+
end
|
40
|
+
|
41
|
+
routes.each do |module_name, actions|
|
42
|
+
[:path, :url].each do |path_or_url|
|
43
|
+
actions.each do |action|
|
44
|
+
action = action ? "#{action}_" : ""
|
45
|
+
method = "#{action}#{module_name}_#{path_or_url}"
|
46
|
+
|
47
|
+
class_eval <<-URL_HELPERS, __FILE__, __LINE__ + 1
|
48
|
+
def #{method}(resource_or_scope, *args)
|
49
|
+
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
50
|
+
_devise_route_context.send("#{action}\#{scope}_#{module_name}_#{path_or_url}", *args)
|
51
|
+
end
|
52
|
+
URL_HELPERS
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
generate_helpers!(Devise::URL_HELPERS)
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def _devise_route_context
|
63
|
+
@_devise_route_context ||= send(Devise.available_router_name)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Devise
|
2
|
+
# Checks the scope in the given environment and returns the associated failure app.
|
3
|
+
class Delegator
|
4
|
+
def call(env)
|
5
|
+
failure_app(env).call(env)
|
6
|
+
end
|
7
|
+
|
8
|
+
def failure_app(env)
|
9
|
+
app = env["warden.options"] &&
|
10
|
+
(scope = env["warden.options"][:scope]) &&
|
11
|
+
Devise.mappings[scope.to_sym] &&
|
12
|
+
Devise.mappings[scope.to_sym].failure_app
|
13
|
+
|
14
|
+
app || Devise::FailureApp
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require "digest/sha2"
|
2
|
+
|
3
|
+
module Devise
|
4
|
+
module Encryptors
|
5
|
+
# = AuthlogicSha512
|
6
|
+
# Simulates Authlogic's default encryption mechanism.
|
7
|
+
# Warning: it uses Devise's stretches configuration to port Authlogic's one. Should be set to 20 in the initializer to simulate
|
8
|
+
# the default behavior.
|
9
|
+
class AuthlogicSha512 < Base
|
10
|
+
# Generates a default password digest based on salt, pepper and the
|
11
|
+
# incoming password.
|
12
|
+
def self.digest(password, stretches, salt, pepper)
|
13
|
+
digest = [password, salt].flatten.join('')
|
14
|
+
stretches.times { digest = Digest::SHA512.hexdigest(digest) }
|
15
|
+
digest
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Devise
|
2
|
+
# Implements a way of adding different encryptions.
|
3
|
+
# The class should implement a self.digest method that taks the following params:
|
4
|
+
# - password
|
5
|
+
# - stretches: the number of times the encryption will be applied
|
6
|
+
# - salt: the password salt as defined by devise
|
7
|
+
# - pepper: Devise config option
|
8
|
+
#
|
9
|
+
module Encryptors
|
10
|
+
class Base
|
11
|
+
def self.digest
|
12
|
+
raise NotImplemented
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.salt(stretches)
|
16
|
+
Devise.friendly_token[0,20]
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.compare(encrypted_password, password, stretches, salt, pepper)
|
20
|
+
Devise.secure_compare(encrypted_password, digest(password, stretches, salt, pepper))
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|