switch_user 0.8.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +1 -0
- data/README.md +15 -13
- data/app/controllers/switch_user_controller.rb +20 -94
- data/app/helpers/switch_user_helper.rb +38 -27
- data/lib/switch_user/provider/authlogic.rb +26 -0
- data/lib/switch_user/provider/base.rb +26 -0
- data/lib/switch_user/provider/devise.rb +24 -0
- data/lib/switch_user/provider/restful_authentication.rb +23 -0
- data/lib/switch_user/provider/sorcery.rb +23 -0
- data/lib/switch_user/provider.rb +8 -0
- data/lib/switch_user/rails.rb +17 -0
- data/lib/switch_user/user_loader.rb +35 -0
- data/lib/switch_user/version.rb +1 -1
- data/lib/switch_user.rb +30 -28
- data/spec/provider/authlogic_spec.rb +37 -0
- data/spec/provider/devise_spec.rb +86 -0
- data/spec/provider/restful_authrentication_spec.rb +17 -0
- data/spec/provider/sorcery_spec.rb +23 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/support/provider.rb +27 -0
- data/spec/switch_user/user_loader_spec.rb +36 -0
- data/spec/switch_user_spec.rb +26 -0
- data/switch_user.gemspec +3 -1
- metadata +60 -5
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/README.md
CHANGED
@@ -1,10 +1,8 @@
|
|
1
|
-
switch_user
|
2
|
-
===========
|
1
|
+
# switch_user
|
3
2
|
|
4
3
|
Inspired from [hobo][0], switch_user provides a convenient way to switch current user that speeds up your development and reproduce user specified error on production.
|
5
4
|
|
6
|
-
Use Case
|
7
|
-
--------
|
5
|
+
## Use Case
|
8
6
|
|
9
7
|
switch_user is very useful in such use cases
|
10
8
|
|
@@ -12,20 +10,17 @@ switch_user is very useful in such use cases
|
|
12
10
|
|
13
11
|
2. reproduce the user specified error on production. Sometimes the error is only raised for specified user, which is difficult to reproduce for developers, switch_user can help you reproduce it by login as that user.
|
14
12
|
|
15
|
-
Example
|
16
|
-
-------
|
13
|
+
## Example
|
17
14
|
|
18
15
|
Visit here: [http://switch-user-example.heroku.com/][1], switch the current user in the select box.
|
19
16
|
|
20
|
-
Install
|
21
|
-
-------
|
17
|
+
## Install
|
22
18
|
|
23
19
|
Add in Gemfile.
|
24
20
|
|
25
21
|
gem "switch_user"
|
26
22
|
|
27
|
-
Usage
|
28
|
-
-----
|
23
|
+
## Usage
|
29
24
|
|
30
25
|
Add following code into your layout page.
|
31
26
|
|
@@ -50,8 +45,14 @@ If you use it in a Rails 2 project, you have to add a route manually.
|
|
50
45
|
# config/routes.rb
|
51
46
|
map.switch_user '/switch_user', :controller => 'switch_user', :action => 'set_current_user'
|
52
47
|
|
53
|
-
|
54
|
-
|
48
|
+
If you have a wildcard route in your Rails 3 project, add a route before the wildcard route.
|
49
|
+
|
50
|
+
# config/routes.rb
|
51
|
+
match 'switch_user' => 'switch_user#set_current_user'
|
52
|
+
# wildcard route that will match anything
|
53
|
+
match ':id' => 'pages#show'
|
54
|
+
|
55
|
+
## Configuration
|
55
56
|
|
56
57
|
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
58
|
|
@@ -120,8 +121,9 @@ If you want to redirect user to "/dashboard" page
|
|
120
121
|
|
121
122
|
config.redirect_path = lambda { |request, params| "/dashboard" }
|
122
123
|
|
124
|
+
## Credit
|
123
125
|
|
124
|
-
Copyright © 2010 Richard Huang (flyerhzm@gmail.com), released under the MIT license
|
126
|
+
Copyright © 2010 - 2012 Richard Huang (flyerhzm@gmail.com), released under the MIT license
|
125
127
|
|
126
128
|
[0]: https://github.com/tablatom/hobo
|
127
129
|
[1]: http://switch-user-example.heroku.com/
|
@@ -7,108 +7,34 @@ class SwitchUserController < ApplicationController
|
|
7
7
|
before_filter :developer_modes_only
|
8
8
|
|
9
9
|
def set_current_user
|
10
|
-
|
10
|
+
handle_request(params)
|
11
|
+
|
11
12
|
redirect_to(SwitchUser.redirect_path.call(request, params))
|
12
13
|
end
|
13
14
|
|
14
15
|
private
|
15
|
-
def developer_modes_only
|
16
|
-
render :text => "Permission Denied", :status => 403 unless available?
|
17
|
-
end
|
18
|
-
|
19
|
-
def available?
|
20
|
-
current_user = send("#{SwitchUser.provider}_current_user")
|
21
|
-
SwitchUser.controller_guard.call(current_user, request)
|
22
|
-
end
|
23
|
-
|
24
|
-
def devise_handle(params)
|
25
|
-
if params[:scope_identifier].blank?
|
26
|
-
SwitchUser.available_users.keys.each do |s|
|
27
|
-
warden.logout(s)
|
28
|
-
end
|
29
|
-
else
|
30
|
-
params[:scope_identifier] =~ /^([^_]+)_(.*)$/
|
31
|
-
scope, identifier = $1, $2
|
32
|
-
|
33
|
-
SwitchUser.available_users.keys.each do |s|
|
34
|
-
if scope == s.to_s
|
35
|
-
user = find_user(scope, s, identifier)
|
36
|
-
warden.set_user(user, :scope => scope)
|
37
|
-
else
|
38
|
-
warden.logout(s)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def authlogic_handle(params)
|
45
|
-
if params[:scope_identifier].blank?
|
46
|
-
current_user_session.destroy
|
47
|
-
else
|
48
|
-
params[:scope_identifier] =~ /^([^_]+)_(.*)$/
|
49
|
-
scope, identifier = $1, $2
|
50
|
-
|
51
|
-
SwitchUser.available_users.keys.each do |s|
|
52
|
-
if scope == s.to_s
|
53
|
-
user = find_user(scope, s, identifier)
|
54
|
-
UserSession.create(user)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def restful_authentication_handle(params)
|
61
|
-
if params[:scope_identifier].blank?
|
62
|
-
logout_killing_session!
|
63
|
-
else
|
64
|
-
params[:scope_identifier] =~ /^([^_]+)_(.*)$/
|
65
|
-
scope, identifier = $1, $2
|
66
|
-
|
67
|
-
SwitchUser.available_users.keys.each do |s|
|
68
|
-
if scope == s.to_s && user = find_user(scope, s, identifier)
|
69
|
-
self.current_user = user
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
def sorcery_handle(params)
|
76
|
-
if params[:scope_identifier].blank?
|
77
|
-
self.logout
|
78
|
-
else
|
79
|
-
params[:scope_identifier] =~ /^([^_]+)_(.*)$/
|
80
|
-
scope, identifier = $1, $2
|
81
|
-
|
82
|
-
SwitchUser.available_users.keys.each do |s|
|
83
|
-
if scope == s.to_s && user = find_user(scope, s, identifier)
|
84
|
-
self.auto_login(user)
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
89
16
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
scope.classify.constantize.find(identifier)
|
94
|
-
else
|
95
|
-
scope.classify.constantize.send("find_by_#{identifier_column}!", identifier)
|
96
|
-
end
|
97
|
-
end
|
17
|
+
def developer_modes_only
|
18
|
+
render :text => "Permission Denied", :status => 403 unless available?
|
19
|
+
end
|
98
20
|
|
99
|
-
|
100
|
-
|
101
|
-
|
21
|
+
def available?
|
22
|
+
SwitchUser.controller_guard.call(provider.current_user, request)
|
23
|
+
end
|
102
24
|
|
103
|
-
|
104
|
-
|
105
|
-
|
25
|
+
def handle_request(params)
|
26
|
+
if params[:scope_identifier].blank?
|
27
|
+
provider.logout_all
|
28
|
+
else
|
29
|
+
params[:scope_identifier] =~ /^([^_]+)_(.*)$/
|
30
|
+
scope, identifier = $1, $2
|
106
31
|
|
107
|
-
|
108
|
-
|
32
|
+
user = SwitchUser::UserLoader.new(scope, identifier).load
|
33
|
+
provider.login_exclusive(user, :scope => scope)
|
109
34
|
end
|
35
|
+
end
|
110
36
|
|
111
|
-
|
112
|
-
|
113
|
-
|
37
|
+
def provider
|
38
|
+
SwitchUser.provider_class.new(self)
|
39
|
+
end
|
114
40
|
end
|
@@ -1,37 +1,48 @@
|
|
1
1
|
module SwitchUserHelper
|
2
2
|
def switch_user_select
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
SwitchUser.
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
if options.respond_to?(:html_safe)
|
22
|
-
options = options.html_safe
|
3
|
+
return unless available?
|
4
|
+
options = ""
|
5
|
+
|
6
|
+
options += content_tag(:option, "Guest", :value => "", :selected => !current_user)
|
7
|
+
SwitchUser.available_users.each do |scope, user_proc|
|
8
|
+
current_user = provider.current_user(scope)
|
9
|
+
id_name = SwitchUser.available_users_identifiers[scope]
|
10
|
+
name = SwitchUser.available_users_names[scope]
|
11
|
+
|
12
|
+
user_proc.call.each do |user|
|
13
|
+
user_match = (user == current_user)
|
14
|
+
options += content_tag(:option,
|
15
|
+
tag_label(user, name),
|
16
|
+
:value => tag_value(user, id_name, scope),
|
17
|
+
:selected => user_match)
|
23
18
|
end
|
24
|
-
select_tag "switch_user_identifier", options,
|
25
|
-
:onchange => "location.href = '/switch_user?scope_identifier=' + encodeURIComponent(this.options[this.selectedIndex].value)"
|
26
19
|
end
|
20
|
+
|
21
|
+
if options.respond_to?(:html_safe)
|
22
|
+
options = options.html_safe
|
23
|
+
end
|
24
|
+
select_tag "switch_user_identifier", options,
|
25
|
+
:onchange => "location.href = '/switch_user?scope_identifier=' + encodeURIComponent(this.options[this.selectedIndex].value)"
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def tag_value(user, id_name, scope)
|
31
|
+
identifier = user.send(id_name)
|
32
|
+
|
33
|
+
"#{scope}_#{identifier}"
|
34
|
+
end
|
35
|
+
|
36
|
+
def tag_label(user, name)
|
37
|
+
user.send(name)
|
27
38
|
end
|
28
39
|
|
29
40
|
def available?
|
30
|
-
user =
|
31
|
-
SwitchUser.available_users.keys.each do |scope|
|
32
|
-
user = send("current_#{scope}")
|
33
|
-
break if user
|
34
|
-
end
|
41
|
+
user = provider.current_users_without_scope.first
|
35
42
|
SwitchUser.view_guard.call(user, request)
|
36
43
|
end
|
44
|
+
|
45
|
+
def provider
|
46
|
+
SwitchUser.provider_class.new(controller)
|
47
|
+
end
|
37
48
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'switch_user/provider/base'
|
2
|
+
|
3
|
+
module SwitchUser
|
4
|
+
module Provider
|
5
|
+
class Authlogic < Base
|
6
|
+
def initialize(controller)
|
7
|
+
@controller = controller
|
8
|
+
end
|
9
|
+
|
10
|
+
def login(user, scope = nil)
|
11
|
+
UserSession.create(user)
|
12
|
+
end
|
13
|
+
|
14
|
+
def logout(scope = nil)
|
15
|
+
@controller.current_user_session.destroy
|
16
|
+
end
|
17
|
+
|
18
|
+
def current_user(scope = nil)
|
19
|
+
result = UserSession.find
|
20
|
+
if result
|
21
|
+
result.record
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module SwitchUser
|
2
|
+
module Provider
|
3
|
+
class Base
|
4
|
+
def current_users_without_scope
|
5
|
+
SwitchUser.available_scopes.inject([]) do |users, scope|
|
6
|
+
user = current_user(scope)
|
7
|
+
users << user if user
|
8
|
+
users
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def login_exclusive(user, args)
|
13
|
+
requested_scope = args.fetch(:scope, :user).to_sym
|
14
|
+
|
15
|
+
logout_all
|
16
|
+
login(user, requested_scope)
|
17
|
+
end
|
18
|
+
|
19
|
+
def logout_all
|
20
|
+
SwitchUser.available_scopes.each do |scope|
|
21
|
+
logout(scope)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'switch_user/provider/base'
|
2
|
+
|
3
|
+
module SwitchUser
|
4
|
+
module Provider
|
5
|
+
class Devise < Base
|
6
|
+
def initialize(controller)
|
7
|
+
@controller = controller
|
8
|
+
@warden = @controller.warden
|
9
|
+
end
|
10
|
+
|
11
|
+
def login(user, scope = :user)
|
12
|
+
@warden.set_user(user, :scope => scope)
|
13
|
+
end
|
14
|
+
|
15
|
+
def logout(scope = :user)
|
16
|
+
@warden.logout(scope)
|
17
|
+
end
|
18
|
+
|
19
|
+
def current_user(scope = :user)
|
20
|
+
@controller.send("current_#{scope}")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'switch_user/provider/base'
|
2
|
+
|
3
|
+
module SwitchUser
|
4
|
+
module Provider
|
5
|
+
class RestfulAuthentication < Base
|
6
|
+
def initialize(controller)
|
7
|
+
@controller = controller
|
8
|
+
end
|
9
|
+
|
10
|
+
def login(user, scope = nil)
|
11
|
+
@controller.current_user = user
|
12
|
+
end
|
13
|
+
|
14
|
+
def logout(scope = nil)
|
15
|
+
@controller.logout_killing_session!
|
16
|
+
end
|
17
|
+
|
18
|
+
def current_user(scope = nil)
|
19
|
+
@controller.current_user
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'switch_user/provider/base'
|
2
|
+
|
3
|
+
module SwitchUser
|
4
|
+
module Provider
|
5
|
+
class Sorcery < Base
|
6
|
+
def initialize(controller)
|
7
|
+
@controller = controller
|
8
|
+
end
|
9
|
+
|
10
|
+
def login(user, scope = nil)
|
11
|
+
@controller.auto_login(user)
|
12
|
+
end
|
13
|
+
|
14
|
+
def logout(scope = nil)
|
15
|
+
@controller.logout
|
16
|
+
end
|
17
|
+
|
18
|
+
def current_user(scope = nil)
|
19
|
+
@controller.current_user
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
module SwitchUser
|
2
|
+
module Provider
|
3
|
+
autoload :Authlogic, "switch_user/provider/authlogic"
|
4
|
+
autoload :Devise, "switch_user/provider/devise"
|
5
|
+
autoload :RestfulAuthentication, "switch_user/provider/restful_authentication"
|
6
|
+
autoload :Sorcery, "switch_user/provider/sorcery"
|
7
|
+
end
|
8
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
if defined?(Rails)
|
2
|
+
if defined? Rails::Engine
|
3
|
+
class Engine < Rails::Engine
|
4
|
+
config.to_prepare do
|
5
|
+
ActionView::Base.send :include, SwitchUserHelper
|
6
|
+
end
|
7
|
+
end
|
8
|
+
else
|
9
|
+
%w(controllers helpers).each do |dir|
|
10
|
+
path = File.join(File.dirname(__FILE__), '..', 'app', dir)
|
11
|
+
$LOAD_PATH << path
|
12
|
+
ActiveSupport::Dependencies.load_paths << path
|
13
|
+
ActiveSupport::Dependencies.load_once_paths.delete(path)
|
14
|
+
ActionView::Base.send :include, SwitchUserHelper
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module SwitchUser
|
2
|
+
class UserLoader
|
3
|
+
attr_reader :scope
|
4
|
+
attr_accessor :id
|
5
|
+
|
6
|
+
def initialize(scope, id)
|
7
|
+
self.scope = scope
|
8
|
+
self.id = id
|
9
|
+
end
|
10
|
+
|
11
|
+
def load
|
12
|
+
user_class.send(finder, id)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def scope=(scope)
|
18
|
+
if SwitchUser.available_scopes.include?(scope.to_sym)
|
19
|
+
@scope = scope
|
20
|
+
else
|
21
|
+
raise InvalidScope
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def user_class
|
26
|
+
scope.classify.constantize
|
27
|
+
end
|
28
|
+
|
29
|
+
def finder
|
30
|
+
column_name = SwitchUser.available_users_identifiers[scope.to_sym]
|
31
|
+
|
32
|
+
"find_by_#{column_name}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/switch_user/version.rb
CHANGED
data/lib/switch_user.rb
CHANGED
@@ -1,41 +1,43 @@
|
|
1
|
+
require 'switch_user/rails'
|
2
|
+
require 'switch_user/provider'
|
3
|
+
require 'active_support/core_ext'
|
4
|
+
|
1
5
|
module SwitchUser
|
2
|
-
|
3
|
-
class Engine < Rails::Engine
|
4
|
-
config.to_prepare do
|
5
|
-
ActionView::Base.send :include, SwitchUserHelper
|
6
|
-
end
|
7
|
-
end
|
8
|
-
else
|
9
|
-
%w(controllers helpers).each do |dir|
|
10
|
-
path = File.join(File.dirname(__FILE__), '..', 'app', dir)
|
11
|
-
$LOAD_PATH << path
|
12
|
-
ActiveSupport::Dependencies.load_paths << path
|
13
|
-
ActiveSupport::Dependencies.load_once_paths.delete(path)
|
14
|
-
ActionView::Base.send :include, SwitchUserHelper
|
15
|
-
end
|
16
|
-
end
|
6
|
+
autoload :UserLoader, "switch_user/user_loader"
|
17
7
|
|
18
|
-
|
19
|
-
self.provider = :devise
|
8
|
+
class InvalidScope < Exception; end
|
20
9
|
|
10
|
+
mattr_accessor :provider
|
21
11
|
mattr_accessor :available_users
|
22
|
-
self.available_users = { :user => lambda { User.all } }
|
23
|
-
|
24
12
|
mattr_accessor :available_users_identifiers
|
25
|
-
self.available_users_identifiers = { :user => :id }
|
26
|
-
|
27
13
|
mattr_accessor :available_users_names
|
28
|
-
self.available_users_names = { :user => :email }
|
29
|
-
|
30
14
|
mattr_accessor :controller_guard
|
31
|
-
self.controller_guard = lambda { |current_user, request| Rails.env.development? }
|
32
15
|
mattr_accessor :view_guard
|
33
|
-
self.view_guard = lambda { |current_user, request| Rails.env.development? }
|
34
|
-
|
35
16
|
mattr_accessor :redirect_path
|
36
|
-
|
37
|
-
|
17
|
+
|
38
18
|
def self.setup
|
39
19
|
yield self
|
40
20
|
end
|
21
|
+
|
22
|
+
def self.provider_class
|
23
|
+
"SwitchUser::Provider::#{provider.to_s.classify}".constantize
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.available_scopes
|
27
|
+
available_users.keys
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def self.reset_config
|
33
|
+
self.provider = :devise
|
34
|
+
self.available_users = { :user => lambda { User.all } }
|
35
|
+
self.available_users_identifiers = { :user => :id }
|
36
|
+
self.available_users_names = { :user => :email }
|
37
|
+
self.controller_guard = lambda { |current_user, request| Rails.env.development? }
|
38
|
+
self.view_guard = lambda { |current_user, request| Rails.env.development? }
|
39
|
+
self.redirect_path = lambda { |request, params| request.env["HTTP_REFERER"] ? :back : root_path }
|
40
|
+
end
|
41
|
+
|
42
|
+
reset_config
|
41
43
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'switch_user/provider/authlogic'
|
3
|
+
|
4
|
+
class UserSession
|
5
|
+
class << self
|
6
|
+
attr_accessor :user
|
7
|
+
|
8
|
+
def create(user)
|
9
|
+
self.user = user
|
10
|
+
end
|
11
|
+
|
12
|
+
def destroy
|
13
|
+
self.user = nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def find
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
def record
|
21
|
+
user
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class AuthlogicController
|
27
|
+
def current_user_session
|
28
|
+
UserSession
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe SwitchUser::Provider::Authlogic do
|
33
|
+
let(:controller) { AuthlogicController.new }
|
34
|
+
let(:provider) { SwitchUser::Provider::Authlogic.new(controller) }
|
35
|
+
|
36
|
+
it_behaves_like "a provider"
|
37
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'switch_user/provider/devise'
|
3
|
+
|
4
|
+
class FakeWarden
|
5
|
+
attr_reader :user_hash
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@user_hash = {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def set_user(user, args)
|
12
|
+
scope = args.fetch(:scope, :user)
|
13
|
+
@user_hash[scope] = user
|
14
|
+
end
|
15
|
+
|
16
|
+
def logout(scope)
|
17
|
+
@user_hash.delete(scope)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class DeviseController
|
22
|
+
def warden
|
23
|
+
@warden ||= FakeWarden.new
|
24
|
+
end
|
25
|
+
|
26
|
+
def current_user
|
27
|
+
@warden.user_hash[:user]
|
28
|
+
end
|
29
|
+
|
30
|
+
def current_admin
|
31
|
+
@warden.user_hash[:admin]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe SwitchUser::Provider::Devise do
|
36
|
+
let(:controller) { DeviseController.new }
|
37
|
+
let(:provider) { SwitchUser::Provider::Devise.new(controller) }
|
38
|
+
let(:user) { stub(:user) }
|
39
|
+
|
40
|
+
it_behaves_like "a provider"
|
41
|
+
|
42
|
+
it "can use alternate scopes" do
|
43
|
+
user = stub(:user)
|
44
|
+
provider.login(user, :admin)
|
45
|
+
|
46
|
+
provider.current_user(:admin).should == user
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "#login_exclusive" do
|
50
|
+
before do
|
51
|
+
SwitchUser.stub(:available_users => {:user => nil, :admin => nil})
|
52
|
+
provider.login(user, :admin)
|
53
|
+
provider.login_exclusive(user, :scope => "user")
|
54
|
+
end
|
55
|
+
|
56
|
+
it "logs the user in" do
|
57
|
+
provider.current_user.should == user
|
58
|
+
end
|
59
|
+
|
60
|
+
it "logs out other scopes" do
|
61
|
+
provider.current_user(:admin).should be_nil
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "#logout_all" do
|
66
|
+
it "logs out users under all scopes" do
|
67
|
+
SwitchUser.stub(:available_users => {:user => nil, :admin => nil})
|
68
|
+
provider.login(user, :admin)
|
69
|
+
provider.login(user, :user)
|
70
|
+
|
71
|
+
provider.logout_all
|
72
|
+
|
73
|
+
provider.current_user(:admin).should be_nil
|
74
|
+
provider.current_user(:user).should be_nil
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "#all_current_users" do
|
79
|
+
it "pulls users from an alternate scope" do
|
80
|
+
SwitchUser.stub(:available_users => {:user => nil, :admin => nil})
|
81
|
+
provider.login(user, :admin)
|
82
|
+
|
83
|
+
provider.current_users_without_scope.should == [user]
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'switch_user/provider/restful_authentication'
|
3
|
+
|
4
|
+
class RestfulAuthenticationController
|
5
|
+
attr_accessor :current_user
|
6
|
+
|
7
|
+
def logout_killing_session!
|
8
|
+
self.current_user = nil
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe SwitchUser::Provider::RestfulAuthentication do
|
13
|
+
let(:controller) { RestfulAuthenticationController.new }
|
14
|
+
let(:provider) { SwitchUser::Provider::RestfulAuthentication.new(controller) }
|
15
|
+
|
16
|
+
it_behaves_like "a provider"
|
17
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'switch_user/provider/sorcery'
|
3
|
+
|
4
|
+
class SorceryController
|
5
|
+
def logout
|
6
|
+
@user = nil
|
7
|
+
end
|
8
|
+
|
9
|
+
def auto_login(user)
|
10
|
+
@user = user
|
11
|
+
end
|
12
|
+
|
13
|
+
def current_user
|
14
|
+
@user
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe SwitchUser::Provider::Sorcery do
|
19
|
+
let(:controller) { SorceryController.new }
|
20
|
+
let(:provider) { SwitchUser::Provider::Sorcery.new(controller) }
|
21
|
+
|
22
|
+
it_behaves_like "a provider"
|
23
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'support/provider'
|
@@ -0,0 +1,27 @@
|
|
1
|
+
shared_examples_for "a provider" do
|
2
|
+
let(:user) { stub(:user) }
|
3
|
+
|
4
|
+
it "can log a user in" do
|
5
|
+
provider.login(user)
|
6
|
+
|
7
|
+
provider.current_user.should == user
|
8
|
+
end
|
9
|
+
|
10
|
+
it "can log a user out" do
|
11
|
+
provider.login(user)
|
12
|
+
|
13
|
+
provider.logout
|
14
|
+
|
15
|
+
provider.current_user.should == nil
|
16
|
+
end
|
17
|
+
|
18
|
+
it "responds to login_exclusive" do
|
19
|
+
provider.should respond_to(:login_exclusive)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "knows if there are any users logged in" do
|
23
|
+
provider.login(user)
|
24
|
+
|
25
|
+
provider.current_users_without_scope.should == [user]
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'switch_user/user_loader'
|
3
|
+
|
4
|
+
class User
|
5
|
+
def self.find_by_id(id)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
describe SwitchUser::UserLoader do
|
10
|
+
let(:user) { stub(:user) }
|
11
|
+
|
12
|
+
it "raises an exception if we are passed an invalid scope" do
|
13
|
+
expect { SwitchUser::UserLoader.new("useeer", 1) }.to raise_error(SwitchUser::InvalidScope)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "loads a user" do
|
17
|
+
User.stub(:find_by_id).with(1).and_return(user)
|
18
|
+
|
19
|
+
loader = SwitchUser::UserLoader.new("user", 1)
|
20
|
+
|
21
|
+
loader.load.should == user
|
22
|
+
end
|
23
|
+
|
24
|
+
it "returns nil if no user is found" do
|
25
|
+
loader = SwitchUser::UserLoader.new("user", 2)
|
26
|
+
loader.load.should == nil
|
27
|
+
end
|
28
|
+
|
29
|
+
it "loads a user with an alternate identifier column" do
|
30
|
+
User.stub(:find_by_email).with(2).and_return(user)
|
31
|
+
SwitchUser.available_users_identifiers = {:user => :email}
|
32
|
+
|
33
|
+
loader = SwitchUser::UserLoader.new("user", 2)
|
34
|
+
loader.load.should == user
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'switch_user'
|
3
|
+
|
4
|
+
describe SwitchUser do
|
5
|
+
it "has a default provider" do
|
6
|
+
SwitchUser.provider.should == :devise
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#available_scopes" do
|
10
|
+
it "returns a list of available scopes" do
|
11
|
+
SwitchUser.available_scopes.should == [:user]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "assigning the provider" do
|
16
|
+
it "sets the provider" do
|
17
|
+
# ensure we aren't breaking existing functionality
|
18
|
+
SwitchUser.provider = :sorcery
|
19
|
+
SwitchUser.provider.should == :sorcery
|
20
|
+
end
|
21
|
+
it "sets the provider class" do
|
22
|
+
SwitchUser.provider = :devise
|
23
|
+
SwitchUser.provider_class.should == SwitchUser::Provider::Devise
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/switch_user.gemspec
CHANGED
@@ -5,7 +5,7 @@ Gem::Specification.new do |s|
|
|
5
5
|
s.name = "switch_user"
|
6
6
|
s.version = SwitchUser::VERSION
|
7
7
|
s.platform = Gem::Platform::RUBY
|
8
|
-
s.authors = ["Richard Huang"]
|
8
|
+
s.authors = ["Richard Huang", "Luke Cowell"]
|
9
9
|
s.email = ["flyerhzm@gmail.com"]
|
10
10
|
s.homepage = "http://rubygems.org/gems/switch_user"
|
11
11
|
s.summary = "Easily switch current user to speed up development"
|
@@ -14,7 +14,9 @@ Gem::Specification.new do |s|
|
|
14
14
|
s.required_rubygems_version = ">= 1.3.6"
|
15
15
|
s.rubyforge_project = "switch_user"
|
16
16
|
|
17
|
+
s.add_dependency "activesupport"
|
17
18
|
s.add_development_dependency "bundler", ">= 1.0.0"
|
19
|
+
s.add_development_dependency "rspec", "~> 2.11.0"
|
18
20
|
|
19
21
|
s.files = `git ls-files`.split("\n")
|
20
22
|
s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
|
metadata
CHANGED
@@ -1,27 +1,65 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: switch_user
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Richard Huang
|
9
|
+
- Luke Cowell
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
date: 2012-
|
13
|
+
date: 2012-10-31 00:00:00.000000000 Z
|
13
14
|
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activesupport
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ! '>='
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '0'
|
14
31
|
- !ruby/object:Gem::Dependency
|
15
32
|
name: bundler
|
16
|
-
requirement:
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: 1.0.0
|
39
|
+
type: :development
|
40
|
+
prerelease: false
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
17
42
|
none: false
|
18
43
|
requirements:
|
19
44
|
- - ! '>='
|
20
45
|
- !ruby/object:Gem::Version
|
21
46
|
version: 1.0.0
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.11.0
|
22
55
|
type: :development
|
23
56
|
prerelease: false
|
24
|
-
version_requirements:
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ~>
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 2.11.0
|
25
63
|
description: Easily switch current user to speed up development
|
26
64
|
email:
|
27
65
|
- flyerhzm@gmail.com
|
@@ -30,6 +68,7 @@ extensions: []
|
|
30
68
|
extra_rdoc_files: []
|
31
69
|
files:
|
32
70
|
- .gitignore
|
71
|
+
- .rspec
|
33
72
|
- Gemfile
|
34
73
|
- LICENSE
|
35
74
|
- README.md
|
@@ -38,8 +77,24 @@ files:
|
|
38
77
|
- app/helpers/switch_user_helper.rb
|
39
78
|
- config/routes.rb
|
40
79
|
- lib/switch_user.rb
|
80
|
+
- lib/switch_user/provider.rb
|
81
|
+
- lib/switch_user/provider/authlogic.rb
|
82
|
+
- lib/switch_user/provider/base.rb
|
83
|
+
- lib/switch_user/provider/devise.rb
|
84
|
+
- lib/switch_user/provider/restful_authentication.rb
|
85
|
+
- lib/switch_user/provider/sorcery.rb
|
86
|
+
- lib/switch_user/rails.rb
|
87
|
+
- lib/switch_user/user_loader.rb
|
41
88
|
- lib/switch_user/version.rb
|
42
89
|
- rails/init.rb
|
90
|
+
- spec/provider/authlogic_spec.rb
|
91
|
+
- spec/provider/devise_spec.rb
|
92
|
+
- spec/provider/restful_authrentication_spec.rb
|
93
|
+
- spec/provider/sorcery_spec.rb
|
94
|
+
- spec/spec_helper.rb
|
95
|
+
- spec/support/provider.rb
|
96
|
+
- spec/switch_user/user_loader_spec.rb
|
97
|
+
- spec/switch_user_spec.rb
|
43
98
|
- switch_user.gemspec
|
44
99
|
homepage: http://rubygems.org/gems/switch_user
|
45
100
|
licenses: []
|
@@ -61,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
61
116
|
version: 1.3.6
|
62
117
|
requirements: []
|
63
118
|
rubyforge_project: switch_user
|
64
|
-
rubygems_version: 1.8.
|
119
|
+
rubygems_version: 1.8.23
|
65
120
|
signing_key:
|
66
121
|
specification_version: 3
|
67
122
|
summary: Easily switch current user to speed up development
|