Skip to content

Commit

Permalink
Merge pull request #8101 from sagemathinc/fix-admin-impersonate-lang_…
Browse files Browse the repository at this point in the history
…temp

frontend/admin: set the user UI lang temporarily to the one of the admin
  • Loading branch information
williamstein authored Jan 8, 2025
2 parents 3efea2b + 7f31812 commit 9c52117
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
11 changes: 8 additions & 3 deletions src/packages/frontend/admin/users/impersonate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

import { Alert, Card } from "antd";
import { join } from "path";

import { Rendered, useEffect, useState } from "@cocalc/frontend/app-framework";
import { Icon, Loading } from "@cocalc/frontend/components";
import { appBasePath } from "@cocalc/frontend/customize/app-base-path";
import { webapp_client } from "@cocalc/frontend/webapp-client";
import { CopyToClipBoard } from "@cocalc/frontend/components";
import { useLocalizationCtx } from "@cocalc/frontend/app/localize";

interface Props {
account_id: string;
Expand All @@ -21,11 +23,13 @@ export function Impersonate({ first_name, last_name, account_id }: Props) {
const [auth_token, set_auth_token] = useState<string | null>(null);
const [err, set_err] = useState<string | null>(null);
const [extraWarning, setExtraWarning] = useState<boolean>(false);
const { locale } = useLocalizationCtx();

async function get_token(): Promise<void> {
try {
const auth_token =
await webapp_client.admin_client.get_user_auth_token(account_id);
const auth_token = await webapp_client.admin_client.get_user_auth_token(
account_id,
);
set_auth_token(auth_token);
set_err(null);
} catch (err) {
Expand All @@ -43,9 +47,10 @@ export function Impersonate({ first_name, last_name, account_id }: Props) {
return <Loading />;
}

// The lang_temp temporarily sets the interface language of the user to impersonate to the one of the admin
const link = join(
appBasePath,
`auth/impersonate?auth_token=${auth_token}&lang_temp=en`,
`auth/impersonate?auth_token=${auth_token}&lang_temp=${locale}`,
);

const handleClick = (event: React.MouseEvent<HTMLAnchorElement>) => {
Expand Down
20 changes: 14 additions & 6 deletions src/packages/server/auth/impersonate.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/* Sign in using an impersonation auth_token. */

import getPool from "@cocalc/database/pool";
import { createRememberMeCookie } from "@cocalc/server/auth/remember-me";
import Cookies from "cookies";

import { REMEMBER_ME_COOKIE_NAME } from "@cocalc/backend/auth/cookie-names";
import clientSideRedirect from "@cocalc/server/auth/client-side-redirect";
import { getServerSettings } from "@cocalc/database/settings/server-settings";
import base_path from "@cocalc/backend/base-path";
import getPool from "@cocalc/database/pool";
import { getServerSettings } from "@cocalc/database/settings/server-settings";
import clientSideRedirect from "@cocalc/server/auth/client-side-redirect";
import { createRememberMeCookie } from "@cocalc/server/auth/remember-me";
import { isLocale } from "@cocalc/util/i18n/const";

export async function signInUsingImpersonateToken({ req, res }) {
try {
Expand All @@ -17,7 +19,7 @@ export async function signInUsingImpersonateToken({ req, res }) {
}

async function doIt({ req, res }) {
const { auth_token } = req.query;
const { auth_token, lang_temp } = req.query;
if (!auth_token) {
throw Error("invalid empty token");
}
Expand All @@ -40,7 +42,13 @@ async function doIt({ req, res }) {
});

const { dns } = await getServerSettings();
const target = `https://${dns}${base_path}`;
let target = `https://${dns}${base_path}app`;

// if lang_temp is a locale, then append it as a query parameter.
// This is usally "en" to help admins understanding the UI without changing the user's language preferences.
if (isLocale(lang_temp)) {
target += `?lang_temp=${encodeURIComponent(lang_temp)}`;
}

clientSideRedirect({ res, target });
}

0 comments on commit 9c52117

Please sign in to comment.