user_switcher 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3387dcbd68cff720cf1b0394964830ce1ad5ea35768f713e16a2cde6287600bb
4
+ data.tar.gz: 01a79572bdc05f16eda5b9a6d0f4a87220491b30597e579cac2d3c1ca2975d45
5
+ SHA512:
6
+ metadata.gz: 989d36c1585d9e63abe5b74e239e58c451f5978a4fa2b1e1e63c294bd72a0a7fde8dd2dd8ddf47811dcc28e26e18935b388b53e7dc5700662e706a12232ad2fd
7
+ data.tar.gz: b93a782c0d35c6f53763edc9168a5230dba4d800f1b3f67e2f2ab3881c962188330a161e8d0fd3aeac49e3dca6e6df30cb17288658bcdc63219d2c5e11d88168
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2019 Yukito Ito
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # UserSwitcher
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+
6
+ config/initializers/user_switcher.rb
7
+
8
+ ```
9
+ UserSwitcher.configure do |config|
10
+ config.users = [
11
+ { id: 'ykpythemind@example.com', password: 'password' },
12
+ { id: 'mckaoru@example.com', password: 'password' }
13
+ ]
14
+ end
15
+ ```
16
+
17
+ ## Installation
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'user_switcher'
22
+ ```
23
+
24
+ And then execute:
25
+ ```bash
26
+ $ bundle
27
+ ```
28
+
29
+ Or install it yourself as:
30
+ ```bash
31
+ $ gem install user_switcher
32
+ ```
33
+
34
+ ## Contributing
35
+ Contribution directions go here.
36
+
37
+ ## License
38
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'UserSwitcher'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,24 @@
1
+ module UserSwitcher
2
+ class UserSessionsController < ActionController::Base
3
+ skip_before_action :verify_authenticity_token
4
+ if defined?(Sorcery)
5
+ skip_before_action :require_login, raise: false
6
+ end
7
+ before_action :prepare_login_params
8
+
9
+ def create
10
+ instance_eval &UserSwitcher.config.login_procedure
11
+ end
12
+
13
+ private
14
+
15
+ def prepare_login_params
16
+ user_param = params[:users].find { |param| param[:id] == params[:user_id] }
17
+ return if user_param.blank?
18
+
19
+ params[:id] = user_param[:id]
20
+ params[:email] = user_param[:id]
21
+ params[:password] = user_param[:password]
22
+ end
23
+ end
24
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ UserSwitcher::Engine.routes.draw do
2
+ post "login", to: "user_sessions#create"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :user_switcher do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1 @@
1
+ require "user_switcher"
@@ -0,0 +1,50 @@
1
+ module UserSwitcher
2
+ class << self
3
+ def configure
4
+ yield config
5
+ end
6
+
7
+ def config
8
+ @_config ||= Config.new
9
+ end
10
+ end
11
+
12
+ class Config
13
+ attr_accessor :login_url, :login_procedure
14
+
15
+ def initialize
16
+ @login_url = "/user_switcher/login"
17
+ @users = load_users_from_file
18
+ @login_procedure = default_login_procedure
19
+ end
20
+
21
+ def default_login_procedure
22
+ proc do
23
+ # You can use params[:email] & params[:password] for login
24
+ logout
25
+ login(params[:email], params[:password])
26
+
27
+ redirect_to main_app.root_path
28
+ end
29
+ end
30
+
31
+ def load_users_from_file
32
+ #
33
+ []
34
+ end
35
+
36
+ def users
37
+ @users
38
+ end
39
+
40
+ def users=(users)
41
+ @users += users.map { |user| User.new(user) }
42
+ end
43
+
44
+ def to_middleware_config
45
+ { users: users, login_url: login_url }
46
+ end
47
+ end
48
+
49
+ User = Struct.new(:id, :password, keyword_init: true)
50
+ end
@@ -0,0 +1,16 @@
1
+ require "rails"
2
+
3
+ module UserSwitcher
4
+ class Engine < ::Rails::Engine
5
+ isolate_namespace UserSwitcher
6
+
7
+ # initializer "user_switcher.mixins" do
8
+ # ActiveSupport.on_load :active_record do
9
+ # end
10
+ # end
11
+
12
+ # config.to_prepare do
13
+ # # init!
14
+ # end
15
+ end
16
+ end
@@ -0,0 +1,62 @@
1
+ require 'erb'
2
+
3
+ module UserSwitcher::Middlewares
4
+ class SwitcherInserter
5
+ attr_reader :users, :login_url
6
+
7
+ def initialize(app, config = {})
8
+ @app = app
9
+
10
+ @users = config[:users] || []
11
+ @login_url = config[:login_url]
12
+ @form_erb = config[:form_erb] || load_form_erb
13
+ end
14
+
15
+ def call(env)
16
+ status, headers, body = @app.call(env)
17
+
18
+ content_type = headers['Content-Type'].to_s
19
+ redirect = 300 <= status.to_i && status.to_i < 400
20
+
21
+ if content_type.strip.start_with?("text/html") && !redirect
22
+ case body
23
+ when ActionDispatch::Response, ActionDispatch::Response::RackBody
24
+ body = body.body
25
+ when Array
26
+ body = body[0]
27
+ end
28
+
29
+ body = body.dup if body.frozen?
30
+ new_body = insert_form(body)
31
+ new_body = insert_style(new_body)
32
+ headers['Content-Length'] &&= new_body.bytesize.to_s
33
+
34
+ [status, headers, [new_body]]
35
+ else
36
+ [status, headers, body]
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def insert_form(html)
43
+ html.gsub %r{<body(.*?)>(.*)<\/body>}mi, '<body\1>' + form + '\2</body>'
44
+ end
45
+
46
+ def insert_style(html)
47
+ html.sub %r{<head(.*?)>(.*)<\/head>}mi, '<head\1>\2' + style + '</head>'
48
+ end
49
+
50
+ def form
51
+ @form ||= ERB.new(@form_erb).result(binding)
52
+ end
53
+
54
+ def style
55
+ @style ||= File.read(File.expand_path('../views/style.html', __dir__))
56
+ end
57
+
58
+ def load_form_erb
59
+ File.read(File.expand_path('../views/form.erb', __dir__))
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,11 @@
1
+ module UserSwitcher
2
+ def self.initialize!(app)
3
+ app.middleware.insert(-1, UserSwitcher::Middlewares::SwitcherInserter, UserSwitcher.config.to_middleware_config)
4
+ end
5
+
6
+ class Railtie < ::Rails::Railtie
7
+ initializer "user_switcher.configure_rails_initialization", after: :load_config_initializers do |app|
8
+ UserSwitcher.initialize!(app)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module UserSwitcher
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,16 @@
1
+ <div id="user_switcher_menu">
2
+ <form method="post" action="<%= login_url %>">
3
+ <select name="user_id">
4
+ <% users.each do |user| %>
5
+ <option value="<%= user.id %>"><%= "#{user.id}" %></option>
6
+ <% end %>
7
+ </select>
8
+
9
+ <% users.each do |user| %>
10
+ <input type="hidden" name="users[][id]" value="<%= user.id %>">
11
+ <input type="hidden" name="users[][password]" value="<%= user.password %>">
12
+ <% end %>
13
+
14
+ <input type="submit" value="login">
15
+ </form>
16
+ </div>
@@ -0,0 +1,23 @@
1
+ <meta http-equiv="Content-Style-Type" content="text/css">
2
+ <style type="text/css">
3
+ #user_switcher_menu {
4
+ position: fixed;
5
+ top: 3px;
6
+ right: 3px;
7
+ font-size: 13px;
8
+ cursor: pointer;
9
+ z-index: 9999;
10
+ }
11
+
12
+ #user_switcher_menu form {
13
+ display: flex;
14
+ }
15
+
16
+ #user_switcher_menu select {
17
+ }
18
+
19
+ #user_switcher_menu input {
20
+ margin: 0; padding: 0;
21
+ }
22
+
23
+ </style>
@@ -0,0 +1,7 @@
1
+ require "user_switcher/config"
2
+ require "user_switcher/middlewares/switcher_inserter"
3
+
4
+ if defined?(Rails)
5
+ require "user_switcher/engine"
6
+ require "user_switcher/railtie"
7
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: user_switcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ykpythemind
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-02-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.2
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.3.6
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.3.6
41
+ - !ruby/object:Gem::Dependency
42
+ name: sorcery
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.13'
55
+ description: Rails plugin for switching 'current_user'
56
+ email:
57
+ - yukibukiyou@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - app/controllers/user_switcher/user_sessions_controller.rb
66
+ - config/routes.rb
67
+ - lib/tasks/user_switcher_tasks.rake
68
+ - lib/user-switcher.rb
69
+ - lib/user_switcher.rb
70
+ - lib/user_switcher/config.rb
71
+ - lib/user_switcher/engine.rb
72
+ - lib/user_switcher/middlewares/switcher_inserter.rb
73
+ - lib/user_switcher/railtie.rb
74
+ - lib/user_switcher/version.rb
75
+ - lib/user_switcher/views/form.erb
76
+ - lib/user_switcher/views/style.html
77
+ homepage: https://github.com/ykpythemind/user_switcher
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.0.1
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Switch user easily via browser.
100
+ test_files: []