devise_masquerade 1.3.1 → 1.3.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7e8cd4d05e6a1c75e17d26588532e261c01e95689d87d491757132d3242faed7
4
- data.tar.gz: a9e581005ebf3f238f39aa83d276cef37716bbc1669462f2a4e80f745e29c70a
3
+ metadata.gz: 289316079ab661599906ee251d276b9151b6d7b43a47131ea3683d4305cb198a
4
+ data.tar.gz: fce111853661fbfe07cab2058aefa0ceb2bf1cd15830a3740dc9f293a2126a42
5
5
  SHA512:
6
- metadata.gz: 27aee8dd6cfd3f270a466bc30c4a4b545c7b4e944c3794567ed6c220b86598ff2e5361cb00a7cb7a49d20fccc6532cbd97cd0e301459c5b980bbe2e6052847d8
7
- data.tar.gz: 4df45047b964dd10855dbf563f907bedd5e497130cfab4e5b7d65168d5ca91d47265ea562aea1345563fe15b17f7e944b867dfae3420f4e9cbd030287952bc8f
6
+ metadata.gz: 6d812afb79fc6e55780d5fff58432c6070bd23b5ec57d88d8706b1ee09eaf4a314bb68803a6924e731866594ba8cf4caeade6d6050c7ba888af6bebfc2058cd9
7
+ data.tar.gz: f9002c0ba65f8a347f9ba3b4b2abc7aa363784e60c3b9c7e9d6071ca131fd5484c7aac664e7b5566030792243f093793702e6d26ee620117c8126bb3316f60b0
data/Gemfile.lock CHANGED
@@ -52,7 +52,7 @@ GIT
52
52
  PATH
53
53
  remote: .
54
54
  specs:
55
- devise_masquerade (1.3.1)
55
+ devise_masquerade (1.3.6)
56
56
  devise (>= 4.7.0)
57
57
  globalid (>= 0.3.6)
58
58
  railties (>= 5.2.0)
@@ -6,14 +6,16 @@ class Devise::MasqueradesController < DeviseController
6
6
  end
7
7
  skip_before_action :masquerade!, raise: false
8
8
 
9
- prepend_before_action :authenticate_scope!, :masquerade_authorize!
9
+ prepend_before_action :authenticate_scope!, only: :show
10
+ prepend_before_action :masquerade_authorize!
10
11
 
11
- before_action :save_masquerade_owner_session, only: :show
12
+ def show
13
+ masqueradable_resource = find_masqueradable_resource
12
14
 
13
- after_action :cleanup_masquerade_owner_session, only: :back
15
+ save_masquerade_owner_session(masqueradable_resource)
14
16
 
15
- def show
16
- self.resource = find_resource
17
+ self.resource = masqueradable_resource
18
+ sign_out(send("current_#{masquerading_resource_name}"))
17
19
 
18
20
  unless resource
19
21
  flash[:error] = "#{masqueraded_resource_class} not found."
@@ -28,16 +30,21 @@ class Devise::MasqueradesController < DeviseController
28
30
  end
29
31
 
30
32
  def back
31
- self.resource = find_owner_resource
33
+ masqueradable_resource = send("current_#{masqueraded_resource_name}")
32
34
 
33
- if resource.class != masqueraded_resource_class
34
- sign_out(send("current_#{masqueraded_resource_name}"))
35
+ unless send("#{masqueraded_resource_name}_signed_in?")
36
+ head(401) and return
35
37
  end
36
38
 
39
+ self.resource = find_owner_resource(masqueradable_resource)
40
+ sign_out(send("current_#{masqueraded_resource_name}"))
41
+
37
42
  masquerade_sign_in(resource)
38
43
  request.env['devise.skip_trackable'] = nil
39
44
 
40
45
  go_back(resource, path: after_back_masquerade_path_for(resource))
46
+
47
+ cleanup_masquerade_owner_session(masqueradable_resource)
41
48
  end
42
49
 
43
50
  protected
@@ -50,12 +57,14 @@ class Devise::MasqueradesController < DeviseController
50
57
  true
51
58
  end
52
59
 
53
- def find_resource
54
- GlobalID::Locator.locate_signed params[Devise.masquerade_param], for: 'masquerade'
60
+ def find_masqueradable_resource
61
+ GlobalID::Locator.locate_signed(params[Devise.masquerade_param], for: 'masquerade')
55
62
  end
56
63
 
57
- def find_owner_resource
58
- GlobalID::Locator.locate_signed(Rails.cache.read(session_key), for: 'masquerade')
64
+ def find_owner_resource(masqueradable_resource)
65
+ skey = session_key(masqueradable_resource)
66
+
67
+ GlobalID::Locator.locate_signed(Rails.cache.read(skey), for: 'masquerade')
59
68
  end
60
69
 
61
70
  def go_back(user, path:)
@@ -120,27 +129,29 @@ class Devise::MasqueradesController < DeviseController
120
129
  '/'
121
130
  end
122
131
 
123
- def save_masquerade_owner_session
124
- resource_gid = send("current_#{masquerading_resource_name}").to_sgid(
125
- expires_in: Devise.masquerade_expires_in, for: 'masquerade')
126
- # skip sharing owner id via session
127
- Rails.cache.write(session_key, resource_gid, expires_in: Devise.masquerade_expires_in)
132
+ def save_masquerade_owner_session(masqueradable_resource)
133
+ skey = session_key(masqueradable_resource)
128
134
 
129
- unless session.key?(session_key)
130
- session[session_key_masquerading_resource_class] = masquerading_resource_class.name
131
- session[session_key_masqueraded_resource_class] = masqueraded_resource_class.name
132
- end
135
+ resource_gid = send("current_#{masquerading_resource_name}").to_sgid(for: 'masquerade')
136
+
137
+ # skip sharing owner id via session
138
+ Rails.cache.write(skey, resource_gid)
139
+ session[skey] = true
140
+ session[session_key_masquerading_resource_class] = masquerading_resource_class.name
141
+ session[session_key_masqueraded_resource_class] = masqueraded_resource_class.name
133
142
  end
134
143
 
135
- def cleanup_masquerade_owner_session
136
- Rails.cache.delete(session_key)
144
+ def cleanup_masquerade_owner_session(masqueradable_resource)
145
+ skey = session_key(masqueradable_resource)
137
146
 
147
+ Rails.cache.delete(skey)
148
+ session.delete(skey)
138
149
  session.delete(session_key_masqueraded_resource_class)
139
150
  session.delete(session_key_masquerading_resource_class)
140
151
  end
141
152
 
142
- def session_key
143
- "devise_masquerade_#{masqueraded_resource_name}".to_sym
153
+ def session_key(masqueradable_resource)
154
+ "devise_masquerade_#{masqueraded_resource_name}_#{masqueradable_resource.to_param}".to_sym
144
155
  end
145
156
 
146
157
  def session_key_masqueraded_resource_class
@@ -148,6 +159,6 @@ class Devise::MasqueradesController < DeviseController
148
159
  end
149
160
 
150
161
  def session_key_masquerading_resource_class
151
- "devise_masquerade_masquerading_resource_class"
162
+ "devise_masquerade_masquerading_resource_class"
152
163
  end
153
164
  end
@@ -0,0 +1,18 @@
1
+ Feature: Use back button for returning to the owner despite on expiration time.
2
+ In order to back to the owner user
3
+ As an masquerade user
4
+ I want to be able to press a simple button on the page
5
+
6
+ Scenario: Use back button
7
+ Given I logged in
8
+ And I have a user for masquerade
9
+
10
+ When I have devise masquerade expiration time in 1 second
11
+
12
+ When I am on the users page
13
+ And I login as one user
14
+ Then I should be login as this user
15
+ And I waited for 2 seconds
16
+
17
+ When I press back masquerade button
18
+ Then I should be login as owner user
@@ -0,0 +1,9 @@
1
+ When("I have devise masquerade expiration time in {int} second") do |seconds|
2
+ Devise.masquerade_expires_in = seconds.second
3
+ end
4
+
5
+ Then("I waited for {int} seconds") do |seconds|
6
+ sleep(seconds)
7
+
8
+ Devise.masquerade_expires_in = 5.minutes
9
+ end
@@ -38,12 +38,19 @@ module DeviseMasquerade
38
38
  end
39
39
 
40
40
  def #{name}_masquerade?
41
- ::Rails.cache.exist?(:"devise_masquerade_#{name}").present?
41
+ return false if current_#{name}.blank?
42
+
43
+ key = "devise_masquerade_#{name}_" + current_#{name}.to_param
44
+ return false if session[key].blank?
45
+
46
+ ::Rails.cache.exist?(key.to_sym).present?
42
47
  end
43
48
 
44
49
  def #{name}_masquerade_owner
