refactor(impersonation): move user selection into action flow
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
parent
d46f480825
commit
6efe23a9e4
2 changed files with 48 additions and 15 deletions
48
src/app/(frontend)/impersonate/actions.ts
Normal file
48
src/app/(frontend)/impersonate/actions.ts
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
"use server";
|
||||||
|
|
||||||
|
import config from "@payload-config";
|
||||||
|
import { isPayloadUser } from "@/utils/access-control/isPayloadUser";
|
||||||
|
import { getPayload } from "payload";
|
||||||
|
import { isSuperuser } from "@/utils/access-control/hasPermission";
|
||||||
|
import { headers } from "next/headers";
|
||||||
|
|
||||||
|
async function authenticate() {
|
||||||
|
const payloadConfig = await config;
|
||||||
|
const payload = await getPayload({ config: payloadConfig });
|
||||||
|
const hdrs = await headers();
|
||||||
|
const { user } = await payload.auth({
|
||||||
|
headers: hdrs,
|
||||||
|
canSetHeaders: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!isPayloadUser(user)) {
|
||||||
|
throw new Error("Unauthorized");
|
||||||
|
}
|
||||||
|
|
||||||
|
return { payload, user };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getImpersonationUsers(): Promise<Array<{
|
||||||
|
id: number | string;
|
||||||
|
username?: string | null;
|
||||||
|
displayName?: string | null;
|
||||||
|
}>> {
|
||||||
|
const { payload, user } = await authenticate();
|
||||||
|
|
||||||
|
if (!(await isSuperuser(payload, user))) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return await payload.find({
|
||||||
|
collection: "users",
|
||||||
|
depth: 0,
|
||||||
|
limit: 1000,
|
||||||
|
pagination: false,
|
||||||
|
sort: "username",
|
||||||
|
select: {
|
||||||
|
username: true,
|
||||||
|
displayName: true,
|
||||||
|
},
|
||||||
|
overrideAccess: true,
|
||||||
|
}).then((result) => result.docs);
|
||||||
|
}
|
||||||
|
|
@ -1,15 +0,0 @@
|
||||||
import { redirect } from "next/navigation";
|
|
||||||
import { getPayload } from "payload";
|
|
||||||
import config from "@payload-config";
|
|
||||||
import { ImpersonationControl } from "@/components/frontend/impersonation/ImpersonationControl";
|
|
||||||
import { findImpersonationUsers } from "@/lib/impersonation";
|
|
||||||
import { isSuperuser } from "@/utils/access-control/hasPermission";
|
|
||||||
import { headers } from "next/headers";
|
|
||||||
|
|
||||||
export default async function ImpersonatePage() {
|
|
||||||
const payload = await getPayload({ config });
|
|
||||||
const { user } = await payload.auth({ headers: await headers(), canSetHeaders: false });
|
|
||||||
if (!user || !(await isSuperuser(payload, user))) redirect("/");
|
|
||||||
const users = await findImpersonationUsers(payload);
|
|
||||||
return <main className="flex-1 space-y-6 p-6 md:p-8"><ImpersonationControl users={users.docs as Parameters<typeof ImpersonationControl>[0]["users"]} /></main>;
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue