pretender 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +23 -28
- data/lib/pretender.rb +8 -2
- data/lib/pretender/version.rb +1 -1
- metadata +12 -24
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 912488b31e2aedf08cf4370da9cd18f5d9b444e8
|
4
|
+
data.tar.gz: 0277513a5ebf12372667de4503cb6fd1a36c4a13
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 563a6e9b4a66732ae8830f2250481029f9a738841efafa457b658952fc1740616dd6a9835534e26d3ba4781083db1881b4c7c40b5a57748557c9923ab5cdcc61
|
7
|
+
data.tar.gz: 8524dae4aef68116d865d8134972b1862e4967a2a0426ef2efdaee71fd2f3e98a098fc9e9aee1029a7fba248966ae0d00356cb57b80ff2c2b86621d3a1922470
|
data/README.md
CHANGED
@@ -11,13 +11,13 @@ As an admin, there are times you want to see exactly what another user sees or t
|
|
11
11
|
Pretender is also flexible and lightweight - less than 40 lines of code :-)
|
12
12
|
|
13
13
|
Pretender works with Rails 2.3+ and almost any authentication system.
|
14
|
-
(devise, authlogic, sorcery, and many more - it
|
14
|
+
(devise, authlogic, sorcery, and many more - it’s agnostic)
|
15
15
|
|
16
16
|
[Battle-tested at Instacart](https://www.instacart.com)
|
17
17
|
|
18
18
|
## Get started
|
19
19
|
|
20
|
-
Add this line to your application
|
20
|
+
Add this line to your application’s Gemfile:
|
21
21
|
|
22
22
|
```ruby
|
23
23
|
# Gemfile
|
@@ -57,13 +57,13 @@ current_user
|
|
57
57
|
|
58
58
|
**Note:** the name of this method is configurable (details at the end)
|
59
59
|
|
60
|
-
Now we need to
|
60
|
+
Now we need to set up a way to login as another user. **Pretender makes no assumptions about how you want to do this**. I like to add this to my admin dashboard.
|
61
61
|
|
62
62
|
### Sample Implementation
|
63
63
|
|
64
64
|
```ruby
|
65
65
|
class Admin::UsersController < ApplicationController
|
66
|
-
before_filter :require_admin
|
66
|
+
before_filter :require_admin
|
67
67
|
|
68
68
|
def impersonate
|
69
69
|
user = User.find(params[:id])
|
@@ -71,40 +71,26 @@ class Admin::UsersController < ApplicationController
|
|
71
71
|
redirect_to root_path
|
72
72
|
end
|
73
73
|
|
74
|
-
# do not require admin for this method if access control
|
75
|
-
# is performed on the current_user instead of true_user
|
76
74
|
def stop_impersonating
|
77
75
|
stop_impersonating_user
|
78
|
-
redirect_to
|
76
|
+
redirect_to root_path
|
79
77
|
end
|
80
|
-
end
|
81
|
-
```
|
82
78
|
|
83
|
-
### Very Important!
|
84
|
-
|
85
|
-
Be sure to call `stop_impersonating_user` before the current user logs out.
|
86
|
-
|
87
|
-
```ruby
|
88
|
-
class SessionsController < ActionController::Base
|
89
|
-
def logout
|
90
|
-
# it's safe to call this regardless of whether the user is being impersonated
|
91
|
-
stop_impersonating_user
|
92
|
-
# now, log out the user
|
93
|
-
# ...
|
94
|
-
end
|
95
79
|
end
|
96
80
|
```
|
97
81
|
|
98
|
-
|
82
|
+
### Show Admins
|
83
|
+
|
84
|
+
You may want to make it obvious to an admin when he / she is signed in as another user. I like to add this to the application layout.
|
99
85
|
|
100
|
-
|
86
|
+
#### Haml / Slim
|
101
87
|
|
102
88
|
```haml
|
103
|
-
- # app/views/layouts/application.haml
|
89
|
+
- # app/views/layouts/application.html.haml
|
104
90
|
- if current_user != true_user
|
105
91
|
.alert
|
106
|
-
You (#{true_user.name}) are
|
107
|
-
= link_to "Back to admin",
|
92
|
+
You (#{true_user.name}) are signed in as #{current_user.name}
|
93
|
+
= link_to "Back to admin", stop_impersonating_path
|
108
94
|
```
|
109
95
|
|
110
96
|
### Audits
|
@@ -117,7 +103,7 @@ Audited.current_user_method = :true_user
|
|
117
103
|
|
118
104
|
### Configuration
|
119
105
|
|
120
|
-
Pretender is super flexible. You can change the names of methods and even impersonate multiple roles at the same time. Here
|
106
|
+
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.
|
121
107
|
|
122
108
|
```ruby
|
123
109
|
# app/controllers/application_controller.rb
|
@@ -145,4 +131,13 @@ stop_impersonating_account
|
|
145
131
|
|
146
132
|
Also, authenticated_account is overridden with `EnterpriseAccount.where(:id => id).first`
|
147
133
|
|
148
|
-
|
134
|
+
## Contributing
|
135
|
+
|
136
|
+
Everyone is encouraged to help improve this project. Here are a few ways you can help:
|
137
|
+
|
138
|
+
- [Report bugs](https://github.com/ankane/pretender/issues)
|
139
|
+
- Fix bugs and [submit pull requests](https://github.com/ankane/pretender/pulls)
|
140
|
+
- Write, clarify, or fix documentation
|
141
|
+
- Suggest or add new features
|
142
|
+
|
143
|
+
That’s all folks!
|
data/lib/pretender.rb
CHANGED
@@ -10,7 +10,13 @@ module Pretender
|
|
10
10
|
impersonated_var = :"@impersonated_#{scope}"
|
11
11
|
|
12
12
|
# define methods
|
13
|
-
|
13
|
+
if respond_to?(impersonated_method)
|
14
|
+
alias_method true_method, impersonated_method
|
15
|
+
else
|
16
|
+
define_method true_method do
|
17
|
+
ActionController::Base.instance_method(impersonated_method).bind(self).call
|
18
|
+
end
|
19
|
+
end
|
14
20
|
helper_method true_method
|
15
21
|
|
16
22
|
define_method impersonated_method do
|
@@ -18,7 +24,7 @@ module Pretender
|
|
18
24
|
# only fetch impersonation if user is logged in and impersonation_id exists
|
19
25
|
true_resource = send(true_method)
|
20
26
|
if session[session_key] and !true_resource
|
21
|
-
|
27
|
+
session[session_key] = nil
|
22
28
|
end
|
23
29
|
value = (session[session_key] && impersonate_with.call(session[session_key])) || true_resource
|
24
30
|
instance_variable_set(impersonated_var, value) if value
|
data/lib/pretender/version.rb
CHANGED
metadata
CHANGED
@@ -1,46 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pretender
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Andrew Kane
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-06-24 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: bundler
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- - ~>
|
17
|
+
- - "~>"
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: '1.3'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- - ~>
|
24
|
+
- - "~>"
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: '1.3'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rake
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - ">="
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - ">="
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '0'
|
46
41
|
description: Simple, powerful user impersonation for Rails
|
@@ -50,7 +45,7 @@ executables: []
|
|
50
45
|
extensions: []
|
51
46
|
extra_rdoc_files: []
|
52
47
|
files:
|
53
|
-
- .gitignore
|
48
|
+
- ".gitignore"
|
54
49
|
- Gemfile
|
55
50
|
- LICENSE.txt
|
56
51
|
- README.md
|
@@ -61,33 +56,26 @@ files:
|
|
61
56
|
homepage: http://ankane.github.com/pretender/
|
62
57
|
licenses:
|
63
58
|
- MIT
|
59
|
+
metadata: {}
|
64
60
|
post_install_message:
|
65
61
|
rdoc_options: []
|
66
62
|
require_paths:
|
67
63
|
- lib
|
68
64
|
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
-
none: false
|
70
65
|
requirements:
|
71
|
-
- -
|
66
|
+
- - ">="
|
72
67
|
- !ruby/object:Gem::Version
|
73
68
|
version: '0'
|
74
|
-
segments:
|
75
|
-
- 0
|
76
|
-
hash: 1172062548524424326
|
77
69
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
-
none: false
|
79
70
|
requirements:
|
80
|
-
- -
|
71
|
+
- - ">="
|
81
72
|
- !ruby/object:Gem::Version
|
82
73
|
version: '0'
|
83
|
-
segments:
|
84
|
-
- 0
|
85
|
-
hash: 1172062548524424326
|
86
74
|
requirements: []
|
87
75
|
rubyforge_project:
|
88
|
-
rubygems_version:
|
76
|
+
rubygems_version: 2.2.2
|
89
77
|
signing_key:
|
90
|
-
specification_version:
|
78
|
+
specification_version: 4
|
91
79
|
summary: Easy to switch back and forth between roles, minimal code changes, and plays
|
92
80
|
nicely with auditing tools
|
93
81
|
test_files: []
|