1
0
Fork 0
polaris-task-force/src/lib/impersonation.ts

46 lines
1.1 KiB
TypeScript

import type { Payload } from "payload";
export const IMPERSONATION_COOKIE = "ptf-impersonation-original";
export const IMPERSONATION_ACTIVE_COOKIE = "ptf-impersonation-active";
export type ImpersonationUser = {
id: number | string;
username?: string | null;
displayName?: string | null;
};
export function getImpersonationCookieOptions(maxAge: number) {
return {
httpOnly: true,
sameSite: "lax" as const,
secure: process.env.NODE_ENV === "production",
path: "/",
maxAge,
};
}
export function userLabel(user: ImpersonationUser): string {
return user.displayName || user.username || `User ${user.id}`;
}
export function isImpersonationTarget(
operator: ImpersonationUser,
target: ImpersonationUser,
): boolean {
return String(operator.id) !== String(target.id);
}
export async function findImpersonationUsers(payload: Payload) {
return payload.find({
collection: "users",
depth: 0,
limit: 1000,
pagination: false,
sort: "username",
select: {
username: true,
displayName: true,
},
overrideAccess: true,
});
}