fix: prevent self-hosted auto-enroll from downgrading admins/tutors to students
This commit is contained in:
@@ -4,6 +4,7 @@ import {
|
|||||||
createOrganizationMember,
|
createOrganizationMember,
|
||||||
getFirstOrganization,
|
getFirstOrganization,
|
||||||
getOrganizationByProfileId,
|
getOrganizationByProfileId,
|
||||||
|
getUserOrgRolesMap,
|
||||||
hasOrgMemberByProfileIdOrEmail
|
hasOrgMemberByProfileIdOrEmail
|
||||||
} from '@cio/db/queries/organization';
|
} from '@cio/db/queries/organization';
|
||||||
import { getPlanLimit, toResourceUsage } from '@cio/utils/plans';
|
import { getPlanLimit, toResourceUsage } from '@cio/utils/plans';
|
||||||
@@ -50,7 +51,12 @@ export async function getAccountData(userId: string): Promise<GetAccountDataResu
|
|||||||
|
|
||||||
// Self-hosted: auto-add user as student to the single org if they are not a member.
|
// Self-hosted: auto-add user as student to the single org if they are not a member.
|
||||||
// Skip if they already have membership (by profileId) or a pending invite (by email).
|
// Skip if they already have membership (by profileId) or a pending invite (by email).
|
||||||
|
// Also skip if the user is an admin/tutor in any org — they should not be downgraded to student.
|
||||||
if (env.PUBLIC_IS_SELFHOSTED === 'true' && organizations.length === 0) {
|
if (env.PUBLIC_IS_SELFHOSTED === 'true' && organizations.length === 0) {
|
||||||
|
const orgRoles = await getUserOrgRolesMap(userId);
|
||||||
|
const isTeamMember = Object.values(orgRoles).some((roleId) => roleId === ROLE.ADMIN || roleId === ROLE.TUTOR);
|
||||||
|
|
||||||
|
if (!isTeamMember) {
|
||||||
const firstOrg = await getFirstOrganization();
|
const firstOrg = await getFirstOrganization();
|
||||||
if (firstOrg) {
|
if (firstOrg) {
|
||||||
const alreadyInOrg = await hasOrgMemberByProfileIdOrEmail(firstOrg.id, userId, profile.email ?? undefined);
|
const alreadyInOrg = await hasOrgMemberByProfileIdOrEmail(firstOrg.id, userId, profile.email ?? undefined);
|
||||||
@@ -67,6 +73,7 @@ export async function getAccountData(userId: string): Promise<GetAccountDataResu
|
|||||||
organizations = await getOrganizationByProfileId(userId);
|
organizations = await getOrganizationByProfileId(userId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Attach per-resource usage + plan limits for admin/tutor members only.
|
// Attach per-resource usage + plan limits for admin/tutor members only.
|
||||||
// Plain COUNTs (no rows) so the payload stays light and students never
|
// Plain COUNTs (no rows) so the payload stays light and students never
|
||||||
|
|||||||
Reference in New Issue
Block a user