switch_user 1.4.0 → 1.5.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 06adf7119a277108834d59b13db51a1db026d52b
4
- data.tar.gz: 993205a4f732d652bf60d4f083d2d9f0ed54b73a
3
+ metadata.gz: 4cc1c6867f857a993d2f742e22150537d5ef29aa
4
+ data.tar.gz: 9c4e70bd6dd34f625000e5862855fffdcb08fe27
5
5
  SHA512:
6
- metadata.gz: a190126c3a2323ff087712933e3ace25110c624435897c6df8c803959fbd288ac19130c32c90fff7215e38708730de2fa4d8568aced23d756da1b36bdfe975d5
7
- data.tar.gz: 91ab7a7bba1f5535f913e230d8f42a47a9eb84b2575843e91d95ff372631dd3d5cbb1d2e05c91538faa79644f7ac22dc14e810fcc6179adfdc725f1446a77f8f
6
+ metadata.gz: c7f26cd2dff27bd70b98f84bbe7e6ab9e9fcecb68903d353b7faa3e853789cc6278773210657f281a8223d8aa6bc0f6a5041490df9a61daa6b2d6112a06c24e7
7
+ data.tar.gz: '08c2e33483cfbac4555843d32f408f4ff8d60c45e26e82e4d1bd65c21d6f4a9cf1a5d992a4a3c440a867bc5d05924232600a14971b5ff0522c9af828f16bf625'
@@ -1,5 +1,10 @@
1
1
  # Next Release
2
2
 
3
+ ## 1.5.0 (10/27/2017)
4
+
5
+ * Allow `SwitchUser.switch_back` to be considered even when not calling `#remember_user`.
6
+ * Use `redirect_back` for rails 5
7
+
3
8
  ## 1.4.0 (10/21/2016)
4
9
 
5
10
  * Add ability to `store_sign_in` info with devise provider
data/README.md CHANGED
@@ -14,7 +14,7 @@ switch_user is very useful in such use cases
14
14
 
15
15
  ## Example
16
16
 
17
- Visit here: <http://switch-user-example.heroku.com>, switch the current user in the select box.
17
+ Visit here: <http://switch-user-example.heroku.com/admin>, switch the current user in the select box.
18
18
 
19
19
  And source code here: <https://github.com/flyerhzm/switch_user_example>
20
20
 
@@ -24,6 +24,9 @@ Add in Gemfile.
24
24
  ```ruby
25
25
  gem "switch_user"
26
26
  ```
27
+
28
+ 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
29
+
27
30
  ## Usage
28
31
 
29
32
  Add following code into your layout page.
@@ -69,7 +72,7 @@ SwitchUser.setup do |config|
69
72
  # available_users is a hash,
70
73
  # key is the model name of user (:user, :admin, or any name you use),
71
74
  # value is a block that return the users that can be switched.
72
- config.available_users = { :user => lambda { User.all } } # use User.scoped instead for rails 3.2
75
+ config.available_users = { user: -> { User.all } } # use User.scoped instead for rails 3.2
73
76
 
74
77
  # available_users_identifiers is a hash,
75
78
  # keys in this hash should match a key in the available_users hash
@@ -77,55 +80,55 @@ SwitchUser.setup do |config|
77
80
  # defaults to id
78
81
  # this hash is to allow you to specify a different column to
79
82
  # expose for instance a username on a User model instead of id
80
- config.available_users_identifiers = { :user => :id }
83
+ config.available_users_identifiers = { user: :id }
81
84
 
82
85
  # available_users_names is a hash,
83
86
  # keys in this hash should match a key in the available_users hash
84
87
  # value is the column name which will be displayed in select box
85
- config.available_users_names = { :user => :email }
88
+ config.available_users_names = { user: :email }
86
89
 
87
90
  # controller_guard is a block,
88
91
  # if it returns true, the request will continue,
89
92
  # else the request will be refused and returns "Permission Denied"
90
93
  # if you switch from "admin" to user, the current_user param is "admin"
91
- config.controller_guard = lambda { |current_user, request| Rails.env.development? }
94
+ config.controller_guard = ->(current_user, request) { Rails.env.development? }
92
95
 
93
96
  # view_guard is a block,
94
97
  # if it returns true, the switch user select box will be shown,
95
98
  # else the select box will not be shown
96
99
  # if you switch from admin to "user", the current_user param is "user"
97
- config.view_guard = lambda { |current_user, request| Rails.env.development? }
100
+ config.view_guard = ->(current_user, request) { Rails.env.development? }
98
101
 
99
102
  # redirect_path is a block, it returns which page will be redirected
100
103
  # after switching a user.
101
- config.redirect_path = lambda { |request, params| '/' }
104
+ config.redirect_path = ->(request, params) { '/' }
102
105
  end
103
106
  ```
104
107
  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
108
 
106
109
  If you want to switch both available users and available admins
107
110
  ```ruby
108
- config.available_users = { :user => lambda { User.available }, :admin => lambda { Admin.available } }
111
+ config.available_users = { :user => -> { User.available }, :admin => -> { Admin.available } }
109
112
  ```
110
113
  If you want to use name column as the user identifier
111
114
  ```ruby
112
- config.available_users_identifiers => { :user => :name }
115
+ config.available_users_identifiers => { user: :name }
113
116
  ```
114
117
  If you want to display the login field in switch user select box
115
118
  ```ruby
116
- config.available_users_names = { :user => :login }
119
+ config.available_users_names = { user: :login }
117
120
  ```
118
121
  If you only allow switching from admin to user in production environment
119
122
  ```ruby
