pretender 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +4 -0
- data/LICENSE.txt +1 -1
- data/README.md +33 -7
- data/lib/pretender.rb +8 -4
- data/lib/pretender/version.rb +1 -1
- metadata +8 -13
- data/.gitignore +0 -17
- data/Gemfile +0 -4
- data/Rakefile +0 -8
- data/pretender.gemspec +0 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3deb0e2dc3a17b16d42141dd2552f14c5060f33c1190bc7507293ad7b1db5684
|
4
|
+
data.tar.gz: 9931e74f8ad318081ee5f2b77148871fe14934f274124e77c6b94a3d6558e135
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c56b632e14b220ae4e795eabc36a5b08b71ed78eb9a19f60c3f4a1b9fb4a3a376a152e3d135b441cbbefe0e1ebd5351ffa2413ce96a9b363006ec5f9896593b4
|
7
|
+
data.tar.gz: 3bf7ae9760fc675dbe3ba74caa02229f542a6eb840041d2089a2730bb182116c9ba50959891559e10a7e3818cbda0f11ac2ccc8d08028e642756952093b9db71
|
data/CHANGELOG.md
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
# Pretender
|
2
2
|
|
3
|
-
As an admin, there are times you want to see exactly what another user sees.
|
3
|
+
As an admin, there are times you want to see exactly what another user sees. Meet Pretender.
|
4
4
|
|
5
5
|
- Easily to 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](
|
9
|
+
:boom: [Rock on](https://www.youtube.com/watch?v=SBjQ9tuuTJQ)
|
10
10
|
|
11
|
-
Pretender is flexible and lightweight - less than
|
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
|
|
@@ -113,14 +113,40 @@ If you keep audit logs with a library like [Audited](https://github.com/collecti
|
|
113
113
|
Audited.current_user_method = :true_user
|
114
114
|
```
|
115
115
|
|
116
|
+
## Action Cable
|
117
|
+
|
118
|
+
And add this to your `ApplicationCable::Connection`:
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
module ApplicationCable
|
122
|
+
class Connection < ActionCable::Connection::Base
|
123
|
+
identified_by :current_user, :true_user
|
124
|
+
impersonates :user
|
125
|
+
|
126
|
+
def connect
|
127
|
+
self.current_user = find_verified_user
|
128
|
+
reject_unauthorized_connection unless current_user
|
129
|
+
end
|
130
|
+
|
131
|
+
private
|
132
|
+
|
133
|
+
def find_verified_user
|
134
|
+
env["warden"].user # for Devise
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
```
|
139
|
+
|
140
|
+
The `current_user` method now returns the impersonated user in channels.
|
141
|
+
|
116
142
|
## Configuration
|
117
143
|
|
118
|
-
Pretender is super flexible.
|
144
|
+
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
145
|
|
120
146
|
```ruby
|
121
147
|
impersonates :user,
|
122
148
|
method: :current_user,
|
123
|
-
with: ->
|
149
|
+
with: ->(id) { User.find_by(id: id) }
|
124
150
|
```
|
125
151
|
|
126
152
|
Mold it to fit your application.
|
@@ -128,7 +154,7 @@ Mold it to fit your application.
|
|
128
154
|
```ruby
|
129
155
|
impersonates :account,
|
130
156
|
method: :authenticated_account,
|
131
|
-
with: ->
|
157
|
+
with: ->(id) { EnterpriseAccount.find_by(id: id) }
|
132
158
|
```
|
133
159
|
|
134
160
|
This creates three methods:
|
data/lib/pretender.rb
CHANGED
@@ -33,14 +33,14 @@ module Pretender
|
|
33
33
|
define_method impersonated_method do
|
34
34
|
impersonated_resource = instance_variable_get(impersonated_var) if instance_variable_defined?(impersonated_var)
|
35
35
|
|
36
|
-
if !impersonated_resource && session[session_key]
|
36
|
+
if !impersonated_resource && request.session[session_key]
|
37
37
|
# only fetch impersonation if user is logged in
|
38
38
|
# this is a safety check (once per request) so
|
39
39
|
# if a user logs out without session being destroyed
|
40
40
|
# or stop_impersonating_user being called,
|
41
41
|
# we can stop the impersonation
|
42
42
|
if send(true_method)
|
43
|
-
impersonated_resource = impersonate_with.call(session[session_key])
|
43
|
+
impersonated_resource = impersonate_with.call(request.session[session_key])
|
44
44
|
instance_variable_set(impersonated_var, impersonated_resource) if impersonated_resource
|
45
45
|
else
|
46
46
|
# TODO better message
|
@@ -58,12 +58,12 @@ module Pretender
|
|
58
58
|
|
59
59
|
instance_variable_set(impersonated_var, resource)
|
60
60
|
# use to_s for Mongoid for BSON::ObjectId
|
61
|
-
session[session_key] = resource.id.is_a?(Numeric) ? resource.id : resource.id.to_s
|
61
|
+
request.session[session_key] = resource.id.is_a?(Numeric) ? resource.id : resource.id.to_s
|
62
62
|
end
|
63
63
|
|
64
64
|
define_method stop_impersonating_method do
|
65
65
|
remove_instance_variable(impersonated_var) if instance_variable_defined?(impersonated_var)
|
66
|
-
session.delete(session_key)
|
66
|
+
request.session.delete(session_key)
|
67
67
|
end
|
68
68
|
end
|
69
69
|
end
|
@@ -72,3 +72,7 @@ end
|
|
72
72
|
ActiveSupport.on_load(:action_controller) do
|
73
73
|
extend Pretender::Methods
|
74
74
|
end
|
75
|
+
|
76
|
+
ActiveSupport.on_load(:action_cable) do
|
77
|
+
ActionCable::Connection::Base.extend Pretender::Methods
|
78
|
+
end
|
data/lib/pretender/version.rb
CHANGED
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.
|
4
|
+
version: 0.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
8
|
autorequire:
|
9
|
-
bindir:
|
9
|
+
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-09-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: actionpack
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '4.2'
|
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: '
|
26
|
+
version: '4.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -67,21 +67,16 @@ dependencies:
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
description:
|
70
|
-
email:
|
71
|
-
- andrew@chartkick.com
|
70
|
+
email: andrew@chartkick.com
|
72
71
|
executables: []
|
73
72
|
extensions: []
|
74
73
|
extra_rdoc_files: []
|
75
74
|
files:
|
76
|
-
- ".gitignore"
|
77
75
|
- CHANGELOG.md
|
78
|
-
- Gemfile
|
79
76
|
- LICENSE.txt
|
80
77
|
- README.md
|
81
|
-
- Rakefile
|
82
78
|
- lib/pretender.rb
|
83
79
|
- lib/pretender/version.rb
|
84
|
-
- pretender.gemspec
|
85
80
|
homepage: https://github.com/ankane/pretender
|
86
81
|
licenses:
|
87
82
|
- MIT
|
@@ -94,7 +89,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
94
89
|
requirements:
|
95
90
|
- - ">="
|
96
91
|
- !ruby/object:Gem::Version
|
97
|
-
version: '
|
92
|
+
version: '2.2'
|
98
93
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
94
|
requirements:
|
100
95
|
- - ">="
|
@@ -102,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
102
97
|
version: '0'
|
103
98
|
requirements: []
|
104
99
|
rubyforge_project:
|
105
|
-
rubygems_version: 2.
|
100
|
+
rubygems_version: 2.7.7
|
106
101
|
signing_key:
|
107
102
|
specification_version: 4
|
108
103
|
summary: Log in as another user in Rails
|
data/.gitignore
DELETED
data/Gemfile
DELETED
data/Rakefile
DELETED
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
|