-
-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Masquerade with Admin and User models and authenticated routes #72
Comments
Update. So I got a little further on the issue This concern is to make sure that a logged in Admin cannot
#access the user sign in page and vice versa.
#Doing so would mess up the authenticity tokens
module Accessible
extend ActiveSupport::Concern
included do
before_action :check_user
end
protected
#If a user or admin is already logged in and they try to access
#the sign in page of another user type, we simply redirect
#them to their corresponding root page
def check_user
if current_user
flash.clear
redirect_to(authenticated_user_path) && return
end
end
end And then this concern is used in the different sign in controllers for the Admin user and the regular User. So I added a skip_before_action on the Users SessionsController to make sure it would not do that class Users::SessionsController < Devise::SessionsController
include Accessible
skip_before_action :check_user, only: [:destroy, :create]
# GET /resource/sign_in
# def new
# super
# end
# POST /resource/sign_in
# def create
# super
# end
# DELETE /resource/sign_out
# def destroy
# super
# end
# protected
# If you have extra params to permit, append them to the sanitizer.
# def configure_sign_in_params
# devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute])
# end
end Now the masquerade process goes further but it now stops at the user sign in and says user not found. It still detects that the masquerading process has been started bassed on the flash message up top in yellow but it can't find the user because based on the application logs, it is still trying to load the Admin user, not the selected user to masquerade.
Definitely getting closer. But any advice or other things to try would be appreciated. |
Hi @PBSITProjects @DanGrenier . Somehow I missed the issue notification for the gem. Sorry to hear that you have the issues with lib. Have you checked the https://github.com/oivoodoo/devise_masquerade/tree/master/spec/dummy it's having the example application for integration test https://github.com/oivoodoo/devise_masquerade/blob/master/features/multiple_masquerading_models.feature . if you see any possible differences in usage, please write me back . I will try to cover it by test cases. For now I am going to give a try to research the problem. |
Hi @oivoodoo . Thanks for the reply. I looked at the dummy application and while it's similar to what I have, It's not quite the same. You can take a look at it here If you seed the database, it will create one Admin and one User. |
Hi @DanGrenier . it was great debugging using your app. Thank you! Please give a try |
Hi @oivoodoo |
Going to make PR for your demo project, yesterday I believe I tested a back button as well. Write you back during the day. |
No rush. I'll try to figure this one out on my own in the meantime. |
OK so I was able to figure this one out. First, to take care of the user_masquerade? not working and therefore not showing up the div that warns you are masquerading and showing the back path, All I had to do was to enable cache in my development environment per the documentation. But then I was faced with an authorization issue coming from CanCan. So in my AdminAbility file I had the following
In my MasqueradesController, I have the following
And then in my ApplicationController, as per CanCan best practice, I set the current_ability instance variable depending on whether the logged-in user is an admin or regular user
I was able to masquerade as a user. But when clicking on the logout button which performs the masquerade_back action, I was getting a "You are not authorized to access this page" that's coming from CanCan and I was still logged in as the masqueraded user but lost the ability to click on the masquerade_back action. After I removed the masquerade_authorize! method from my masquerades controller, it worked. So it looks as if when going through the masquerades controller the first time (as an Admin) it works but then when going through it again (as a User) that's where the authorization gets triggered |
I'm hitting a similar issue (can start masquerading, but trying to logout returns the not authorized error @DanGrenier referenced. This only started happening after a Rails 7 app update from Rails 6.1.3. Commenting out the |
I have seen similar issues to what I am experiencing but the suggested solutions don't seem to be working.
I have a Rails app with 2 devise models. Admin and User
I also use CanCan for authorization.
The app is set up to use authenticated routes and also authenticate users before accessing routes (instead of using a before_action in a controller to check whether the user is signed in or not)
So the routes for devise are
And then the authenticated routes are
This just shows a different root (default) page once an Admin or User is signed in. If no one is signed in, it`s a different root page with the login screen etc
Then the regular routes are set up like the following to ensure the user or admin be authenticated before accessing those routes
I have added masquerade to my Gemfile and added masqueradable to the User model.
I also added the following code to the devise.rb in the initializers folder
In my AdminAbility file for CanCan, I added
Then I created a view on the Admin side of the application that lists the users with a link using the following code, where u is the user object
When I click the link, it just goes back to the root of the application, and thus, shows me the root page of the user I was already signed in as (The Admin user).
If I add the following to the application.html.erb
The message appears after I click the link, but I still get the admin root page and not the user root page. If I try to access current_user in that div above, I get the undefined method for nil:NilClass error, suggesting that current_user is not initiated.
When I check the application log, I noticed the following (There are a few debug messages that I put in there to make sure the classes and names were correct)
So from the looks of it, It tries to access the users/sign_in page in order to sign in as the chosen user but that doesn't seem to be happening and it looks like it's just reloading the Admin user (based on the SQL showing up).
Not sure if it's related to the authenticated routes. If so, is there a way to set that up to work?
If not, what else can I try to get this to behave the way it is intended?
Let me know what I can debug/trace/provide in order to find out what's going on.
Thank you
The text was updated successfully, but these errors were encountered: