switch_user 1.5.0 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/Gemfile +2 -0
  4. data/README.md +1 -0
  5. data/Rakefile +2 -0
  6. data/app/controllers/switch_user_controller.rb +6 -4
  7. data/app/helpers/switch_user_helper.rb +8 -8
  8. data/config/routes.rb +2 -0
  9. data/lib/generators/switch_user/install/install_generator.rb +2 -1
  10. data/lib/generators/switch_user/install/templates/switch_user.rb +5 -3
  11. data/lib/switch_user.rb +12 -12
  12. data/lib/switch_user/base_guard.rb +4 -2
  13. data/lib/switch_user/data_source.rb +6 -6
  14. data/lib/switch_user/lambda_guard.rb +2 -0
  15. data/lib/switch_user/provider.rb +15 -13
  16. data/lib/switch_user/provider/authlogic.rb +6 -6
  17. data/lib/switch_user/provider/base.rb +5 -4
  18. data/lib/switch_user/provider/clearance.rb +5 -3
  19. data/lib/switch_user/provider/devise.rb +2 -0
  20. data/lib/switch_user/provider/dummy.rb +8 -12
  21. data/lib/switch_user/provider/restful_authentication.rb +5 -3
  22. data/lib/switch_user/provider/session.rb +5 -3
  23. data/lib/switch_user/provider/sorcery.rb +6 -6
  24. data/lib/switch_user/rails.rb +3 -1
  25. data/lib/switch_user/rspec.rb +2 -2
  26. data/lib/switch_user/rspec/feature_helpers.rb +6 -7
  27. data/lib/switch_user/user_loader.rb +5 -2
  28. data/lib/switch_user/user_set.rb +5 -3
  29. data/lib/switch_user/version.rb +3 -1
  30. data/spec/controllers/switch_user_controller_spec.rb +21 -18
  31. data/spec/helpers/switch_user_helper_spec.rb +45 -46
  32. data/spec/integration/switch_user_spec.rb +91 -30
  33. data/spec/provider/authlogic_spec.rb +3 -1
  34. data/spec/provider/clearance_spec.rb +3 -1
  35. data/spec/provider/devise_spec.rb +17 -17
  36. data/spec/provider/dummy_spec.rb +3 -1
  37. data/spec/provider/restful_authentication_spec.rb +3 -1
  38. data/spec/provider/session_spec.rb +3 -1
  39. data/spec/provider/sorcery_spec.rb +3 -1
  40. data/spec/provider_spec.rb +3 -1
  41. data/spec/rspec/feature_helpers_spec.rb +31 -30
  42. data/spec/spec_helper.rb +3 -1
  43. data/spec/support/application.rb +13 -12
  44. data/spec/support/provider.rb +11 -9
  45. data/spec/switch_user/data_source_spec.rb +3 -1
  46. data/spec/switch_user/lambda_guard_spec.rb +4 -2
  47. data/spec/switch_user/user_loader_spec.rb +18 -16
  48. data/spec/switch_user/user_set_spec.rb +9 -7
  49. data/spec/switch_user_spec.rb +6 -4
  50. data/switch_user.gemspec +24 -23
  51. metadata +19 -19
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4cc1c6867f857a993d2f742e22150537d5ef29aa
4
- data.tar.gz: 9c4e70bd6dd34f625000e5862855fffdcb08fe27
3
+ metadata.gz: 49886e9e1c82e46feeb807ca7c44362dca6ebf35
4
+ data.tar.gz: caf2f62643f90910f792c41511ffde18cdfc19f4
5
5
  SHA512:
6
- metadata.gz: c7f26cd2dff27bd70b98f84bbe7e6ab9e9fcecb68903d353b7faa3e853789cc6278773210657f281a8223d8aa6bc0f6a5041490df9a61daa6b2d6112a06c24e7
7
- data.tar.gz: '08c2e33483cfbac4555843d32f408f4ff8d60c45e26e82e4d1bd65c21d6f4a9cf1a5d992a4a3c440a867bc5d05924232600a14971b5ff0522c9af828f16bf625'
6
+ metadata.gz: c44bffb58cfdc158faf321fe268eb63fbc2d982433c84d675918ed7f227341482bf0bf49961a2b5c0c58f29ac5f7dd59e59f588c636bc6cf2b73f6968dc9ec0b
7
+ data.tar.gz: d3c75243ee248fdc3bd4a136f3447d0d9432fe84d0a37cfede20409f3cede749e841e33965a86e58396b646feade108cec0ef40bc34883c78f7f60457f25c0e3
@@ -1,5 +1,10 @@
1
1
  # Next Release
2
2
 
3
+ ## 1.5.1 (10/28/2018)
4
+
5
+ * Respect identifier when storing `original_user`
6
+ * Refactor code by awesomecode.io
7
+
3
8
  ## 1.5.0 (10/27/2017)
