{"id":13747269,"url":"https://github.com/oivoodoo/devise_masquerade","last_synced_at":"2025-04-10T19:32:21.393Z","repository":{"id":5559094,"uuid":"6763948","full_name":"oivoodoo/devise_masquerade","owner":"oivoodoo","description":"Extension for devise, enable login as functionality. Add link to the masquerade_path(resource) and use it.","archived":false,"fork":false,"pushed_at":"2025-03-23T08:20:36.000Z","size":270,"stargazers_count":542,"open_issues_count":14,"forks_count":45,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-03T09:53:09.783Z","etag":null,"topics":["devise","devise-gem-extension","gem","rails","ruby","ruby-gem","ruby-on-rails","ruby-rails","rubyonrails"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/oivoodoo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"patreon":"oivoodoo"}},"created_at":"2012-11-19T17:05:03.000Z","updated_at":"2025-03-23T08:20:40.000Z","dependencies_parsed_at":"2022-07-07T02:05:07.753Z","dependency_job_id":"bf080cda-4cf7-4c86-bc49-d2aafe7bb0bd","html_url":"https://github.com/oivoodoo/devise_masquerade","commit_stats":{"total_commits":228,"total_committers":29,"mean_commits":7.862068965517241,"dds":0.3157894736842105,"last_synced_commit":"c8e8a3c79f1f717ebae377d6861d73b8410e4380"},"previous_names":[],"tags_count":56,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oivoodoo%2Fdevise_masquerade","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oivoodoo%2Fdevise_masquerade/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oivoodoo%2Fdevise_masquerade/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oivoodoo%2Fdevise_masquerade/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oivoodoo","download_url":"https://codeload.github.com/oivoodoo/devise_masquerade/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248281415,"owners_count":21077423,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["devise","devise-gem-extension","gem","rails","ruby","ruby-gem","ruby-on-rails","ruby-rails","rubyonrails"],"created_at":"2024-08-03T06:01:23.411Z","updated_at":"2025-04-10T19:32:21.365Z","avatar_url":"https://github.com/oivoodoo.png","language":"Ruby","readme":"# Devise Masquerade\n\n [![Gitter chat](https://badges.gitter.im/oivoodoo/devise_masquerade.svg)](https://gitter.im/oivoodoo/devise_masquerade?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Foivoodoo%2Fdevise_masquerade.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Foivoodoo%2Fdevise_masquerade?ref=badge_shield)\n\n[![Build Status](https://secure.travis-ci.org/oivoodoo/devise_masquerade.svg?branch=master)](https://travis-ci.org/oivoodoo/devise_masquerade)\n\n[![Maintainability](https://api.codeclimate.com/v1/badges/cf63d775dc014a7ebc03/maintainability)](https://codeclimate.com/github/oivoodoo/devise_masquerade/maintainability)\n\n[Consulting](https://bitscorp.co)\n\nIt's a utility library for enabling functionallity like login as button for\nadmin.\n\nIf you have multi users application and sometimes you want to test functionally\nusing login of existing user without requesting the password, define login as\nbutton with url helper and use it.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n    gem 'devise_masquerade'\n\nAnd then execute:\n\n    $ bundle\n\n## Usage\n\nIn the view you can use url helper for defining link:\n\n```ruby\n    = link_to \"Login As\", masquerade_path(user)\n```\n\n`masquerade_path` would create specific `/masquerade` path with query params `masquerade`(key) and `masqueraded_resource_class` to know\nwhich model to choose to search and sign in by masquerade key.\n\nIn the model you'll need to add the parameter :masqueradable to the existing comma separated values in the devise method:\n\n```ruby\n    devise :invitable, :confirmable, :database_authenticatable, :registerable, :masqueradable\n```\n\nAdd into your `application_controller.rb` if you want to have custom way on sign in by using masquerade token otherwise you can still\nuse only `masquerade_path` in your view to generate temporary token and link to make `Login As`:\n\n```ruby\n    before_action :masquerade_user!\n```\n\nor\n\n```ruby\n    before_action :masquerade!\n```\n\n`masquerade!` is generic way in case if you want to support multiple models on masquerade.\n\nInstead of user you can use your resource name admin, student or another names.\n\nIf you want to back to the owner of masquerade action user you could use\nhelpers:\n\n```ruby\n    user_masquerade? # current user was masqueraded by owner?\n\n    = link_to \"Reverse masquerade\", back_masquerade_path(current_user)\n```\n\n## Custom controller for adding cancan for authorization\n\n```ruby\n    class Admin::MasqueradesController \u003c Devise::MasqueradesController\n      def show\n        super\n      end\n\n      protected\n\n      def masquerade_authorize!\n        authorize!(:masquerade, User)\n      end\n\n      # or you can define:\n      # def masquerade_authorized?\n      #   \u003chas access to something?\u003e (true/false)\n      # end\n    end\n```\n\n## Alternatively using Pundit\n\nController:\n\n```ruby\n    class Admin::MasqueradesController \u003c Devise::MasqueradesController\n      protected\n\n      def masquerade_authorize!\n        authorize(User, :masquerade?) unless params[:action] == 'back'\n      end\n    end\n```\n\nIn your view:\n\n```erb\n    \u003c% if policy(@user).masquerade? %\u003e\n      \u003c%= link_to \"Login as\", masquerade_path(@user) %\u003e\n    \u003c% end %\u003e\n```\n\n## Custom url redirect after masquerade:\n\n```ruby\n    class Admin::MasqueradesController \u003c Devise::MasqueradesController\n      protected\n\n      def after_masquerade_path_for(resource)\n        \"/custom_url\"\n      end\n    end\n```\n\n## Custom url redirect after finishing masquerade:\n\n```ruby\n    class Admin::MasqueradesController \u003c Devise::MasqueradesController\n      protected\n\n      def after_back_masquerade_path_for(resource)\n        \"/custom_url\"\n      end\n    end\n```\n\n## Overriding the finder\n\nFor example, if you use FriendlyId:\n\n```ruby\n    class Admin::MasqueradesController \u003c Devise::MasqueradesController\n      protected\n\n      def find_masqueradable_resource\n        masqueraded_resource_class.friendly.find(params[:id])\n      end\n    end\n```\n\n#### Dont forget to update your Devise routes to point at your Custom Authorization Controller\nin `routes.rb`:\n\n```ruby\n    devise_for :users, controllers: { masquerades: \"admin/masquerades\" }\n```\n\n## You can redefine few options:\n\n```ruby\n    Devise.masquerade_param = 'masquerade'\n    Devise.masquerade_expires_in = 10.seconds\n    Devise.masquerade_key_size = 16 # size of the generate by SecureRandom.urlsafe_base64\n    Devise.masquerade_bypass_warden_callback = false\n    Devise.masquerade_routes_back = false # if true, route back to the page the user was on via redirect_back\n    Devise.masquerading_resource_class = AdminUser\n    # optional: Devise.masquerading_resource_class_name = 'AdminUser'\n\n    # optional, default: masquerading_resource_class.model_name.param_key\n    Devise.masquerading_resource_name = :admin_user\n\n    Devise.masqueraded_resource_class = User\n    # optional: Devise.masqueraded_resource_class_name = 'User'\n\n    # optional, default: masqueraded_resource_class.model_name.param_key\n    Devise.masqueraded_resource_name = :user\n\n    # optional, default: masquerade_storage_method = :session\n    # values: :session, :cache\n    Devise.masquerade_storage_method = :session\n```\n\n## Demo project\n\n    cd spec/dummy\n    rake db:setup\n    rails server\n\nAnd check http://localhost:3000/, use for login user1@example.com and\n'password'\n\n## Troubleshooting\n\nAre you working in development mode and wondering why masquerade attempts result in a [Receiving \"You are already signed in\" flash[:error]](https://github.com/oivoodoo/devise_masquerade/issues/58) 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:\n\n    rails dev:cache\n\nThis 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.\n\n## Test project\n\n    make test\n\n## Contributing\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Add some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n\n\n## License\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Foivoodoo%2Fdevise_masquerade.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Foivoodoo%2Fdevise_masquerade?ref=badge_large)\n","funding_links":["https://patreon.com/oivoodoo"],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foivoodoo%2Fdevise_masquerade","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foivoodoo%2Fdevise_masquerade","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foivoodoo%2Fdevise_masquerade/lists"}