45
- return nil unless send(:#{name}_masquerade?)
46
- GlobalID::Locator.locate_signed(Rails.cache.read(:"devise_masquerade_#{name}"), for: 'masquerade')
50
+ return unless send(:#{name}_masquerade?)
51
+
52
+ key = "devise_masquerade_#{name}_" + current_#{name}.to_param
53
+ GlobalID::Locator.locate_signed(::Rails.cache.read(key.to_sym, for: 'masquerade'))
47
54
  end
48
55
 
49
56
  private
@@ -1,3 +1,3 @@
1
1
  module DeviseMasquerade
2
- VERSION = '1.3.1'.freeze
2
+ VERSION = '1.3.6'.freeze
3
3
  end
@@ -14,7 +14,7 @@ describe Devise::MasqueradesController, type: :controller do
14
14
  get :show, params: { id: mask.to_param, masqueraded_resource_class: mask.class.name, masquerade: mask.masquerade_key }
15
15
  end
16
16
 
17
- it { expect(Rails.cache.read('devise_masquerade_student')).to be }
17
+ it { expect(Rails.cache.read("devise_masquerade_student_#{mask.to_param}")).to be }
18
18
 
19
19
  it 'should have warden keys defined' do
20
20
  expect(session["warden.user.student.key"].first.first).to eq(mask.id)
@@ -30,7 +30,7 @@ describe Devise::MasqueradesController, type: :controller do
30
30
  get :show, params: { id: mask.to_param, masquerade: mask.masquerade_key }
31
31
  end
32
32
 
33
- it { expect(Rails.cache.read('devise_masquerade_user')).to be }
33
+ it { expect(Rails.cache.read("devise_masquerade_user_#{mask.to_param}")).to be }
34
34
  it { expect(session["warden.user.user.key"].first.first).to eq(mask.id) }
35
35
  it { should redirect_to('/') }
36
36
 
@@ -39,7 +39,7 @@ describe Devise::MasqueradesController, type: :controller do
39
39
 
40
40
  it { should redirect_to(masquerade_page) }
41
41
  it { expect(current_user.reload).to eq(@user) }
42
- it { expect(Rails.cache.read('devise_masquerade_user')).not_to be }
42
+ it { expect(Rails.cache.read("devise_masquerade_user_#{mask.to_param}")).not_to be }
43
43
  end
44
44
  end
45
45
 
@@ -16,7 +16,7 @@ describe MasqueradesTestsController, type: :controller do
16
16
  before { get :show, params: { id: mask.to_param, masquerade: mask.masquerade_key } }
17
17
 
18
18
  it { expect(response.status).to eq(403) }
19
- it { expect(Rails.cache.read('devise_masquerade_user')).not_to be }
19
+ it { expect(Rails.cache.read("devise_masquerade_user_#{mask.to_param}")).not_to be }
20
20
  it { expect(session['warden.user.user.key'].first.first).not_to eq(mask.id) }
21
21
  end
22
22
 
@@ -35,7 +35,7 @@ describe MasqueradesTestsController, type: :controller do
35
35
  end
36
36
 
37
37
  it { expect(response.status).to eq(302) }
38
- it { expect(Rails.cache.read('devise_masquerade_user')).to be }
38
+ it { expect(Rails.cache.read("devise_masquerade_user_#{mask.to_param}")).to be }
39
39
  it { expect(session['warden.user.user.key'].first.first).to eq(mask.id) }
40
40
  end
41
41
  end
@@ -17,7 +17,7 @@
17
17
  <% end %>
18
18
 
19
19
  <% if user_masquerade? %>
20
- <%= link_to "Back masquerade", back_masquerade_path(current_user) %>
20
+ <%= link_to "Back masquerade", back_masquerade_path(User.new) %>
21
21
  <% end %>
22
22
  <% end %>
23
23
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: devise_masquerade
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexandr Korsak
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-03 00:00:00.000000000 Z
11
+ date: 2021-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -92,9 +92,11 @@ files:
92
92
  - config/environment.rb
93
93
  - devise_masquerade.gemspec
94
94
  - features/back.feature
95
+ - features/expires_masquerade.feature
95
96
  - features/multiple_masquerading_models.feature
96
97
  - features/step_definitions/auth_steps.rb
97
98
  - features/step_definitions/back_steps.rb
99
+ - features/step_definitions/expires_steps.rb
98
100
  - features/step_definitions/url_helpers_steps.rb
99
101
  - features/support/env.rb
100
102
  - features/url_helpers.feature
@@ -185,9 +187,11 @@ specification_version: 4
185
187
  summary: use for login as functionallity on your admin users pages
186
188
  test_files:
187
189
  - features/back.feature
190
+ - features/expires_masquerade.feature
188
191
  - features/multiple_masquerading_models.feature
189
192
  - features/step_definitions/auth_steps.rb
190
193
  - features/step_definitions/back_steps.rb
194
+ - features/step_definitions/expires_steps.rb
191
195
  - features/step_definitions/url_helpers_steps.rb
192
196
  - features/support/env.rb
193
197
  - features/url_helpers.feature