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 CHANGED
@@ -1,3 +1,4 @@
1
1
  pkg/*
2
2
  *.gem
3
3
  .bundle
4
+ .rvmrc
data/Gemfile.lock ADDED
@@ -0,0 +1,15 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ switch_user (0.5.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+
10
+ PLATFORMS
11
+ ruby
12
+
13
+ DEPENDENCIES
14
+ bundler (>= 1.0.0)
15
+ switch_user!
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?scope_id=user_#{user.id}" %>
43
- <%= link_to admin.login, "/switch_user?scope_id=admin_#{admin.id}" %>
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?scope_id=user_#{user.id}"
46
- = link_to admin.login, "/switch_user?scope_id=admin_#{admin.id}"
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 :authologic
59
+ # provider may be :devise or :authlogic
60
60
  config.provider = :devise
61
61
 
62
- # avaliable_users is a hash,
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
- # what field should be displayed on select box
68
- config.display_field = :email
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| Rails_env == "development" }
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 == "development" }
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.display_field = :login
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
- user = nil
21
- if params[:scope_id].present?
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[:scope_id].blank?
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
- scope, id = params[:scope_id].split('_')
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.classify.constantize.find(id)
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[:scope_id].blank?
45
+ if params[:scope_identifier].blank?
53
46
  current_user_session.destroy
54
47
  else
55
- scope, id = params[:scope_id].split('_')
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.classify.constantize.find(id)
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 = send("current_#{scope}")
12
- if current and current.id == user.id
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.id}'>#{user.send(SwitchUser.display_field)}</option>"
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 "switch_user_id", options,
23
- :onchange => "location.href = '/switch_user?scope_id=' + encodeURIComponent(this.options[this.selectedIndex].value)"
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 :display_field
25
- self.display_field = :email
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 == "development" }
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 == "development" }
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
@@ -1,3 +1,3 @@
1
1
  module SwitchUser
2
- VERSION = "0.5.1"
2
+ VERSION = "0.6.0"
3
3
  end
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: 9
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 5
9
- - 1
10
- version: 0.5.1
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-11 00:00:00 +08:00
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