pretender 0.3.2 → 0.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
- SHA1:
3
- metadata.gz: c0ab4d0149f46b7e67a8dce743f1ca1af138dc83
4
- data.tar.gz: 024cda09d36830a14181e4c2804f0e0de91e79d0
2
+ SHA256:
3
+ metadata.gz: 624438bfbdcef436923e6f49129b0a9ea5720e40f0ddb00c01049a2550142d53
4
+ data.tar.gz: 2791622f97f44d0235e4b49df110710236311f8a6ddec028c09a5635f8d7510c
5
5
  SHA512:
6
- metadata.gz: c9363670918c4f314ca836ef35e0f605e072592ec52bcd71bd8e24302338a06f6acc004dfe38daf83a9071305edc522b811aab9537e7bea450e233b6ced32e10
7
- data.tar.gz: 372bde2ce4d5e55abaa2ef747afa726b243e2fe6dc9bbcf7a90f72722f454bb1f5d33e561147e0e35e34959a4a44656f03c970bc3fa514d0f56eb1ee1a37dd83
6
+ metadata.gz: fadb24776c856be26bbf61b7c374b9720388aacbd334b31727d54a6ee9685e5b8a87dff97d884ac7690c7279c55276ebf703bdd22b927ed3271dd3f15ba7c3cb
7
+ data.tar.gz: d347f12d73ad8618e82bdcae16101271689810ba1b90cbb83c08b7cf4b9e3513bebeb51d76645b71b2793661f0e0e8414b32379841844d9287b89d4b9cf79afc
data/CHANGELOG.md CHANGED
@@ -1,22 +1,38 @@
1
- ## 0.3.2
1
+ ## 0.5.0 (2023-07-02)
2
+
3
+ - Dropped support for Ruby < 3 and Rails < 6.1
4
+
5
+ ## 0.4.0 (2022-01-10)
6
+
7
+ - Dropped support for Ruby < 2.6 and Rails < 5.2
8
+
9
+ ## 0.3.4 (2019-01-31)
10
+
11
+ - Fixed error with Action Cable and eager loading
12
+
13
+ ## 0.3.3 (2018-09-21)
14
+
15
+ - Added support for Action Cable
16
+
17
+ ## 0.3.2 (2018-01-21)
2
18
 
3
19
  - Support for Mongoid `BSON::ObjectId` out of the box
4
20
  - Fixed issue with impersonated resource caching
5
21
 
6
- ## 0.3.1
22
+ ## 0.3.1 (2017-06-18)
7
23
 
8
24
  - Fixed `stack level too deep` error
9
25
 
10
- ## 0.3.0
26
+ ## 0.3.0 (2017-06-11)
11
27
 
12
28
  - Fixed compatibility with Clearance
13
29
  - Added support for Rails API
14
30
  - Added support for custom primary key
15
31
 
16
- ## 0.2.1
32
+ ## 0.2.1 (2016-07-07)
17
33
 
18
34
  - Better error message
19
35
 
20
- ## 0.2.0
36
+ ## 0.2.0 (2016-07-07)
21
37
 
22
38
  - Started changelog
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013 Andrew Kane
1
+ Copyright (c) 2013-2023 Andrew Kane
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -1,25 +1,27 @@
1
1
  # Pretender
2
2
 
3
- As an admin, there are times you want to see exactly what another user sees. Meet Pretender.
3
+ As an admin, there are times you want to see exactly what another user sees. Meet Pretender.
4
4
 
5
- - Easily to switch between users
5
+ - Easily switch between users
6
6
  - Minimal code changes
7
- - Plays nicely with auditing tools
7
+ - Plays nicely with Action Cable and auditing tools
8
8
 
9
- :boom: [Rock on](http://www.youtube.com/watch?v=SBjQ9tuuTJQ)
9
+ :boom: [Rock on](https://www.youtube.com/watch?v=SBjQ9tuuTJQ)
10
10
 
11
- Pretender is flexible and lightweight - less than 60 lines of code :-)
11
+ Pretender is flexible and lightweight - less than 100 lines of code :-)
12
12
 
