• Stars
    star
    503
  • Rank 84,414 (Top 2 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 11 years ago
  • Updated 11 months ago

Reviews

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

Repository Details

Extension for devise, enable login as functionality. Add link to the masquerade_path(resource) and use it.

Devise Masquerade

Gitter chat

FOSSA Status

Build Status

Maintainability

Consulting

It's a utility library for enabling functionallity like login as button for admin.

If you have multi users application and sometimes you want to test functionally using login of existing user without requesting the password, define login as button with url helper and use it.

Installation

Add this line to your application's Gemfile:

gem 'devise_masquerade'

And then execute:

$ bundle

Usage

In the view you can use url helper for defining link:

    = link_to "Login As", masquerade_path(user)

masquerade_path would create specific /masquerade path with query params masquerade(key) and masqueraded_resource_class to know which model to choose to search and sign in by masquerade key.

In the model you'll need to add the parameter :masqueradable to the existing comma separated values in the devise method:

    devise :invitable, :confirmable, :database_authenticatable, :registerable, :masqueradable

Add into your application_controller.rb if you want to have custom way on sign in by using masquerade token otherwise you can still use only masquerade_path in your view to generate temporary token and link to make Login As:

    before_action :masquerade_user!

or

    before_action :masquerade!

masquerade! is generic way in case if you want to support multiple models on masquerade.

Instead of user you can use your resource name admin, student or another names.

If you want to back to the owner of masquerade action user you could use helpers:

    user_masquerade? # current user was masqueraded by owner?

    = link_to "Reverse masquerade", back_masquerade_path(current_user)

Custom controller for adding cancan for authorization

    class Admin::MasqueradesController < Devise::MasqueradesController
      def show
        super
      end

      protected

      def masquerade_authorize!
        authorize!(:masquerade, User)
      end

      # or you can define:
      # def masquerade_authorized?
      #   <has access to something?> (true/false)
      # end
    end

Alternatively using Pundit

Controller:

    class Admin::MasqueradesController < Devise::MasqueradesController
      protected

      def masquerade_authorize!
        authorize(User, :masquerade?) unless params[:action] == 'back'
      end
    end

In your view:

    <% if policy(@user).masquerade? %>
      <%= link_to "Login as", masquerade_path(@user) %>
    <% end %>

Custom url redirect after masquerade:

    class Admin::MasqueradesController < Devise::MasqueradesController
      protected

      def after_masquerade_path_for(resource)
        "/custom_url"
      end
    end

Custom url redirect after finishing masquerade:

    class Admin::MasqueradesController < Devise::MasqueradesController
      protected

      def after_back_masquerade_path_for(resource)
        "/custom_url"
      end
    end

Overriding the finder

For example, if you use FriendlyId:

    class Admin::MasqueradesController < Devise::MasqueradesController
      protected

      def find_masqueradable_resource
        masqueraded_resource_class.friendly.find(params[:id])
      end
    end

Dont forget to update your Devise routes to point at your Custom Authorization Controller

in routes.rb:

    devise_for :users, controllers: { masquerades: "admin/masquerades" }

You can redefine few options:

    Devise.masquerade_param = 'masquerade'
    Devise.masquerade_expires_in = 10.seconds
    Devise.masquerade_key_size = 16 # size of the generate by SecureRandom.urlsafe_base64
    Devise.masquerade_bypass_warden_callback = false
    Devise.masquerade_routes_back = false # if true, route back to the page the user was on via redirect_back
    Devise.masquerading_resource_class = AdminUser
    # optional: Devise.masquerading_resource_class_name = 'AdminUser'

    # optional, default: masquerading_resource_class.model_name.param_key
    Devise.masquerading_resource_name = :admin_user

    Devise.masqueraded_resource_class = User
    # optional: Devise.masqueraded_resource_class_name = 'User'

    # optional, default: masqueraded_resource_class.model_name.param_key
    Devise.masqueraded_resource_name = :user

    # optional, default: masquerade_storage_method = :session
    # values: :session, :cache
    Devise.masquerade_storage_method = :session

Demo project

cd spec/dummy
rake db:setup
rails server

And check http://localhost:3000/, use for login [email protected] and 'password'

Troubleshooting

Are you working in development mode and wondering why masquerade attempts result in a Receiving "You are already signed in" flash[:error] message? Filter chain halted as :require_no_authentication rendered or redirected showing up in your logfile? Do you find that your user_masquerade? method is always returning false? Chances are that you need to enable caching:

rails dev:cache

This is a one-time operation, so you can set it and forget it. Should you ever need to disable caching in development, you can re-run the command as required.

Test project

make test

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

FOSSA Status

More Repositories

1

tomato-app

JavaScript
46
star
2

angular-image-spinner

Awesome image spinner using angular and spin.js libraries.
CoffeeScript
22
star
3

redis_poolex

Redis pool using poolboy(connection pool) and exredis(redis client).
Elixir
20
star
4

exfacebook

Facebook API
Elixir
18
star
5

backbone-queues

Local queues for backbone. Lib is saving queues to the localStorage.
JavaScript
15
star
6

mongoid-report

Ruby
4
star
7

dotvim

Vim Script
4
star
8

mono-clish

C# Mono implementation of http://clish.sourceforge.net/ project.
C#
4
star
9

itrainer

nodejs application
JavaScript
3
star
10

cakejs-tetris

Tetris you can find in the chrome webstore
JavaScript
3
star
11

home

home website
CSS
3
star
12

deployments-app

deployments - app it's a sinatra application, you can run it as standalone app on the heroku or attach to the existing rails application.
Ruby
2
star
13

VooNetworkDemo

Pseudo parallel client and server for sending files.
Java
2
star
14

VooFTPServer

Java
2
star
15

3DCube-Shadow-OpenGL

Cube with shadow using stencil buffer.
1
star
16

MoonWalker-OpenGL

C#
1
star
17

projects

projects
Ruby
1
star
18

godot-kenney_import

Godot addon to make kenney atlas import
GDScript
1
star
19

humangram

humangram
Ruby
1
star
20

redirect-app

Ruby
1
star
21

tiltbrush-json-convertor-server

Convert JSON to OBJ and FBX as web app.
CSS
1
star
22

rails-api-demo

rails-api-demo project
Ruby
1
star
23

dreamydays

old old old
Ruby
1
star
24

zipme

Rails app for generating short links(backbonejs, offline, coffee, cucumber, rspec)
Ruby
1
star
25

dreamy

dreamy
JavaScript
1
star
26

promist

JavaScript
1
star
27

timer.js

timer.js for smart js polling
JavaScript
1
star
28

exgoogle

Elixir
1
star
29

Voodoo-Admin-Plugin

Voodoo CMS Plugin
Ruby
1
star
30

sinatra-template

Sinatra, Sprockets
Ruby
1
star
31

rails3-subdomain-example

Ruby
1
star
32

simple-conf

Simple - conf library only for loading yml file from the config/ folder without any dependencies to the web applications.
Ruby
1
star
33

deployments

gem for posting deployments details(tag, commits, env, username) to the path for generating reports.
Ruby
1
star
34

mini-canvas-game

15 boxes and 1 blank. It's mini canvas game. I have created this only for demo project.
JavaScript
1
star
35

csv-redis-example

Ruby
1
star
36

Demo-Machine

Cache, Stack, Memory, CPU and very simple assembler based on the VHDL language, only for demo.
1
star
37

versionius

generate version.txt file for the selected git directory, for example: public/version.txt
Ruby
1
star
38

voo-university

Project try to implement basic university management program.
C#
1
star
39

hacker-news-app

I have created this project just for trying to use twitter-boostrap, sinatra, heroku
JavaScript
1
star
40

MoonWalker

Demo graphic of alg. Brezenhema.
C++
1
star
41

gist-app

gist app for sharing gists on the tumblr.
Ruby
1
star
42

estates

JavaScript
1
star
43

RS485_Network_Demo_Library

RS485 token ring implementation
1
star