• Stars
    star
    376
  • Rank 110,266 (Top 3 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 8 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Easy way to login as any user in system

AnyLogin Gem

Tests Gem Version RailsJazz https://www.patreon.com/igorkasyanchuk Listed on OpenSource-Heroes.com

Description

Demo available on: https://any-login.herokuapp.com (source code for demo https://github.com/igorkasyanchuk/any_login_test)

Video Demo: https://youtu.be/978DlHvufSY

AnyLogin was created to speed up the development process by allowing developers to quickly log in as any user.

Give it a try; if you like it please share AnyLogin with friends. If you have any suggestions please feel free to contact me.

Requirements

Ruby: 1.9.3, 2.X, Rails: >= 4.0. Pre-configured to work with Devise, Authlogic, Clearance or Sorcery gems.

For Rails < 4.2.7 please use gem 'any_login', '1.3'.

Installation

  1. Add AnyLogin to your Gemfile:

    gem 'any_login'
    
  2. Execute in console:

    bundle install
    
  3. In application layout (for example app/views/layouts/application.html.erb) add the following to the bottom of the page:

    = any_login_here if defined?(AnyLogin)
    
  4. Open your app and on the bottom left corner you will see semi-transparent user icon. Click on it and now you can select any user to log in as without a password.

Integrations

Mongoid

After defining a class method called primary_key, any_login works seamlessly. Otherwise, you will see this error undefined method 'primary_key' for User:Class

class User
  ...

  class << self
    def primary_key
      # if your primary key is :id, if anything else use accordingly.
      :id
    end
  end
end

Devise

By default no additional steps are required to make it work with Devise gem. If you have a User model everything should work fine. If you have different user model you need to set it in options (see Customization section).

Authlogic

By default no additional steps are required to make it work with Authlogic gem. If you have a User model and a current_user method in application controller everything should work fine. If you have different user model you need to set it in options (see Customization section).

Clearance

By default no additional steps are required to make it work with Clearance gem. If you have a User model everything should work fine. If you have different user model you need to set it in options (see Customization section).

Sorcery

By default no additional steps are required to make it work with Sorcery gem. If you have a User model everything should work fine. If you have different user model you need to set it in options (see Customization section).

Custom Providers

If you are utilizing a custom login strategy, you can also utilize it with this gem. Below is an example configuration if a user was utilizing OmniAuth and rails sessions directly as described in this article:

In app/lib/anylogin_omniauth.rb

module AnyloginOmniauth
  module Controller
    def self.any_login_current_user_method
      @@any_login_current_user_method ||= "current_#{AnyLogin.klass.to_s.parameterize.underscore}".to_sym
    end

    def any_login_sign_in
      session[:user_id] = user_id
      redirect_to main_app.send(AnyLogin.redirect_path_after_login)
    end
  end
end

Then in your initializer configure provider to your new class. In this example it would be config.provider = "AnyloginOmniauth"

Customization

If you want to customize gem execute in console:

rails g any_login initializer

It will create the initializer file config/initializers/any_login.rb.

Options

  • enabled - enable or disable gem (by default this gem is enabled only in development mode).
  • klass_name - class name for "User" object. Defaults to User.
  • collection_method - method which returns collection of users. Sample: .all, .active, .admins, .groupped_users. Value is a simple. Defaults to :all.
  • name_method - default value is proc { |e| [e.email, e.id] }. You can change the label of users displayed in dropdown. For example you can add roles, permissions and any other important information.
  • limit - limit number of records in dropdown. Default 10. You can put :none if you don't want to limit the number of users for select.
  • redirect_path_after_login - redirect user to path. Default is :root_path.
  • login_on - you can enable login with select field, ID input or both. Default: :both.
  • position - position of AnyLogin box on page. Possible values: top_left, top_right, bottom_left, bottom_right. Default: bottom_left.
  • login_button_label - login button label.
  • select_prompt - select prompt message.
  • auto_show - automatically show AnyLogin box.
  • http_basic_authentication_enabled - Enable HTTP_BASIC authentication.
  • http_basic_authentication_user_name - HTTP_BASIC authentication user name.
  • http_basic_authentication_password - HTTP_BASIC authentication password.
  • verify_access_proc - controller based access (condition on request.remote_ip, current_user, etc.)
  • previous_limit - specify limit of records for history. Default: 6.
  • provider - Manually specify the login provider, one of Authlogic, Clearance, Devise, Sorcery, or a custom handler class name (as a string or symbol).

Advanced Options

If you want to display users grouped by role you can do it with:

# Initializer: config/initializers/any_login.rb
AnyLogin.setup do |config|
  config.collection_method = :grouped_collection_by_role
end

# User class: app/models/user.rb
class User < ActiveRecord::Base
  def self.grouped_collection_by_role
    {
      'admin'     => User.admins.limit(10),
      'moderator' => User.moderators.limit(10),
      'user'      => User.users.limit(10)
    }
  end
end

Or another sample:

# Initializer: config/initializers/any_login.rb
AnyLogin.setup do |config|
  config.collection_method = :grouped_users
  # to format user name in dropdown list
  config.name_method = :any_login_name
end

# User class: app/models/user.rb
class User < ActiveRecord::Base
  def any_login_name
    [full_name + ' - ' + email + " Domains: #{domains.collect(&:short_code).join(',').presence || 'none'}; Role: #{role}; ID: #{id}", id]
  end

  def self.grouped_users
    Organization.ordered.includes(:employees).inject({}) do |res, org|
      res[org.name] = org.employees.ordered.includes([:domains, :organization])
      res
    end
  end
end

Production

If you want to completely disable gem in production add following code in your config/environments/production.rb file.

AnyLogin.setup do |config|
  config.enabled = false
end

You can also try to debug your application in production and secure AnyLogin with HTTP_BASIC authentication. See Options sections for more details.

Future Plans

  1. Add tests
  2. Add support for more authentication gems like Devise, Authlogic, Clearance (restful_authentication, monban, letmein)

Contributors

  • @incubus
  • @bbenezech
  • @manastyretskyi
  • @saiqulhaq
  • @zorab47
  • @shivabhusal
  • @eliotsykes
  • @sviatoslav-krupa
  • @daichirata
  • @linshaodongsam
  • @jr180180
  • @vmyts539
  • @rbclark
  • @OskarsEzerins
  • @kyohah

For CI

Update gemspec:

bundle
BUNDLE_GEMFILE=./gemfiles/rails_6_1.gemfile bundle
BUNDLE_GEMFILE=./gemfiles/rails_6.gemfile bundle
BUNDLE_GEMFILE=./gemfiles/rails_7.gemfile bundle

Other

More Repositories

1

rails_db

Rails Database Viewer and SQL Query Runner
JavaScript
1,447
star
2

active_storage_validations

Do it like => validates :photos, attached: true, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: { less_than: 500.kilobytes }, limit: { min: 1, max: 3 }, aspect_ratio: :landscape, dimension: { width: { in: 800..1600 }
Ruby
959
star
3

rails_performance

Monitor performance of you Rails applications (self-hosted and free)
Ruby
793
star
4

log_analyzer

Rails logs analyzer (see how fast your views are rendering)
Ruby
349
star
5

rails_pdf

A reliable way to generate PDF of any complexity in Ruby on Rails apps
HTML
175
star
6

fake_api

The fastest way to prototype API in your Rails application
Ruby
142
star
7

execution_time

How fast is your code? See it directly in Rails console.
Ruby
111
star
8

benchmark_methods

Benchmark and measure execution time your Ruby methods without an additional code changes
Ruby
89
star
9

new_google_recaptcha

reCAPTCHA v3 Ruby on Rails gem
Ruby
82
star
10

transactify

Wrap your methods in DB Transactions
Ruby
55
star
11

sql_view

Rails SQL Views made easy ;)
Ruby
49
star
12

sweet_staging

Access your Rails console, see logs, execute rake commands directly from the browser. Great addition to your Staging ENV.
JavaScript
46
star
13

execute_sql

Execute SQL inside Rails console, or app itself
Ruby
41
star
14

cache_with_locale

Easy wait to do view caching with automatically added "locale" value to the cached key.
Ruby
37
star
15

avatarro

Generate google-style avatars in your application
Ruby
37
star
16

awesome_back_url

Redirect the user to the proper "back" page
Ruby
33
star
17

new_ckeditor

Ruby on Rails + CKEditor 5
Ruby
31
star
18

records_count

See in development logs how many records your queries returns. It can help with solving performance issues.
Ruby
31
star
19

amazon_static_site

Static website using https with your own domain name using Amazon S3 and Cloudflare for FREE
Ruby
29
star
20

omg_image

Generate PNG previews for HTML snippets (html/css/js). Any complexity.
Ruby
28
star
21

wrapped_print

Easy print debug information to your console in Ruby/Rails app.
Ruby
23
star
22

calculate_in_group

Group Active Record by ranges or set of values with a single SQL query.
Ruby
22
star
23

embed_view

Embed ERB files inside another ERB files for faster performance (5-20% BOOST!!!)
HTML
21
star
24

rails_time_travel

HTML
19
star
25

sabotage

Coding & debugging must be fun. Make life a bit harder for your colleagues :)
Ruby
18
star
26

