devise-bootstrap 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +31 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/app/controllers/devise/confirmations_controller.rb +47 -0
- data/app/controllers/devise/omniauth_callbacks_controller.rb +30 -0
- data/app/controllers/devise/passwords_controller.rb +70 -0
- data/app/controllers/devise/registrations_controller.rb +137 -0
- data/app/controllers/devise/sessions_controller.rb +53 -0
- data/app/controllers/devise/unlocks_controller.rb +46 -0
- data/app/controllers/devise_controller.rb +176 -0
- data/app/helpers/devise_helper.rb +25 -0
- data/app/mailers/devise/mailer.rb +20 -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 +29 -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-bootstrap.gemspec +30 -0
- data/gemfiles/Gemfile.rails-3.2-stable +29 -0
- data/gemfiles/Gemfile.rails-4.0-stable +29 -0
- data/gemfiles/Gemfile.rails-head +29 -0
- data/lib/devise/bootstrap.rb +7 -0
- data/lib/devise/bootstrap/version.rb +5 -0
- data/lib/devise/devise.rb +491 -0
- data/lib/devise/devise/controllers/helpers.rb +213 -0
- data/lib/devise/devise/controllers/rememberable.rb +47 -0
- data/lib/devise/devise/controllers/scoped_views.rb +17 -0
- data/lib/devise/devise/controllers/sign_in_out.rb +103 -0
- data/lib/devise/devise/controllers/store_location.rb +50 -0
- data/lib/devise/devise/controllers/url_helpers.rb +67 -0
- data/lib/devise/devise/delegator.rb +16 -0
- data/lib/devise/devise/failure_app.rb +205 -0
- data/lib/devise/devise/hooks/activatable.rb +11 -0
- data/lib/devise/devise/hooks/csrf_cleaner.rb +5 -0
- data/lib/devise/devise/hooks/forgetable.rb +9 -0
- data/lib/devise/devise/hooks/lockable.rb +7 -0
- data/lib/devise/devise/hooks/proxy.rb +21 -0
- data/lib/devise/devise/hooks/rememberable.rb +7 -0
- data/lib/devise/devise/hooks/timeoutable.rb +28 -0
- data/lib/devise/devise/hooks/trackable.rb +9 -0
- data/lib/devise/devise/mailers/helpers.rb +90 -0
- data/lib/devise/devise/mapping.rb +172 -0
- data/lib/devise/devise/models.rb +119 -0
- data/lib/devise/devise/models/authenticatable.rb +284 -0
- data/lib/devise/devise/models/confirmable.rb +295 -0
- data/lib/devise/devise/models/database_authenticatable.rb +164 -0
- data/lib/devise/devise/models/lockable.rb +196 -0
- data/lib/devise/devise/models/omniauthable.rb +27 -0
- data/lib/devise/devise/models/recoverable.rb +131 -0
- data/lib/devise/devise/models/registerable.rb +25 -0
- data/lib/devise/devise/models/rememberable.rb +129 -0
- data/lib/devise/devise/models/timeoutable.rb +49 -0
- data/lib/devise/devise/models/trackable.rb +35 -0
- data/lib/devise/devise/models/validatable.rb +66 -0
- data/lib/devise/devise/modules.rb +28 -0
- data/lib/devise/devise/omniauth.rb +28 -0
- data/lib/devise/devise/omniauth/config.rb +45 -0
- data/lib/devise/devise/omniauth/url_helpers.rb +18 -0
- data/lib/devise/devise/orm/active_record.rb +3 -0
- data/lib/devise/devise/orm/mongoid.rb +3 -0
- data/lib/devise/devise/parameter_filter.rb +40 -0
- data/lib/devise/devise/parameter_sanitizer.rb +99 -0
- data/lib/devise/devise/rails.rb +56 -0
- data/lib/devise/devise/rails/routes.rb +496 -0
- data/lib/devise/devise/rails/warden_compat.rb +22 -0
- data/lib/devise/devise/strategies/authenticatable.rb +167 -0
- data/lib/devise/devise/strategies/base.rb +20 -0
- data/lib/devise/devise/strategies/database_authenticatable.rb +23 -0
- data/lib/devise/devise/strategies/rememberable.rb +55 -0
- data/lib/devise/devise/test_helpers.rb +132 -0
- data/lib/devise/devise/time_inflector.rb +14 -0
- data/lib/devise/devise/token_generator.rb +70 -0
- data/lib/devise/devise/version.rb +3 -0
- data/lib/devise/generators/active_record/devise_generator.rb +73 -0
- data/lib/devise/generators/active_record/templates/migration.rb +18 -0
- data/lib/devise/generators/active_record/templates/migration_existing.rb +25 -0
- data/lib/devise/generators/devise/devise_generator.rb +26 -0
- data/lib/devise/generators/devise/install_generator.rb +29 -0
- data/lib/devise/generators/devise/orm_helpers.rb +51 -0
- data/lib/devise/generators/devise/views_generator.rb +135 -0
- data/lib/devise/generators/mongoid/devise_generator.rb +55 -0
- data/lib/devise/generators/templates/README +35 -0
- data/lib/devise/generators/templates/devise.rb +260 -0
- data/lib/devise/generators/templates/markerb/confirmation_instructions.markerb +5 -0
- data/lib/devise/generators/templates/markerb/reset_password_instructions.markerb +8 -0
- data/lib/devise/generators/templates/markerb/unlock_instructions.markerb +7 -0
- data/lib/devise/generators/templates/simple_form_for/confirmations/new.html.erb +16 -0
- data/lib/devise/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
- data/lib/devise/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
- data/lib/devise/generators/templates/simple_form_for/registrations/edit.html.erb +27 -0
- data/lib/devise/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
- data/lib/devise/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
- data/lib/devise/generators/templates/simple_form_for/unlocks/new.html.erb +16 -0
- metadata +250 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
module Devise
|
|
2
|
+
module Controllers
|
|
3
|
+
# Those helpers are convenience methods added to ApplicationController.
|
|
4
|
+
module Helpers
|
|
5
|
+
extend ActiveSupport::Concern
|
|
6
|
+
include Devise::Controllers::SignInOut
|
|
7
|
+
include Devise::Controllers::StoreLocation
|
|
8
|
+
|
|
9
|
+
included do
|
|
10
|
+
helper_method :warden, :signed_in?, :devise_controller?
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
module ClassMethods
|
|
14
|
+
def log_process_action(payload)
|
|
15
|
+
payload[:status] ||= 401 unless payload[:exception]
|
|
16
|
+
super
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Define authentication filters and accessor helpers based on mappings.
|
|
21
|
+
# These filters should be used inside the controllers as before_filters,
|
|
22
|
+
# so you can control the scope of the user who should be signed in to
|
|
23
|
+
# access that specific controller/action.
|
|
24
|
+
# Example:
|
|
25
|
+
#
|
|
26
|
+
# Roles:
|
|
27
|
+
# User
|
|
28
|
+
# Admin
|
|
29
|
+
#
|
|
30
|
+
# Generated methods:
|
|
31
|
+
# authenticate_user! # Signs user in or redirect
|
|
32
|
+
# authenticate_admin! # Signs admin in or redirect
|
|
33
|
+
# user_signed_in? # Checks whether there is a user signed in or not
|
|
34
|
+
# admin_signed_in? # Checks whether there is an admin signed in or not
|
|
35
|
+
# current_user # Current signed in user
|
|
36
|
+
# current_admin # Current signed in admin
|
|
37
|
+
# user_session # Session data available only to the user scope
|
|
38
|
+
# admin_session # Session data available only to the admin scope
|
|
39
|
+
#
|
|
40
|
+
# Use:
|
|
41
|
+
# before_filter :authenticate_user! # Tell devise to use :user map
|
|
42
|
+
# before_filter :authenticate_admin! # Tell devise to use :admin map
|
|
43
|
+
#
|
|
44
|
+
def self.define_helpers(mapping) #:nodoc:
|
|
45
|
+
mapping = mapping.name
|
|
46
|
+
|
|
47
|
+
class_eval <<-METHODS, __FILE__, __LINE__ + 1
|
|
48
|
+
def authenticate_#{mapping}!(opts={})
|
|
49
|
+
opts[:scope] = :#{mapping}
|
|
50
|
+
warden.authenticate!(opts) if !devise_controller? || opts.delete(:force)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def #{mapping}_signed_in?
|
|
54
|
+
!!current_#{mapping}
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def current_#{mapping}
|
|
58
|
+
@current_#{mapping} ||= warden.authenticate(scope: :#{mapping})
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def #{mapping}_session
|
|
62
|
+
current_#{mapping} && warden.session(:#{mapping})
|
|
63
|
+
end
|
|
64
|
+
METHODS
|
|
65
|
+
|
|
66
|
+
ActiveSupport.on_load(:action_controller) do
|
|
67
|
+
helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session"
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# The main accessor for the warden proxy instance
|
|
72
|
+
def warden
|
|
73
|
+
request.env['warden']
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Return true if it's a devise_controller. false to all controllers unless
|
|
77
|
+
# the controllers defined inside devise. Useful if you want to apply a before
|
|
78
|
+
# filter to all controllers, except the ones in devise:
|
|
79
|
+
#
|
|
80
|
+
# before_filter :my_filter, unless: :devise_controller?
|
|
81
|
+
def devise_controller?
|
|
82
|
+
is_a?(::DeviseController)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Setup a param sanitizer to filter parameters using strong_parameters. See
|
|
86
|
+
# lib/devise/parameter_sanitizer.rb for more info. Override this
|
|
87
|
+
# method in your application controller to use your own parameter sanitizer.
|
|
88
|
+
def devise_parameter_sanitizer
|
|
89
|
+
@devise_parameter_sanitizer ||= if defined?(ActionController::StrongParameters)
|
|
90
|
+
Devise::ParameterSanitizer.new(resource_class, resource_name, params)
|
|
91
|
+
else
|
|
92
|
+
Devise::BaseSanitizer.new(resource_class, resource_name, params)
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Tell warden that params authentication is allowed for that specific page.
|
|
97
|
+
def allow_params_authentication!
|
|
98
|
+
request.env["devise.allow_params_authentication"] = true
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# The scope root url to be used when they're signed in. By default, it first
|
|
102
|
+
# tries to find a resource_root_path, otherwise it uses the root_path.
|
|
103
|
+
def signed_in_root_path(resource_or_scope)
|
|
104
|
+
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
|
105
|
+
home_path = "#{scope}_root_path"
|
|
106
|
+
if respond_to?(home_path, true)
|
|
107
|
+
send(home_path)
|
|
108
|
+
elsif respond_to?(:root_path)
|
|
109
|
+
root_path
|
|
110
|
+
else
|
|
111
|
+
"/"
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# The default url to be used after signing in. This is used by all Devise
|
|
116
|
+
# controllers and you can overwrite it in your ApplicationController to
|
|
117
|
+
# provide a custom hook for a custom resource.
|
|
118
|
+
#
|
|
119
|
+
# By default, it first tries to find a valid resource_return_to key in the
|
|
120
|
+
# session, then it fallbacks to resource_root_path, otherwise it uses the
|
|
121
|
+
# root path. For a user scope, you can define the default url in
|
|
122
|
+
# the following way:
|
|
123
|
+
#
|
|
124
|
+
# map.user_root '/users', controller: 'users' # creates user_root_path
|
|
125
|
+
#
|
|
126
|
+
# map.namespace :user do |user|
|
|
127
|
+
# user.root controller: 'users' # creates user_root_path
|
|
128
|
+
# end
|
|
129
|
+
#
|
|
130
|
+
# If the resource root path is not defined, root_path is used. However,
|
|
131
|
+
# if this default is not enough, you can customize it, for example:
|
|
132
|
+
#
|
|
133
|
+
# def after_sign_in_path_for(resource)
|
|
134
|
+
# stored_location_for(resource) ||
|
|
135
|
+
# if resource.is_a?(User) && resource.can_publish?
|
|
136
|
+
# publisher_url
|
|
137
|
+
# else
|
|
138
|
+
# super
|
|
139
|
+
# end
|
|
140
|
+
# end
|
|
141
|
+
#
|
|
142
|
+
def after_sign_in_path_for(resource_or_scope)
|
|
143
|
+
stored_location_for(resource_or_scope) || signed_in_root_path(resource_or_scope)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# Method used by sessions controller to sign out a user. You can overwrite
|
|
147
|
+
# it in your ApplicationController to provide a custom hook for a custom
|
|
148
|
+
# scope. Notice that differently from +after_sign_in_path_for+ this method
|
|
149
|
+
# receives a symbol with the scope, and not the resource.
|
|
150
|
+
#
|
|
151
|
+
# By default it is the root_path.
|
|
152
|
+
def after_sign_out_path_for(resource_or_scope)
|
|
153
|
+
respond_to?(:root_path) ? root_path : "/"
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# Sign in a user and tries to redirect first to the stored location and
|
|
157
|
+
# then to the url specified by after_sign_in_path_for. It accepts the same
|
|
158
|
+
# parameters as the sign_in method.
|
|
159
|
+
def sign_in_and_redirect(resource_or_scope, *args)
|
|
160
|
+
options = args.extract_options!
|
|
161
|
+
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
|
162
|
+
resource = args.last || resource_or_scope
|
|
163
|
+
sign_in(scope, resource, options)
|
|
164
|
+
redirect_to after_sign_in_path_for(resource)
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# Sign out a user and tries to redirect to the url specified by
|
|
168
|
+
# after_sign_out_path_for.
|
|
169
|
+
def sign_out_and_redirect(resource_or_scope)
|
|
170
|
+
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
|
171
|
+
redirect_path = after_sign_out_path_for(scope)
|
|
172
|
+
Devise.sign_out_all_scopes ? sign_out : sign_out(scope)
|
|
173
|
+
redirect_to redirect_path
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
# Overwrite Rails' handle unverified request to sign out all scopes,
|
|
177
|
+
# clear run strategies and remove cached variables.
|
|
178
|
+
def handle_unverified_request
|
|
179
|
+
sign_out_all_scopes(false)
|
|
180
|
+
request.env["devise.skip_storage"] = true
|
|
181
|
+
expire_data_after_sign_out!
|
|
182
|
+
super # call the default behaviour which resets the session
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def request_format
|
|
186
|
+
@request_format ||= request.format.try(:ref)
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
def is_navigational_format?
|
|
190
|
+
Devise.navigational_formats.include?(request_format)
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
# Check if flash messages should be emitted. Default is to do it on
|
|
194
|
+
# navigational formats
|
|
195
|
+
def is_flashing_format?
|
|
196
|
+
is_navigational_format?
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
private
|
|
200
|
+
|
|
201
|
+
def expire_session_data_after_sign_in!
|
|
202
|
+
ActiveSupport::Deprecation.warn "expire_session_data_after_sign_in! is deprecated " \
|
|
203
|
+
"in favor of expire_data_after_sign_in!"
|
|
204
|
+
expire_data_after_sign_in!
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def expire_data_after_sign_out!
|
|
208
|
+
Devise.mappings.each { |_,m| instance_variable_set("@current_#{m.name}", nil) }
|
|
209
|
+
super
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
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. Useful when signing in is done
|
|
5
|
+
# through a callback, like in Omniauth.
|
|
6
|
+
module Rememberable
|
|
7
|
+
# Return default cookie values retrieved from session options.
|
|
8
|
+
def self.cookie_values
|
|
9
|
+
Rails.configuration.session_options.slice(:path, :domain, :secure)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# Remembers the given resource by setting up a cookie
|
|
13
|
+
def remember_me(resource)
|
|
14
|
+
return if env["devise.skip_storage"]
|
|
15
|
+
scope = Devise::Mapping.find_scope!(resource)
|
|
16
|
+
resource.remember_me!(resource.extend_remember_period)
|
|
17
|
+
cookies.signed[remember_key(resource, scope)] = remember_cookie_values(resource)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Forgets the given resource by deleting a cookie
|
|
21
|
+
def forget_me(resource)
|
|
22
|
+
scope = Devise::Mapping.find_scope!(resource)
|
|
23
|
+
resource.forget_me!
|
|
24
|
+
cookies.delete(remember_key(resource, scope), forget_cookie_values(resource))
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
protected
|
|
28
|
+
|
|
29
|
+
def forget_cookie_values(resource)
|
|
30
|
+
Devise::Controllers::Rememberable.cookie_values.merge!(resource.rememberable_options)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def remember_cookie_values(resource)
|
|
34
|
+
options = { httponly: true }
|
|
35
|
+
options.merge!(forget_cookie_values(resource))
|
|
36
|
+
options.merge!(
|
|
37
|
+
value: resource.class.serialize_into_cookie(resource),
|
|
38
|
+
expires: resource.remember_expires_at
|
|
39
|
+
)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def remember_key(resource, scope)
|
|
43
|
+
resource.rememberable_options.fetch(:key, "remember_#{scope}_token")
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
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,103 @@
|
|
|
1
|
+
module Devise
|
|
2
|
+
module Controllers
|
|
3
|
+
# Provide sign in and sign out functionality.
|
|
4
|
+
# Included by default in all controllers.
|
|
5
|
+
module SignInOut
|
|
6
|
+
# Return true if the given scope is signed in session. If no scope given, return
|
|
7
|
+
# true if any scope is signed in. Does not run authentication hooks.
|
|
8
|
+
def signed_in?(scope=nil)
|
|
9
|
+
[ scope || Devise.mappings.keys ].flatten.any? do |_scope|
|
|
10
|
+
warden.authenticate?(scope: _scope)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Sign in a user that already was authenticated. This helper is useful for logging
|
|
15
|
+
# users in after sign up.
|
|
16
|
+
#
|
|
17
|
+
# All options given to sign_in is passed forward to the set_user method in warden.
|
|
18
|
+
# The only exception is the :bypass option, which bypass warden callbacks and stores
|
|
19
|
+
# the user straight in session. This option is useful in cases the user is already
|
|
20
|
+
# signed in, but we want to refresh the credentials in session.
|
|
21
|
+
#
|
|
22
|
+
# Examples:
|
|
23
|
+
#
|
|
24
|
+
# sign_in :user, @user # sign_in(scope, resource)
|
|
25
|
+
# sign_in @user # sign_in(resource)
|
|
26
|
+
# sign_in @user, event: :authentication # sign_in(resource, options)
|
|
27
|
+
# sign_in @user, store: false # sign_in(resource, options)
|
|
28
|
+
# sign_in @user, bypass: true # sign_in(resource, options)
|
|
29
|
+
#
|
|
30
|
+
def sign_in(resource_or_scope, *args)
|
|
31
|
+
options = args.extract_options!
|
|
32
|
+
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
|
33
|
+
resource = args.last || resource_or_scope
|
|
34
|
+
|
|
35
|
+
expire_data_after_sign_in!
|
|
36
|
+
|
|
37
|
+
if options[:bypass]
|
|
38
|
+
warden.session_serializer.store(resource, scope)
|
|
39
|
+
elsif warden.user(scope) == resource && !options.delete(:force)
|
|
40
|
+
# Do nothing. User already signed in and we are not forcing it.
|
|
41
|
+
true
|
|
42
|
+
else
|
|
43
|
+
warden.set_user(resource, options.merge!(scope: scope))
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Sign out a given user or scope. This helper is useful for signing out a user
|
|
48
|
+
# after deleting accounts. Returns true if there was a logout and false if there
|
|
49
|
+
# is no user logged in on the referred scope
|
|
50
|
+
#
|
|
51
|
+
# Examples:
|
|
52
|
+
#
|
|
53
|
+
# sign_out :user # sign_out(scope)
|
|
54
|
+
# sign_out @user # sign_out(resource)
|
|
55
|
+
#
|
|
56
|
+
def sign_out(resource_or_scope=nil)
|
|
57
|
+
return sign_out_all_scopes unless resource_or_scope
|
|
58
|
+
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
|
59
|
+
user = warden.user(scope: scope, run_callbacks: false) # If there is no user
|
|
60
|
+
|
|
61
|
+
warden.raw_session.inspect # Without this inspect here. The session does not clear.
|
|
62
|
+
warden.logout(scope)
|
|
63
|
+
warden.clear_strategies_cache!(scope: scope)
|
|
64
|
+
instance_variable_set(:"@current_#{scope}", nil)
|
|
65
|
+
|
|
66
|
+
!!user
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Sign out all active users or scopes. This helper is useful for signing out all roles
|
|
70
|
+
# in one click. This signs out ALL scopes in warden. Returns true if there was at least one logout
|
|
71
|
+
# and false if there was no user logged in on all scopes.
|
|
72
|
+
def sign_out_all_scopes(lock=true)
|
|
73
|
+
users = Devise.mappings.keys.map { |s| warden.user(scope: s, run_callbacks: false) }
|
|
74
|
+
|
|
75
|
+
warden.raw_session.inspect
|
|
76
|
+
warden.logout
|
|
77
|
+
expire_data_after_sign_out!
|
|
78
|
+
warden.clear_strategies_cache!
|
|
79
|
+
warden.lock! if lock
|
|
80
|
+
|
|
81
|
+
users.any?
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
private
|
|
85
|
+
|
|
86
|
+
def expire_data_after_sign_in!
|
|
87
|
+
# session.keys will return an empty array if the session is not yet loaded.
|
|
88
|
+
# This is a bug in both Rack and Rails.
|
|
89
|
+
# A call to #empty? forces the session to be loaded.
|
|
90
|
+
session.empty?
|
|
91
|
+
session.keys.grep(/^devise\./).each { |k| session.delete(k) }
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def expire_data_after_sign_out!
|
|
95
|
+
# session.keys will return an empty array if the session is not yet loaded.
|
|
96
|
+
# This is a bug in both Rack and Rails.
|
|
97
|
+
# A call to #empty? forces the session to be loaded.
|
|
98
|
+
session.empty?
|
|
99
|
+
session.keys.grep(/^devise\./).each { |k| session.delete(k) }
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
require "uri"
|
|
2
|
+
|
|
3
|
+
module Devise
|
|
4
|
+
module Controllers
|
|
5
|
+
# Provide the ability to store a location.
|
|
6
|
+
# Used to redirect back to a desired path after sign in.
|
|
7
|
+
# Included by default in all controllers.
|
|
8
|
+
module StoreLocation
|
|
9
|
+
# Returns and delete (if it's navigational format) the url stored in the session for
|
|
10
|
+
# the given scope. Useful for giving redirect backs after sign up:
|
|
11
|
+
#
|
|
12
|
+
# Example:
|
|
13
|
+
#
|
|
14
|
+
# redirect_to stored_location_for(:user) || root_path
|
|
15
|
+
#
|
|
16
|
+
def stored_location_for(resource_or_scope)
|
|
17
|
+
session_key = stored_location_key_for(resource_or_scope)
|
|
18
|
+
|
|
19
|
+
if is_navigational_format?
|
|
20
|
+
session.delete(session_key)
|
|
21
|
+
else
|
|
22
|
+
session[session_key]
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Stores the provided location to redirect the user after signing in.
|
|
27
|
+
# Useful in combination with the `stored_location_for` helper.
|
|
28
|
+
#
|
|
29
|
+
# Example:
|
|
30
|
+
#
|
|
31
|
+
# store_location_for(:user, dashboard_path)
|
|
32
|
+
# redirect_to user_omniauth_authorize_path(:facebook)
|
|
33
|
+
#
|
|
34
|
+
def store_location_for(resource_or_scope, location)
|
|
35
|
+
session_key = stored_location_key_for(resource_or_scope)
|
|
36
|
+
if location
|
|
37
|
+
uri = URI.parse(location)
|
|
38
|
+
session[session_key] = [uri.path.sub(/\A\/+/, '/'), uri.query].compact.join('?')
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
def stored_location_key_for(resource_or_scope)
|
|
45
|
+
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
|
46
|
+
"#{scope}_return_to"
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
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
|