switch_user 1.3.1 → 1.5.3
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 +5 -5
- data/CHANGELOG.md +20 -4
- data/Gemfile +2 -0
- data/README.md +23 -21
- data/Rakefile +2 -0
- data/app/controllers/switch_user_controller.rb +25 -11
- data/app/helpers/switch_user_helper.rb +16 -19
- data/app/views/switch_user/_widget.html.erb +2 -2
- data/config/routes.rb +4 -2
- data/lib/generators/switch_user/install/install_generator.rb +3 -2
- data/lib/generators/switch_user/install/templates/switch_user.rb +8 -6
- data/lib/switch_user.rb +22 -21
- data/lib/switch_user/base_guard.rb +7 -5
- data/lib/switch_user/data_source.rb +8 -8
- data/lib/switch_user/lambda_guard.rb +2 -0
- data/lib/switch_user/provider.rb +20 -10
- data/lib/switch_user/provider/authlogic.rb +6 -6
- data/lib/switch_user/provider/base.rb +7 -8
- data/lib/switch_user/provider/clearance.rb +8 -6
- data/lib/switch_user/provider/devise.rb +10 -4
- data/lib/switch_user/provider/dummy.rb +8 -12
- data/lib/switch_user/provider/restful_authentication.rb +5 -3
- data/lib/switch_user/provider/session.rb +5 -3
- data/lib/switch_user/provider/sorcery.rb +8 -9
- data/lib/switch_user/rails.rb +7 -3
- data/lib/switch_user/rspec.rb +4 -4
- data/lib/switch_user/rspec/feature_helpers.rb +13 -14
- data/lib/switch_user/user_loader.rb +5 -2
- data/lib/switch_user/user_set.rb +14 -9
- data/lib/switch_user/version.rb +3 -1
- data/spec/controllers/switch_user_controller_spec.rb +24 -27
- data/spec/helpers/switch_user_helper_spec.rb +58 -55
- data/spec/integration/switch_user_spec.rb +105 -21
- data/spec/provider/authlogic_spec.rb +3 -1
- data/spec/provider/clearance_spec.rb +3 -1
- data/spec/provider/devise_spec.rb +19 -18
- data/spec/provider/dummy_spec.rb +4 -3
- data/spec/provider/restful_authentication_spec.rb +3 -1
- data/spec/provider/session_spec.rb +4 -2
- data/spec/provider/sorcery_spec.rb +3 -1
- data/spec/provider_spec.rb +3 -1
- data/spec/rspec/feature_helpers_spec.rb +38 -37
- data/spec/spec_helper.rb +4 -2
- data/spec/support/application.rb +35 -23
- data/spec/support/provider.rb +13 -11
- data/spec/switch_user/data_source_spec.rb +7 -5
- data/spec/switch_user/lambda_guard_spec.rb +6 -4
- data/spec/switch_user/user_loader_spec.rb +23 -23
- data/spec/switch_user/user_set_spec.rb +12 -10
- data/spec/switch_user_spec.rb +6 -4
- data/switch_user.gemspec +24 -23
- metadata +28 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4211c3d6aa5b7360b61069cc07798462adf5341a7d139809c8435de17854b617
|
4
|
+
data.tar.gz: b63ea0d7464484cbb099153c2f96f4a100f907a719647dd8f45d8461455c8afe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1286a0855b08342856252ac8b7db07cf9cbb0c65b01c702c4d8aac7d59ace250bf51957606183843ea5fb3d0be1c312f2de2bcc58cedb7c494f6f85b69c8b50c
|
7
|
+
data.tar.gz: d52d65016d91198f69e78dedc9dfff53c788debe24a589dfc98619621ad054abf774f31df87289dd067fff948130fe5126c17737f1d45a44f8e667ce0a10b35f
|
data/CHANGELOG.md
CHANGED
@@ -1,7 +1,26 @@
|
|
1
1
|
# Next Release
|
2
2
|
|
3
|
-
## 1.3
|
3
|
+
## 1.5.3 (10/20/2020)
|
4
4
|
|
5
|
+
* Fix zeitwerk autloading issue
|
6
|
+
|
7
|
+
## 1.5.2 (03/01/2019)
|
8
|
+
|
9
|
+
* Clearance has moved their controller methods to be private
|
10
|
+
|
11
|
+
## 1.5.1 (10/28/2018)
|
12
|
+
|
13
|
+
* Respect identifier when storing `original_user`
|
14
|
+
* Refactor code by awesomecode.io
|
15
|
+
|
16
|
+
## 1.5.0 (10/27/2017)
|
17
|
+
|
18
|
+
* Allow `SwitchUser.switch_back` to be considered even when not calling `#remember_user`.
|
19
|
+
* Use `redirect_back` for rails 5
|
20
|
+
|
21
|
+
## 1.4.0 (10/21/2016)
|
22
|
+
|
23
|
+
* Add ability to `store_sign_in` info with devise provider
|
5
24
|
* Respect `relative_url_root`
|
6
25
|
* Fix `selected_user_of_helper` bug
|
7
26
|
|
@@ -9,9 +28,6 @@
|
|
9
28
|
|
10
29
|
* Add capybara support
|
11
30
|
* Add class and style options to `switch_user_select`
|
12
|
-
|
13
|
-
## 1.2.1 (12/22/2015)
|
14
|
-
|
15
31
|
* Fix `grouped_options_for_select` for rails 3.2
|
16
32
|
|
17
33
|
## 1.2.0 (12/01/2015)
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# switch_user
|
2
2
|
|
3
|
-
[](http://travis-ci.org/flyerhzm/switch_user)
|
4
|
+
[](https://awesomecode.io/repos/flyerhzm/switch_user)
|
4
5
|
|
5
6
|
Inspired from [hobo][0], switch_user provides a convenient way to switch current user without needing to log out and log in manually.
|
6
7
|
|
@@ -14,7 +15,7 @@ switch_user is very useful in such use cases
|
|
14
15
|
|
15
16
|
## Example
|
16
17
|
|
17
|
-
Visit here: <http://switch-user-example.
|
18
|
+
Visit here: <http://switch-user-example.herokuapp.com/admin>, switch the current user in the select box.
|
18
19
|
|
19
20
|
And source code here: <https://github.com/flyerhzm/switch_user_example>
|
20
21
|
|
@@ -24,6 +25,9 @@ Add in Gemfile.
|
|
24
25
|
```ruby
|
25
26
|
gem "switch_user"
|
26
27
|
```
|
28
|
+
|
29
|
+
If you get the following error: **undefined method `before_action' for SwitchUserController:Class**, you are probably using an older version of Rails (<4). You can use this gem: https://github.com/pschambacher/rails3-before_action
|
30
|
+
|
27
31
|
## Usage
|
28
32
|
|
29
33
|
Add following code into your layout page.
|
@@ -63,13 +67,13 @@ get ':id' => 'pages#show'
|
|
63
67
|
By default, you can switch between Guest and all users in users table, you don't need to do anything. The following is some of the more commonly used configuration options.
|
64
68
|
```ruby
|
65
69
|
SwitchUser.setup do |config|
|
66
|
-
# provider may be :devise, :authlogic, :clearance, :restful_authentication or :
|
70
|
+
# provider may be :devise, :authlogic, :clearance, :restful_authentication, :sorcery, or {name: :devise, store_sign_in: true}
|
67
71
|
config.provider = :devise
|
68
72
|
|
69
73
|
# available_users is a hash,
|
70
74
|
# key is the model name of user (:user, :admin, or any name you use),
|
71
75
|
# value is a block that return the users that can be switched.
|
72
|
-
config.available_users = { :
|
76
|
+
config.available_users = { user: -> { User.all } } # use User.scoped instead for rails 3.2
|
73
77
|
|
74
78
|
# available_users_identifiers is a hash,
|
75
79
|
# keys in this hash should match a key in the available_users hash
|
@@ -77,55 +81,55 @@ SwitchUser.setup do |config|
|
|
77
81
|
# defaults to id
|
78
82
|
# this hash is to allow you to specify a different column to
|
79
83
|
# expose for instance a username on a User model instead of id
|
80
|
-
config.available_users_identifiers = { :
|
84
|
+
config.available_users_identifiers = { user: :id }
|
81
85
|
|
82
86
|
# available_users_names is a hash,
|
83
87
|
# keys in this hash should match a key in the available_users hash
|
84
88
|
# value is the column name which will be displayed in select box
|
85
|
-
config.available_users_names = { :
|
89
|
+
config.available_users_names = { user: :email }
|
86
90
|
|
87
91
|
# controller_guard is a block,
|
88
92
|
# if it returns true, the request will continue,
|
89
93
|
# else the request will be refused and returns "Permission Denied"
|
90
94
|
# if you switch from "admin" to user, the current_user param is "admin"
|
91
|
-
config.controller_guard =
|
95
|
+
config.controller_guard = ->(current_user, request) { Rails.env.development? }
|
92
96
|
|
93
97
|
# view_guard is a block,
|
94
98
|
# if it returns true, the switch user select box will be shown,
|
95
99
|
# else the select box will not be shown
|
96
100
|
# if you switch from admin to "user", the current_user param is "user"
|
97
|
-
config.view_guard =
|
101
|
+
config.view_guard = ->(current_user, request) { Rails.env.development? }
|
98
102
|
|
99
103
|
# redirect_path is a block, it returns which page will be redirected
|
100
104
|
# after switching a user.
|
101
|
-
config.redirect_path =
|
105
|
+
config.redirect_path = ->(request, params) { '/' }
|
102
106
|
end
|
103
107
|
```
|
104
108
|
If you need to override the default configuration, run <code>rails g switch_user:install</code> and a copy of the configuration file will be copied to <code>config/initializers/switch_user.rb</code> in your project.
|
105
109
|
|
106
110
|
If you want to switch both available users and available admins
|
107
111
|
```ruby
|
108
|
-
config.available_users = { :user =>
|
112
|
+
config.available_users = { :user => -> { User.available }, :admin => -> { Admin.available } }
|
109
113
|
```
|
110
114
|
If you want to use name column as the user identifier
|
111
115
|
```ruby
|
112
|
-
config.available_users_identifiers => { :
|
116
|
+
config.available_users_identifiers => { user: :name }
|
113
117
|
```
|
114
118
|
If you want to display the login field in switch user select box
|
115
119
|
```ruby
|
116
|
-
config.available_users_names = { :
|
120
|
+
config.available_users_names = { user: :login }
|
117
121
|
```
|
118
122
|
If you only allow switching from admin to user in production environment
|
119
123
|
```ruby
|
120
|
-
config.controller_guard =
|
124
|
+
config.controller_guard = ->(current_user, request) { Rails.env.production? && current_user.admin? }
|
121
125
|
```
|
122
126
|
If you only want to display switch user select box for admins in production environment
|
123
127
|
```ruby
|
124
|
-
config.view_guard =
|
128
|
+
config.view_guard = ->(current_user, request) { Rails.env.production? && current_user && current_user.admin? }
|
125
129
|
```
|
126
130
|
If you want to redirect user to "/dashboard" page
|
127
131
|
```ruby
|
128
|
-
config.redirect_path =
|
132
|
+
config.redirect_path = ->(request, params) { "/dashboard" }
|
129
133
|
```
|
130
134
|
If you want to hide a 'Guest' item in the helper dropdown list
|
131
135
|
```ruby
|
@@ -137,16 +141,14 @@ Sometimes you'll want to be able to switch to an unprivileged user and then back
|
|
137
141
|
You will need to make the following modifications to your configuration:
|
138
142
|
```ruby
|
139
143
|
config.switch_back = true
|
140
|
-
config.controller_guard =
|
141
|
-
current_user && current_user.admin? || original_user && original_user.super_admin?
|
142
|
-
}
|
144
|
+
config.controller_guard = ->(current_user, request, original_user) { current_user && current_user.admin? || original_user && original_user.super_admin? }
|
143
145
|
# Do something similar for the view_guard as well.
|
144
146
|
```
|
145
147
|
This example would allow an admin user to user switch_user, but would only let you switch back to another user if the original user was a super admin.
|
146
148
|
|
147
149
|
## Using SwitchUser with RSpec and Capybara
|
148
150
|
|
149
|
-
Add the following code to spec/support/switch_user.rb or spec/spec_helper.rb
|
151
|
+
Add the following code to spec/support/switch_user.rb or spec/spec_helper.rb:
|
150
152
|
|
151
153
|
```ruby
|
152
154
|
require 'switch_user/rspec'
|
@@ -157,7 +159,7 @@ You can now write your specs like so :
|
|
157
159
|
```ruby
|
158
160
|
feature "Your feature", type: :feature do
|
159
161
|
background do
|
160
|
-
@user = User.make(:
|
162
|
+
@user = User.make(email: 'user@example.com', password: 'password')
|
161
163
|
end
|
162
164
|
|
163
165
|
scenario "Your scenario" do
|
@@ -189,6 +191,6 @@ This feature should be used with extreme caution because of the security implica
|
|
189
191
|
|
190
192
|
## Credit
|
191
193
|
|
192
|
-
Copyright © 2010 -
|
194
|
+
Copyright © 2010 - 2017 Richard Huang (flyerhzm@gmail.com), released under the MIT license
|
193
195
|
|
194
196
|
[0]: https://github.com/tablatom/hobo
|
data/Rakefile
CHANGED
@@ -1,25 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class SwitchUserController < ApplicationController
|
2
|
-
before_action :developer_modes_only
|
4
|
+
before_action :developer_modes_only, :switch_back
|
3
5
|
|
4
6
|
def set_current_user
|
5
7
|
handle_request(params)
|
6
8
|
|
7
|
-
|
9
|
+
redirect_path = SwitchUser.redirect_path.call(request, params)
|
10
|
+
if Rails.version.to_i >= 5 && redirect_path == :back
|
11
|
+
redirect_back(fallback_location: root_path)
|
12
|
+
else
|
13
|
+
redirect_to(redirect_path)
|
14
|
+
end
|
8
15
|
end
|
9
16
|
|
10
17
|
def remember_user
|
11
|
-
|
12
|
-
if
|
13
|
-
|
18
|
+
redirect_path = SwitchUser.redirect_path.call(request, params)
|
19
|
+
if Rails.version.to_i >= 5 && redirect_path == :back
|
20
|
+
redirect_back(fallback_location: root_path)
|
21
|
+
else
|
22
|
+
redirect_to(redirect_path)
|
14
23
|
end
|
15
|
-
|
16
|
-
redirect_to(SwitchUser.redirect_path.call(request, params))
|
17
24
|
end
|
18
25
|
|
19
26
|
private
|
20
27
|
|
28
|
+
def switch_back
|
29
|
+
if SwitchUser.switch_back
|
30
|
+
provider.remember_current_user(true) if params[:remember] == 'true'
|
31
|
+
provider.remember_current_user(false) if params[:remember] == 'false'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
21
35
|
def developer_modes_only
|
22
|
-
raise ActionController::RoutingError
|
36
|
+
raise ActionController::RoutingError, 'Do not try to hack us.' unless available?
|
23
37
|
end
|
24
38
|
|
25
39
|
def available?
|
@@ -36,14 +50,14 @@ class SwitchUserController < ApplicationController
|
|
36
50
|
return
|
37
51
|
end
|
38
52
|
if SwitchUser.login_exclusive
|
39
|
-
provider.login_exclusive(record.user, :
|
53
|
+
provider.login_exclusive(record.user, scope: record.scope)
|
40
54
|
else
|
41
|
-
provider.login_inclusive(record.user, :
|
55
|
+
provider.login_inclusive(record.user, scope: record.scope)
|
42
56
|
end
|
43
57
|
end
|
44
58
|
end
|
45
59
|
|
46
|
-
# TODO make helper methods, so this can be eliminated from the
|
60
|
+
# TODO: make helper methods, so this can be eliminated from the
|
47
61
|
# SwitchUserHelper
|
48
62
|
def provider
|
49
63
|
SwitchUser::Provider.init(self)
|
@@ -1,34 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SwitchUserHelper
|
2
4
|
SelectOption = Struct.new(:label, :scope_id)
|
5
|
+
|
3
6
|
def switch_user_select(options = {})
|
4
7
|
return unless available?
|
5
8
|
|
6
9
|
selected_user = nil
|
7
10
|
|
8
|
-
grouped_options_container =
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
unless
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
end
|
11
|
+
grouped_options_container =
|
12
|
+
{}.tap do |h|
|
13
|
+
SwitchUser.all_users.each do |record|
|
14
|
+
scope = record.is_a?(SwitchUser::GuestRecord) ? :Guest : record.scope.to_s.capitalize
|
15
|
+
h[scope] ||= []
|
16
|
+
h[scope] << [record.label, record.scope_id]
|
17
|
+
|
18
|
+
next unless selected_user.nil?
|
19
|
+
next if record.is_a?(SwitchUser::GuestRecord)
|
20
|
+
|
21
|
+
selected_user = record.scope_id if provider.current_user?(record.user, record.scope)
|
20
22
|
end
|
21
23
|
end
|
22
|
-
end
|
23
24
|
|
24
25
|
option_tags = grouped_options_for_select(grouped_options_container.to_a, selected_user)
|
25
26
|
|
26
|
-
render :
|
27
|
-
:
|
28
|
-
:option_tags => option_tags,
|
29
|
-
:classes => options[:class],
|
30
|
-
:styles => options[:style],
|
31
|
-
}
|
27
|
+
render partial: 'switch_user/widget',
|
28
|
+
locals: { option_tags: option_tags, classes: options[:class], styles: options[:style] }
|
32
29
|
end
|
33
30
|
|
34
31
|
private
|
@@ -1,4 +1,4 @@
|
|
1
1
|
<% if SwitchUser.switch_back %>
|
2
|
-
<%= check_box_tag "remember_user", "remember_user", provider.original_user.present?, :
|
2
|
+
<%= check_box_tag "remember_user", "remember_user", provider.original_user.present?, onchange: "location.href = '#{ActionController::Base.relative_url_root || '/'}switch_user/remember_user?remember=' + encodeURIComponent(this.checked)" %>
|
3
3
|
<% end %>
|
4
|
-
<%= select_tag "switch_user_identifier", option_tags, :
|
4
|
+
<%= select_tag "switch_user_identifier", option_tags, onchange: "location.href = '#{ActionController::Base.relative_url_root || '/'}switch_user?scope_identifier=' + encodeURIComponent(this.options[this.selectedIndex].value)", class: classes, style: styles %>
|
data/config/routes.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
Rails.application.routes.draw do
|
2
|
-
get :switch_user, :
|
3
|
-
get 'switch_user/remember_user', :
|
4
|
+
get :switch_user, to: 'switch_user#set_current_user'
|
5
|
+
get 'switch_user/remember_user', to: 'switch_user#remember_user'
|
4
6
|
end
|
@@ -1,7 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SwitchUser
|
2
4
|
module Generators
|
3
5
|
class InstallGenerator < Rails::Generators::Base
|
4
|
-
TEMPLATES_PATH = File.expand_path('
|
6
|
+
TEMPLATES_PATH = File.expand_path('templates', __dir__)
|
5
7
|
source_root File.expand_path(Engine.root, __FILE__)
|
6
8
|
|
7
9
|
def install_initializer
|
@@ -10,4 +12,3 @@ module SwitchUser
|
|
10
12
|
end
|
11
13
|
end
|
12
14
|
end
|
13
|
-
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
SwitchUser.setup do |config|
|
2
4
|
# provider may be :devise, :authlogic, :clearance, :restful_authentication, :sorcery, or :session
|
3
5
|
config.provider = :devise
|
@@ -5,7 +7,7 @@ SwitchUser.setup do |config|
|
|
5
7
|
# available_users is a hash,
|
6
8
|
# key is the model name of user (:user, :admin, or any name you use),
|
7
9
|
# value is a block that return the users that can be switched.
|
8
|
-
config.available_users = { :
|
10
|
+
config.available_users = { user: -> { User.all } }
|
9
11
|
|
10
12
|
# available_users_identifiers is a hash,
|
11
13
|
# keys in this hash should match a key in the available_users hash
|
@@ -13,28 +15,28 @@ SwitchUser.setup do |config|
|
|
13
15
|
# defaults to id
|
14
16
|
# this hash is to allow you to specify a different column to
|
15
17
|
# expose for instance a username on a User model instead of id
|
16
|
-
config.available_users_identifiers = { :
|
18
|
+
config.available_users_identifiers = { user: :id }
|
17
19
|
|
18
20
|
# available_users_names is a hash,
|
19
21
|
# keys in this hash should match a key in the available_users hash
|
20
22
|
# value is the column name which will be displayed in select box
|
21
|
-
config.available_users_names = { :
|
23
|
+
config.available_users_names = { user: :email }
|
22
24
|
|
23
25
|
# controller_guard is a block,
|
24
26
|
# if it returns true, the request will continue,
|
25
27
|
# else the request will be refused and returns "Permission Denied"
|
26
28
|
# if you switch from "admin" to user, the current_user param is "admin"
|
27
|
-
config.controller_guard =
|
29
|
+
config.controller_guard = ->(_current_user, _request) { Rails.env.development? }
|
28
30
|
|
29
31
|
# view_guard is a block,
|
30
32
|
# if it returns true, the switch user select box will be shown,
|
31
33
|
# else the select box will not be shown
|
32
34
|
# if you switch from admin to "user", the current_user param is "user"
|
33
|
-
config.view_guard =
|
35
|
+
config.view_guard = ->(_current_user, _request) { Rails.env.development? }
|
34
36
|
|
35
37
|
# redirect_path is a block, it returns which page will be redirected
|
36
38
|
# after switching a user.
|
37
|
-
config.redirect_path =
|
39
|
+
config.redirect_path = ->(_request, _params) { '/' }
|
38
40
|
|
39
41
|
# helper_with_guest is a boolean value, if it set to false
|
40
42
|
# the guest item in the helper won't be shown
|
data/lib/switch_user.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'switch_user/rails' if defined?(Rails)
|
4
4
|
|
5
5
|
module SwitchUser
|
6
6
|
require 'switch_user/data_source'
|
7
|
-
autoload :UserSet,
|
8
|
-
autoload :UserLoader,
|
9
|
-
autoload :Provider,
|
10
|
-
autoload :BaseGuard,
|
7
|
+
autoload :UserSet, 'switch_user/user_set'
|
8
|
+
autoload :UserLoader, 'switch_user/user_loader'
|
9
|
+
autoload :Provider, 'switch_user/provider'
|
10
|
+
autoload :BaseGuard, 'switch_user/base_guard'
|
11
11
|
autoload :LambdaGuard, 'switch_user/lambda_guard'
|
12
12
|
|
13
|
-
class InvalidScope <
|
13
|
+
class InvalidScope < RuntimeError; end
|
14
14
|
|
15
15
|
mattr_accessor :provider
|
16
16
|
mattr_accessor :available_users
|
@@ -23,7 +23,7 @@ module SwitchUser
|
|
23
23
|
mattr_accessor :login_exclusive
|
24
24
|
mattr_accessor :controller_guard
|
25
25
|
mattr_accessor :view_guard
|
26
|
-
mattr_reader
|
26
|
+
mattr_reader :guard_class
|
27
27
|
|
28
28
|
def self.setup
|
29
29
|
yield self
|
@@ -42,24 +42,25 @@ module SwitchUser
|
|
42
42
|
end
|
43
43
|
|
44
44
|
def self.data_sources
|
45
|
-
sources =
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
45
|
+
sources =
|
46
|
+
available_users.map do |scope, loader|
|
47
|
+
identifier = available_users_identifiers.fetch(scope)
|
48
|
+
name = available_users_names.fetch(scope)
|
49
|
+
DataSource.new(loader, scope, identifier, name)
|
50
|
+
end
|
50
51
|
sources.unshift(GuestDataSource.new) if helper_with_guest
|
51
52
|
DataSources.new(sources)
|
52
53
|
end
|
53
54
|
|
54
55
|
def self.reset_config
|
55
56
|
self.provider = :devise
|
56
|
-
self.available_users = { :
|
57
|
-
self.available_users_identifiers = { :
|
58
|
-
self.available_users_names = { :
|
59
|
-
self.guard_class =
|
60
|
-
self.controller_guard =
|
61
|
-
self.view_guard =
|
62
|
-
self.redirect_path =
|
57
|
+
self.available_users = { user: -> { User.all } }
|
58
|
+
self.available_users_identifiers = { user: :id }
|
59
|
+
self.available_users_names = { user: :email }
|
60
|
+
self.guard_class = 'SwitchUser::LambdaGuard'
|
61
|
+
self.controller_guard = ->(_current_user, _request) { Rails.env.development? }
|
62
|
+
self.view_guard = ->(_current_user, _request) { Rails.env.development? }
|
63
|
+
self.redirect_path = ->(request, _params) { request.env['HTTP_REFERER'] ? :back : root_path }
|
63
64
|
self.session_key = :user_id
|
64
65
|
self.helper_with_guest = true
|
65
66
|
self.switch_back = false
|