4
9
 
5
10
  * Allow `SwitchUser.switch_back` to be considered even when not calling `#remember_user`.
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in switch_user.gemspec
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # switch_user
2
2
 
3
3
  [![Build Status](https://secure.travis-ci.org/flyerhzm/switch_user.png)](http://travis-ci.org/flyerhzm/switch_user)
4
+ [![AwesomeCode Status for flyerhzm/switch_user](https://awesomecode.io/projects/5f029f36-047b-4332-8405-9c7913b195bc/status)](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,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'bundler'
2
4
  Bundler::GemHelper.install_tasks
@@ -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] == "true"
29
- provider.remember_current_user(false) if params[:remember] == "false"
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.new('Do not try to hack us.') unless available?
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
- 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
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: "switch_user/widget",
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
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Rails.application.routes.draw do
2
4
  get :switch_user, to: 'switch_user#set_current_user'
3
5
  get 'switch_user/remember_user', to: 'switch_user#remember_user'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SwitchUser
2
4
  module Generators
3
5
  class InstallGenerator < Rails::Generators::Base
@@ -10,4 +12,3 @@ module SwitchUser
10
12
  end
11
13
  end
12
14
  end
13
-
@@ -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 = ->(current_user, request) { Rails.env.development? }
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 = ->(current_user, request) { Rails.env.development? }
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 = ->(request, params) { '/' }
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
@@ -1,16 +1,16 @@
1
- if defined?(Rails)
2
- require 'switch_user/rails'
3
- end
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, "switch_user/user_set"
8
- autoload :UserLoader, "switch_user/user_loader"
9
- autoload :Provider, "switch_user/provider"
10
- autoload :BaseGuard, "switch_user/base_guard"
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 < Exception; end
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 = "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 }
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.new("you must implement controller_available?")
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 { |source| source.all }
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
- "Guest"
74
+ 'Guest'
74
75
  end
75
76
 
76
- def scope_id
77
- end
77
+ def scope_id; end
78
78
  end
79
79
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SwitchUser
2
4
  class LambdaGuard < BaseGuard
3
5
  def controller_available?
@@ -1,20 +1,22 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SwitchUser
2
4
  module Provider
3
- autoload :Base, "switch_user/provider/base"
4
- autoload :Authlogic, "switch_user/provider/authlogic"
5
- autoload :Clearance, "switch_user/provider/clearance"
6
- autoload :Devise, "switch_user/provider/devise"
7
- autoload :RestfulAuthentication, "switch_user/provider/restful_authentication"
8
- autoload :Sorcery, "switch_user/provider/sorcery"
9
- autoload :Dummy, "switch_user/provider/dummy"
10
- autoload :Session, "switch_user/provider/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
- klass_part = SwitchUser.provider[:name]
15
- else
16
- klass_part = SwitchUser.provider
17
- end
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, scope = nil)
10
+ def login(user, _scope = nil)
9
11
  UserSession.create(user)
10
12
  end
11
13
 
12
- def logout(scope = nil)
14
+ def logout(_scope = nil)
13
15
  @controller.current_user_session.destroy
14
16
  end
15
17
 
16
- def current_user(scope = nil)
18
+ def current_user(_scope = nil)
17
19
  result = UserSession.find
18
- if result
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.inject([]) do |users, scope|
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
- user_identifier = "#{user_type}_#{user.id}"
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, scope = nil)
10
+ def login(user, _scope = nil)
9
11
  @controller.sign_in(user)
10
12
  end
11
13
 
12
- def logout(scope = nil)
14
+ def logout(_scope = nil)
13
15
  @controller.sign_out
14
16
  end
15
17
 
16
- def current_user(scope = nil)
18
+ def current_user(_scope = nil)
17
19
  @controller.current_user
18
20
  end
19
21
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SwitchUser
2
4
  module Provider
3
5
  class Devise < Base
@@ -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(controller)
6
+ def initialize(_controller)
5
7
  @user = nil
6
8
  end
7
9
 
8
- def login(user, scope = nil)
10
+ def login(user, _scope = nil)
9
11
  @user = user
10
12
  end
11
13
 
12
- def logout(scope = nil)
14
+ def logout(_scope = nil)
13
15
  @user = nil
14
16
  end
15
17
 
16
- def current_user(scope = nil)
18
+ def current_user(_scope = nil)
17
19
  @user
18
20
  end
19
21
 
20
- def original_user
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, scope = nil)
10
+ def login(user, _scope = nil)
9
11
  @controller.current_user = user
10
12
  end
11
13
 
12
- def logout(scope = nil)
14
+ def logout(_scope = nil)
13
15
  @controller.logout_killing_session!
14
16
  end
15
17
 
16
- def current_user(scope = nil)
18
+ def current_user(_scope = nil)
17
19
  @controller.current_user
18
20
  end
19
21
  end