Git Product home page Git Product logo

devise_masquerade's Introduction

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

devise_masquerade's People

Contributors

bbuchalter avatar dbalatero avatar dbastin avatar dependabot[bot] avatar excid3 avatar fjg avatar fossabot avatar gitter-badger avatar gorkunov avatar jeanmertz avatar leastbad avatar mattwoodage avatar mbrien12 avatar mcyoung avatar mikedebock avatar oivoodoo avatar olleolleolle avatar pedantic-git avatar project-eutopia avatar rnestler avatar rraub avatar shioyama avatar skazi0 avatar sogoodday avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.