feat: Prevent watchlisted entities from creating bookings (#19107)
* Add `searchForAllBlockedRecords` to watchlist repository * Create `checkIfUsersAreBlocked` * Call `checkIfUsersAreBlocked` in `loadAndValidateUsers` * Draft test * Add tests * Type fix * Type fix
This commit is contained in:
1 parent
fc8ff2d4ae
commit
c0aa97178c
8 files changed
+366
-1
No files matched your search
@@ -0,0 +1,18 @@
|
||||
import prismock from "../../../../tests/libs/__mocks__/prisma";
|
||||
|
||||
import type { WatchlistSeverity, WatchlistType } from "@calcom/prisma/enums";
|
||||
|
||||
interface WatchlistInput {
|
||||
severity: WatchlistSeverity;
|
||||
type: WatchlistType;
|
||||
value: string;
|
||||
}
|
||||
|
||||
export const createWatchlistEntry = async (input: WatchlistInput) => {
|
||||
await prismock.watchlist.create({
|
||||
data: {
|
||||
...input,
|
||||
createdById: 0,
|
||||
},
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,35 @@
|
||||
import { startSpan } from "@sentry/nextjs";
|
||||
|
||||
import { WatchlistRepository } from "../watchlist.repository";
|
||||
|
||||
function presenter(containsBlockedUser: boolean) {
|
||||
return startSpan({ name: "checkIfUsersAreBlocked Presenter", op: "serialize" }, () => {
|
||||
return !!containsBlockedUser;
|
||||
});
|
||||
}
|
||||
|
||||
export async function checkIfUsersAreBlocked(
|
||||
users: { email: string; username: string | null; locked: boolean }[]
|
||||
): Promise<ReturnType<typeof presenter>> {
|
||||
const usernamesToCheck = [],
|
||||
emailsToCheck = [],
|
||||
domainsToCheck = [];
|
||||
|
||||
for (const user of users) {
|
||||
// If any user is locked, then return true
|
||||
if (user.locked) return true;
|
||||
const emailDomain = user.email.split("@")[1];
|
||||
if (user.username) usernamesToCheck.push(user.username);
|
||||
emailsToCheck.push(user.email);
|
||||
domainsToCheck.push(emailDomain);
|
||||
}
|
||||
|
||||
const watchlistRepository = new WatchlistRepository();
|
||||
const blockedRecords = await watchlistRepository.searchForAllBlockedRecords({
|
||||
usernames: usernamesToCheck,
|
||||
emails: emailsToCheck,
|
||||
domains: domainsToCheck,
|
||||
});
|
||||
|
||||
return presenter(blockedRecords.length > 0);
|
||||
}
|
||||
@@ -39,4 +39,60 @@ export class WatchlistRepository implements IWatchlistRepository {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns a boolean if any of the users passed are blocked */
|
||||
async searchForAllBlockedRecords({
|
||||
usernames,
|
||||
emails,
|
||||
domains,
|
||||
}: {
|
||||
usernames: string[];
|
||||
emails: string[];
|
||||
domains: string[];
|
||||
}) {
|
||||
try {
|
||||
const blockedRecords = await db.watchlist.findMany({
|
||||
where: {
|
||||
severity: WatchlistSeverity.CRITICAL,
|
||||
OR: [
|
||||
...(usernames.length > 0
|
||||
? [
|
||||
{
|
||||
type: WatchlistType.USERNAME,
|
||||
value: {
|
||||
in: usernames,
|
||||
},
|
||||
},
|
||||
]
|
||||
: []),
|
||||
...(emails.length > 0
|
||||
? [
|
||||
{
|
||||
type: WatchlistType.EMAIL,
|
||||
value: {
|
||||
in: emails,
|
||||
},
|
||||
},
|
||||
]
|
||||
: []),
|
||||
...(domains.length > 0
|
||||
? [
|
||||
{
|
||||
type: WatchlistType.DOMAIN,
|
||||
value: {
|
||||
in: domains,
|
||||
},
|
||||
},
|
||||
]
|
||||
: []),
|
||||
],
|
||||
},
|
||||
});
|
||||
return blockedRecords;
|
||||
} catch (err) {
|
||||
const captureException = (await import("@sentry/nextjs")).captureException;
|
||||
captureException(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user