switch_user 1.5.0 → 1.5.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 +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile +2 -0
- data/README.md +1 -0
- data/Rakefile +2 -0
- data/app/controllers/switch_user_controller.rb +6 -4
- data/app/helpers/switch_user_helper.rb +8 -8
- data/config/routes.rb +2 -0
- data/lib/generators/switch_user/install/install_generator.rb +2 -1
- data/lib/generators/switch_user/install/templates/switch_user.rb +5 -3
- data/lib/switch_user.rb +12 -12
- data/lib/switch_user/base_guard.rb +4 -2
- data/lib/switch_user/data_source.rb +6 -6
- data/lib/switch_user/lambda_guard.rb +2 -0
- data/lib/switch_user/provider.rb +15 -13
- data/lib/switch_user/provider/authlogic.rb +6 -6
- data/lib/switch_user/provider/base.rb +5 -4
- data/lib/switch_user/provider/clearance.rb +5 -3
- data/lib/switch_user/provider/devise.rb +2 -0
- 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 +6 -6
- data/lib/switch_user/rails.rb +3 -1
- data/lib/switch_user/rspec.rb +2 -2
- data/lib/switch_user/rspec/feature_helpers.rb +6 -7
- data/lib/switch_user/user_loader.rb +5 -2
- data/lib/switch_user/user_set.rb +5 -3
- data/lib/switch_user/version.rb +3 -1
- data/spec/controllers/switch_user_controller_spec.rb +21 -18
- data/spec/helpers/switch_user_helper_spec.rb +45 -46
- data/spec/integration/switch_user_spec.rb +91 -30
- data/spec/provider/authlogic_spec.rb +3 -1
- data/spec/provider/clearance_spec.rb +3 -1
- data/spec/provider/devise_spec.rb +17 -17
- data/spec/provider/dummy_spec.rb +3 -1
- data/spec/provider/restful_authentication_spec.rb +3 -1
- data/spec/provider/session_spec.rb +3 -1
- data/spec/provider/sorcery_spec.rb +3 -1
- data/spec/provider_spec.rb +3 -1
- data/spec/rspec/feature_helpers_spec.rb +31 -30
- data/spec/spec_helper.rb +3 -1
- data/spec/support/application.rb +13 -12
- data/spec/support/provider.rb +11 -9
- data/spec/switch_user/data_source_spec.rb +3 -1
- data/spec/switch_user/lambda_guard_spec.rb +4 -2
- data/spec/switch_user/user_loader_spec.rb +18 -16
- data/spec/switch_user/user_set_spec.rb +9 -7
- data/spec/switch_user_spec.rb +6 -4
- data/switch_user.gemspec +24 -23
- metadata +19 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 49886e9e1c82e46feeb807ca7c44362dca6ebf35
|
4
|
+
data.tar.gz: caf2f62643f90910f792c41511ffde18cdfc19f4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c44bffb58cfdc158faf321fe268eb63fbc2d982433c84d675918ed7f227341482bf0bf49961a2b5c0c58f29ac5f7dd59e59f588c636bc6cf2b73f6968dc9ec0b
|
7
|
+
data.tar.gz: d3c75243ee248fdc3bd4a136f3447d0d9432fe84d0a37cfede20409f3cede749e841e33965a86e58396b646feade108cec0ef40bc34883c78f7f60457f25c0e3
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# switch_user
|
2
2
|
|
3
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
|
|
data/Rakefile
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class SwitchUserController < ApplicationController
|
2
4
|
before_action :developer_modes_only, :switch_back
|
3
5
|
|
@@ -25,13 +27,13 @@ class SwitchUserController < ApplicationController
|
|
25
27
|
|
26
28
|
def switch_back
|
27
29
|
if SwitchUser.switch_back
|
28
|
-
provider.remember_current_user(true) if params[:remember] ==
|
29
|
-
provider.remember_current_user(false) if params[:remember] ==
|
30
|
+
provider.remember_current_user(true) if params[:remember] == 'true'
|
31
|
+
provider.remember_current_user(false) if params[:remember] == 'false'
|
30
32
|
end
|
31
33
|
end
|
32
34
|
|
33
35
|
def developer_modes_only
|
34
|
-
raise ActionController::RoutingError
|
36
|
+
raise ActionController::RoutingError, 'Do not try to hack us.' unless available?
|
35
37
|
end
|
36
38
|
|
37
39
|
def available?
|
@@ -55,7 +57,7 @@ class SwitchUserController < ApplicationController
|
|
55
57
|
end
|
56
58
|
end
|
57
59
|
|
58
|
-
# TODO make helper methods, so this can be eliminated from the
|
60
|
+
# TODO: make helper methods, so this can be eliminated from the
|
59
61
|
# SwitchUserHelper
|
60
62
|
def provider
|
61
63
|
SwitchUser::Provider.init(self)
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SwitchUserHelper
|
2
4
|
SelectOption = Struct.new(:label, :scope_id)
|
3
5
|
def switch_user_select(options = {})
|
@@ -11,23 +13,21 @@ module SwitchUserHelper
|
|
11
13
|
h[scope] ||= []
|
12
14
|
h[scope] << [record.label, record.scope_id]
|
13
15
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
end
|
19
|
-
end
|
16
|
+
next unless selected_user.nil?
|
17
|
+
next if record.is_a?(SwitchUser::GuestRecord)
|
18
|
+
if provider.current_user?(record.user, record.scope)
|
19
|
+
selected_user = record.scope_id
|
20
20
|
end
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
24
|
option_tags = grouped_options_for_select(grouped_options_container.to_a, selected_user)
|
25
25
|
|
26
|
-
render partial:
|
26
|
+
render partial: 'switch_user/widget',
|
27
27
|
locals: {
|
28
28
|
option_tags: option_tags,
|
29
29
|
classes: options[:class],
|
30
|
-
styles: options[:style]
|
30
|
+
styles: options[:style]
|
31
31
|
}
|
32
32
|
end
|
33
33
|
|
data/config/routes.rb
CHANGED
@@ -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
|
@@ -24,17 +26,17 @@ SwitchUser.setup do |config|
|
|
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
|
@@ -56,10 +56,10 @@ module SwitchUser
|
|
56
56
|
self.available_users = { user: -> { User.all } }
|
57
57
|
self.available_users_identifiers = { user: :id }
|
58
58
|
self.available_users_names = { user: :email }
|
59
|
-
self.guard_class =
|
60
|
-
self.controller_guard = ->(
|
61
|
-
self.view_guard = ->(
|
62
|
-
self.redirect_path = ->(request,
|
59
|
+
self.guard_class = 'SwitchUser::LambdaGuard'
|
60
|
+
self.controller_guard = ->(_current_user, _request) { Rails.env.development? }
|
61
|
+
self.view_guard = ->(_current_user, _request) { Rails.env.development? }
|
62
|
+
self.redirect_path = ->(request, _params) { request.env['HTTP_REFERER'] ? :back : root_path }
|
63
63
|
self.session_key = :user_id
|
64
64
|
self.helper_with_guest = true
|
65
65
|
self.switch_back = false
|
@@ -1,6 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SwitchUser
|
2
4
|
class BaseGuard
|
3
|
-
# TODO is this the best arguments for the initializer ?
|
5
|
+
# TODO: is this the best arguments for the initializer ?
|
4
6
|
# TODO should @provider be set and current/original_user be added as # accessors ?
|
5
7
|
def initialize(controller, provider)
|
6
8
|
@controller = controller
|
@@ -10,7 +12,7 @@ module SwitchUser
|
|
10
12
|
end
|
11
13
|
|
12
14
|
def controller_available?
|
13
|
-
raise NotImplementedError
|
15
|
+
raise NotImplementedError, 'you must implement controller_available?'
|
14
16
|
end
|
15
17
|
|
16
18
|
def view_available?
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SwitchUser
|
2
4
|
class DataSources
|
3
5
|
attr_reader :sources
|
@@ -7,7 +9,7 @@ module SwitchUser
|
|
7
9
|
end
|
8
10
|
|
9
11
|
def all
|
10
|
-
sources.flat_map
|
12
|
+
sources.flat_map(&:all)
|
11
13
|
end
|
12
14
|
|
13
15
|
def find_scope_id(scope_id)
|
@@ -43,8 +45,7 @@ module SwitchUser
|
|
43
45
|
[GuestRecord.new]
|
44
46
|
end
|
45
47
|
|
46
|
-
def find_scope_id(scope_id)
|
47
|
-
end
|
48
|
+
def find_scope_id(scope_id); end
|
48
49
|
end
|
49
50
|
|
50
51
|
class Record
|
@@ -70,10 +71,9 @@ module SwitchUser
|
|
70
71
|
|
71
72
|
class GuestRecord
|
72
73
|
def label
|
73
|
-
|
74
|
+
'Guest'
|
74
75
|
end
|
75
76
|
|
76
|
-
def scope_id
|
77
|
-
end
|
77
|
+
def scope_id; end
|
78
78
|
end
|
79
79
|
end
|
data/lib/switch_user/provider.rb
CHANGED
@@ -1,20 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SwitchUser
|
2
4
|
module Provider
|
3
|
-
autoload :Base,
|
4
|
-
autoload :Authlogic,
|
5
|
-
autoload :Clearance,
|
6
|
-
autoload :Devise,
|
7
|
-
autoload :RestfulAuthentication,
|
8
|
-
autoload :Sorcery,
|
9
|
-
autoload :Dummy,
|
10
|
-
autoload :Session,
|
5
|
+
autoload :Base, 'switch_user/provider/base'
|
6
|
+
autoload :Authlogic, 'switch_user/provider/authlogic'
|
7
|
+
autoload :Clearance, 'switch_user/provider/clearance'
|
8
|
+
autoload :Devise, 'switch_user/provider/devise'
|
9
|
+
autoload :RestfulAuthentication, 'switch_user/provider/restful_authentication'
|
10
|
+
autoload :Sorcery, 'switch_user/provider/sorcery'
|
11
|
+
autoload :Dummy, 'switch_user/provider/dummy'
|
12
|
+
autoload :Session, 'switch_user/provider/session'
|
11
13
|
|
12
14
|
def self.init(controller)
|
13
|
-
if SwitchUser.provider.is_a?(Hash)
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
klass_part = if SwitchUser.provider.is_a?(Hash)
|
16
|
+
SwitchUser.provider[:name]
|
17
|
+
else
|
18
|
+
SwitchUser.provider
|
19
|
+
end
|
18
20
|
|
19
21
|
klass_part = klass_part.to_s.classify
|
20
22
|
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SwitchUser
|
2
4
|
module Provider
|
3
5
|
class Authlogic < Base
|
@@ -5,19 +7,17 @@ module SwitchUser
|
|
5
7
|
@controller = controller
|
6
8
|
end
|
7
9
|
|
8
|
-
def login(user,
|
10
|
+
def login(user, _scope = nil)
|
9
11
|
UserSession.create(user)
|
10
12
|
end
|
11
13
|
|
12
|
-
def logout(
|
14
|
+
def logout(_scope = nil)
|
13
15
|
@controller.current_user_session.destroy
|
14
16
|
end
|
15
17
|
|
16
|
-
def current_user(
|
18
|
+
def current_user(_scope = nil)
|
17
19
|
result = UserSession.find
|
18
|
-
|
19
|
-
result.record
|
20
|
-
end
|
20
|
+
result&.record
|
21
21
|
end
|
22
22
|
end
|
23
23
|
end
|
@@ -1,11 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SwitchUser
|
2
4
|
module Provider
|
3
5
|
class Base
|
4
6
|
def current_users_without_scope
|
5
|
-
SwitchUser.available_scopes.
|
7
|
+
SwitchUser.available_scopes.each_with_object([]) do |scope, users|
|
6
8
|
user = current_user(scope)
|
7
9
|
users << user if user
|
8
|
-
users
|
9
10
|
end
|
10
11
|
end
|
11
12
|
|
@@ -39,7 +40,8 @@ module SwitchUser
|
|
39
40
|
|
40
41
|
def original_user=(user)
|
41
42
|
user_type = user.class.to_s.underscore
|
42
|
-
|
43
|
+
column_name = SwitchUser.available_users_identifiers[user_type.to_sym]
|
44
|
+
user_identifier = "#{user_type}_#{user.send(column_name)}"
|
43
45
|
|
44
46
|
@controller.session[:original_user_scope_identifier] = user_identifier
|
45
47
|
end
|
@@ -59,7 +61,6 @@ module SwitchUser
|
|
59
61
|
def current_user?(user, scope = :user)
|
60
62
|
current_user(scope) == user
|
61
63
|
end
|
62
|
-
|
63
64
|
end
|
64
65
|
end
|
65
66
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SwitchUser
|
2
4
|
module Provider
|
3
5
|
class Clearance < Base
|
@@ -5,15 +7,15 @@ module SwitchUser
|
|
5
7
|
@controller = controller
|
6
8
|
end
|
7
9
|
|
8
|
-
def login(user,
|
10
|
+
def login(user, _scope = nil)
|
9
11
|
@controller.sign_in(user)
|
10
12
|
end
|
11
13
|
|
12
|
-
def logout(
|
14
|
+
def logout(_scope = nil)
|
13
15
|
@controller.sign_out
|
14
16
|
end
|
15
17
|
|
16
|
-
def current_user(
|
18
|
+
def current_user(_scope = nil)
|
17
19
|
@controller.current_user
|
18
20
|
end
|
19
21
|
end
|
@@ -1,32 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SwitchUser
|
2
4
|
module Provider
|
3
5
|
class Dummy < Base
|
4
|
-
def initialize(
|
6
|
+
def initialize(_controller)
|
5
7
|
@user = nil
|
6
8
|
end
|
7
9
|
|
8
|
-
def login(user,
|
10
|
+
def login(user, _scope = nil)
|
9
11
|
@user = user
|
10
12
|
end
|
11
13
|
|
12
|
-
def logout(
|
14
|
+
def logout(_scope = nil)
|
13
15
|
@user = nil
|
14
16
|
end
|
15
17
|
|
16
|
-
def current_user(
|
18
|
+
def current_user(_scope = nil)
|
17
19
|
@user
|
18
20
|
end
|
19
21
|
|
20
|
-
|
21
|
-
@original_user
|
22
|
-
end
|
22
|
+
attr_reader :original_user
|
23
23
|
|
24
24
|
def remember_current_user(remember)
|
25
|
-
if remember
|
26
|
-
@original_user = current_user
|
27
|
-
else
|
28
|
-
@original_user = nil
|
29
|
-
end
|
25
|
+
@original_user = (current_user if remember)
|
30
26
|
end
|
31
27
|
end
|
32
28
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SwitchUser
|
2
4
|
module Provider
|
3
5
|
class RestfulAuthentication < Base
|
@@ -5,15 +7,15 @@ module SwitchUser
|
|
5
7
|
@controller = controller
|
6
8
|
end
|
7
9
|
|
8
|
-
def login(user,
|
10
|
+
def login(user, _scope = nil)
|
9
11
|
@controller.current_user = user
|
10
12
|
end
|
11
13
|
|
12
|
-
def logout(
|
14
|
+
def logout(_scope = nil)
|
13
15
|
@controller.logout_killing_session!
|
14
16
|
end
|
15
17
|
|
16
|
-
def current_user(
|
18
|
+
def current_user(_scope = nil)
|
17
19
|
@controller.current_user
|
18
20
|
end
|
19
21
|
end
|