switch_user 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  switch_user
2
2
  ===========
3
3
 
4
- Inspired from hobo, switch_user provides a convenient way to switch current user that speed up your development.
4
+ Inspired from hobo, switch_user provides a convenient way to switch current user that speed up your development, without logout, login and input login or passowrd.
5
5
 
6
6
  switch_user promises that it only be activated in the development environment.
7
7
 
@@ -17,13 +17,19 @@ Install
17
17
 
18
18
  Add in Gemfile.
19
19
 
20
- gem "switch_user", "~> 0.1.0"
20
+ gem "switch_user"
21
21
 
22
22
  Usage
23
23
  -----
24
24
 
25
25
  Add following code into your layout page.
26
26
 
27
+ erb
28
+
29
+ <%= switch_user_select %>
30
+
31
+ haml
32
+
27
33
  = switch_user_select
28
34
 
29
35
  Configuration
@@ -34,10 +40,12 @@ By default, you can switch between Guest and all users in users table.
34
40
  But if you want to use different scope users in devise or you want to customize the users that can be switched, you should do like this
35
41
 
36
42
  SwitchUser.setup do |config|
37
- # model may be :devise or :authologic, but now we only support devise
38
- config.mode = :devise
43
+ # provider may be :devise or :authologic, but now we only support devise
44
+ config.provider = :devise
39
45
  # avaliable_users is a hash, key is the scope of devise user, value is the proc that return the users that can be switched.
40
46
  config.available_users = { :user => lambda { User.all }, :admin => lambda { Admin.all } }
47
+ # what field should be displayed on select box
48
+ config.display_field = :email
41
49
  end
42
50
 
43
51
 
@@ -3,27 +3,30 @@ class SwitchUserController < ApplicationController
3
3
  before_filter :developer_modes_only
4
4
 
5
5
  def set_current_user
6
- if params[:scope_id].blank?
7
- SwitchUser.available_users.keys.each do |s|
8
- warden.logout(s)
9
- end
10
- else
11
- scope, id = params[:scope_id].split('_')
12
- SwitchUser.available_users.keys.each do |s|
13
- if scope == s.to_s
14
- user = scope.classify.constantize.find(id)
15
- warden.set_user(user, :scope => scope)
16
- else
17
- warden.logout(s)
18
- end
19
- end
20
- end
6
+ send("#{SwitchUser.provider}_handle", params)
21
7
  redirect_to(request.env["HTTP_REFERER"] ? :back : root_path)
22
8
  end
23
9
 
24
10
  private
11
+ def developer_modes_only
12
+ render :text => "Permission Denied", :status => 403 unless Rails.env == "development"
13
+ end
25
14
 
26
- def developer_modes_only
27
- render :text => "Permission Denied", :status => 403 unless Rails.env == "development"
28
- end
15
+ def devise_handle(params)
16
+ if params[:scope_id].blank?
17
+ SwitchUser.available_users.keys.each do |s|
18
+ warden.logout(s)
19
+ end
20
+ else
21
+ scope, id = params[:scope_id].split('_')
22
+ SwitchUser.available_users.keys.each do |s|
23
+ if scope == s.to_s
24
+ user = scope.classify.constantize.find(id)
25
+ warden.set_user(user, :scope => scope)
26
+ else
27
+ warden.logout(s)
28
+ end
29
+ end
30
+ end
31
+ end
29
32
  end
@@ -1,6 +1,14 @@
1
1
  module SwitchUserHelper
2
2
  def switch_user_select
3
3
  if Rails.env == "development"
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
4
12
  if current_user
5
13
  options = "<option value=''>Guest</option>"
6
14
  else
@@ -16,8 +24,6 @@ module SwitchUserHelper
16
24
  end
17
25
  end
18
26
  end
19
- select_tag "switch_user_id", options.html_safe,
20
- :onchange => "location.href = '/switch_user?scope_id=' + encodeURIComponent(this.options[this.selectedIndex].value)"
27
+ options
21
28
  end
22
- end
23
29
  end
data/lib/switch_user.rb CHANGED
@@ -7,14 +7,14 @@ module SwitchUser
7
7
  end
8
8
  end
9
9
 
10
- mattr_accessor :mode
11
- self.mode = 'devise'
10
+ mattr_accessor :provider
11
+ self.provider = :devise
12
12
 
13
13
  mattr_accessor :available_users
14
14
  self.available_users = { :user => lambda { User.all } }
15
15
 
16
16
  mattr_accessor :display_field
17
- self.display_field = 'email'
17
+ self.display_field = :email
18
18
 
19
19
  def self.setup
20
20
  yield self
@@ -1,3 +1,3 @@
1
1
  module SwitchUser
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.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: 23
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 0.2.0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Richard Huang