fix: Organization Migration - Prevent the redirected username from being claimed by others. (#13675)

* Check for migrated username

* Add e2e for reserved username due to migration

* Add username change test

* Update username.ts

* fixed type checks

* Refactor to use isOrganization

* Typing

* Typing

* Removed the isOrganization function

---------

Co-authored-by: Joe Au-Yeung <[email protected]>
Co-authored-by: Keith Williams <[email protected]>
This commit is contained in:
authored and GitHub committed 2024-03-05 17:08:44 +00:00
1 parent d8cee20d0c
commit a677d0d2c4
18 files changed
+216 -45

No files matched your search

+1 -5
View File
@@ -1,4 +1,4 @@
import type { Membership, Team } from "@calcom/prisma/client";
import type { Membership } from "@calcom/prisma/client";
import { MembershipRole } from "@calcom/prisma/enums";
export const enum ENTITY_PERMISSION_LEVEL {
@@ -19,10 +19,6 @@ export function canEditEntity(
);
}
export function isOrganization({ team }: { team: Pick<Team, "isOrganization"> }) {
return team.isOrganization;
}
export function getEntityPermissionLevel(
entity: {
userId: number | null;
+8 -1
View File
@@ -1,8 +1,10 @@
import slugify from "@calcom/lib/slugify";
import { ProfileRepository } from "./repository/profile";
import { isUsernameReservedDueToMigration } from "./username";
export async function checkRegularUsername(_username: string, currentOrgDomain?: string | null) {
const isCheckingUsernameInGlobalNamespace = !currentOrgDomain;
const username = slugify(_username);
const premium = !!process.env.NEXT_PUBLIC_IS_E2E && username.length < 5;
@@ -23,8 +25,13 @@ export async function checkRegularUsername(_username: string, currentOrgDomain?:
message: "A user exists with that username",
};
}
const isUsernameAvailable = isCheckingUsernameInGlobalNamespace
? !(await isUsernameReservedDueToMigration(username))
: true;
return {
available: true as const,
available: isUsernameAvailable,
premium,
};
}
+2
View File
@@ -3,4 +3,6 @@ import { IS_PREMIUM_USERNAME_ENABLED } from "@calcom/lib/constants";
import { checkRegularUsername } from "./checkRegularUsername";
import { usernameCheck as checkPremiumUsername } from "./username";
// TODO: Replace `lib/checkPremiumUsername` with `usernameCheck` and then import checkPremiumUsername directly here.
// We want to remove dependency on website for signup stuff as signup is now part of app.
export const checkUsername = !IS_PREMIUM_USERNAME_ENABLED ? checkRegularUsername : checkPremiumUsername;
+2 -3
View File
@@ -4,7 +4,6 @@ import { Prisma } from "@calcom/prisma/client";
import type { User as UserType } from "@calcom/prisma/client";
import type { UpId, UserProfile } from "@calcom/types/UserProfile";
import { isOrganization } from "../../entityPermissionUtils";
import logger from "../../logger";
import { safeStringify } from "../../safeStringify";
import { ProfileRepository } from "./profile";
@@ -92,8 +91,8 @@ export class UserRepository {
userId,
});
const acceptedOrgMemberships = acceptedTeamMemberships.filter((membership) =>
isOrganization({ team: membership.team })
const acceptedOrgMemberships = acceptedTeamMemberships.filter(
(membership) => membership.team.isOrganization
);
const organizations = acceptedOrgMemberships.map((membership) => membership.team);
+27 -3
View File
@@ -2,10 +2,13 @@ import type { NextApiRequest, NextApiResponse } from "next";
import slugify from "@calcom/lib/slugify";
import prisma from "@calcom/prisma";
import { RedirectType } from "@calcom/prisma/enums";
import { IS_PREMIUM_USERNAME_ENABLED } from "../constants";
import logger from "../logger";
import notEmpty from "../notEmpty";
const log = logger.getSubLogger({ prefix: ["server/username"] });
const cachedData: Set<string> = new Set();
export type RequestWithUsernameStatus = NextApiRequest & {
@@ -106,7 +109,9 @@ const usernameHandler =
return handler(req, res);
};
const usernameCheck = async (usernameRaw: string) => {
const usernameCheck = async (usernameRaw: string, currentOrgDomain?: string | null) => {
log.debug("usernameCheck", { usernameRaw, currentOrgDomain });
const isCheckingUsernameInGlobalNamespace = !currentOrgDomain;
const response = {
available: true,
premium: false,
@@ -129,6 +134,10 @@ const usernameCheck = async (usernameRaw: string) => {
if (user) {
response.available = false;
} else {
response.available = isCheckingUsernameInGlobalNamespace
? !(await isUsernameReservedDueToMigration(username))
: true;
}
if (await isPremiumUserName(username)) {
@@ -158,6 +167,20 @@ const usernameCheck = async (usernameRaw: string) => {
return response;
};
/**
* Should be used when in global namespace(i.e. outside of an organization)
*/
export const isUsernameReservedDueToMigration = async (username: string) =>
!!(await prisma.tempOrgRedirect.findUnique({
where: {
from_type_fromOrgId: {
type: RedirectType.User,
from: username,
fromOrgId: 0,
},
},
}));
/**
* It is a bit different from usernameCheck because it also check if the user signing up is the same user that has a pending invitation to organization
* So, it uses email to uniquely identify the user and then also checks if the username requested by that user is available for taking or not.
@@ -207,12 +230,13 @@ const usernameCheckForSignup = async ({
const isClaimingAlreadySetUsername = user.username === username;
const isClaimingUnsetUsername = !user.username;
response.available = isClaimingUnsetUsername || isClaimingAlreadySetUsername;
// There are no premium users outside an organization only
// There are premium users outside an organization only
response.premium = await isPremiumUserName(username);
}
// If user isn't found, it's a direct signup and that can't be of an organization
} else {
// If user isn't found, it's a new signup and that can't be of an organization
response.premium = await isPremiumUserName(username);
response.available = !(await isUsernameReservedDueToMigration(username));
}
// get list of similar usernames in the db