Impersonation and programmatical login #236
-
Hello, I want to provide an ability for admins to impersonate an existing account. # frozen_string_literal: true
class RodauthImpersonate < RodauthBase
configure do
enable :login, :logout, :internal_request
session_key_prefix "impersonate_"
# ==> Redirects
# Redirect to home page after logout.
logout_redirect '/'
# Redirect user to their company page after login. Works only if `login_return_to_requested_location_path` is nil.
login_redirect { rails_routes.company_path(rails_account.member.company) }
end
end And call it like this: def impersonate
rodauth = Rodauth::Rails.rodauth(:impersonate, account: Member.find(params[:id]).account)
rodauth.login('impersonate')
redirect_to company_url(resource_member.company)
end I have no errors, but it simply does not work. Do you have any ideas on implementing this and what I am doing wrong? Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 11 replies
-
Yeah, this example won't work, because I don't know what's the best way to implement this. My gut tells me that a separate configuration isn't ideal, because then in places where you're calling e.g. def impersonate
rodauth(:admin).require_account # only admins can impersonate accounts
account = Member.find(params[:id]).account
rodauth.account_from_login(account.email)
rodauth.login("impersonate")
# if main configuration is using multifactor authentication
rodauth.two_factor_update_session("impersonate-two") if rodauth.uses_two_factor_authentication?
redirect_to company_url(resource_member.company)
end And then in your layout you could add a banner or indication notifying the admin that they're impersonating another user: <% if rodauth(:admin).authenticated? && rodauth.authenticated? %>
<div>You're impersonating user <%= rodauth.rails_account.email %></div>
<% end %> When logging out from impersonated account, you just need to be careful to avoid clearing the admin session as well, because by default the whole session is cleared. I don't remember now what's the recommended way to do that. |
Beta Was this translation helpful? Give feedback.
-
Sorry for disturbing you again. I decided to combine my original idea with yours. So I still have a separate configuration to impersonate users. It still seems to me, that it would be easier, but again I faced problems. This works: account = Member.find(params[:id]).account
rodauth.account_from_login(account.email)
rodauth.login("impersonate")
# the line below will never be executed, because `login` makes a redirect. Probably better to use `login_session`
rodauth.two_factor_update_session("impersonate-2fa") if rodauth(:impersonate).uses_two_factor_authentication? However this does not work and account = Member.find(params[:id]).account
rodauth(:impersonate).account_from_login(account.email)
rodauth(:impersonate).login_session("impersonate")
rodauth(:impersonate).two_factor_update_session("impersonate-2fa") if rodauth(:impersonate).uses_two_factor_authentication? Don't really have any ideas where is the problem. |
Beta Was this translation helpful? Give feedback.
-
This impersonate feature is very similar to the become_account feature. |
Beta Was this translation helpful? Give feedback.
Ok, I found a solution. As I written above
clear_session
deleted all session keys, no matter what configuration created them.My main configuration had
remeber
feature enabled. So here is what happened:clear_session
was called, and removed all session keysremember
feature from the main configuration checked that session is not valid (it didn't exist) and restored it and calledclear_session
again, which cleared impersonated user session.Don't know is it a bug or not, but for now I fixed this by redefining
clear_session
.