user_impersonate 0.7.0 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +9 -2
- data/app/controllers/user_impersonate/application_controller.rb +1 -1
- data/app/controllers/user_impersonate/impersonate_controller.rb +42 -7
- data/app/helpers/user_impersonate/application_helper.rb +7 -0
- data/lib/generators/user_impersonate/templates/config/initializers/user_impersonate.rb +3 -0
- data/lib/user_impersonate/engine.rb +5 -0
- data/lib/user_impersonate/version.rb +1 -1
- data/test/dummy/log/test.log +67 -0
- metadata +5 -5
data/README.md
CHANGED
@@ -131,10 +131,16 @@ The `app/views/user_impersonate/_header.html.haml` HAML partial for this header
|
|
131
131
|
|
132
132
|
By default, when you impersonate and when you stop impersonating a user you are redirected to the root url.
|
133
133
|
|
134
|
-
Configure alternate paths
|
134
|
+
Configure alternate paths in `config/initializers/user_impersonate.rb`, which is created by the generator above.
|
135
135
|
|
136
136
|
``` ruby
|
137
|
-
|
137
|
+
# config/initializers/user_impersonate.rb
|
138
|
+
module UserImpersonate
|
139
|
+
class Engine < Rails::Engine
|
140
|
+
config.redirect_on_impersonate = "/"
|
141
|
+
config.redirect_on_revert = "/impersonate"
|
142
|
+
end
|
143
|
+
end
|
138
144
|
```
|
139
145
|
|
140
146
|
### User model & lookup
|
@@ -144,6 +150,7 @@ By default, it assumes the User model is `User`, that you use `User.find(id)` to
|
|
144
150
|
You can fix this default behavior in `config/initializers/user_impersonate.rb`, which is created by the generator above.
|
145
151
|
|
146
152
|
``` ruby
|
153
|
+
# config/initializers/user_impersonate.rb
|
147
154
|
module UserImpersonate
|
148
155
|
class Engine < Rails::Engine
|
149
156
|
config.user_class = "User"
|
@@ -2,7 +2,7 @@ require_dependency "user_impersonate/application_controller"
|
|
2
2
|
|
3
3
|
module UserImpersonate
|
4
4
|
class ImpersonateController < ApplicationController
|
5
|
-
before_filter :
|
5
|
+
before_filter :authenticate_the_user
|
6
6
|
before_filter :current_user_must_be_staff!, except: ["destroy"]
|
7
7
|
|
8
8
|
# Display list of all users, except current (staff) user
|
@@ -58,6 +58,31 @@ module UserImpersonate
|
|
58
58
|
redirect_to '/'
|
59
59
|
end
|
60
60
|
|
61
|
+
# current_user changes from a staff user to
|
62
|
+
# +new_user+; current user stored in +session[:staff_user_id]+
|
63
|
+
def impersonate(new_user)
|
64
|
+
session[:staff_user_id] = current_user.id #
|
65
|
+
sign_in_user new_user
|
66
|
+
end
|
67
|
+
|
68
|
+
# revert the +current_user+ back to the staff user
|
69
|
+
# stored in +session[:staff_user_id]+
|
70
|
+
def revert_impersonate
|
71
|
+
return unless current_staff_user
|
72
|
+
sign_in_user current_staff_user
|
73
|
+
session[:staff_user_id] = nil
|
74
|
+
end
|
75
|
+
|
76
|
+
def sign_in_user(user)
|
77
|
+
method = config_or_default :sign_in_user_method, "sign_in"
|
78
|
+
self.send(method.to_sym, user)
|
79
|
+
end
|
80
|
+
|
81
|
+
def authenticate_the_user
|
82
|
+
method = config_or_default :authenticate_user_method, "authenticate_user!"
|
83
|
+
self.send(method.to_sym)
|
84
|
+
end
|
85
|
+
|
61
86
|
# Helper to load a User, using all the UserImpersonate config options
|
62
87
|
def find_user(id)
|
63
88
|
user_class.send(user_finder_method, id)
|
@@ -71,11 +96,11 @@ module UserImpersonate
|
|
71
96
|
end
|
72
97
|
|
73
98
|
def user_finder_method
|
74
|
-
(
|
99
|
+
(config_or_default :user_finder, "find").to_sym
|
75
100
|
end
|
76
101
|
|
77
102
|
def user_class_name
|
78
|
-
|
103
|
+
config_or_default :user_class, "User"
|
79
104
|
end
|
80
105
|
|
81
106
|
def user_class
|
@@ -87,21 +112,31 @@ module UserImpersonate
|
|
87
112
|
end
|
88
113
|
|
89
114
|
def user_id_column
|
90
|
-
|
115
|
+
config_or_default :user_id_column, "id"
|
91
116
|
end
|
92
117
|
|
93
118
|
def user_is_staff_method
|
94
|
-
|
119
|
+
config_or_default :user_is_staff_method, "staff?"
|
95
120
|
end
|
96
121
|
|
97
122
|
def redirect_on_impersonate(impersonated_user)
|
98
|
-
url =
|
123
|
+
url = config_or_default :redirect_on_impersonate, main_app.root_url
|
99
124
|
redirect_to url
|
100
125
|
end
|
101
126
|
|
102
127
|
def redirect_on_revert(impersonated_user = nil)
|
103
|
-
url =
|
128
|
+
url = config_or_default :redirect_on_revert, root_url
|
104
129
|
redirect_to url
|
105
130
|
end
|
131
|
+
|
132
|
+
# gets overridden config value for engine, else returns default
|
133
|
+
def config_or_default(attribute, default)
|
134
|
+
attribute = attribute.to_sym
|
135
|
+
if UserImpersonate::Engine.config.respond_to?(attribute)
|
136
|
+
UserImpersonate::Engine.config.send(attribute)
|
137
|
+
else
|
138
|
+
default
|
139
|
+
end
|
140
|
+
end
|
106
141
|
end
|
107
142
|
end
|
@@ -1,4 +1,11 @@
|
|
1
1
|
module UserImpersonate
|
2
2
|
module ApplicationHelper
|
3
|
+
def current_staff_user
|
4
|
+
return unless session[:staff_user_id]
|
5
|
+
user_finder_method = (UserImpersonate::Engine.config.user_finder || "find").to_sym
|
6
|
+
user_class_name = UserImpersonate::Engine.config.user_class || "User"
|
7
|
+
user_class = user_class_name.constantize
|
8
|
+
@staff_user ||= user_class.send(user_finder_method, session[:staff_user_id])
|
9
|
+
end
|
3
10
|
end
|
4
11
|
end
|
@@ -7,5 +7,8 @@ module UserImpersonate
|
|
7
7
|
|
8
8
|
config.redirect_on_impersonate = "/"
|
9
9
|
config.redirect_on_revert = "/impersonate"
|
10
|
+
|
11
|
+
config.authenticate_user_method = "authenticate_user!" # protect impersonation controller
|
12
|
+
config.sign_in_user_method = "sign_in" # sign_in(user)
|
10
13
|
end
|
11
14
|
end
|
@@ -8,5 +8,10 @@ module UserImpersonate
|
|
8
8
|
Devise.include_helpers(UserImpersonate::DeviseHelpers)
|
9
9
|
end
|
10
10
|
end
|
11
|
+
|
12
|
+
config.to_prepare do
|
13
|
+
::ApplicationController.helper(UserImpersonate::ApplicationHelper)
|
14
|
+
::ApplicationController.send(:include, UserImpersonate::ApplicationHelper)
|
15
|
+
end
|
11
16
|
end
|
12
17
|
end
|
data/test/dummy/log/test.log
CHANGED
@@ -10078,3 +10078,70 @@ Processing by HomeController#index as HTML
|
|
10078
10078
|
Rendered home/index.html.erb within layouts/application (1.1ms)
|
10079
10079
|
Completed 200 OK in 2ms (Views: 2.0ms | ActiveRecord: 0.1ms)
|
10080
10080
|
[1m[36m (1.8ms)[0m [1mrollback transaction[0m
|
10081
|
+
Connecting to database specified by database.yml
|
10082
|
+
[1m[36m (10.2ms)[0m [1mbegin transaction[0m
|
10083
|
+
[1m[35m (0.1ms)[0m SAVEPOINT active_record_1
|
10084
|
+
[1m[36mFixture Delete (0.4ms)[0m [1mDELETE FROM "users"[0m
|
10085
|
+
[1m[35mFixture Insert (0.7ms)[0m INSERT INTO "users" ("id", "name", "email", "staff", "encrypted_password", "created_at", "updated_at") VALUES (1, 'Dr Nic Williams', 'drnicwilliams@gmail.com', 't', '$2a$10$HV6E2Hgk2z6hqow76r6IL.5gcnqCSqJYuOuOT.nbqdPffBLvwglzK', '2012-09-19 23:09:52', '2012-09-19 23:09:52')
|
10086
|
+
[1m[36mFixture Insert (0.1ms)[0m [1mINSERT INTO "users" ("id", "name", "email", "encrypted_password", "created_at", "updated_at") VALUES (2, 'Normal User', 'normaluser@gmail.com', '$2a$10$HV6E2Hgk2z6hqow76r6IL.5gcnqCSqJYuOuOT.nbqdPffBLvwglzK', '2012-09-19 23:09:52', '2012-09-19 23:09:52')[0m
|
10087
|
+
[1m[35m (0.0ms)[0m RELEASE SAVEPOINT active_record_1
|
10088
|
+
[1m[36mUser Load (0.2ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."name" = 'Normal User' LIMIT 1[0m
|
10089
|
+
|
10090
|
+
|
10091
|
+
Started GET "/users/sign_in" for 127.0.0.1 at 2012-09-19 16:09:53 -0700
|
10092
|
+
Processing by Devise::SessionsController#new as HTML
|
10093
|
+
Rendered /Users/drnic/.rvm/gems/ruby-1.9.3-p194@global/gems/devise-2.1.2/app/views/devise/shared/_links.erb (1.6ms)
|
10094
|
+
Rendered /Users/drnic/.rvm/gems/ruby-1.9.3-p194@global/gems/devise-2.1.2/app/views/devise/sessions/new.html.erb within layouts/application (22.0ms)
|
10095
|
+
Completed 200 OK in 37ms (Views: 36.4ms | ActiveRecord: 0.0ms)
|
10096
|
+
|
10097
|
+
|
10098
|
+
Started POST "/users/sign_in" for 127.0.0.1 at 2012-09-19 16:09:53 -0700
|
10099
|
+
Processing by Devise::SessionsController#create as HTML
|
10100
|
+
Parameters: {"utf8"=>"✓", "user"=>{"email"=>"normaluser@gmail.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
|
10101
|
+
[1m[35mUser Load (0.1ms)[0m SELECT "users".* FROM "users" WHERE "users"."email" = 'normaluser@gmail.com' LIMIT 1
|
10102
|
+
[1m[36m (0.1ms)[0m [1mSAVEPOINT active_record_1[0m
|
10103
|
+
[1m[35m (0.4ms)[0m UPDATE "users" SET "last_sign_in_at" = '2012-09-19 23:09:53.322713', "current_sign_in_at" = '2012-09-19 23:09:53.322713', "last_sign_in_ip" = '127.0.0.1', "current_sign_in_ip" = '127.0.0.1', "sign_in_count" = 1, "updated_at" = '2012-09-19 23:09:53.323719' WHERE "users"."id" = 2
|
10104
|
+
[1m[36m (0.0ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
|
10105
|
+
Redirected to http://www.example.com/
|
10106
|
+
Completed 302 Found in 104ms (ActiveRecord: 0.0ms)
|
10107
|
+
|
10108
|
+
|
10109
|
+
Started GET "/" for 127.0.0.1 at 2012-09-19 16:09:53 -0700
|
10110
|
+
Processing by HomeController#index as HTML
|
10111
|
+
[1m[35mUser Load (0.2ms)[0m SELECT "users".* FROM "users" WHERE "users"."id" = 2 LIMIT 1
|
10112
|
+
Rendered home/index.html.erb within layouts/application (2.0ms)
|
10113
|
+
Completed 200 OK in 6ms (Views: 5.1ms | ActiveRecord: 0.2ms)
|
10114
|
+
[1m[36m (0.4ms)[0m [1mrollback transaction[0m
|
10115
|
+
[1m[35m (0.1ms)[0m begin transaction
|
10116
|
+
[1m[36m (0.1ms)[0m [1mSAVEPOINT active_record_1[0m
|
10117
|
+
[1m[35mFixture Delete (0.2ms)[0m DELETE FROM "users"
|
10118
|
+
[1m[36mFixture Insert (0.1ms)[0m [1mINSERT INTO "users" ("id", "name", "email", "staff", "encrypted_password", "created_at", "updated_at") VALUES (1, 'Dr Nic Williams', 'drnicwilliams@gmail.com', 't', '$2a$10$HV6E2Hgk2z6hqow76r6IL.5gcnqCSqJYuOuOT.nbqdPffBLvwglzK', '2012-09-19 23:09:53', '2012-09-19 23:09:53')[0m
|
10119
|
+
[1m[35mFixture Insert (0.1ms)[0m INSERT INTO "users" ("id", "name", "email", "encrypted_password", "created_at", "updated_at") VALUES (2, 'Normal User', 'normaluser@gmail.com', '$2a$10$HV6E2Hgk2z6hqow76r6IL.5gcnqCSqJYuOuOT.nbqdPffBLvwglzK', '2012-09-19 23:09:53', '2012-09-19 23:09:53')
|
10120
|
+
[1m[36m (0.0ms)[0m [1mRELEASE SAVEPOINT active_record_1[0m
|
10121
|
+
[1m[35mUser Load (0.1ms)[0m SELECT "users".* FROM "users" WHERE "users"."name" = 'Dr Nic Williams' LIMIT 1
|
10122
|
+
|
10123
|
+
|
10124
|
+
Started GET "/users/sign_in" for 127.0.0.1 at 2012-09-19 16:09:53 -0700
|
10125
|
+
Processing by Devise::SessionsController#new as HTML
|
10126
|
+
Rendered /Users/drnic/.rvm/gems/ruby-1.9.3-p194@global/gems/devise-2.1.2/app/views/devise/shared/_links.erb (0.4ms)
|
10127
|
+
Rendered /Users/drnic/.rvm/gems/ruby-1.9.3-p194@global/gems/devise-2.1.2/app/views/devise/sessions/new.html.erb within layouts/application (2.8ms)
|
10128
|
+
Completed 200 OK in 5ms (Views: 4.1ms | ActiveRecord: 0.0ms)
|
10129
|
+
|
10130
|
+
|
10131
|
+
Started POST "/users/sign_in" for 127.0.0.1 at 2012-09-19 16:09:53 -0700
|
10132
|
+
Processing by Devise::SessionsController#create as HTML
|
10133
|
+
Parameters: {"utf8"=>"✓", "user"=>{"email"=>"drnicwilliams@gmail.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
|
10134
|
+
[1m[36mUser Load (0.2ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."email" = 'drnicwilliams@gmail.com' LIMIT 1[0m
|
10135
|
+
[1m[35m (0.1ms)[0m SAVEPOINT active_record_1
|
10136
|
+
[1m[36m (0.3ms)[0m [1mUPDATE "users" SET "last_sign_in_at" = '2012-09-19 23:09:53.497475', "current_sign_in_at" = '2012-09-19 23:09:53.497475', "last_sign_in_ip" = '127.0.0.1', "current_sign_in_ip" = '127.0.0.1', "sign_in_count" = 1, "updated_at" = '2012-09-19 23:09:53.498065' WHERE "users"."id" = 1[0m
|
10137
|
+
[1m[35m (0.0ms)[0m RELEASE SAVEPOINT active_record_1
|
10138
|
+
Redirected to http://www.example.com/
|
10139
|
+
Completed 302 Found in 91ms (ActiveRecord: 0.0ms)
|
10140
|
+
|
10141
|
+
|
10142
|
+
Started GET "/" for 127.0.0.1 at 2012-09-19 16:09:53 -0700
|
10143
|
+
Processing by HomeController#index as HTML
|
10144
|
+
[1m[36mUser Load (0.1ms)[0m [1mSELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1[0m
|
10145
|
+
Rendered home/index.html.erb within layouts/application (1.5ms)
|
10146
|
+
Completed 200 OK in 3ms (Views: 2.6ms | ActiveRecord: 0.1ms)
|
10147
|
+
[1m[35m (1.9ms)[0m rollback transaction
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: user_impersonate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
requirements:
|
20
20
|
- - ~>
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: 3.2.
|
22
|
+
version: 3.2.0
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -27,7 +27,7 @@ dependencies:
|
|
27
27
|
requirements:
|
28
28
|
- - ~>
|
29
29
|
- !ruby/object:Gem::Version
|
30
|
-
version: 3.2.
|
30
|
+
version: 3.2.0
|
31
31
|
- !ruby/object:Gem::Dependency
|
32
32
|
name: sqlite3
|
33
33
|
requirement: !ruby/object:Gem::Requirement
|
@@ -279,7 +279,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
279
279
|
version: '0'
|
280
280
|
segments:
|
281
281
|
- 0
|
282
|
-
hash:
|
282
|
+
hash: 36552099061295841
|
283
283
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
284
284
|
none: false
|
285
285
|
requirements:
|
@@ -288,7 +288,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
288
288
|
version: '0'
|
289
289
|
segments:
|
290
290
|
- 0
|
291
|
-
hash:
|
291
|
+
hash: 36552099061295841
|
292
292
|
requirements: []
|
293
293
|
rubyforge_project:
|
294
294
|
rubygems_version: 1.8.24
|