13
13
  Works with any authentication system - [Devise](https://github.com/plataformatec/devise), [Authlogic](https://github.com/binarylogic/authlogic), and [Sorcery](https://github.com/Sorcery/sorcery) to name a few.
14
14
 
15
15
  :tangerine: Battle-tested at [Instacart](https://www.instacart.com/opensource)
16
16
 
17
+ [![Build Status](https://github.com/ankane/pretender/workflows/build/badge.svg?branch=master)](https://github.com/ankane/pretender/actions)
18
+
17
19
  ## Installation
18
20
 
19
21
  Add this line to your application’s Gemfile:
20
22
 
21
23
  ```ruby
22
- gem 'pretender'
24
+ gem "pretender"
23
25
  ```
24
26
 
25
27
  And add this to your `ApplicationController`:
@@ -113,14 +115,40 @@ If you keep audit logs with a library like [Audited](https://github.com/collecti
113
115
  Audited.current_user_method = :true_user
114
116
  ```
115
117
 
118
+ ## Action Cable
119
+
120
+ And add this to your `ApplicationCable::Connection`:
121
+
122
+ ```ruby
123
+ module ApplicationCable
124
+ class Connection < ActionCable::Connection::Base
125
+ identified_by :current_user, :true_user
126
+ impersonates :user
127
+
128
+ def connect
129
+ self.current_user = find_verified_user
130
+ reject_unauthorized_connection unless current_user
131
+ end
132
+
133
+ private
134
+
135
+ def find_verified_user
136
+ env["warden"].user # for Devise
137
+ end
138
+ end
139
+ end
140
+ ```
141
+
142
+ The `current_user` method now returns the impersonated user in channels.
143
+
116
144
  ## Configuration
117
145
 
118
- Pretender is super flexible. You can change the names of methods and even impersonate multiple roles at the same time. Here’s the default configuration.
146
+ Pretender is super flexible. You can change the names of methods and even impersonate multiple roles at the same time. Here’s the default configuration.
119
147
 
120
148
  ```ruby
121
149
  impersonates :user,
122
150
  method: :current_user,
123
- with: -> (id) { User.find_by(id: id) }
151
+ with: ->(id) { User.find_by(id: id) }
124
152
  ```
125
153
 
126
154
  Mold it to fit your application.
@@ -128,7 +156,7 @@ Mold it to fit your application.
128
156
  ```ruby
129
157
  impersonates :account,
130
158
  method: :authenticated_account,
131
- with: -> (id) { EnterpriseAccount.find_by(id: id) }
159
+ with: ->(id) { EnterpriseAccount.find_by(id: id) }
132
160
  ```
133
161
 
134
162
  This creates three methods:
@@ -139,6 +167,10 @@ impersonate_account
139
167
  stop_impersonating_account
140
168
  ```
141
169
 
170
+ ## History
171
+
172
+ View the [changelog](https://github.com/ankane/pretender/blob/master/CHANGELOG.md)
173
+
142
174
  ## Contributing
143
175
 
144
176
  Everyone is encouraged to help improve this project. Here are a few ways you can help:
@@ -147,3 +179,12 @@ Everyone is encouraged to help improve this project. Here are a few ways you can
147
179
  - Fix bugs and [submit pull requests](https://github.com/ankane/pretender/pulls)
148
180
  - Write, clarify, or fix documentation
149
181
  - Suggest or add new features
182
+
183
+ To get started with development:
184
+
185
+ ```sh
186
+ git clone https://github.com/ankane/pretender.git
187
+ cd pretender
188
+ bundle install
189
+ bundle exec rake test
190
+ ```
@@ -1,3 +1,3 @@
1
1
  module Pretender
2
- VERSION = "0.3.2"
2
+ VERSION = "0.5.0"
3
3
  end
data/lib/pretender.rb CHANGED
@@ -1,6 +1,9 @@
1
- require "pretender/version"
1
+ # dependencies
2
2
  require "active_support"
3
3
 
4
+ # modules
5
+ require_relative "pretender/version"
6
+
4
7
  module Pretender
5
8
  class Error < StandardError; end
6
9
 
@@ -33,14 +36,14 @@ module Pretender
33
36
  define_method impersonated_method do
34
37
  impersonated_resource = instance_variable_get(impersonated_var) if instance_variable_defined?(impersonated_var)
35
38
 
36
- if !impersonated_resource && session[session_key]
39
+ if !impersonated_resource && request.session[session_key]
37
40
  # only fetch impersonation if user is logged in
38
41
  # this is a safety check (once per request) so
39
42
  # if a user logs out without session being destroyed
40
43
  # or stop_impersonating_user being called,
41
44
  # we can stop the impersonation
42
45
  if send(true_method)
43
- impersonated_resource = impersonate_with.call(session[session_key])
46
+ impersonated_resource = impersonate_with.call(request.session[session_key])
44
47
  instance_variable_set(impersonated_var, impersonated_resource) if impersonated_resource
45
48
  else
46
49
  # TODO better message
@@ -58,12 +61,12 @@ module Pretender
58
61
 
59
62
  instance_variable_set(impersonated_var, resource)
60
63
  # use to_s for Mongoid for BSON::ObjectId
61
- session[session_key] = resource.id.is_a?(Numeric) ? resource.id : resource.id.to_s
64
+ request.session[session_key] = resource.id.is_a?(Numeric) ? resource.id : resource.id.to_s
62
65
  end
63
66
 
64
67
  define_method stop_impersonating_method do
65
68
  remove_instance_variable(impersonated_var) if instance_variable_defined?(impersonated_var)
66
- session.delete(session_key)
69
+ request.session.delete(session_key)
67
70
  end
68
71
  end
69
72
  end
@@ -72,3 +75,6 @@ end
72
75
  ActiveSupport.on_load(:action_controller) do
73
76
  extend Pretender::Methods
74
77
  end
78
+
79
+ # ActiveSupport.on_load(:action_cable) runs too late with Unicorn
80
+ ActionCable::Connection::Base.extend(Pretender::Methods) if defined?(ActionCable)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pretender
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
- autorequire:
9
- bindir: exe
8
+ autorequire:
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-22 00:00:00.000000000 Z
11
+ date: 2023-07-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -16,77 +16,30 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '6.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: bundler
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: rake
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: minitest
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
- description:
70
- email:
71
- - andrew@chartkick.com
26
+ version: '6.1'
27
+ description:
28
+ email: andrew@ankane.org
72
29
  executables: []
73
30
  extensions: []
74
31
  extra_rdoc_files: []
75
32
  files:
76
- - ".gitignore"
77
33
  - CHANGELOG.md
78
- - Gemfile
79
34
  - LICENSE.txt
80
35
  - README.md
81
- - Rakefile
82
36
  - lib/pretender.rb
83
37
  - lib/pretender/version.rb
84
- - pretender.gemspec
85
38
  homepage: https://github.com/ankane/pretender
86
39
  licenses:
87
40
  - MIT
88
41
  metadata: {}
89
- post_install_message:
42
+ post_install_message:
90
43
  rdoc_options: []
91
44
  require_paths:
92
45
  - lib
@@ -94,16 +47,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
94
47
  requirements:
95
48
  - - ">="
96
49
  - !ruby/object:Gem::Version
97
- version: '0'
50
+ version: '3'
98
51
  required_rubygems_version: !ruby/object:Gem::Requirement
99
52
  requirements:
100
53
  - - ">="
101
54
  - !ruby/object:Gem::Version
102
55
  version: '0'
103
56
  requirements: []
104
- rubyforge_project:
105
- rubygems_version: 2.6.13
106
- signing_key:
57
+ rubygems_version: 3.4.10
58
+ signing_key:
107
59
  specification_version: 4
108
60
  summary: Log in as another user in Rails
109
61
  test_files: []
data/.gitignore DELETED
@@ -1,17 +0,0 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- lib/bundler/man
12
- pkg
13
- rdoc
14
- spec/reports
15
- test/tmp
16
- test/version_tmp
17
- tmp
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- # Specify your gem's dependencies in pretender.gemspec
4
- gemspec
data/Rakefile DELETED
@@ -1,8 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rake/testtask"
3
-
4
- task default: :test
5
- Rake::TestTask.new do |t|
6
- t.libs << "test"
7
- t.pattern = "test/**/*_test.rb"
8
- end
data/pretender.gemspec DELETED
@@ -1,27 +0,0 @@
1
- # coding: utf-8
2
- lib = File.expand_path("../lib", __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "pretender/version"
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "pretender"
8
- spec.version = Pretender::VERSION
9
- spec.authors = ["Andrew Kane"]
10
- spec.email = ["andrew@chartkick.com"]
11
- spec.summary = "Log in as another user in Rails"
12
- spec.homepage = "https://github.com/ankane/pretender"
13
- spec.license = "MIT"
14
-
15
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
- f.match(%r{^(test|spec|features)/})
17
- end
18
- spec.bindir = "exe"
19
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
- spec.require_paths = ["lib"]
21
-
22
- spec.add_dependency "actionpack"
23
-
24
- spec.add_development_dependency "bundler"
25
- spec.add_development_dependency "rake"
26
- spec.add_development_dependency "minitest"
27
- end