switch_user 1.3.0 → 1.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/app/helpers/switch_user_helper.rb +9 -5
- data/app/views/switch_user/_widget.html.erb +2 -2
- data/lib/switch_user/provider/base.rb +5 -0
- data/lib/switch_user/version.rb +1 -1
- data/spec/helpers/switch_user_helper_spec.rb +44 -5
- data/spec/provider/devise_spec.rb +10 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f5884c55c483cc8783d01b9826a61036e4c8c0f8
|
4
|
+
data.tar.gz: c427f1b701df9bf6689f75ec46c12422421bd494
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dce7ff61f4148662b60ef5e7763bb46c31f4250de7ff986679a4a96554744be4b9d57dcfd63c9c102cb0485bb043c73a3a0d885b17503eae19f3e4da6e9e623e
|
7
|
+
data.tar.gz: c03171c6b23c247638253224d5fc9a2016abb8a90bbc409f4e3492284d1e2857c29bb6830b3818768bd939b6988e846202f2ad485b4334cbd5eb93cc1fc719d9
|
data/CHANGELOG.md
CHANGED
@@ -3,17 +3,21 @@ module SwitchUserHelper
|
|
3
3
|
def switch_user_select(options = {})
|
4
4
|
return unless available?
|
5
5
|
|
6
|
-
|
7
|
-
selected_user = "user_#{provider.current_user.id}"
|
8
|
-
else
|
9
|
-
selected_user = nil
|
10
|
-
end
|
6
|
+
selected_user = nil
|
11
7
|
|
12
8
|
grouped_options_container = {}.tap do |h|
|
13
9
|
SwitchUser.all_users.each do |record|
|
14
10
|
scope = record.is_a?(SwitchUser::GuestRecord) ? :Guest : record.scope.to_s.capitalize
|
15
11
|
h[scope] ||= []
|
16
12
|
h[scope] << [record.label, record.scope_id]
|
13
|
+
|
14
|
+
if selected_user.nil?
|
15
|
+
unless record.is_a?(SwitchUser::GuestRecord)
|
16
|
+
if provider.current_user?(record.user, record.scope)
|
17
|
+
selected_user = record.scope_id
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
17
21
|
end
|
18
22
|
end
|
19
23
|
|
@@ -1,4 +1,4 @@
|
|
1
1
|
<% if SwitchUser.switch_back %>
|
2
|
-
<%= check_box_tag "remember_user", "remember_user", provider.original_user.present?, :onchange => "location.href = '/switch_user/remember_user?remember=' + encodeURIComponent(this.checked)" %>
|
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, :onchange => "location.href = '/switch_user?scope_identifier=' + encodeURIComponent(this.options[this.selectedIndex].value)", :class => classes, :style => styles %>
|
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/lib/switch_user/version.rb
CHANGED
@@ -8,19 +8,22 @@ RSpec.describe SwitchUserHelper, :type => :helper do
|
|
8
8
|
end
|
9
9
|
|
10
10
|
let(:user) { double(:user, :id => 1) }
|
11
|
-
let(:
|
12
|
-
|
13
|
-
_provider.
|
11
|
+
let(:admin) { double(:admin, :id => 1) }
|
12
|
+
let(:provider) {
|
13
|
+
_provider = SwitchUser::Provider::Dummy.new(controller)
|
14
14
|
_provider
|
15
15
|
}
|
16
16
|
|
17
17
|
describe "#switch_user_select" do
|
18
18
|
let(:guest_record) { SwitchUser::GuestRecord.new }
|
19
|
-
let(:user_record) { double(:user_record, :scope => :user, :label => 'user1', :scope_id => 'user_1') }
|
20
|
-
|
19
|
+
let(:user_record) { double(:user_record, :user => user, :scope => :user, :label => 'user1', :scope_id => 'user_1') }
|
20
|
+
let(:admin_record) { double(:admin_record, :user => admin, :scope => :admin, :label => 'admin1', :scope_id => 'admin_1') }
|
21
|
+
|
21
22
|
let(:guest_option_tags) { %Q^<optgroup label="Guest"><option value="">Guest</option></optgroup>^ }
|
22
23
|
let(:user_option_tags) { %Q^<optgroup label="User"><option value="user_1">user1</option></optgroup>^ }
|
23
24
|
let(:user_selected_option_tags) { %Q^<optgroup label="User"><option selected="selected" value="user_1">user1</option></optgroup>^ }
|
25
|
+
let(:admin_option_tags) { %Q^<optgroup label="Admin"><option value="admin_1">admin1</option></optgroup>^ }
|
26
|
+
let(:admin_selected_option_tags) { %Q^<optgroup label="Admin"><option selected="selected" value="admin_1">admin1</option></optgroup>^ }
|
24
27
|
|
25
28
|
|
26
29
|
before do
|
@@ -28,7 +31,9 @@ RSpec.describe SwitchUserHelper, :type => :helper do
|
|
28
31
|
|
29
32
|
allow(helper).to receive(:available?).and_return(true)
|
30
33
|
|
34
|
+
provider.instance_variable_set(:@user, user)
|
31
35
|
allow(helper).to receive(:provider).and_return(provider)
|
36
|
+
|
32
37
|
allow(provider).to receive(:current_user).and_return(user)
|
33
38
|
|
34
39
|
allow(SwitchUser).to receive(:all_users).and_return([guest_record, user_record])
|
@@ -82,6 +87,40 @@ RSpec.describe SwitchUserHelper, :type => :helper do
|
|
82
87
|
expect(helper.switch_user_select).to match(%r{#{user_selected_option_tags}})
|
83
88
|
end
|
84
89
|
|
90
|
+
it "when current_user is user and all_users is [guest_record, user_record, admin_record]" do
|
91
|
+
allow(provider).to receive(:current_user).and_return(user)
|
92
|
+
allow(SwitchUser).to receive(:all_users).and_return([guest_record, user_record, admin_record])
|
93
|
+
|
94
|
+
expect(helper.switch_user_select).to match(%r{#{guest_option_tags}})
|
95
|
+
expect(helper.switch_user_select).to match(%r{#{user_selected_option_tags}})
|
96
|
+
expect(helper.switch_user_select).to match(%r{#{admin_option_tags}})
|
97
|
+
end
|
98
|
+
|
99
|
+
it "when current_user is admin and all_users is [guest_record, user_record, admin_record]" do
|
100
|
+
provider.instance_variable_set(:@user, admin)
|
101
|
+
allow(helper).to receive(:provider).and_return(provider)
|
102
|
+
|
103
|
+
allow(provider).to receive(:current_user).and_return(admin)
|
104
|
+
|
105
|
+
allow(SwitchUser).to receive(:all_users).and_return([guest_record, user_record, admin_record])
|
106
|
+
|
107
|
+
expect(helper.switch_user_select).to match(%r{#{guest_option_tags}})
|
108
|
+
expect(helper.switch_user_select).to match(%r{#{user_option_tags}})
|
109
|
+
expect(helper.switch_user_select).to match(%r{#{admin_selected_option_tags}})
|
110
|
+
end
|
111
|
+
|
112
|
+
it "when current_user is admin and all_users is [guest_record, user_record]" do
|
113
|
+
provider.instance_variable_set(:@user, admin)
|
114
|
+
allow(helper).to receive(:provider).and_return(provider)
|
115
|
+
|
116
|
+
allow(provider).to receive(:current_user).and_return(admin)
|
117
|
+
|
118
|
+
allow(SwitchUser).to receive(:all_users).and_return([guest_record, user_record])
|
119
|
+
|
120
|
+
expect(helper.switch_user_select).to match(%r{#{guest_option_tags}})
|
121
|
+
expect(helper.switch_user_select).to match(%r{#{user_option_tags}})
|
122
|
+
expect(helper.switch_user_select).to_not match(%r{#{admin_option_tags}})
|
123
|
+
end
|
85
124
|
end
|
86
125
|
|
87
126
|
describe "#user_tag_value" do
|
@@ -100,4 +100,14 @@ RSpec.describe SwitchUser::Provider::Devise do
|
|
100
100
|
expect(provider.current_users_without_scope).to eq [user]
|
101
101
|
end
|
102
102
|
end
|
103
|
+
|
104
|
+
describe "#current_user?" do
|
105
|
+
it "logs the user in" do
|
106
|
+
user = double(:user)
|
107
|
+
provider.login(user, :user)
|
108
|
+
|
109
|
+
expect(provider.current_user?(user, :user)).to eq true
|
110
|
+
expect(provider.current_user?(user, :admin)).to eq false
|
111
|
+
end
|
112
|
+
end
|
103
113
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: switch_user
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Richard Huang
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-
|
12
|
+
date: 2016-08-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|