user_switcher 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/user_switcher/user_sessions_controller.rb +2 -1
- data/config/routes.rb +6 -2
- data/lib/user_switcher/config.rb +11 -1
- data/lib/user_switcher/middlewares/switcher_inserter.rb +12 -9
- data/lib/user_switcher/railtie.rb +2 -0
- data/lib/user_switcher/version.rb +1 -1
- data/lib/user_switcher/views/{form.erb → form.html.erb} +0 -0
- data/lib/user_switcher/views/{style.html → head.html.erb} +0 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a5ebb17d7d0453866c0f74c182403298555cfdb2fd5dd301c2dede6368bf028a
|
4
|
+
data.tar.gz: 3fe71ea3d7ed7c5a0b7af6f68e081804675b66509354cc7903a7823080d4771f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 232cda7d8dd162df723e7746fa0b128c66ddf0c73233bf40ea5c62e8c13b52848f0dc67c29a64936fb06b782fc0005965833bb60b0d48f65947d656c91aa15b8
|
7
|
+
data.tar.gz: 76bf8a37d560402d73e7553237fea7bbe28dceafee499292745f4e9fbbc3c4e319af07b3f8271ace8eb1681c4e4b19f1f1d4ad27fcde64a6bb216c55a5269f34
|
@@ -4,6 +4,7 @@ module UserSwitcher
|
|
4
4
|
if defined?(Sorcery)
|
5
5
|
skip_before_action :require_login, raise: false
|
6
6
|
end
|
7
|
+
|
7
8
|
before_action :prepare_login_params
|
8
9
|
|
9
10
|
def create
|
@@ -13,7 +14,7 @@ module UserSwitcher
|
|
13
14
|
private
|
14
15
|
|
15
16
|
def prepare_login_params
|
16
|
-
user_param = params[:users].find { |
|
17
|
+
user_param = (params[:users] || [{}]).find { |user| user[:id] == params[:user_id] }
|
17
18
|
return if user_param.blank?
|
18
19
|
|
19
20
|
params[:id] = user_param[:id]
|
data/config/routes.rb
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
Rails.application.routes.draw do
|
2
|
+
if UserSwitcher.enabled?
|
3
|
+
scope UserSwitcher.config.routes_prefix, module: "user_switcher" do
|
4
|
+
post "login", to: "user_sessions#create", as: "user_switcher_login"
|
5
|
+
end
|
6
|
+
end
|
3
7
|
end
|
data/lib/user_switcher/config.rb
CHANGED
@@ -7,15 +7,25 @@ module UserSwitcher
|
|
7
7
|
def config
|
8
8
|
@_config ||= Config.new
|
9
9
|
end
|
10
|
+
|
11
|
+
def enabled?
|
12
|
+
!!config.enabled
|
13
|
+
end
|
10
14
|
end
|
11
15
|
|
12
16
|
class Config
|
13
|
-
attr_accessor :login_url, :login_procedure
|
17
|
+
attr_accessor :login_url, :login_procedure, :enabled, :routes_prefix
|
14
18
|
|
15
19
|
def initialize
|
16
20
|
@login_url = "/user_switcher/login"
|
17
21
|
@users = load_users_from_file
|
18
22
|
@login_procedure = default_login_procedure
|
23
|
+
@routes_prefix = "user_switcher"
|
24
|
+
@enabled = if defined?(Rails)
|
25
|
+
Rails.env.development?
|
26
|
+
else
|
27
|
+
false
|
28
|
+
end
|
19
29
|
end
|
20
30
|
|
21
31
|
def default_login_procedure
|
@@ -9,7 +9,6 @@ module UserSwitcher::Middlewares
|
|
9
9
|
|
10
10
|
@users = config[:users] || []
|
11
11
|
@login_url = config[:login_url]
|
12
|
-
@form_erb = config[:form_erb] || load_form_erb
|
13
12
|
end
|
14
13
|
|
15
14
|
def call(env)
|
@@ -25,10 +24,10 @@ module UserSwitcher::Middlewares
|
|
25
24
|
when Array
|
26
25
|
body = body[0]
|
27
26
|
end
|
28
|
-
|
29
27
|
body = body.dup if body.frozen?
|
28
|
+
|
30
29
|
new_body = insert_form(body)
|
31
|
-
new_body =
|
30
|
+
new_body = insert_inner_head(new_body)
|
32
31
|
headers['Content-Length'] &&= new_body.bytesize.to_s
|
33
32
|
|
34
33
|
[status, headers, [new_body]]
|
@@ -43,20 +42,24 @@ module UserSwitcher::Middlewares
|
|
43
42
|
html.gsub %r{<body(.*?)>(.*)<\/body>}mi, '<body\1>' + form + '\2</body>'
|
44
43
|
end
|
45
44
|
|
46
|
-
def
|
47
|
-
html.sub %r{<head(.*?)>(.*)<\/head>}mi, '<head\1>\2' +
|
45
|
+
def insert_inner_head(html)
|
46
|
+
html.sub %r{<head(.*?)>(.*)<\/head>}mi, '<head\1>\2' + head + '</head>'
|
48
47
|
end
|
49
48
|
|
50
49
|
def form
|
51
|
-
@form ||= ERB.new(
|
50
|
+
@form ||= ERB.new(load_form_erb).result(binding)
|
51
|
+
end
|
52
|
+
|
53
|
+
def head
|
54
|
+
@head ||= ERB.new(load_head_erb).result(binding)
|
52
55
|
end
|
53
56
|
|
54
|
-
def
|
55
|
-
|
57
|
+
def load_head_erb
|
58
|
+
File.read(File.expand_path('../views/head.html.erb', __dir__))
|
56
59
|
end
|
57
60
|
|
58
61
|
def load_form_erb
|
59
|
-
File.read(File.expand_path('../views/form.erb', __dir__))
|
62
|
+
File.read(File.expand_path('../views/form.html.erb', __dir__))
|
60
63
|
end
|
61
64
|
end
|
62
65
|
end
|
File without changes
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: user_switcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ykpythemind
|
@@ -72,8 +72,8 @@ files:
|
|
72
72
|
- lib/user_switcher/middlewares/switcher_inserter.rb
|
73
73
|
- lib/user_switcher/railtie.rb
|
74
74
|
- lib/user_switcher/version.rb
|
75
|
-
- lib/user_switcher/views/form.erb
|
76
|
-
- lib/user_switcher/views/
|
75
|
+
- lib/user_switcher/views/form.html.erb
|
76
|
+
- lib/user_switcher/views/head.html.erb
|
77
77
|
homepage: https://github.com/ykpythemind/user_switcher
|
78
78
|
licenses:
|
79
79
|
- MIT
|