switch_user 1.5.2 → 1.5.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +2 -2
- data/app/helpers/switch_user_helper.rb +12 -15
- data/lib/generators/switch_user/install/install_generator.rb +1 -1
- data/lib/switch_user.rb +7 -6
- data/lib/switch_user/base_guard.rb +3 -3
- data/lib/switch_user/provider.rb +6 -5
- data/lib/switch_user/provider/base.rb +3 -5
- data/lib/switch_user/provider/sorcery.rb +2 -3
- data/lib/switch_user/rails.rb +4 -2
- data/lib/switch_user/rspec/feature_helpers.rb +10 -10
- data/lib/switch_user/user_set.rb +7 -4
- data/lib/switch_user/version.rb +1 -1
- data/spec/controllers/switch_user_controller_spec.rb +5 -9
- data/spec/helpers/switch_user_helper_spec.rb +8 -4
- data/spec/integration/switch_user_spec.rb +3 -1
- data/spec/provider/devise_spec.rb +1 -0
- data/spec/provider/dummy_spec.rb +1 -2
- data/spec/provider/session_spec.rb +1 -1
- data/spec/support/application.rb +4 -5
- data/spec/switch_user/user_loader_spec.rb +2 -4
- data/switch_user.gemspec +0 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
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
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# switch_user
|
2
2
|
|
3
|
-
[![Build Status](https://secure.travis-ci.org/flyerhzm/switch_user.
|
3
|
+
[![Build Status](https://secure.travis-ci.org/flyerhzm/switch_user.svg)](http://travis-ci.org/flyerhzm/switch_user)
|
4
4
|
[![AwesomeCode Status for flyerhzm/switch_user](https://awesomecode.io/projects/5f029f36-047b-4332-8405-9c7913b195bc/status)](https://awesomecode.io/repos/flyerhzm/switch_user)
|
5
5
|
|
6
6
|
Inspired from [hobo][0], switch_user provides a convenient way to switch current user without needing to log out and log in manually.
|
@@ -15,7 +15,7 @@ switch_user is very useful in such use cases
|
|
15
15
|
|
16
16
|
## Example
|
17
17
|
|
18
|
-
Visit here: <http://switch-user-example.
|
18
|
+
Visit here: <http://switch-user-example.herokuapp.com/admin>, switch the current user in the select box.
|
19
19
|
|
20
20
|
And source code here: <https://github.com/flyerhzm/switch_user_example>
|
21
21
|
|
@@ -2,33 +2,30 @@
|
|
2
2
|
|
3
3
|
module SwitchUserHelper
|
4
4
|
SelectOption = Struct.new(:label, :scope_id)
|
5
|
+
|
5
6
|
def switch_user_select(options = {})
|
6
7
|
return unless available?
|
7
8
|
|
8
9
|
selected_user = nil
|
9
10
|
|
10
|
-
grouped_options_container =
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
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)
|
15
20
|
|
16
|
-
|
17
|
-
next if record.is_a?(SwitchUser::GuestRecord)
|
18
|
-
if provider.current_user?(record.user, record.scope)
|
19
|
-
selected_user = record.scope_id
|
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
27
|
render partial: 'switch_user/widget',
|
27
|
-
locals: {
|
28
|
-
option_tags: option_tags,
|
29
|
-
classes: options[:class],
|
30
|
-
styles: options[:style]
|
31
|
-
}
|
28
|
+
locals: { option_tags: option_tags, classes: options[:class], styles: options[:style] }
|
32
29
|
end
|
33
30
|
|
34
31
|
private
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module SwitchUser
|
4
4
|
module Generators
|
5
5
|
class InstallGenerator < Rails::Generators::Base
|
6
|
-
TEMPLATES_PATH = File.expand_path('
|
6
|
+
TEMPLATES_PATH = File.expand_path('templates', __dir__)
|
7
7
|
source_root File.expand_path(Engine.root, __FILE__)
|
8
8
|
|
9
9
|
def install_initializer
|
data/lib/switch_user.rb
CHANGED
@@ -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,11 +42,12 @@ 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
|
@@ -5,9 +5,9 @@ module SwitchUser
|
|
5
5
|
# TODO: is this the best arguments for the initializer ?
|
6
6
|
# TODO should @provider be set and current/original_user be added as # accessors ?
|
7
7
|
def initialize(controller, provider)
|
8
|
-
@controller
|
9
|
-
@request
|
10
|
-
@current_user
|
8
|
+
@controller = controller
|
9
|
+
@request = controller.request
|
10
|
+
@current_user = provider.current_user
|
11
11
|
@original_user = provider.original_user
|
12
12
|
end
|
13
13
|
|
data/lib/switch_user/provider.rb
CHANGED
@@ -12,11 +12,12 @@ module SwitchUser
|
|
12
12
|
autoload :Session, 'switch_user/provider/session'
|
13
13
|
|
14
14
|
def self.init(controller)
|
15
|
-
klass_part =
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
15
|
+
klass_part =
|
16
|
+
if SwitchUser.provider.is_a?(Hash)
|
17
|
+
SwitchUser.provider[:name]
|
18
|
+
else
|
19
|
+
SwitchUser.provider
|
20
|
+
end
|
20
21
|
|
21
22
|
klass_part = klass_part.to_s.classify
|
22
23
|
|
@@ -33,14 +33,12 @@ module SwitchUser
|
|
33
33
|
def original_user
|
34
34
|
user_identifier = @controller.session[:original_user_scope_identifier]
|
35
35
|
|
36
|
-
if user_identifier
|
37
|
-
UserLoader.prepare(scope_identifier: user_identifier).user
|
38
|
-
end
|
36
|
+
UserLoader.prepare(scope_identifier: user_identifier).user if user_identifier
|
39
37
|
end
|
40
38
|
|
41
39
|
def original_user=(user)
|
42
|
-
user_type
|
43
|
-
column_name
|
40
|
+
user_type = user.class.to_s.underscore
|
41
|
+
column_name = SwitchUser.available_users_identifiers[user_type.to_sym]
|
44
42
|
user_identifier = "#{user_type}_#{user.send(column_name)}"
|
45
43
|
|
46
44
|
@controller.session[:original_user_scope_identifier] = user_identifier
|
@@ -24,9 +24,8 @@ module SwitchUser
|
|
24
24
|
end
|
25
25
|
|
26
26
|
def restore_original_user_identifier
|
27
|
-
|
28
|
-
@
|
29
|
-
end
|
27
|
+
@controller.session[:original_user_scope_identifier] =
|
28
|
+
@original_user_scope_identifier if @original_user_scope_identifier
|
30
29
|
end
|
31
30
|
|
32
31
|
def current_user(_scope = nil)
|
data/lib/switch_user/rails.rb
CHANGED
@@ -3,8 +3,10 @@
|
|
3
3
|
module SwitchUser
|
4
4
|
class Engine < Rails::Engine
|
5
5
|
initializer 'switch_user.view' do
|
6
|
-
|
7
|
-
|
6
|
+
config.to_prepare do
|
7
|
+
ActiveSupport.on_load(:action_view) do
|
8
|
+
include SwitchUserHelper
|
9
|
+
end
|
8
10
|
end
|
9
11
|
end
|
10
12
|
end
|
@@ -15,25 +15,25 @@ module SwitchUser
|
|
15
15
|
|
16
16
|
_user_scope = _user_scope.to_s
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
raise SwitchUser::InvalidScope,
|
19
|
+
"don't allow this user sign in, please check config.available_users" unless SwitchUser.available_scopes
|
20
|
+
.include?(_user_scope) || SwitchUser.available_scopes.include?(_user_scope.to_sym)
|
21
21
|
|
22
22
|
_user_id =
|
23
23
|
case user_record_or_scope
|
24
24
|
when ActiveRecord::Base
|
25
|
-
identifier =
|
26
|
-
|
27
|
-
|
28
|
-
|
25
|
+
identifier =
|
26
|
+
SwitchUser.available_users_identifiers[_user_scope] ||
|
27
|
+
SwitchUser.available_users_identifiers[_user_scope.to_sym]
|
28
|
+
raise SwitchUser::InvalidScope,
|
29
|
+
"don't allow switch this user, please check config.available_users_identifiers" if identifier.nil?
|
30
|
+
|
29
31
|
user_record_or_scope.send identifier
|
30
32
|
else
|
31
33
|
user_id
|
32
34
|
end
|
33
35
|
|
34
|
-
if _user_id.to_s.empty?
|
35
|
-
raise InvalidArgument, "don't allow switch this user, user_id is empty"
|
36
|
-
end
|
36
|
+
raise InvalidArgument, "don't allow switch this user, user_id is empty" if _user_id.to_s.empty?
|
37
37
|
|
38
38
|
scope_identifier = "#{_user_scope}_#{_user_id}"
|
39
39
|
|
data/lib/switch_user/user_set.rb
CHANGED
@@ -5,7 +5,7 @@ module SwitchUser
|
|
5
5
|
def self.init_from_config
|
6
6
|
SwitchUser.available_users.map do |scope, base_scope|
|
7
7
|
identifier = SwitchUser.available_users_identifiers[scope]
|
8
|
-
label
|
8
|
+
label = SwitchUser.available_users_names[scope]
|
9
9
|
new(scope, identifier, label, base_scope)
|
10
10
|
end
|
11
11
|
end
|
@@ -17,17 +17,19 @@ module SwitchUser
|
|
17
17
|
end
|
18
18
|
|
19
19
|
attr_reader :scope, :user_class, :identifier, :label, :base_scope
|
20
|
+
|
20
21
|
def initialize(scope, identifier, label, base_scope)
|
21
|
-
@scope
|
22
|
+
@scope = scope
|
22
23
|
@user_class = normalize_class(scope)
|
23
24
|
@identifier = identifier
|
24
|
-
@label
|
25
|
+
@label = label
|
25
26
|
@base_scope = normalize_scope(base_scope)
|
26
27
|
end
|
27
28
|
|
28
29
|
def find_user(id)
|
29
30
|
Record.build(users.where(id: id).first, self)
|
30
31
|
end
|
32
|
+
|
31
33
|
alias [] find_user
|
32
34
|
|
33
35
|
def users
|
@@ -56,9 +58,10 @@ module SwitchUser
|
|
56
58
|
user_class.respond_to?(:scoped) ? user_class.scoped : user_class.all
|
57
59
|
end
|
58
60
|
end
|
61
|
+
|
59
62
|
class Record < Struct.new(:id, :label, :scope)
|
60
63
|
def self.build(user, set)
|
61
|
-
id
|
64
|
+
id = user.send(set.identifier)
|
62
65
|
label = user.send(set.label)
|
63
66
|
scope = set.scope
|
64
67
|
|
data/lib/switch_user/version.rb
CHANGED
@@ -10,11 +10,7 @@ RSpec.describe SwitchUserController, type: :controller do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
let(:admin) { double(:admin, admin?: true) }
|
13
|
-
let(:provider) {
|
14
|
-
double(:provider,
|
15
|
-
original_user: admin,
|
16
|
-
current_user: nil)
|
17
|
-
}
|
13
|
+
let(:provider) { double(:provider, original_user: admin, current_user: nil) }
|
18
14
|
describe '#set_current_user' do
|
19
15
|
it 'redirects the user to the specified location' do
|
20
16
|
SwitchUser.redirect_path = ->(_, _) { '/path' }
|
@@ -26,14 +22,14 @@ RSpec.describe SwitchUserController, type: :controller do
|
|
26
22
|
|
27
23
|
it 'denies access according to the guard block' do
|
28
24
|
SwitchUser.controller_guard = ->(_, _, _) { false }
|
29
|
-
expect {
|
30
|
-
get :set_current_user
|
31
|
-
}.to raise_error(ActionController::RoutingError)
|
25
|
+
expect { get :set_current_user }.to raise_error(ActionController::RoutingError)
|
32
26
|
end
|
33
27
|
|
34
28
|
describe 'requests with a privileged original_user' do
|
35
29
|
before do
|
36
|
-
SwitchUser.controller_guard =
|
30
|
+
SwitchUser.controller_guard = lambda do |current_user, _, original_user|
|
31
|
+
current_user.try(:admin?) || original_user.try(:admin?)
|
32
|
+
end
|
37
33
|
end
|
38
34
|
it 'allows access using the original_user param' do
|
39
35
|
allow(controller).to receive(:provider).and_return(provider)
|
@@ -11,10 +11,10 @@ RSpec.describe SwitchUserHelper, type: :helper do
|
|
11
11
|
|
12
12
|
let(:user) { double(:user, id: 1) }
|
13
13
|
let(:admin) { double(:admin, id: 1) }
|
14
|
-
let(:provider)
|
14
|
+
let(:provider) do
|
15
15
|
_provider = SwitchUser::Provider::Dummy.new(controller)
|
16
16
|
_provider
|
17
|
-
|
17
|
+
end
|
18
18
|
|
19
19
|
describe '#switch_user_select' do
|
20
20
|
let(:guest_record) { SwitchUser::GuestRecord.new }
|
@@ -23,9 +23,13 @@ RSpec.describe SwitchUserHelper, type: :helper do
|
|
23
23
|
|
24
24
|
let(:guest_option_tags) { '<optgroup label="Guest"><option value="">Guest</option></optgroup>' }
|
25
25
|
let(:user_option_tags) { '<optgroup label="User"><option value="user_1">user1</option></optgroup>' }
|
26
|
-
let(:user_selected_option_tags)
|
26
|
+
let(:user_selected_option_tags) do
|
27
|
+
'<optgroup label="User"><option selected="selected" value="user_1">user1</option></optgroup>'
|
28
|
+
end
|
27
29
|
let(:admin_option_tags) { '<optgroup label="Admin"><option value="admin_1">admin1</option></optgroup>' }
|
28
|
-
let(:admin_selected_option_tags)
|
30
|
+
let(:admin_selected_option_tags) do
|
31
|
+
'<optgroup label="Admin"><option selected="selected" value="admin_1">admin1</option></optgroup>'
|
32
|
+
end
|
29
33
|
|
30
34
|
before do
|
31
35
|
allow(SwitchUser).to receive(:switch_back).and_return(false)
|
@@ -29,7 +29,9 @@ RSpec.describe 'Using SwitchUser', type: :request do
|
|
29
29
|
context 'using switch_back' do
|
30
30
|
before do
|
31
31
|
SwitchUser.switch_back = true
|
32
|
-
SwitchUser.controller_guard =
|
32
|
+
SwitchUser.controller_guard = lambda do |current_user, _request, original_user|
|
33
|
+
current_user && current_user.admin? || original_user && original_user.admin?
|
34
|
+
end
|
33
35
|
end
|
34
36
|
|
35
37
|
it 'can switch back to a different user through remember_user endpoint' do
|
data/spec/provider/dummy_spec.rb
CHANGED
data/spec/support/application.rb
CHANGED
@@ -10,7 +10,7 @@ class ApplicationController < ActionController::Base
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def current_user
|
13
|
-
User.
|
13
|
+
User.find_by(id: session[SwitchUser.session_key])
|
14
14
|
end
|
15
15
|
|
16
16
|
def login
|
@@ -66,6 +66,7 @@ module MyApp
|
|
66
66
|
config.action_dispatch.show_exceptions = false
|
67
67
|
end
|
68
68
|
end
|
69
|
+
|
69
70
|
Rails.application.initialize!
|
70
71
|
Rails.application.routes.draw do
|
71
72
|
get 'dummy/protected', to: 'dummy#protected'
|
@@ -83,12 +84,10 @@ connection.create_table :users do |t|
|
|
83
84
|
t.column :admin, :boolean
|
84
85
|
end
|
85
86
|
|
86
|
-
class User < ActiveRecord::Base
|
87
|
-
end
|
87
|
+
class User < ActiveRecord::Base; end
|
88
88
|
|
89
89
|
connection.create_table :clients do |t|
|
90
90
|
t.column :email, :string
|
91
91
|
end
|
92
92
|
|
93
|
-
class Client < ActiveRecord::Base
|
94
|
-
end
|
93
|
+
class Client < ActiveRecord::Base; end
|
@@ -27,9 +27,7 @@ RSpec.describe SwitchUser::UserLoader do
|
|
27
27
|
expect(loaded_user).to eq user
|
28
28
|
end
|
29
29
|
it 'raises an error for an invalid scope' do
|
30
|
-
expect {
|
31
|
-
loaded_user = SwitchUser::UserLoader.prepare(nil, '1')
|
32
|
-
}.to raise_error(SwitchUser::InvalidScope)
|
30
|
+
expect { loaded_user = SwitchUser::UserLoader.prepare(nil, '1') }.to raise_error(SwitchUser::InvalidScope)
|
33
31
|
end
|
34
32
|
end
|
35
33
|
|
@@ -44,7 +42,7 @@ RSpec.describe SwitchUser::UserLoader do
|
|
44
42
|
it 'returns nil if no user is found' do
|
45
43
|
loader = SwitchUser::UserLoader.new('user', 3)
|
46
44
|
|
47
|
-
expect(User.
|
45
|
+
expect(User.find_by(id: 3)).to be_nil
|
48
46
|
expect(loader.user).to eq nil
|
49
47
|
end
|
50
48
|
|
data/switch_user.gemspec
CHANGED
@@ -14,7 +14,6 @@ Gem::Specification.new do |s|
|
|
14
14
|
s.description = 'Easily switch current user to speed up development'
|
15
15
|
|
16
16
|
s.required_rubygems_version = '>= 1.3.6'
|
17
|
-
s.rubyforge_project = 'switch_user'
|
18
17
|
|
19
18
|
s.add_development_dependency 'actionpack'
|
20
19
|
s.add_development_dependency 'activerecord'
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: switch_user
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.5.
|
4
|
+
version: 1.5.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Huang
|
8
8
|
- Luke Cowell
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2020-10-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: actionpack
|
@@ -246,7 +246,7 @@ homepage: http://rubygems.org/gems/switch_user
|
|
246
246
|
licenses:
|
247
247
|
- MIT
|
248
248
|
metadata: {}
|
249
|
-
post_install_message:
|
249
|
+
post_install_message:
|
250
250
|
rdoc_options: []
|
251
251
|
require_paths:
|
252
252
|
- lib
|
@@ -261,8 +261,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
261
261
|
- !ruby/object:Gem::Version
|
262
262
|
version: 1.3.6
|
263
263
|
requirements: []
|
264
|
-
rubygems_version: 3.
|
265
|
-
signing_key:
|
264
|
+
rubygems_version: 3.1.2
|
265
|
+
signing_key:
|
266
266
|
specification_version: 4
|
267
267
|
summary: Easily switch current user to speed up development
|
268
268
|
test_files: []
|