fix: locked users admin list and bypass query (#17902)

This commit is contained in:
Omar López authored and GitHub committed 2024-11-28 21:45:40 +00:00
1 parent f2efd6dc28
commit 249bcaca68
2 files changed
+18 -6

No files matched your search

@@ -25,6 +25,14 @@ export function excludeLockedUsersExtension() {
}); });
} }
function safeJSONStringify(x: any) {
try {
return JSON.stringify(x);
} catch {
return "";
}
}
async function excludeLockedUsers( async function excludeLockedUsers(
args: args:
| Prisma.UserFindUniqueArgs<InternalArgs & DefaultArgs> | Prisma.UserFindUniqueArgs<InternalArgs & DefaultArgs>
@@ -35,8 +43,10 @@ async function excludeLockedUsers(
query: <T>(args: T) => Promise<unknown> query: <T>(args: T) => Promise<unknown>
) { ) {
args.where = args.where || {}; args.where = args.where || {};
const whereString = safeJSONStringify(args.where);
const shouldIncludeLocked = whereString.includes('"locked":');
// Unless explicitly specified, we exclude locked users // Unless explicitly specified, we exclude locked users
if (args.where.locked === undefined) { if (!shouldIncludeLocked) {
args.where.locked = false; args.where.locked = false;
} }
return query(args); return query(args);
@@ -17,9 +17,12 @@ const listPaginatedHandler = async ({ input }: GetOptions) => {
const getTotalUsers = await prisma.user.count(); const getTotalUsers = await prisma.user.count();
let searchFilters: Prisma.UserWhereInput = {}; let searchFilters: Prisma.UserWhereInput = {};
const bothLockedAndUnlockedWhere = { OR: [{ locked: false }, { locked: true }] };
if (searchTerm) { if (searchTerm) {
searchFilters = { searchFilters = {
// To bypass the excludeLockedUsersExtension
AND: bothLockedAndUnlockedWhere,
OR: [ OR: [
{ {
email: { email: {
@@ -33,16 +36,15 @@ const listPaginatedHandler = async ({ input }: GetOptions) => {
}, },
], ],
}; };
} else {
// To bypass the excludeLockedUsersExtension
searchFilters = bothLockedAndUnlockedWhere;
} }
const users = await prisma.user.findMany({ const users = await prisma.user.findMany({
cursor: cursor ? { id: cursor } : undefined, cursor: cursor ? { id: cursor } : undefined,
take: limit + 1, // We take +1 as itll be used for the next cursor take: limit + 1, // We take +1 as itll be used for the next cursor
where: { where: searchFilters,
...searchFilters,
// To bypass the excludeLockedUsersExtension
OR: [{ locked: false }, { locked: true }],
},
orderBy: { orderBy: {
id: "asc", id: "asc",
}, },