fix: End Impersonation Without Logging Out Admin (#25395)

Co-authored-by: sean-brydon <55134778+sean-brydon@users.noreply.github.com>
This commit is contained in:
Anshumancanrock
2025-11-29 21:38:31 +00:00
committed by GitHub
co-authored by sean-brydon
parent 8fcbf2c167
commit 7dbc888848
2 changed files with 9 additions and 19 deletions
@@ -12,29 +12,21 @@ function ImpersonatingBanner({ data }: ImpersonatingBannerProps) {
if (!data?.user.impersonatedBy) return null;
const returnToId = data.user.impersonatedBy.id;
const canReturnToSelf = data.user.impersonatedBy.role == "ADMIN" || data.user?.org?.id;
return (
<>
<TopBanner
text={t("impersonating_user_warning", { user: data.user.orgAwareUsername || data.user.username })}
variant="warning"
actions={
canReturnToSelf ? (
<form
onSubmit={(e) => {
e.preventDefault();
signIn("impersonation-auth", { returnToId });
}}>
<button className="text-emphasis hover:underline" data-testid="stop-impersonating-button">
{t("impersonating_stop_instructions")}
</button>
</form>
) : (
<a className="border-b border-b-black" href="/auth/logout">
<form
onSubmit={(e) => {
e.preventDefault();
signIn("impersonation-auth", { returnToId });
}}>
<button className="text-emphasis hover:underline" data-testid="stop-impersonating-button">
{t("impersonating_stop_instructions")}
</a>
)
</button>
</form>
}
/>
</>
@@ -301,13 +301,11 @@ async function isReturningToSelf({ session, creds }: { session: Session | null;
});
if (returningUser) {
// Skip for none org users
const inOrg =
returningUser.organizationId || // Keep for backwards compatibility
returningUser.profiles.some((profile) => profile.organizationId !== undefined); // New way of seeing if the user has a profile in orgs.
if (returningUser.role !== UserPermissionRole.ADMIN && !inOrg) return;
const hasTeams = returningUser.teams.length >= 1;
if (returningUser.role !== UserPermissionRole.ADMIN && !inOrg && !hasTeams) return;
const profile = await findProfile(returningUser);
return {