diff --git a/app/scenes/Settings/Authentication.tsx b/app/scenes/Settings/Authentication.tsx index 023e44a604..2697ce9551 100644 --- a/app/scenes/Settings/Authentication.tsx +++ b/app/scenes/Settings/Authentication.tsx @@ -17,6 +17,7 @@ import Text from "~/components/Text"; import env from "~/env"; import useConsumeQueryParam from "~/hooks/useConsumeQueryParam"; import useCurrentTeam from "~/hooks/useCurrentTeam"; +import usePolicy from "~/hooks/usePolicy"; import useRequest from "~/hooks/useRequest"; import useStores from "~/hooks/useStores"; import SettingRow from "./components/SettingRow"; @@ -33,6 +34,7 @@ import { VStack } from "~/components/primitives/VStack"; function Authentication() { const { authenticationProviders, dialogs } = useStores(); const team = useCurrentTeam(); + const can = usePolicy(team); const { t } = useTranslation(); const theme = useTheme(); @@ -193,129 +195,139 @@ function Authentication() { {t("Sign In")} - {authenticationProviders.orderedData.map((provider) => ( - - - {provider.displayName} - - } - name={provider.name} - description={ - provider.isConnected - ? t("Allow members to sign-in with {{ authProvider }}", { - authProvider: provider.displayName, - }) - : t("Connect {{ authProvider }} to allow members to sign-in", { - authProvider: provider.displayName, - }) - } - border={!(provider.isActive && provider.groupSyncSupported)} - > - - {provider.isConnected ? ( - + {authenticationProviders.orderedData.map((provider) => { + // Group sync can still be disabled after losing the entitlement. + const showGroupSync = + provider.isActive && + provider.groupSyncSupported && + (!!can.syncGroups || !!provider.settings?.groupSyncEnabled); + + return ( + + + {provider.displayName} + + } + name={provider.name} + description={ + provider.isConnected + ? t("Allow members to sign-in with {{ authProvider }}", { + authProvider: provider.displayName, + }) + : t( + "Connect {{ authProvider }} to allow members to sign-in", + { + authProvider: provider.displayName, + } + ) + } + border={!showGroupSync} + > + + {provider.isConnected ? ( + + + + {provider.providerId} + + + ) : ( - - {provider.providerId} - - - ) : ( - - )} - - - {provider.isActive && provider.groupSyncSupported && ( - - {provider.groupSyncRequiresSetup && - !provider.settings?.groupSyncEnabled ? ( - - ) : ( - - handleToggleGroupSync(provider, checked) - } - /> - )} - - )} - {provider.isActive && - provider.groupSyncSupported && - provider.groupSyncUsesClaim && - provider.settings?.groupSyncEnabled && ( - + + {showGroupSync && ( + - ) => { - const value = ev.target.value.trim(); - if (value !== (provider.settings?.groupClaim ?? "")) { - void handleGroupClaimChange(provider, value); + {provider.groupSyncRequiresSetup && + !provider.settings?.groupSyncEnabled ? ( + + ) : ( + + handleToggleGroupSync(provider, checked) } - }} - /> + /> + )} )} - - ))} + {showGroupSync && + provider.groupSyncUsesClaim && + provider.settings?.groupSyncEnabled && ( + + ) => { + const value = ev.target.value.trim(); + if (value !== (provider.settings?.groupClaim ?? "")) { + void handleGroupClaimChange(provider, value); + } + }} + /> + + )} + + ); + })} diff --git a/server/policies/team.ts b/server/policies/team.ts index aeb21cce9f..ac9cc565e9 100644 --- a/server/policies/team.ts +++ b/server/policies/team.ts @@ -59,6 +59,14 @@ allow(User, "audit", Team, (actor, team) => ) ); +allow(User, "syncGroups", Team, (actor, team) => + and( + // + isTeamAdmin(actor, team), + teamHasEntitlement(team, PlanFeature.GroupSync) + ) +); + allow(User, ["createTemplate", "updateTemplate"], Team, (actor, team) => and( // diff --git a/server/queues/tasks/SyncUserGroupsTask.test.ts b/server/queues/tasks/SyncUserGroupsTask.test.ts index cd86121a69..3af1f29af2 100644 --- a/server/queues/tasks/SyncUserGroupsTask.test.ts +++ b/server/queues/tasks/SyncUserGroupsTask.test.ts @@ -1,8 +1,10 @@ import { vi } from "vitest"; +import { Plan } from "@shared/types"; import { AuthenticationProvider, Group, GroupUser, + Team, UserAuthentication, } from "@server/models"; import { buildUser } from "@server/test/factories"; @@ -38,6 +40,10 @@ async function setup() { describe("SyncUserGroupsTask", () => { const schedule = mockTaskSchedule(); + beforeEach(() => { + vi.spyOn(Team.prototype, "plan", "get").mockReturnValue(Plan.Business); + }); + afterEach(() => { vi.restoreAllMocks(); }); @@ -104,6 +110,17 @@ describe("SyncUserGroupsTask", () => { expect(fetchUserGroups).not.toHaveBeenCalled(); }); + it("should not sync groups when the team is not entitled to group sync", async () => { + const { authentication, fetchUserGroups } = await setup(); + vi.spyOn(Team.prototype, "plan", "get").mockReturnValue(Plan.Community); + + await new SyncUserGroupsTask().perform({ + userAuthenticationId: authentication.id, + }); + + expect(fetchUserGroups).not.toHaveBeenCalled(); + }); + it("should throw when the provider fails so the task is retried", async () => { const { authentication, fetchUserGroups } = await setup(); fetchUserGroups.mockRejectedValue(new Error("provider unavailable")); diff --git a/server/queues/tasks/SyncUserGroupsTask.ts b/server/queues/tasks/SyncUserGroupsTask.ts index 6830281d99..bbeac46854 100644 --- a/server/queues/tasks/SyncUserGroupsTask.ts +++ b/server/queues/tasks/SyncUserGroupsTask.ts @@ -1,4 +1,5 @@ import type { JobOptions } from "bull"; +import { PlanFeature } from "@shared/types"; import groupsSyncer from "@server/commands/groupsSyncer"; import { createContext } from "@server/context"; import Logger from "@server/logging/Logger"; @@ -8,6 +9,7 @@ import { User, UserAuthentication, } from "@server/models"; +import { teamHasEntitlement } from "@server/policies/utils"; import { sequelize } from "@server/storage/database"; import { PluginManager } from "@server/utils/PluginManager"; import { BaseTask, TaskPriority } from "./base/BaseTask"; @@ -21,8 +23,8 @@ type Props = { * Fetches a user's group memberships from the authentication provider linked * to the given user authentication and synchronizes them with internal * groups. This is a no-op when group sync is not enabled for the provider, - * no access token is stored, or the provider plugin does not support group - * sync. + * the team is not entitled to group sync, no access token is stored, or the + * provider plugin does not support group sync. */ export default class SyncUserGroupsTask extends BaseTask { /** @@ -80,7 +82,7 @@ export default class SyncUserGroupsTask extends BaseTask { } const team = await Team.findByPk(user.teamId); - if (!team) { + if (!team || !teamHasEntitlement(team, PlanFeature.GroupSync)) { return; } diff --git a/server/routes/api/authenticationProviders/authenticationProviders.test.ts b/server/routes/api/authenticationProviders/authenticationProviders.test.ts index 24fbfe08e6..46846f994a 100644 --- a/server/routes/api/authenticationProviders/authenticationProviders.test.ts +++ b/server/routes/api/authenticationProviders/authenticationProviders.test.ts @@ -1,8 +1,10 @@ import { randomUUID } from "node:crypto"; +import type { MockInstance } from "vitest"; import { vi } from "vitest"; import sharedEnv from "@shared/env"; +import { Plan } from "@shared/types"; import env from "@server/env"; -import { AuthenticationProvider } from "@server/models"; +import { AuthenticationProvider, Team } from "@server/models"; import { buildUser, buildAdmin, buildTeam } from "@server/test/factories"; import { getTestServer, setSelfHosted } from "@server/test/support"; import { PluginManager } from "@server/utils/PluginManager"; @@ -61,6 +63,18 @@ describe("#authenticationProviders.info", () => { }); describe("#authenticationProviders.update", () => { + let planSpy: MockInstance<() => Plan>; + + beforeEach(() => { + planSpy = vi + .spyOn(Team.prototype, "plan", "get") + .mockReturnValue(Plan.Business); + }); + + afterEach(() => { + planSpy.mockRestore(); + }); + it("requires the admin setup flow before enabling group sync", async () => { const team = await buildTeam(); const user = await buildAdmin({ teamId: team.id }); @@ -97,6 +111,65 @@ describe("#authenticationProviders.update", () => { } }); + it("requires the group sync entitlement to enable group sync", async () => { + const team = await buildTeam(); + const user = await buildAdmin({ teamId: team.id }); + const provider = await team.$create("authenticationProvider", { + name: "oidc", + providerId: randomUUID(), + }); + + planSpy.mockReturnValue(Plan.Community); + const getGroupSyncProvider = vi + .spyOn(PluginManager, "getGroupSyncProvider") + .mockReturnValue({ + useGroupClaim: true, + fetchUserGroups: async () => [], + }); + + try { + const res = await server.post( + "/api/authenticationProviders.update", + user, + { + body: { + id: provider.id, + settings: { groupSyncEnabled: true }, + }, + } + ); + + expect(res.status).toEqual(403); + const reloaded = await AuthenticationProvider.findByPk(provider.id); + expect(reloaded?.settings?.groupSyncEnabled).not.toBe(true); + } finally { + getGroupSyncProvider.mockRestore(); + } + }); + + it("allows disabling group sync without the group sync entitlement", async () => { + const team = await buildTeam(); + const user = await buildAdmin({ teamId: team.id }); + const provider = await team.$create("authenticationProvider", { + name: "oidc", + providerId: randomUUID(), + settings: { groupSyncEnabled: true }, + }); + + planSpy.mockReturnValue(Plan.Community); + + const res = await server.post("/api/authenticationProviders.update", user, { + body: { + id: provider.id, + settings: { groupSyncEnabled: false }, + }, + }); + + expect(res.status).toEqual(200); + const reloaded = await AuthenticationProvider.findByPk(provider.id); + expect(reloaded?.settings?.groupSyncEnabled).toBe(false); + }); + it("should not allow admins to disable when last authentication provider", async () => { const team = await buildTeam({ guestSignin: false, @@ -163,6 +236,18 @@ describe("#authenticationProviders.update", () => { }); describe("#authenticationProviders.startGroupSync", () => { + let planSpy: MockInstance<() => Plan>; + + beforeEach(() => { + planSpy = vi + .spyOn(Team.prototype, "plan", "get") + .mockReturnValue(Plan.Business); + }); + + afterEach(() => { + planSpy.mockRestore(); + }); + it("returns the provider setup URL for a workspace admin", async () => { const team = await buildTeam(); const user = await buildAdmin({ teamId: team.id }); @@ -198,6 +283,38 @@ describe("#authenticationProviders.startGroupSync", () => { } }); + it("requires the group sync entitlement", async () => { + const team = await buildTeam(); + const user = await buildAdmin({ teamId: team.id }); + const provider = await team.$create("authenticationProvider", { + name: "azure", + providerId: randomUUID(), + }); + const startGroupSync = vi + .fn() + .mockResolvedValue("https://example.com/consent"); + planSpy.mockReturnValue(Plan.Community); + const getGroupSyncProvider = vi + .spyOn(PluginManager, "getGroupSyncProvider") + .mockReturnValue({ + useGroupClaim: false, + startGroupSync, + fetchUserGroups: async () => [], + }); + + try { + const res = await server.post( + "/api/authenticationProviders.startGroupSync", + user, + { body: { id: provider.id } } + ); + expect(res.status).toEqual(403); + expect(startGroupSync).not.toHaveBeenCalled(); + } finally { + getGroupSyncProvider.mockRestore(); + } + }); + it("rejects providers without an admin setup flow", async () => { const team = await buildTeam(); const user = await buildAdmin({ teamId: team.id }); diff --git a/server/routes/api/authenticationProviders/authenticationProviders.ts b/server/routes/api/authenticationProviders/authenticationProviders.ts index fafb1ecebe..06ebfdcf45 100644 --- a/server/routes/api/authenticationProviders/authenticationProviders.ts +++ b/server/routes/api/authenticationProviders/authenticationProviders.ts @@ -57,6 +57,8 @@ router.post( settings?.groupSyncEnabled === true && !authenticationProvider.settings?.groupSyncEnabled ) { + authorize(user, "syncGroups", user.team); + const groupSyncProvider = PluginManager.getGroupSyncProvider( authenticationProvider.name ); @@ -106,6 +108,7 @@ router.post( ctx.input.body.id ); authorize(user, "update", authenticationProvider); + authorize(user, "syncGroups", user.team); const groupSyncProvider = PluginManager.getGroupSyncProvider( authenticationProvider.name