mechanical

All models in a single table, new attributes without migrations. Works like regular AR model
Ruby
17
star
27

secrett11tto

Simple way to protect your content from copy-pasting
Ruby
15
star
28

rails_live

Ruby
15
star
29

mini-guard

Ruby
14
star
30

railsjazz.com

Rails Jazz (personal web site)
JavaScript
14
star
31

sidekiq_log_analyzer

SidekiqLogAnalyser gem allows to see summary of your sidekiq workers (based on log file).
Ruby
13
star
32

hasharay_ext

Painless work with complex Ruby hashes/arrays.
Ruby
13
star
33

active_storage_silent_logs

The idea of this gem is to hide as much as possible Active Storage logs from console so you can see only important information and requests
Ruby
13
star
34

rails_cached_method

Simple way to cache results of methods.
Ruby
11
star
35

with_record

Returns relations/association for soft deleted records in DB
Ruby
10
star
36

rrr

Run recent rspec files only (the only recently modified).
Ruby
9
star
37

font_awesome_file_icons

Ruby
4
star
38

unwhere

Ruby
4
star
39

travel_and_talk

JavaScript
3
star
40

lazy_mobile_tester

Rails Lazy Mobile Tester
Ruby
3
star
41

serpjazz

SERP keywords tracking
JavaScript
3
star
42

