rails_admin_pretender 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4db0cfb54240ae1ec44f3f983c5a59f79cfa11547b2504cedbf5bab0d5fd76f7
4
+ data.tar.gz: 0b831eb0f39845c385ddcaf33bf67a1c918eab551f92ee75210f36960ded4da1
5
+ SHA512:
6
+ metadata.gz: 055c3e0e094deac3bb9398f574e8c17b32911172fdf443253db0e3b353e21caf319783bb42edcb1fd84a645578438fae9302aa174d03cf07ed543b7190eab194
7
+ data.tar.gz: dd9edcee84966a800fe2c317865dca604e9086ac7af3e92b7f2c8bbda0c727906190dec7d813ad5dba434503619c00699b325a5dff544b71d99badfe0906028b
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2019 Chien Tran
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,88 @@
1
+ # Why not using [RailsAdminImpersonate](https://github.com/astrails/rails_admin_impersonate)?
2
+
3
+ I found that the `rails_admin_impersonate` is missing an important feature, to allow Admin to sign out of the current impersonated user and restore the Admin session. It is a bit annoying to sign in after I have finished impersonating someone.
4
+
5
+ Using [Pretender gem](https://github.com/ankane/pretender) seems to be a solution, but integrating it with RailsAdmin is not straightforward (and no tutorial for doing that so far). This gem is for that purpose. It is inspired by [RailsAdminImpersonate](https://github.com/astrails/rails_admin_impersonate).
6
+
7
+ # RailsAdminPretender
8
+
9
+ Add an ability to [rails_admin](https://github.com/sferik/rails_admin) to impersonate as any user or actually any member
10
+ that is devise authenticatable.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ gem 'rails_admin_pretender'
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ ## Usage
23
+
24
+ Add to your `config/initializers/rails_admin.rb` an action `impersonate` to actions:
25
+
26
+ config.actions do
27
+ # root actions
28
+ dashboard # mandatory
29
+ # collection actions
30
+ index # mandatory
31
+ new
32
+ export
33
+ history_index
34
+ bulk_delete
35
+ # member actions
36
+ show
37
+ edit
38
+ delete
39
+ history_show
40
+ show_in_app
41
+ impersonate
42
+ end
43
+
44
+ At the bottom of `config/initializers/rails_admin.rb`, add the following line:
45
+
46
+ RailsAdminPretender.init
47
+
48
+ Finally, in your `ApplicationController`, include the following module:
49
+
50
+ include RailsAdminPretender
51
+
52
+
53
+ Now restart the application and visit User table in the admin. You should see home icon and Impersonate link for every model that uses Devise. In your views, you can use the helper `is_impersonating?` to check whether current_user is an impersonated user or not.
54
+
55
+ For example:
56
+
57
+ <% if is_impersonating? %>
58
+ <li class="nav-item">
59
+ <%= link_to "Sign out of this user", stop_impersonating_path, method: :post, class: "nav-link" %>
60
+ </li>
61
+ <% else %>
62
+ <li class="nav-item">
63
+ <%= link_to "Sign out", destroy_user_session_path, method: :delete, class: "nav-link" %>
64
+ </li>
65
+ <% end %>
66
+
67
+ Note: by default impersonation is disabled for model `Admin`. If you want to
68
+ disable it for some other model you can modify the above code like this:
69
+
70
+ config.actions do
71
+ ...
72
+ impersonate do
73
+ authorized do
74
+ 'ModelName' != bindings[:abstract_model].model_name
75
+ end
76
+ end
77
+
78
+ ## Contributing
79
+
80
+ 1. Fork it
81
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
82
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
83
+ 4. Push to the branch (`git push origin my-new-feature`)
84
+ 5. Create new Pull Request
85
+
86
+ ## Copyright
87
+
88
+ &copy; 2019 [Chien Tran]
@@ -0,0 +1,5 @@
1
+ en:
2
+ admin:
3
+ actions:
4
+ impersonate:
5
+ menu: "Impersonate"
@@ -0,0 +1,26 @@
1
+ require 'pretender'
2
+ require_relative '../rails_admin/config/actions/impersonate'
3
+
4
+ module RailsAdminPretender
5
+ extend ActiveSupport::Concern
6
+
7
+ def self.init
8
+ RailsAdmin::MainController.extend(::Pretender::Methods)
9
+ RailsAdmin::MainController.impersonates(:user)
10
+ end
11
+
12
+ def is_impersonating?
13
+ true_user != current_user
14
+ end
15
+
16
+ included do
17
+ if respond_to?(:impersonates)
18
+ impersonates(:user)
19
+ end
20
+ if respond_to?(:helper_method)
21
+ helper_method :is_impersonating?
22
+ end
23
+ end
24
+ end
25
+
26
+ I18n.load_path += Dir.glob(File.expand_path("../../config/locales/*.{rb,yml}", __FILE__))
@@ -0,0 +1,37 @@
1
+ module RailsAdmin
2
+ module Config
3
+ module Actions
4
+ class Impersonate < RailsAdmin::Config::Actions::Base
5
+ register_instance_option :visible? do
6
+ ('Admin' != bindings[:abstract_model].model_name) &&
7
+ authorized? && bindings[:object].respond_to?(:devise_modules)
8
+ end
9
+
10
+ register_instance_option :member? do
11
+ true
12
+ end
13
+
14
+ register_instance_option :pjax? do
15
+ false
16
+ end
17
+
18
+ register_instance_option :http_methods do
19
+ [:get]
20
+ end
21
+
22
+ register_instance_option :link_icon do
23
+ 'icon-home'
24
+ end
25
+
26
+ register_instance_option :controller do
27
+ Proc.new do
28
+ impersonate_user(@object)
29
+ redirect_to "/"
30
+ end
31
+ end
32
+
33
+ RailsAdmin::Config::Actions.register(self)
34
+ end
35
+ end
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_admin_pretender
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Chien Tran
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-04-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pretender
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.3.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.3.4
27
+ description: A simple gem to integrate pretender with RailsAdmin
28
+ email: chientranx@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - LICENSE.txt
34
+ - README.md
35
+ - config/locales/rails_admin_pretender.yml
36
+ - lib/rails_admin_pretender.rb
37
+ - rails_admin/config/actions/impersonate.rb
38
+ homepage: http://rubygems.org/gems/rails_admin_pretender
39
+ licenses:
40
+ - MIT
41
+ metadata: {}
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 2.7.6
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: Rails Admin Pretender
62
+ test_files: []