switch_user 0.5.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Gemfile.lock +15 -0
- data/README.md +25 -11
- data/app/controllers/switch_user_controller.rb +29 -17
- data/app/helpers/switch_user_helper.rb +8 -6
- data/lib/switch_user.rb +8 -5
- data/lib/switch_user/version.rb +1 -1
- metadata +6 -5
data/.gitignore
CHANGED
data/Gemfile.lock
ADDED
data/README.md
CHANGED
@@ -39,11 +39,11 @@ or haml
|
|
39
39
|
|
40
40
|
If there are too many users (in production), the switch_user_select is not a good choice, you should call the switch user request by yourself.
|
41
41
|
|
42
|
-
<%= link_to user.login, "/switch_user?
|
43
|
-
<%= link_to admin.login, "/switch_user?
|
42
|
+
<%= link_to user.login, "/switch_user?scope_identifier=user_#{user.id}" %>
|
43
|
+
<%= link_to admin.login, "/switch_user?scope_identifier=admin_#{admin.id}" %>
|
44
44
|
|
45
|
-
= link_to user.login, "/switch_user?
|
46
|
-
= link_to admin.login, "/switch_user?
|
45
|
+
= link_to user.login, "/switch_user?scope_identifier=user_#{user.id}"
|
46
|
+
= link_to admin.login, "/switch_user?scope_identifier=admin_#{admin.id}"
|
47
47
|
|
48
48
|
If you use it in a Rails 2 project, you have to add a route manually.
|
49
49
|
|
@@ -56,28 +56,38 @@ Configuration
|
|
56
56
|
By default, you can switch between Guest and all users in users table, you don't need to do anything. The following is the default configuration.
|
57
57
|
|
58
58
|
SwitchUser.setup do |config|
|
59
|
-
# provider may be :devise or :
|
59
|
+
# provider may be :devise or :authlogic
|
60
60
|
config.provider = :devise
|
61
61
|
|
62
|
-
#
|
62
|
+
# available_users is a hash,
|
63
63
|
# key is the model name of user (:user, :admin, or any name you use),
|
64
64
|
# value is a block that return the users that can be switched.
|
65
65
|
config.available_users = { :user => lambda { User.all } }
|
66
66
|
|
67
|
-
#
|
68
|
-
|
67
|
+
# available_users_identifiers is a hash,
|
68
|
+
# keys in this hash should match a key in the available_users hash
|
69
|
+
# value is the name of the identifying column to find by,
|
70
|
+
# defaults to id
|
71
|
+
# this hash is to allow you to specify a different column to
|
72
|
+
# expose for instance a username on a User model instead of id
|
73
|
+
config.available_users_identifiers = { :user => :id }
|
74
|
+
|
75
|
+
# available_users_names is a hash,
|
76
|
+
# keys in this hash should match a key in the available_users hash
|
77
|
+
# value is the column name which will be displayed in select box
|
78
|
+
config.available_users_names = { :user => :email }
|
69
79
|
|
70
80
|
# controller_guard is a block,
|
71
81
|
# if it returns true, the request will continue,
|
72
82
|
# else the request will be refused and returns "Permission Denied"
|
73
83
|
# if you switch from "admin" to user, the current_user param is "admin"
|
74
|
-
config.controller_guard = lambda { |current_user, request|
|
84
|
+
config.controller_guard = lambda { |current_user, request| Rails.env.development? }
|
75
85
|
|
76
86
|
# view_guard is a block,
|
77
87
|
# if it returns true, the switch user select box will be shown,
|
78
88
|
# else the select box will not be shown
|
79
89
|
# if you switch from admin to "user", the current_user param is "user"
|
80
|
-
config.view_guard == lambda { |current_user, request| Rails.env
|
90
|
+
config.view_guard == lambda { |current_user, request| Rails.env.development? }
|
81
91
|
|
82
92
|
# redirect_path is a block, it returns which page will be redirected
|
83
93
|
# after switching a user.
|
@@ -89,10 +99,14 @@ If the default configuration can't meet your requirement, you can define your cu
|
|
89
99
|
If you want to switch both available users and available admins
|
90
100
|
|
91
101
|
config.available_users = { :user => lambda { User.available }, :admin => lambda { Admin.available } }
|
102
|
+
|
103
|
+
If you want to use name column as the user identifier
|
104
|
+
|
105
|
+
config.available_users_identifiers => { :user => :name }
|
92
106
|
|
93
107
|
If you want to display the login field in switch user select box
|
94
108
|
|
95
|
-
config.
|
109
|
+
config.available_users_names = { :user => :login }
|
96
110
|
|
97
111
|
If you only allow switching from admin to user in production environment
|
98
112
|
|
@@ -17,29 +17,22 @@ class SwitchUserController < ApplicationController
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def available?
|
20
|
-
|
21
|
-
|
22
|
-
scope, id = params[:scope_id].split('_')
|
23
|
-
SwitchUser.available_users.keys.each do |s|
|
24
|
-
if scope == s.to_s
|
25
|
-
user = scope.classify.constantize.find(id)
|
26
|
-
break
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
SwitchUser.controller_guard.call(user, request)
|
20
|
+
current_user = send("#{SwitchUser.provider}_current_user")
|
21
|
+
SwitchUser.controller_guard.call(current_user, request)
|
31
22
|
end
|
32
23
|
|
33
24
|
def devise_handle(params)
|
34
|
-
if params[:
|
25
|
+
if params[:scope_identifier].blank?
|
35
26
|
SwitchUser.available_users.keys.each do |s|
|
36
27
|
warden.logout(s)
|
37
28
|
end
|
38
29
|
else
|
39
|
-
|
30
|
+
params[:scope_identifier] =~ /^([^_]+)_(.*)$/
|
31
|
+
scope, identifier = $1, $2
|
32
|
+
|
40
33
|
SwitchUser.available_users.keys.each do |s|
|
41
34
|
if scope == s.to_s
|
42
|
-
user = scope
|
35
|
+
user = find_user(scope, s, identifier)
|
43
36
|
warden.set_user(user, :scope => scope)
|
44
37
|
else
|
45
38
|
warden.logout(s)
|
@@ -49,16 +42,35 @@ class SwitchUserController < ApplicationController
|
|
49
42
|
end
|
50
43
|
|
51
44
|
def authlogic_handle(params)
|
52
|
-
if params[:
|
45
|
+
if params[:scope_identifier].blank?
|
53
46
|
current_user_session.destroy
|
54
47
|
else
|
55
|
-
|
48
|
+
params[:scope_identifier] =~ /^([^_]+)_(.*)$/
|
49
|
+
scope, identifier = $1, $2
|
50
|
+
|
56
51
|
SwitchUser.available_users.keys.each do |s|
|
57
52
|
if scope == s.to_s
|
58
|
-
user = scope
|
53
|
+
user = find_user(scope, s, identifier)
|
59
54
|
UserSession.create(user)
|
60
55
|
end
|
61
56
|
end
|
62
57
|
end
|
63
58
|
end
|
59
|
+
|
60
|
+
def find_user(scope, identifier_scope, identifier)
|
61
|
+
identifier_column = SwitchUser.available_users_identifiers[identifier_scope] || :id
|
62
|
+
if identifier_column == :id
|
63
|
+
scope.classify.constantize.find(identifier)
|
64
|
+
else
|
65
|
+
scope.classify.constantize.send("find_by_#{identifier_column}!", identifier)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def devise_current_user
|
70
|
+
current_user
|
71
|
+
end
|
72
|
+
|
73
|
+
def authlogic_current_user
|
74
|
+
UserSession.find.try(:record)
|
75
|
+
end
|
64
76
|
end
|
@@ -7,20 +7,22 @@ module SwitchUserHelper
|
|
7
7
|
options = "<option selected='selected' value=''>Guest</option>"
|
8
8
|
end
|
9
9
|
SwitchUser.available_users.each do |scope, user_proc|
|
10
|
+
current = send("current_#{scope}")
|
11
|
+
identifier = SwitchUser.available_users_identifiers[scope]
|
12
|
+
name = SwitchUser.available_users_names[scope]
|
10
13
|
user_proc.call.each do |user|
|
11
|
-
current
|
12
|
-
|
13
|
-
options += "<option selected='selected' value='#{scope}_#{user.id}'>#{user.send(SwitchUser.display_field)}</option>"
|
14
|
+
if current and current.send(identifier) == user.send(identifier)
|
15
|
+
options += "<option selected='selected' value='#{scope}_#{user.send(identifier)}'>#{user.send(name)}</option>"
|
14
16
|
else
|
15
|
-
options += "<option value='#{scope}_#{user.
|
17
|
+
options += "<option value='#{scope}_#{user.send(identifier)}'>#{user.send(name)}</option>"
|
16
18
|
end
|
17
19
|
end
|
18
20
|
end
|
19
21
|
if options.respond_to?(:html_safe)
|
20
22
|
options = options.html_safe
|
21
23
|
end
|
22
|
-
select_tag "
|
23
|
-
:onchange => "location.href = '/switch_user?
|
24
|
+
select_tag "switch_user_identifier", options,
|
25
|
+
:onchange => "location.href = '/switch_user?scope_identifier=' + encodeURIComponent(this.options[this.selectedIndex].value)"
|
24
26
|
end
|
25
27
|
end
|
26
28
|
|
data/lib/switch_user.rb
CHANGED
@@ -21,17 +21,20 @@ module SwitchUser
|
|
21
21
|
mattr_accessor :available_users
|
22
22
|
self.available_users = { :user => lambda { User.all } }
|
23
23
|
|
24
|
-
mattr_accessor :
|
25
|
-
self.
|
24
|
+
mattr_accessor :available_users_identifiers
|
25
|
+
self.available_users_identifiers = { :user => :id }
|
26
|
+
|
27
|
+
mattr_accessor :available_users_names
|
28
|
+
self.available_users_names = { :user => :email }
|
26
29
|
|
27
30
|
mattr_accessor :controller_guard
|
28
|
-
self.controller_guard = lambda { |current_user, request| Rails.env
|
31
|
+
self.controller_guard = lambda { |current_user, request| Rails.env.development? }
|
29
32
|
mattr_accessor :view_guard
|
30
|
-
self.view_guard = lambda { |current_user, request| Rails.env
|
33
|
+
self.view_guard = lambda { |current_user, request| Rails.env.development? }
|
31
34
|
|
32
35
|
mattr_accessor :redirect_path
|
33
36
|
self.redirect_path = lambda { |request, params| request.env["HTTP_REFERER"] ? :back : root_path }
|
34
|
-
|
37
|
+
|
35
38
|
def self.setup
|
36
39
|
yield self
|
37
40
|
end
|
data/lib/switch_user/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: switch_user
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 7
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 6
|
9
|
+
- 0
|
10
|
+
version: 0.6.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Richard Huang
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-11-
|
18
|
+
date: 2010-11-18 00:00:00 +08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -46,6 +46,7 @@ extra_rdoc_files: []
|
|
46
46
|
files:
|
47
47
|
- .gitignore
|
48
48
|
- Gemfile
|
49
|
+
- Gemfile.lock
|
49
50
|
- LICENSE
|
50
51
|
- README.md
|
51
52
|
- Rakefile
|