120
- config.controller_guard = lambda { |current_user, request| Rails.env == "production" and current_user.admin? }
123
+ config.controller_guard = ->(current_user, request) { Rails.env.production? && current_user.admin? }
121
124
  ```
122
125
  If you only want to display switch user select box for admins in production environment
123
126
  ```ruby
124
- config.view_guard = lambda { |current_user, request| Rails.env == "production" and current_user and current_user.admin? }
127
+ config.view_guard = ->(current_user, request) { Rails.env.production? && current_user && current_user.admin? }
125
128
  ```
126
129
  If you want to redirect user to "/dashboard" page
127
130
  ```ruby
128
- config.redirect_path = lambda { |request, params| "/dashboard" }
131
+ config.redirect_path = ->(request, params) { "/dashboard" }
129
132
  ```
130
133
  If you want to hide a 'Guest' item in the helper dropdown list
131
134
  ```ruby
@@ -137,16 +140,14 @@ Sometimes you'll want to be able to switch to an unprivileged user and then back
137
140
  You will need to make the following modifications to your configuration:
138
141
  ```ruby
139
142
  config.switch_back = true
140
- config.controller_guard = lambda { |current_user, request, original_user|
141
- current_user && current_user.admin? || original_user && original_user.super_admin?
142
- }
143
+ config.controller_guard = ->(current_user, request, original_user) { current_user && current_user.admin? || original_user && original_user.super_admin? }
143
144
  # Do something similar for the view_guard as well.
144
145
  ```
145
146
  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
147
 
147
148
  ## Using SwitchUser with RSpec and Capybara
148
149
 
149
- Add the following code to spec/support/switch_user.rb or spec/spec_helper.rb :
150
+ Add the following code to spec/support/switch_user.rb or spec/spec_helper.rb:
150
151
 
151
152
  ```ruby
152
153
  require 'switch_user/rspec'
@@ -157,7 +158,7 @@ You can now write your specs like so :
157
158
  ```ruby
158
159
  feature "Your feature", type: :feature do
159
160
  background do
160
- @user = User.make(:email => 'user@example.com', :password => 'password')
161
+ @user = User.make(email: 'user@example.com', password: 'password')
161
162
  end
162
163
 
163
164
  scenario "Your scenario" do
@@ -189,6 +190,6 @@ This feature should be used with extreme caution because of the security implica
189
190
 
190
191
  ## Credit
191
192
 
192
- Copyright © 2010 - 2015 Richard Huang (flyerhzm@gmail.com), released under the MIT license
193
+ Copyright © 2010 - 2017 Richard Huang (flyerhzm@gmail.com), released under the MIT license
193
194
 
194
195
  [0]: https://github.com/tablatom/hobo
@@ -1,23 +1,35 @@
1
1
  class SwitchUserController < ApplicationController
2
- before_action :developer_modes_only
2
+ before_action :developer_modes_only, :switch_back
3
3
 
4
4
  def set_current_user
5
5
  handle_request(params)
6
6
 
7
- redirect_to(SwitchUser.redirect_path.call(request, params))
7
+ redirect_path = SwitchUser.redirect_path.call(request, params)
8
+ if Rails.version.to_i >= 5 && redirect_path == :back
9
+ redirect_back(fallback_location: root_path)
10
+ else
11
+ redirect_to(redirect_path)
12
+ end
8
13
  end
9
14
 
10
15
  def remember_user
11
- # NOOP unless the user has explicity enabled this feature
12
- if SwitchUser.switch_back
13
- provider.remember_current_user(params[:remember] == "true")
16
+ redirect_path = SwitchUser.redirect_path.call(request, params)
17
+ if Rails.version.to_i >= 5 && redirect_path == :back
18
+ redirect_back(fallback_location: root_path)
19
+ else
20
+ redirect_to(redirect_path)
14
21
  end
15
-
16
- redirect_to(SwitchUser.redirect_path.call(request, params))
17
22
  end
18
23
 
19
24
  private
20
25
 
26
+ def switch_back
27
+ if SwitchUser.switch_back
28
+ provider.remember_current_user(true) if params[:remember] == "true"
29
+ provider.remember_current_user(false) if params[:remember] == "false"
30
+ end
31
+ end
32
+
21
33
  def developer_modes_only
22
34
  raise ActionController::RoutingError.new('Do not try to hack us.') unless available?
23
35
  end
@@ -36,9 +48,9 @@ class SwitchUserController < ApplicationController
36
48
  return
37
49
  end
38
50
  if SwitchUser.login_exclusive
39
- provider.login_exclusive(record.user, :scope => record.scope)
51
+ provider.login_exclusive(record.user, scope: record.scope)
40
52
  else
41
- provider.login_inclusive(record.user, :scope => record.scope)
53
+ provider.login_inclusive(record.user, scope: record.scope)
42
54
  end
43
55
  end
44
56
  end
@@ -23,11 +23,11 @@ module SwitchUserHelper
23
23
 
24
24
  option_tags = grouped_options_for_select(grouped_options_container.to_a, selected_user)
25
25
 
