fix: Add adminReview option to allow impersonating org members (#14275)

* added layouts for moving teams step

* added admin section for org creation step

* extended org creation to also move existing teams

* wip

* wip

* wip

* further changes

* Add checkout for org in onboarding

* Fix ts errors

* Self review feedback

* Self review addressed

* Fix unit tests

* Fix ts error

* Seans feedback addressed

* feat: fix correct accounts pending

* fix: unit tests for new invite member permissions

* tests: org admin onboarding tests for existing user

* tests: Inital user self serve flow

* chore: update teamAndUserFixture to create X amount of teams

* chore: add testId to card actionButton

* test: add test-Id to continue or checkout button

* tests: add tests for migrating existing teams

* feat: match new designs

* fix: isAdminCheck

* chore: fix pricing copy

* fix: flacky tests

* Fix tests?

* More test fixes

* Check all checkboxes

* Fix type error

* Fix failing test and typescript issues

* Fix unpaid org allowing auto-add users

* Add self-serve flag

* Skip tests

* Add adminReview option

* Fixes

* Add server checks for impersonation

* Improved error

* feat: extract - and disalow updating from org uset list if not reviewed

---------

Co-authored-by: Peer Richelsen <[email protected]>
Co-authored-by: Alex van Andel <[email protected]>
Co-authored-by: Peer Richelsen <[email protected]>
Co-authored-by: sean-brydon <[email protected]>
Co-authored-by: Joe Au-Yeung <[email protected]>
Co-authored-by: Joe Au-Yeung <[email protected]>
This commit is contained in:
authored and GitHub committed 2024-04-04 04:48:41 +00:00
1 parent 3f02de8214
commit d30fb213d2
14 files changed
+98 -18

No files matched your search

@@ -3,6 +3,7 @@ import type { Session } from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import { z } from "zod";
import { ensureOrganizationIsReviewed } from "@calcom/ee/organizations/lib/ensureOrganizationIsReviewed";
import { getSession } from "@calcom/features/auth/lib/getSession";
import { ProfileRepository } from "@calcom/lib/server/repository/profile";
import prisma from "@calcom/prisma";
@@ -281,7 +282,9 @@ const ImpersonationProvider = CredentialsProvider({
);
}
if (!teamId) throw new Error("You do not have permission to do this.");
await ensureOrganizationIsReviewed(session?.user.org?.id);
if (!teamId) throw new Error("Error-teamNotFound: You do not have permission to do this.");
// Check session
const sessionUserFromDb = await prisma.user.findUnique({
@@ -312,7 +315,7 @@ const ImpersonationProvider = CredentialsProvider({
});
if (sessionUserFromDb?.teams.length === 0 || impersonatedUser.teams.length === 0) {
throw new Error("You do not have permission to do this.");
throw new Error("Error-UserHasNoTeams: You do not have permission to do this.");
}
// We find team by ID so we know there is only one team in the array
@@ -329,6 +332,7 @@ const ImpersonationProvider = CredentialsProvider({
});
export default ImpersonationProvider;
async function findProfile(returningUser: { id: number; username: string | null }) {
const allOrgProfiles = await ProfileRepository.findAllProfilesForUserIncludingMovedUser({
id: returningUser.id,
@@ -0,0 +1,22 @@
import { OrganizationRepository } from "@calcom/lib/server/repository/organization";
/**
* It assumes that a user can only impersonate the members of the organization he is logged in to.
* Note: Ensuring that one organization's member can't impersonate other organization's member isn't the job of this function
*/
export async function ensureOrganizationIsReviewed(loggedInUserOrgId: number | undefined) {
if (loggedInUserOrgId) {
const org = await OrganizationRepository.findByIdIncludeOrganizationSettings({
id: loggedInUserOrgId,
});
if (!org) {
throw new Error("Error-OrgNotFound: You do not have permission to do this.");
}
if (!org.organizationSettings?.isAdminReviewed) {
// If the org is not reviewed, we can't allow impersonation
throw new Error("Error-OrgNotReviewed: You do not have permission to do this.");
}
}
}
@@ -78,14 +78,13 @@ function AdminOrgTable() {
<Header>
<ColumnTitle widthClassNames="w-auto">{t("organization")}</ColumnTitle>
<ColumnTitle widthClassNames="w-auto">{t("owner")}</ColumnTitle>
<ColumnTitle widthClassNames="w-auto">{t("verified")}</ColumnTitle>
<ColumnTitle widthClassNames="w-auto">{t("reviewed")}</ColumnTitle>
<ColumnTitle widthClassNames="w-auto">{t("dns_configured")}</ColumnTitle>
<ColumnTitle widthClassNames="w-auto">{t("published")}</ColumnTitle>
<ColumnTitle widthClassNames="w-auto">
<span className="sr-only">{t("edit")}</span>
</ColumnTitle>
</Header>
<Body>
{data.map((org) => (
<Row key={org.id}>
@@ -105,10 +104,10 @@ function AdminOrgTable() {
</Cell>
<Cell>
<div className="space-x-2">
{!org.organizationSettings?.isOrganizationVerified ? (
<Badge variant="red">{t("unverified")}</Badge>
{!org.organizationSettings?.isAdminReviewed ? (
<Badge variant="red">{t("unreviewed")}</Badge>
) : (
<Badge variant="blue">{t("verified")}</Badge>
<Badge variant="green">{t("reviewed")}</Badge>
)}
</div>
</Cell>
@@ -134,14 +133,17 @@ function AdminOrgTable() {
<div className="flex w-full justify-end">
<DropdownActions
actions={[
...(!org.organizationSettings?.isOrganizationVerified
...(!org.organizationSettings?.isAdminReviewed
? [
{
id: "verify",
label: t("verify"),
id: "review",
label: t("review"),
onClick: () => {
verifyMutation.mutate({
orgId: org.id,
updateMutation.mutate({
id: org.id,
organizationSettings: {
isAdminReviewed: true,
},
});
},
icon: "check" as const,