:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(settings): guard project move by target org admin level (#66959)
Christiaan Hendriksen1184088767ec
diff --git a/frontend/src/scenes/settings/project/ProjectMove.test.tsx b/frontend/src/scenes/settings/project/ProjectMove.test.tsxnew file mode 100644index 000000000000..e5a1ce59bb96--- /dev/null+++ b/frontend/src/scenes/settings/project/ProjectMove.test.tsx@@ -0,0 +1,90 @@+import { MOCK_DEFAULT_ORGANIZATION, MOCK_DEFAULT_TEAM, MOCK_DEFAULT_USER } from 'lib/api.mock'++import '@testing-library/jest-dom'++import { cleanup, render, screen } from '@testing-library/react'+import userEvent from '@testing-library/user-event'++import { OrganizationMembershipLevel } from 'lib/constants'+import { projectLogic } from 'scenes/projectLogic'+import { teamLogic } from 'scenes/teamLogic'+import { userLogic } from 'scenes/userLogic'++import { initKeaTests } from '~/test/init'+import { OrganizationBasicType } from '~/types'++import { ProjectMove } from './ProjectMove'++// Render LemonSelect as a native <select> so each option's disabled state is assertable from the DOM+// without driving the (portal-rendered) popover. We only override LemonSelect; the other Lemon components stay real.+jest.mock('@posthog/lemon-ui', () => ({+ ...jest.requireActual('@posthog/lemon-ui'),+ LemonSelect: ({ options, value, onChange, placeholder }: any): JSX.Element => (+ <select+ aria-label="target organization"+ value={value === undefined ? '' : String(value)}+ onChange={(e) => onChange?.(options.find((o: any) => String(o.value) === e.target.value)?.value)}+ >+ <option value="">{placeholder}</option>+ {options.map((o: any) => (+ <option key={String(o.value)} value={String(o.value)} disabled={!!o.disabledReason}>+ {o.label}+ </option>+ ))}+ </select>+ ),+}))++const targetOrg = (overrides: Partial<OrganizationBasicType>): OrganizationBasicType => ({+ ...MOCK_DEFAULT_ORGANIZATION,+ ...overrides,+})++describe('<ProjectMove />', () => {+ const adminTargetOrg = targetOrg({+ id: 'org-admin',+ name: 'Admin Target Org',+ membership_level: OrganizationMembershipLevel.Admin,+ })+ const memberTargetOrg = targetOrg({+ id: 'org-member',+ name: 'Member Target Org',+ membership_level: OrganizationMembershipLevel.Member,+ })++ beforeEach(() => {+ initKeaTests()+ userLogic.mount()+ teamLogic.mount()+ projectLogic.mount()+ // Admin of the current team so the source-org guard (`restrictedReason`) passes and the target guard is what's under test.+ teamLogic.actions.loadCurrentTeamSuccess({+ ...MOCK_DEFAULT_TEAM,+ effective_membership_level: OrganizationMembershipLevel.Admin,+ })+ userLogic.actions.loadUserSuccess({+ ...MOCK_DEFAULT_USER,+ organization: MOCK_DEFAULT_ORGANIZATION,+ organizations: [MOCK_DEFAULT_ORGANIZATION, adminTargetOrg, memberTargetOrg],+ })+ projectLogic.actions.loadCurrentProjectSuccess({ id: 1, name: 'Source Project' } as any)+ })++ afterEach(() => {+ cleanup()+ })++ it('disables target organizations the user is not an admin of', () => {+ const { container } = render(<ProjectMove />)+ expect(screen.getByRole('option', { name: 'Admin Target Org' })).toBeEnabled()+ expect(screen.getByRole('option', { name: 'Member Target Org' })).toBeDisabled()+ // No org picked yet, so the Move button is blocked.+ expect(container.querySelector('[data-attr="move-project-button"]')).toHaveAttribute('aria-disabled', 'true')+ })++ it('enables the Move button once an admin-eligible target org is selected', async () => {+ const { container } = render(<ProjectMove />)+ await userEvent.selectOptions(screen.getByLabelText('target organization'), 'org-admin')+ expect(container.querySelector('[data-attr="move-project-button"]')).toHaveAttribute('aria-disabled', 'false')+ })+})diff --git a/frontend/src/scenes/settings/project/ProjectMove.tsx b/frontend/src/scenes/settings/project/ProjectMove.tsxindex 2c229aafee96..084124eed4ae 100644--- a/frontend/src/scenes/settings/project/ProjectMove.tsx+++ b/frontend/src/scenes/settings/project/ProjectMove.tsx@@ -71,6 +71,12 @@ export function MoveProjectModal({)}+const NOT_ADMIN_OF_TARGET_REASON = 'You need to be an admin or owner of this organization to move a project into it'++function notAdminOfOrg(organization: OrganizationBasicType): boolean {+ return (organization.membership_level ?? OrganizationMembershipLevel.Member) < OrganizationMembershipLevel.Admin+}+export function ProjectMove(): JSX.Element {const { currentProject } = useValues(projectLogic)const { otherOrganizations } = useValues(userLogic)@@ -84,6 +90,12 @@ export function ProjectMove(): JSX.Element {})const { moveProjectDisabledReason } = useValues(projectLogic)+ // Moving a project requires admin (or owner) on BOTH organizations — the API enforces this. The source org is+ // covered by `restrictedReason` above; guard the target org here so a member of the target org gets a clear+ // message instead of a failed request.+ const targetOrgRestrictionReason =+ targetOrganization && notAdminOfOrg(targetOrganization) ? NOT_ADMIN_OF_TARGET_REASON : null+return (<><p>@@ -95,6 +107,7 @@ export function ProjectMove(): JSX.Element {options={otherOrganizations.map((o) => ({label: o.name,value: o.id,+ disabledReason: notAdminOfOrg(o) ? NOT_ADMIN_OF_TARGET_REASON : undefined,}))}placeholder="Select target organization"onChange={(value) => {@@ -114,7 +127,9 @@ export function ProjectMove(): JSX.Element {disabledReason={restrictedReason ??moveProjectDisabledReason ??- (targetOrganization === null && 'Please select the target organization')+ (targetOrganization === null+ ? 'Please select the target organization'+ : (targetOrgRestrictionReason ?? undefined))}>Move {currentProject?.name || 'the current project'}