jeanappv2

JavaScript
2
star
43

mega-simple-authorization

mega simple authorization plugin for RoR
Ruby
2
star
44

tv

eb5 tv
JavaScript
2
star
45

spring_rspec_commands_addon

rails+spring+rspec = friends :)
Ruby
2
star
46

layouts_from_db_sample

Allow store layouts to DB (Sample)
2
star
47

CheaperDrinker

CheaperDrinker web site
JavaScript
2
star
48

any_login_test

AnyLogin gem test application
Ruby
2
star
49

tell_my_env

Ruby
2
star
50

slim_erb_backport

Slim 4+ and ERB friends again :)
Ruby
2
star
51

VerySimple

1
star
52

ShareT

online translations
JavaScript
1
star
53

test-ec2

test-ec2
1
star
54

sa1

1
star
55

better_tempfile

Ruby
1
star
56

just_for_fun

Do you want to call 42.to_user, [42, 43, 44].to_users? Try this gem :)
Ruby
1
star
57

tophouse.com.ua

JavaScript
1
star
58

ar_enumerations_test_application

ActiveRecord enumeration field type - test application
Ruby
1
star
59

capistranotest

1
star
60

zip_and_phone

Zips & Phones
Ruby
1
star
61

portfolio

1
star
62

tdemo

tdemo
Ruby
1
star
63

cisarska_and_frankivska

Cisarska & Frankivska
JavaScript
1
star
64

seminars

JavaScript
1
star
65

deprecations_collector

Save all Rails deprecation in log file for future investigation
Ruby
1
star
66

rails_logs

Ruby
1
star