switch_user 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +27 -11
- data/app/controllers/switch_user_controller.rb +30 -2
- data/app/helpers/switch_user_helper.rb +13 -10
- data/lib/switch_user.rb +8 -0
- data/lib/switch_user/version.rb +1 -1
- metadata +4 -4
data/README.md
CHANGED
@@ -1,16 +1,12 @@
|
|
1
1
|
switch_user
|
2
2
|
===========
|
3
3
|
|
4
|
-
Inspired from hobo, switch_user provides a convenient way to switch current user that
|
5
|
-
|
6
|
-
switch_user promises that it only be activated in the development environment.
|
7
|
-
|
8
|
-
It supports only devise now, but it will support authlogic in the future.
|
4
|
+
Inspired from [hobo][0], switch_user provides a convenient way to switch current user that speeds up your development, you don't waste your time to logout, login and input login or passowrd any more.
|
9
5
|
|
10
6
|
Example
|
11
7
|
-------
|
12
8
|
|
13
|
-
Visit here: [http://switch-user-example.heroku.com/][
|
9
|
+
Visit here: [http://switch-user-example.heroku.com/][1], switch the current user in the select box.
|
14
10
|
|
15
11
|
Install
|
16
12
|
-------
|
@@ -37,19 +33,39 @@ Configuration
|
|
37
33
|
|
38
34
|
By default, you can switch between Guest and all users in users table.
|
39
35
|
|
40
|
-
But if you want to use different scope users in devise or you want to customize the users that can be switched,
|
36
|
+
But if you want to use different scope users in devise or you want to customize the users that can be switched, The following is the default configuration.
|
41
37
|
|
38
|
+
# config/initializers/switch_user.rb
|
42
39
|
SwitchUser.setup do |config|
|
43
|
-
# provider may be :devise or :authologic
|
40
|
+
# provider may be :devise or :authologic
|
44
41
|
config.provider = :devise
|
45
|
-
|
46
|
-
|
42
|
+
|
43
|
+
# avaliable_users is a hash,
|
44
|
+
# key is the model name of user (:user, :admin, or any name you use),
|
45
|
+
# value is a block that return the users that can be switched.
|
46
|
+
config.available_users = { :user => lambda { User.all } }
|
47
|
+
|
47
48
|
# what field should be displayed on select box
|
48
49
|
config.display_field = :email
|
50
|
+
|
51
|
+
# controller_guard is a block,
|
52
|
+
# if it returns true, the request will continue,
|
53
|
+
# else the request will be refused and returns "Permission Denied"
|
54
|
+
config.controller_guard = lambda { |current_user, request| Rails_env == "development" }
|
55
|
+
|
56
|
+
# view_guard is a block,
|
57
|
+
# if it returns true, the switch user select box will be shown,
|
58
|
+
# else the select box will not be shown
|
59
|
+
config.view_guard == lambda { |current_user, request| Rails.env == "development" }
|
60
|
+
|
61
|
+
# redirect_path is a block, it returns which page will be redirected
|
62
|
+
# after switching a user.
|
63
|
+
config.redirect_path = lambda { |request, params| '/' }
|
49
64
|
end
|
50
65
|
|
51
66
|
|
52
67
|
Copyright © 2010 Richard Huang (flyerhzm@gmail.com), released under the MIT license
|
53
68
|
|
54
|
-
[0]:
|
69
|
+
[0]: https://github.com/tablatom/hobo
|
70
|
+
[1]: http://switch-user-example.heroku.com/
|
55
71
|
|
@@ -4,12 +4,26 @@ class SwitchUserController < ApplicationController
|
|
4
4
|
|
5
5
|
def set_current_user
|
6
6
|
send("#{SwitchUser.provider}_handle", params)
|
7
|
-
redirect_to(request
|
7
|
+
redirect_to(SwitchUser.redirect_path.call(request, params))
|
8
8
|
end
|
9
9
|
|
10
10
|
private
|
11
11
|
def developer_modes_only
|
12
|
-
render :text => "Permission Denied", :status => 403 unless
|
12
|
+
render :text => "Permission Denied", :status => 403 unless available?
|
13
|
+
end
|
14
|
+
|
15
|
+
def available?
|
16
|
+
user = nil
|
17
|
+
if params[:scope_id].present?
|
18
|
+
scope, id = params[:scope_id].split('_')
|
19
|
+
SwitchUser.available_users.keys.each do |s|
|
20
|
+
if scope == s.to_s
|
21
|
+
user = scope.classify.constantize.find(id)
|
22
|
+
break
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
SwitchUser.controller_guard.call(user, request)
|
13
27
|
end
|
14
28
|
|
15
29
|
def devise_handle(params)
|
@@ -29,4 +43,18 @@ class SwitchUserController < ApplicationController
|
|
29
43
|
end
|
30
44
|
end
|
31
45
|
end
|
46
|
+
|
47
|
+
def authlogic_handle(params)
|
48
|
+
if params[:scope_id].blank?
|
49
|
+
current_user_session.destroy
|
50
|
+
else
|
51
|
+
scope, id = params[:scope_id].split('_')
|
52
|
+
SwitchUser.available_users.keys.each do |s|
|
53
|
+
if scope == s.to_s
|
54
|
+
user = scope.classify.constantize.find(id)
|
55
|
+
UserSession.create(user)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
32
60
|
end
|
@@ -1,14 +1,6 @@
|
|
1
1
|
module SwitchUserHelper
|
2
2
|
def switch_user_select
|
3
|
-
if
|
4
|
-
options = send("#{SwitchUser.provider}_select_options")
|
5
|
-
select_tag "switch_user_id", options.html_safe,
|
6
|
-
:onchange => "location.href = '/switch_user?scope_id=' + encodeURIComponent(this.options[this.selectedIndex].value)"
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
private
|
11
|
-
def devise_select_options
|
3
|
+
if available?
|
12
4
|
if current_user
|
13
5
|
options = "<option value=''>Guest</option>"
|
14
6
|
else
|
@@ -24,6 +16,17 @@ module SwitchUserHelper
|
|
24
16
|
end
|
25
17
|
end
|
26
18
|
end
|
27
|
-
options
|
19
|
+
select_tag "switch_user_id", options.html_safe,
|
20
|
+
:onchange => "location.href = '/switch_user?scope_id=' + encodeURIComponent(this.options[this.selectedIndex].value)"
|
28
21
|
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def available?
|
25
|
+
user = nil
|
26
|
+
SwitchUser.available_users.keys.each do |scope|
|
27
|
+
user = send("current_#{scope}")
|
28
|
+
break if user
|
29
|
+
end
|
30
|
+
SwitchUser.view_guard.call(user, request)
|
31
|
+
end
|
29
32
|
end
|
data/lib/switch_user.rb
CHANGED
@@ -16,6 +16,14 @@ module SwitchUser
|
|
16
16
|
mattr_accessor :display_field
|
17
17
|
self.display_field = :email
|
18
18
|
|
19
|
+
mattr_accessor :controller_guard
|
20
|
+
self.controller_guard = lambda { Rails.env == "development" }
|
21
|
+
mattr_accessor :view_guard
|
22
|
+
self.view_guard = lambda { Rails.evn == "development" }
|
23
|
+
|
24
|
+
mattr_accessor :redirect_path
|
25
|
+
self.redirect_path = lambda { |request| request.env["HTTP_REFERER"] ? :back : root_path }
|
26
|
+
|
19
27
|
def self.setup
|
20
28
|
yield self
|
21
29
|
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: 15
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 4
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.4.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-10 00:00:00 +08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|