26
- render :partial => "switch_user/widget",
27
- :locals => {
28
- :option_tags => option_tags,
29
- :classes => options[:class],
30
- :styles => options[:style],
26
+ render partial: "switch_user/widget",
27
+ locals: {
28
+ option_tags: option_tags,
29
+ classes: options[:class],
30
+ styles: options[:style],
31
31
  }
32
32
  end
33
33
 
@@ -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 = '#{ActionController::Base.relative_url_root || '/'}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 = '#{ActionController::Base.relative_url_root || '/'}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 %>
@@ -1,4 +1,4 @@
1
1
  Rails.application.routes.draw do
2
- get :switch_user, :to => 'switch_user#set_current_user'
3
- get 'switch_user/remember_user', :to => 'switch_user#remember_user'
2
+ get :switch_user, to: 'switch_user#set_current_user'
3
+ get 'switch_user/remember_user', to: 'switch_user#remember_user'
4
4
  end
@@ -5,7 +5,7 @@ SwitchUser.setup do |config|
5
5
  # available_users is a hash,
6
6
  # key is the model name of user (:user, :admin, or any name you use),
7
7
  # value is a block that return the users that can be switched.
8
- config.available_users = { :user => lambda { User.all } }
8
+ config.available_users = { user: -> { User.all } }
9
9
 
10
10
  # available_users_identifiers is a hash,
11
11
  # keys in this hash should match a key in the available_users hash
@@ -13,28 +13,28 @@ SwitchUser.setup do |config|
13
13
  # defaults to id
14
14
  # this hash is to allow you to specify a different column to
15
15
  # expose for instance a username on a User model instead of id
16
- config.available_users_identifiers = { :user => :id }
16
+ config.available_users_identifiers = { user: :id }
17
17
 
18
18
  # available_users_names is a hash,
19
19
  # keys in this hash should match a key in the available_users hash
20
20
  # value is the column name which will be displayed in select box
21
- config.available_users_names = { :user => :email }
21
+ config.available_users_names = { user: :email }
22
22
 
23
23
  # controller_guard is a block,
24
24
  # if it returns true, the request will continue,
25
25
  # else the request will be refused and returns "Permission Denied"
26
26
  # if you switch from "admin" to user, the current_user param is "admin"
27
- config.controller_guard = lambda { |current_user, request| Rails.env.development? }
27
+ config.controller_guard = ->(current_user, request) { Rails.env.development? }
28
28
 
29
29
  # view_guard is a block,
30
30
  # if it returns true, the switch user select box will be shown,
31
31
  # else the select box will not be shown
32
32
  # if you switch from admin to "user", the current_user param is "user"
33
- config.view_guard = lambda { |current_user, request| Rails.env.development? }
33
+ config.view_guard = ->(current_user, request) { Rails.env.development? }
34
34
 
35
35
  # redirect_path is a block, it returns which page will be redirected
36
36
  # after switching a user.
37
- config.redirect_path = lambda { |request, params| '/' }
37
+ config.redirect_path = ->(request, params) { '/' }
38
38
 
39
39
  # helper_with_guest is a boolean value, if it set to false
40
40
  # the guest item in the helper won't be shown
@@ -53,13 +53,13 @@ module SwitchUser
53
53
 
54
54
  def self.reset_config
55
55
  self.provider = :devise
56
- self.available_users = { :user => lambda { User.all } }
57
- self.available_users_identifiers = { :user => :id }
58
- self.available_users_names = { :user => :email }
56
+ self.available_users = { user: -> { User.all } }
57
+ self.available_users_identifiers = { user: :id }
58
+ self.available_users_names = { user: :email }
59
59
  self.guard_class = "SwitchUser::LambdaGuard"
60
- self.controller_guard = lambda { |current_user, request| Rails.env.development? }
61
- self.view_guard = lambda { |current_user, request| Rails.env.development? }
62
- self.redirect_path = lambda { |request, params| request.env["HTTP_REFERER"] ? :back : root_path }
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
@@ -11,7 +11,7 @@ module SwitchUser
11
11
  end
12
12
 
13
13
  def find_scope_id(scope_id)
14
- sources.map {|source| source.find_scope_id(scope_id) }.compact.first
14
+ sources.map { |source| source.find_scope_id(scope_id) }.compact.first
15
15
  end
16
16
  end
17
17
 
@@ -40,7 +40,7 @@ module SwitchUser
40
40
 
41
41
  class GuestDataSource
42
42
  def all
43
- [ GuestRecord.new ]
43
+ [GuestRecord.new]
44
44
  end
45
45
 
46
46
  def find_scope_id(scope_id)
@@ -33,7 +33,7 @@ module SwitchUser
33
33
  user_identifier = @controller.session[:original_user_scope_identifier]
34
34
 
35
35
  if user_identifier
36
- UserLoader.prepare(:scope_identifier => user_identifier).user
36
+ UserLoader.prepare(scope_identifier: user_identifier).user
37
37
  end
38
38
  end
39
39
 
@@ -6,7 +6,7 @@ module SwitchUser
6
6
  @warden = @controller.warden
7
7
  end
8
8
 
9
- def login(user, scope = :user)
9
+ def login(user, scope = nil)
10
10
  if SwitchUser.provider.is_a?(Hash) && SwitchUser.provider[:store_sign_in]
11
11
  @warden.set_user(user, scope: scope)
12
12
  else
@@ -14,11 +14,11 @@ module SwitchUser
14
14
  end
15
15
  end
16
16
 
17
- def logout(scope = :user)
17
+ def logout(scope = nil)
18
18
  @warden.logout(scope)
19
19
  end
20
20
 
21
- def current_user(scope = :user)
21
+ def current_user(scope = nil)
22
22
  @warden.user(scope)
23
23
  end
24
24
  end
@@ -4,9 +4,9 @@ require 'rspec/core'
4
4
 
5
5
  RSpec.configure do |config|
6
6
 
7
- config.include SwitchUser::RSpecFeatureHelpers, :type => :feature
7
+ config.include SwitchUser::RSpecFeatureHelpers, type: :feature
8
8
 
9
- config.before(:each, :type => :feature) do
9
+ config.before(:each, type: :feature) do
10
10
  allow_any_instance_of(SwitchUserController).to receive(:available?).and_return(true)
11
11
  end
12
12
 
@@ -3,7 +3,7 @@ module SwitchUser
3
3
 
4
4
  class InvalidArgument < StandardError; end
5
5
 
6
- def switch_user(user_record_or_scope, user_id=nil)
6
+ def switch_user(user_record_or_scope, user_id = nil)
7
7
  _user_scope =
8
8
  case user_record_or_scope
9
9
  when ActiveRecord::Base
@@ -9,8 +9,8 @@ module SwitchUser
9
9
  end
10
10
 
11
11
  def self.users
12
- init_from_config.flat_map {|user_set|
13
- user_set.users.map {|user| Record.build(user, user_set) }
12
+ init_from_config.flat_map { |user_set|
13
+ user_set.users.map { |user| Record.build(user, user_set) }
14
14
  }
15
15
  end
16
16
 
@@ -24,7 +24,7 @@ module SwitchUser
24
24
  end
25
25
 
26
26
  def find_user(id)
27
- Record.build(users.where(:id => id).first, self)
27
+ Record.build(users.where(id: id).first, self)
28
28
  end
29
29
  alias :[] :find_user
30
30
 
@@ -1,3 +1,3 @@
1
1
  module SwitchUser
2
- VERSION = "1.4.0"
2
+ VERSION = "1.5.0"
3
3
  end
@@ -2,27 +2,27 @@ require 'spec_helper'
2
2
  require 'switch_user'
3
3
  require 'switch_user_controller'
4
4
 
5
- RSpec.describe SwitchUserController, :type => :controller do
5
+ RSpec.describe SwitchUserController, type: :controller do
6
6
  before do
7
7
  SwitchUser.provider = :dummy
8
8
  end
9
9
 
10
- let(:admin) { double(:admin, :admin? => true) }
10
+ let(:admin) { double(:admin, admin?: true) }
11
11
  let(:provider) { double(:provider,
12
- :original_user => admin,
13
- :current_user => nil)
12
+ original_user: admin,
13
+ current_user: nil)
14
14
  }
15
15
  describe "#set_current_user" do
16
16
  it "redirects the user to the specified location" do
17
- SwitchUser.redirect_path = lambda {|_,_| "/path"}
17
+ SwitchUser.redirect_path = ->(_, _) { "/path" }
18
18
  allow(controller).to receive(:available?).and_return(true)
19
- get :set_current_user, :scope_identifier => "user_1"
19
+ get :set_current_user, params: { scope_identifier: "user_1" }
20
20
 
21
21
  expect(response).to redirect_to("/path")
22
22
  end
23
23
 
24
24
  it "denies access according to the guard block" do
25
- SwitchUser.controller_guard = lambda {|_,_,_| false }
25
+ SwitchUser.controller_guard = ->(_, _, _) { false }
26
26
  expect {
27
27
  get :set_current_user
28
28
  }.to raise_error(ActionController::RoutingError)
@@ -30,9 +30,7 @@ RSpec.describe SwitchUserController, :type => :controller do
30
30
 
31
31
  describe "requests with a privileged original_user" do
32
32
  before do
33
- SwitchUser.controller_guard = lambda {|current_user, _, original_user|
34
- current_user.try(:admin?) || original_user.try(:admin?)
35
- }
33
+ SwitchUser.controller_guard = ->(current_user, _, original_user) { current_user.try(:admin?) || original_user.try(:admin?) }
36
34
  end
37
35
  it "allows access using the original_user param" do
38
36
  allow(controller).to receive(:provider).and_return(provider)
@@ -54,18 +52,18 @@ RSpec.describe SwitchUserController, :type => :controller do
54
52
  it "can remember the current user" do
55
53
  expect(provider).to receive(:remember_current_user).with(true)
56
54
 
57
- get :remember_user, :remember => "true"
55
+ get :remember_user, params: { remember: "true" }
58
56
  end
59
57
  it "can forget the current user" do
60
58
  expect(provider).to receive(:remember_current_user).with(false)
61
59
 
62
- get :remember_user, :remember => "false"
60
+ get :remember_user, params: { remember: "false" }
63
61
  end
64
62
  it "does nothing if switch_back is not enabled" do
65
63
  SwitchUser.switch_back = false
66
64
  expect(provider).not_to receive(:remember_current_user)
67
65
 
68
- get :remember_user, :remember => "true"
66
+ get :remember_user, params: { remember: "true" }
69
67
  end
70
68
  end
71
69
  end
@@ -2,13 +2,13 @@ require 'spec_helper'
2
2
  require 'switch_user'
3
3
  require 'switch_user_helper'
4
4
 
5
- RSpec.describe SwitchUserHelper, :type => :helper do
5
+ RSpec.describe SwitchUserHelper, type: :helper do
6
6
  before do
7
7
  SwitchUser.provider = :dummy
8
8
  end
9
9
 
10
- let(:user) { double(:user, :id => 1) }
11
- let(:admin) { double(:admin, :id => 1) }
10
+ let(:user) { double(:user, id: 1) }
11
+ let(:admin) { double(:admin, id: 1) }
12
12
  let(:provider) {
13
13
  _provider = SwitchUser::Provider::Dummy.new(controller)
14
14
  _provider
@@ -16,8 +16,8 @@ RSpec.describe SwitchUserHelper, :type => :helper do
16
16
 
17
17
  describe "#switch_user_select" do
18
18
  let(:guest_record) { SwitchUser::GuestRecord.new }
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') }
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
22
  let(:guest_option_tags) { %Q^<optgroup label="Guest"><option value="">Guest</option></optgroup>^ }
23
23
  let(:user_option_tags) { %Q^<optgroup label="User"><option value="user_1">user1</option></optgroup>^ }
@@ -125,7 +125,7 @@ RSpec.describe SwitchUserHelper, :type => :helper do
125
125
 
126
126
  describe "#user_tag_value" do
127
127
  it "for user" do
128
- user = double(:user, :id => 1)
128
+ user = double(:user, id: 1)
129
129
 
130
130
  expect(helper.send(:user_tag_value, user, :id, :user)).to eq('user_1')
131
131
  end
@@ -134,13 +134,13 @@ RSpec.describe SwitchUserHelper, :type => :helper do
134
134
  describe "#user_tag_label" do
135
135
  it "when name has call method" do
136
136
  user = double(:user)
137
- name = ->(user){ 'user1' }
137
+ name = ->(user) { 'user1' }
138
138
 
139
139
  expect(helper.send(:user_tag_label, user, name)).to eq('user1')
140
140
  end
141
141
 
142
142
  it "when name not has call method" do
143
- user = double(:name, :name => 'user1')
143
+ user = double(:name, name: 'user1')
144
144
  name = :name
145
145
 
146
146
  expect(helper.send(:user_tag_label, user, name)).to eq('user1')
@@ -1,14 +1,14 @@
1
1
  require 'spec_helper'
2
2
 
3
- RSpec.describe "Using SwitchUser", :type => :request do
4
- let(:user) { User.create!(:email => "foo@bar.com", :admin => true) }
5
- let(:other_user) { User.create!(:email => "other@bar.com", :admin => false) }
3
+ RSpec.describe "Using SwitchUser", type: :request do
4
+ let(:user) { User.create!(email: "foo@bar.com", admin: true) }
5
+ let(:other_user) { User.create!(email: "other@bar.com", admin: false) }
6
6
 
7
7
  before do
8
8
  SwitchUser.reset_config
9
9
  SwitchUser.provider = :session
10
- SwitchUser.controller_guard = lambda { |current_user, request| Rails.env.test? }
11
- SwitchUser.redirect_path = lambda {|_,_| "/dummy/open"}
10
+ SwitchUser.controller_guard = ->(current_user, request) { Rails.env.test? }
11
+ SwitchUser.redirect_path = ->(_, _) { "/dummy/open" }
12
12
  end
13
13
 
14
14
  it "signs a user in using switch_user" do
@@ -27,18 +27,16 @@ RSpec.describe "Using SwitchUser", :type => :request do
27
27
  context "using switch_back" do
28
28
  before do
29
29
  SwitchUser.switch_back = true
30
- SwitchUser.controller_guard = lambda { |current_user, request, original_user|
31
- current_user && current_user.admin? || original_user && original_user.admin?
32
- }
30
+ SwitchUser.controller_guard = ->(current_user, request, original_user) { current_user && current_user.admin? || original_user && original_user.admin? }
33
31
  end
34
32
 
35
- it "can switch back to a different user" do
33
+ it "can switch back to a different user through remember_user endpoint" do
36
34
  # login
37
- post "/login", :id => user.id
35
+ post "/login", params: { id: user.id }
38
36
  follow_redirect!
39
37
 
40
38
  # have SwitchUser remember us
41
- get "/switch_user/remember_user", :remember => true
39
+ get "/switch_user/remember_user", params: { remember: true }
42
40
  expect(session["original_user_scope_identifier"]).to be_present
43
41
 
44
42
  # check that we can switch to another user
@@ -54,7 +52,30 @@ RSpec.describe "Using SwitchUser", :type => :request do
54
52
  expect(session["user_id"]).to eq user.id
55
53
 
56
54
  # check that we can be un-remembered
57
- get "/switch_user/remember_user", :remember => false
55
+ get "/switch_user/remember_user", params: { remember: false }
56
+ expect(session["original_user"]).to be_nil
57
+ end
58
+
59
+ it "can switch back to a different user without hitting remember_user endpoint" do
60
+ # login
61
+ post "/login", params: { :id => user.id }
62
+ follow_redirect!
63
+
64
+ # check that we can switch to another user
65
+ get "/switch_user?scope_identifier=user_#{other_user.id}", params: { :remember => true }
66
+ expect(session["user_id"]).to eq other_user.id
67
+ expect(session["original_user_scope_identifier"]).to_not be_nil
68
+
69
+ # logout
70
+ get "/logout"
71
+ expect(session["user_id"]).to be_nil
72
+
73
+ # check that we can still switch to another user
74
+ get "/switch_user?scope_identifier=user_#{user.id}"
75
+ expect(session["user_id"]).to eq user.id
76
+
77
+ # check that we can be un-remembered
78
+ get "/switch_user/remember_user", params: { :remember => false }
58
79
  expect(session["original_user"]).to be_nil
59
80
  end
60
81
  end
@@ -65,13 +65,13 @@ RSpec.describe SwitchUser::Provider::Devise do
65
65
 
66
66
  describe "#login_exclusive" do
67
67
  before do
68
- allow(SwitchUser).to receive(:available_users).and_return({:user => nil, :admin => nil})
68
+ allow(SwitchUser).to receive(:available_users).and_return({ user: nil, admin: nil })
69
69
  provider.login(user, :admin)
70
- provider.login_exclusive(user, :scope => "user")
70
+ provider.login_exclusive(user, scope: "user")
71
71
  end
72
72
 
73
73
  it "logs the user in" do
74
- expect(provider.current_user).to eq user
74
+ expect(provider.current_user(:user)).to eq user
75
75
  end
76
76
 
77
77
  it "logs out other scopes" do
@@ -81,7 +81,7 @@ RSpec.describe SwitchUser::Provider::Devise do
81
81
 
82
82
  describe "#logout_all" do
83
83
  it "logs out users under all scopes" do
84
- allow(SwitchUser).to receive(:available_users).and_return({:user => nil, :admin => nil})
84
+ allow(SwitchUser).to receive(:available_users).and_return({ user: nil, admin: nil })
85
85
  provider.login(user, :admin)
86
86
  provider.login(user, :user)
87
87
 
@@ -94,7 +94,7 @@ RSpec.describe SwitchUser::Provider::Devise do
94
94
 
95
95
  describe "#all_current_users" do
96
96
  it "pulls users from an alternate scope" do
97
- allow(SwitchUser).to receive(:available_users).and_return({:user => nil, :admin => nil})
97
+ allow(SwitchUser).to receive(:available_users).and_return({ user: nil, admin: nil })
98
98
  provider.login(user, :admin)
99
99
 
100
100
  expect(provider.current_users_without_scope).to eq [user]
@@ -6,32 +6,32 @@ Capybara.app = MyApp::Application
6
6
 
7
7
  require 'switch_user/rspec'
8
8
 
9
- RSpec.feature "SwitchUser::RSpecFeatureHelpers", :type => :feature do
9
+ RSpec.feature "SwitchUser::RSpecFeatureHelpers", type: :feature do
10
10
  background do
11
- @user = User.create!(:email => "foo@bar.com", :admin => true)
12
- @client = Client.create!(:email => "foo@bar.com")
11
+ @user = User.create!(email: "foo@bar.com", admin: true)
12
+ @client = Client.create!(email: "foo@bar.com")
13
13
  end
14
14
 
15
- before(:each) do
16
- allow(SwitchUser).to receive(:controller_guard).and_return(lambda{|current_user, request| true})
15
+ before(:example) do
16
+ allow(SwitchUser).to receive(:controller_guard).and_return(->(current_user, request) { true })
17
17
 
18
- allow(SwitchUser).to receive(:available_users).and_return({:user => lambda { User.all }})
18
+ allow(SwitchUser).to receive(:available_users).and_return({ user: -> { User.all } })
19
19
 
20
- allow(SwitchUser).to receive(:available_users_identifiers).and_return({:user => :id})
20
+ allow(SwitchUser).to receive(:available_users_identifiers).and_return({ user: :id })
21
21
 
22
- allow(SwitchUser).to receive(:available_users_names).and_return({:user => :email})
22
+ allow(SwitchUser).to receive(:available_users_names).and_return({ user: :email })
23
23
  end
24
24
 
25
25
  scenario "when controller_guard return false" do
26
- allow(SwitchUser).to receive(:controller_guard).and_return(lambda{|current_user, request| false})
26
+ allow(SwitchUser).to receive(:controller_guard).and_return(->(current_user, request) { false })
27
27
 
28
28
  expect do
29
29
  switch_user @user
30
- end.to_not raise_error ActionController::RoutingError, /Do not try to hack us/
30
+ end.not_to raise_error
31
31
  end
32
32
 
33
33
  scenario "when controller_guard return false and controller call original available?" do
34
- allow(SwitchUser).to receive(:controller_guard).and_return(lambda{|current_user, request| false})
34
+ allow(SwitchUser).to receive(:controller_guard).and_return(->(current_user, request) { false })
35
35
 
36
36
  allow_any_instance_of(SwitchUserController).to receive(:available?).and_call_original
37
37
 
@@ -43,20 +43,20 @@ RSpec.feature "SwitchUser::RSpecFeatureHelpers", :type => :feature do
43
43
  scenario "arg is @user, available_users is default, and available_users_identifiers is default" do
44
44
  expect do
45
45
  switch_user @user
46
- end.to_not raise_error
46
+ end.not_to raise_error
47
47
  end
48
48
 
49
49
  scenario "arg is @user, available_users is default, and available_users_identifiers is {user: id}" do
50
- allow(SwitchUser).to receive(:available_users_identifiers).and_return({user: :id})
50
+ allow(SwitchUser).to receive(:available_users_identifiers).and_return({ user: :id })
51
51
 
52
52
  expect do
53
53
  switch_user @user
54
- end.to_not raise_error
54
+ end.not_to raise_error
55
55
  end
56
56
 
57
57
  scenario "arg is @user, available_users is default, and available_users_identifiers is {:client => :id}" do
58
- allow(SwitchUser).to receive(:available_users_identifiers).and_return({:client => :id})
59
- allow(SwitchUser).to receive(:available_users_names).and_return({:client => :email})
58
+ allow(SwitchUser).to receive(:available_users_identifiers).and_return({ client: :id })
59
+ allow(SwitchUser).to receive(:available_users_names).and_return({ client: :email })
60
60
 
61
61
  expect do
62
62
  switch_user @user
@@ -70,7 +70,7 @@ RSpec.feature "SwitchUser::RSpecFeatureHelpers", :type => :feature do
70
70
  end
71
71
 
72
72
  scenario "arg is @client, available_users is {:user => lambda { User.all }, :client => lambda {Client.all}}, and available_users_identifiers is default" do
73
- allow(SwitchUser).to receive(:available_users).and_return({:user => lambda { User.all }, :client => lambda {Client.all}})
73
+ allow(SwitchUser).to receive(:available_users).and_return({ user: -> { User.all }, client: -> { Client.all } })
74
74
 
75
75
  expect do
76
76
  switch_user @client
@@ -78,20 +78,20 @@ RSpec.feature "SwitchUser::RSpecFeatureHelpers", :type => :feature do
78
78
  end
79
79
 
80
80
  scenario "arg is @client, available_users is {:user => lambda { User.all }, :client => lambda {Client.all}}, and available_users_identifiers is {:user => id, :client => id}" do
81
- allow(SwitchUser).to receive(:available_users).and_return({:user => lambda { User.all }, :client => lambda {Client.all}})
81
+ allow(SwitchUser).to receive(:available_users).and_return({ user: -> { User.all }, client: -> { Client.all } })
82
82
 
83
- allow(SwitchUser).to receive(:available_users_identifiers).and_return({:user => :id, :client => :id})
84
- allow(SwitchUser).to receive(:available_users_names).and_return({:user => :email, :client => :email})
83
+ allow(SwitchUser).to receive(:available_users_identifiers).and_return({ user: :id, client: :id })
84
+ allow(SwitchUser).to receive(:available_users_names).and_return({ user: :email, client: :email })
85
85
 
86
86
  expect do
87
87
  switch_user @client
88
- end.to_not raise_error
88
+ end.not_to raise_error
89
89
  end
90
90
 
91
91
  scenario "args is :user and @user.id, available_users is default, and available_users_identifiers is default" do
92
92
  expect do
93
93
  switch_user :user, @user.id
94
- end.to_not raise_error
94
+ end.not_to raise_error
95
95
  end
96
96
 
97
97
  scenario "arg is :client and @client.id, available_users is default, and available_users_identifiers is default" do
@@ -121,7 +121,7 @@ RSpec.feature "SwitchUser::RSpecFeatureHelpers", :type => :feature do
121
121
  scenario "args is :user and 0, available_users is default, and available_users_identifiers is default" do
122
122
  expect do
123
123
  switch_user :user, 0
124
- end.to_not raise_error
124
+ end.not_to raise_error
125
125
  end
126
126
 
127
127
  end
@@ -7,7 +7,7 @@ require 'pry'
7
7
  require 'awesome_print'
8
8
 
9
9
  RSpec.configure do |config|
10
- config.filter_run :focus => true
10
+ config.filter_run focus: true
11
11
  config.run_all_when_everything_filtered = true
12
12
  config.use_transactional_fixtures = true
13
13
  config.expose_dsl_globally = false
@@ -27,18 +27,30 @@ class ApplicationController < ActionController::Base
27
27
  end
28
28
 
29
29
  class DummyController < ApplicationController
30
- before_filter :require_user, :only => :protected
30
+ before_action :require_user, only: :protected
31
31
 
32
32
  def authenticated
33
- render :text => current_user.inspect
33
+ if Rails.version.to_i >= 5
34
+ render plain: current_user.inspect
35
+ else
36
+ render text: current_user.inspect
37
+ end
34
38
  end
35
39
 
36
40
  def open
37
- render :text => view_context.switch_user_select
41
+ if Rails.version.to_i >= 5
42
+ render plain: view_context.switch_user_select
43
+ else
44
+ render text: view_context.switch_user_select
45
+ end
38
46
  end
39
47
 
40
48
  def protected
41
- render :text => view_context.switch_user_select
49
+ if Rails.version.to_i >= 5
50
+ render plain: view_context.switch_user_select
51
+ else
52
+ render text: view_context.switch_user_select
53
+ end
42
54
  end
43
55
  end
44
56
 
@@ -55,13 +67,13 @@ module MyApp
55
67
  end
56
68
  Rails.application.initialize!
57
69
  Rails.application.routes.draw do
58
- get 'dummy/protected', :to => "dummy#protected"
59
- get 'dummy/open', :to => "dummy#open"
60
- post 'login', :to => "dummy#login"
61
- get 'logout', :to => "dummy#logout"
62
- get 'authenticated', :to => "dummy#authenticated"
63
- get :switch_user, :to => 'switch_user#set_current_user'
64
- get 'switch_user/remember_user', :to => 'switch_user#remember_user'
70
+ get 'dummy/protected', to: "dummy#protected"
71
+ get 'dummy/open', to: "dummy#open"
72
+ post 'login', to: "dummy#login"
73
+ get 'logout', to: "dummy#logout"
74
+ get 'authenticated', to: "dummy#authenticated"
75
+ get :switch_user, to: 'switch_user#set_current_user'
76
+ get 'switch_user/remember_user', to: 'switch_user#remember_user'
65
77
  end
66
78
 
67
79
  connection = ActiveRecord::Base.connection
@@ -25,7 +25,7 @@ RSpec.shared_examples_for "a provider" do
25
25
  end
26
26
 
27
27
  it "knows if there are any users logged in" do
28
- provider.login(user)
28
+ provider.login(user, :user)
29
29
 
30
30
  expect(provider.current_users_without_scope).to eq [user]
31
31
  end
@@ -36,7 +36,7 @@ RSpec.shared_examples_for "a provider" do
36
36
  provider.login_exclusive(other_user, scope: "user")
37
37
 
38
38
  expect(provider.original_user).to eq user
39
- expect(provider.current_user).to eq other_user
39
+ expect(provider.current_user(:user)).to eq other_user
40
40
  end
41
41
 
42
42
  it "can forget the original_user" do
@@ -5,7 +5,7 @@ module SwitchUser
5
5
  describe '#all' do
6
6
  it 'aggregates multiple data_sources' do
7
7
  user = double(:user)
8
- s1 = double(:s1, :all => [user])
8
+ s1 = double(:s1, all: [user])
9
9
  source = DataSources.new([s1, s1])
10
10
 
11
11
  expect(source.all).to eq [user, user]
@@ -15,8 +15,8 @@ module SwitchUser
15
15
  describe '#find_scope_id' do
16
16
  it 'can find a corresponding record across data sources' do
17
17
  user = double(:user)
18
- s1 = double(:s1, :find_scope_id => nil)
19
- s2 = double(:s2, :find_scope_id => user)
18
+ s1 = double(:s1, find_scope_id: nil)
19
+ s2 = double(:s2, find_scope_id: user)
20
20
  source = DataSources.new([s1, s2])
21
21
 
22
22
  expect(source.find_scope_id("user_10")).to eq user
@@ -25,7 +25,7 @@ module SwitchUser
25
25
  end
26
26
 
27
27
  RSpec.describe DataSource do
28
- pending # it's tested in integration test, need to find a good way to test it here.
28
+ skip # it's tested in integration test, need to find a good way to test it here.
29
29
  end
30
30
 
31
31
  RSpec.describe GuestDataSource do
@@ -8,10 +8,10 @@ module SwitchUser
8
8
  provider = double.as_null_object
9
9
  guard = SwitchUser::LambdaGuard.new(controller, provider)
10
10
 
11
- SwitchUser.controller_guard = lambda {|a| a }
11
+ SwitchUser.controller_guard = ->(a) { a }
12
12
  expect(guard).to be_controller_available
13
13
 
14
- SwitchUser.controller_guard = lambda {|a| !a }
14
+ SwitchUser.controller_guard = ->(a) { !a }
15
15
  expect(guard).not_to be_controller_available
16
16
  end
17
17
  end
@@ -11,16 +11,16 @@ RSpec.describe SwitchUser::UserLoader do
11
11
 
12
12
  describe ".user" do
13
13
  before do
14
- SwitchUser.available_users_identifiers = {:user => :id}
15
- allow(User).to receive(:where).with(:id => "1").and_return(user_result)
14
+ SwitchUser.available_users_identifiers = { user: :id }
15
+ allow(User).to receive(:where).with(id: "1").and_return(user_result)
16
16
  end
17
17
  it "can be loaded from a scope and identifier" do
18
- loaded_user = SwitchUser::UserLoader.prepare("user","1").user
18
+ loaded_user = SwitchUser::UserLoader.prepare("user", "1").user
19
19
 
20
20
  expect(loaded_user).to eq user
21
21
  end
22
22
  it "can be loaded by a passing an unprocessed scope identifier" do
23
- loaded_user = SwitchUser::UserLoader.prepare(:scope_identifier => "user_1").user
23
+ loaded_user = SwitchUser::UserLoader.prepare(scope_identifier: "user_1").user
24
24
 
25
25
  expect(loaded_user).to eq user
26
26
  end
@@ -32,7 +32,7 @@ RSpec.describe SwitchUser::UserLoader do
32
32
  end
33
33
 
34
34
  it "returns a user" do
35
- allow(User).to receive(:where).with(:id => 1).and_return(user_result)
35
+ allow(User).to receive(:where).with(id: 1).and_return(user_result)
36
36
 
37
37
  loader = SwitchUser::UserLoader.new("user", 1)
38
38
 
@@ -47,8 +47,8 @@ RSpec.describe SwitchUser::UserLoader do
47
47
  end
48
48
 
49
49
  it "loads a user with an alternate identifier column" do
50
- allow(User).to receive(:where).with(:email => 2).and_return(user_result)
51
- SwitchUser.available_users_identifiers = {:user => :email}
50
+ allow(User).to receive(:where).with(email: 2).and_return(user_result)
51
+ SwitchUser.available_users_identifiers = { user: :email }
52
52
 
53
53
  loader = SwitchUser::UserLoader.new("user", 2)
54
54
  expect(loader.user).to eq user
@@ -3,8 +3,8 @@ require 'switch_user/user_set'
3
3
 
4
4
  module SwitchUser
5
5
  RSpec.describe UserSet do
6
- let!(:user) { User.create(:email => "test@example.com") }
7
- let(:set) { UserSet.new(:user, :id, :email, lambda { User.all }) }
6
+ let!(:user) { User.create(email: "test@example.com") }
7
+ let(:set) { UserSet.new(:user, :id, :email, -> { User.all }) }
8
8
  after { User.delete_all }
9
9
  it "returns an object that knows it's scope, id and label" do
10
10
  found_user = set[user.id]
@@ -17,14 +17,14 @@ module SwitchUser
17
17
  expect(set.users).to eq [user]
18
18
  end
19
19
  it "chains the where on to the provided scope" do
20
- set = UserSet.new(:user, :id, :email, lambda { User.all })
20
+ set = UserSet.new(:user, :id, :email, -> { User.all })
21
21
  expect(set.find_user(user.id).label).to eq user.email
22
22
  end
23
23
  end
24
24
  RSpec.describe UserSet::Record do
25
25
  it "correctly configures the record using the set" do
26
- user = double(:user, :id => 100, :email => "test@example.com")
27
- set = double(:set, :identifier => :id, :label => :email, :scope => :user)
26
+ user = double(:user, id: 100, email: "test@example.com")
27
+ set = double(:set, identifier: :id, label: :email, scope: :user)
28
28
  record = UserSet::Record.build(user, set)
29
29
  expect(record.id).to eq 100
30
30
  expect(record.label).to eq "test@example.com"
@@ -28,6 +28,6 @@ Gem::Specification.new do |s|
28
28
  s.add_development_dependency "capybara"
29
29
 
30
30
  s.files = `git ls-files`.split("\n")
31
- s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
31
+ s.executables = `git ls-files`.split("\n").map { |f| f =~ /^bin\/(.*)/ ? $1 : nil }.compact
32
32
  s.require_path = 'lib'
33
33
  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.4.0
4
+ version: 1.5.0
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-10-21 00:00:00.000000000 Z
12
+ date: 2017-10-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -261,7 +261,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
261
261
  version: 1.3.6
262
262
  requirements: []
263
263
  rubyforge_project: switch_user
264
- rubygems_version: 2.5.1
264
+ rubygems_version: 2.6.13
265
265
  signing_key:
266
266
  specification_version: 4
267
267
  summary: Easily switch current user to speed up development