mirror of
https://github.com/documenso/documenso.git
synced 2025-11-14 00:32:43 +10:00
feat: admin member role updates (#2093)
This commit is contained in:
@ -39,6 +39,11 @@ export const getAdminOrganisation = async ({ organisationId }: GetOrganisationOp
|
||||
teams: true,
|
||||
members: {
|
||||
include: {
|
||||
organisationGroupMembers: {
|
||||
include: {
|
||||
group: true,
|
||||
},
|
||||
},
|
||||
user: {
|
||||
select: {
|
||||
id: true,
|
||||
|
||||
@ -3,6 +3,8 @@ import { z } from 'zod';
|
||||
import { ZOrganisationSchema } from '@documenso/lib/types/organisation';
|
||||
import OrganisationClaimSchema from '@documenso/prisma/generated/zod/modelSchema/OrganisationClaimSchema';
|
||||
import OrganisationGlobalSettingsSchema from '@documenso/prisma/generated/zod/modelSchema/OrganisationGlobalSettingsSchema';
|
||||
import OrganisationGroupMemberSchema from '@documenso/prisma/generated/zod/modelSchema/OrganisationGroupMemberSchema';
|
||||
import OrganisationGroupSchema from '@documenso/prisma/generated/zod/modelSchema/OrganisationGroupSchema';
|
||||
import OrganisationMemberSchema from '@documenso/prisma/generated/zod/modelSchema/OrganisationMemberSchema';
|
||||
import SubscriptionSchema from '@documenso/prisma/generated/zod/modelSchema/SubscriptionSchema';
|
||||
import TeamSchema from '@documenso/prisma/generated/zod/modelSchema/TeamSchema';
|
||||
@ -30,6 +32,18 @@ export const ZGetAdminOrganisationResponseSchema = ZOrganisationSchema.extend({
|
||||
email: true,
|
||||
name: true,
|
||||
}),
|
||||
organisationGroupMembers: z.array(
|
||||
OrganisationGroupMemberSchema.pick({
|
||||
id: true,
|
||||
groupId: true,
|
||||
}).extend({
|
||||
group: OrganisationGroupSchema.pick({
|
||||
id: true,
|
||||
type: true,
|
||||
organisationRole: true,
|
||||
}),
|
||||
}),
|
||||
),
|
||||
}).array(),
|
||||
subscription: SubscriptionSchema.nullable(),
|
||||
organisationClaim: OrganisationClaimSchema,
|
||||
|
||||
@ -17,6 +17,7 @@ import { promoteMemberToOwnerRoute } from './promote-member-to-owner';
|
||||
import { resealDocumentRoute } from './reseal-document';
|
||||
import { resetTwoFactorRoute } from './reset-two-factor-authentication';
|
||||
import { updateAdminOrganisationRoute } from './update-admin-organisation';
|
||||
import { updateOrganisationMemberRoleRoute } from './update-organisation-member-role';
|
||||
import { updateRecipientRoute } from './update-recipient';
|
||||
import { updateSiteSettingRoute } from './update-site-setting';
|
||||
import { updateSubscriptionClaimRoute } from './update-subscription-claim';
|
||||
@ -31,6 +32,7 @@ export const adminRouter = router({
|
||||
},
|
||||
organisationMember: {
|
||||
promoteToOwner: promoteMemberToOwnerRoute,
|
||||
updateRole: updateOrganisationMemberRoleRoute,
|
||||
},
|
||||
claims: {
|
||||
find: findSubscriptionClaimsRoute,
|
||||
|
||||
@ -0,0 +1,220 @@
|
||||
import { OrganisationGroupType, OrganisationMemberRole } from '@prisma/client';
|
||||
|
||||
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
||||
import { generateDatabaseId } from '@documenso/lib/universal/id';
|
||||
import { getHighestOrganisationRoleInGroup } from '@documenso/lib/utils/organisations';
|
||||
import { prisma } from '@documenso/prisma';
|
||||
|
||||
import { adminProcedure } from '../trpc';
|
||||
import {
|
||||
ZUpdateOrganisationMemberRoleRequestSchema,
|
||||
ZUpdateOrganisationMemberRoleResponseSchema,
|
||||
} from './update-organisation-member-role.types';
|
||||
|
||||
/**
|
||||
* Admin mutation to update organisation member role or transfer ownership.
|
||||
*
|
||||
* This mutation handles two scenarios:
|
||||
* 1. When role='OWNER': Transfers organisation ownership and promotes to ADMIN
|
||||
* 2. When role=ADMIN/MANAGER/MEMBER: Updates group membership
|
||||
*
|
||||
* Admin privileges bypass normal hierarchy restrictions.
|
||||
*/
|
||||
export const updateOrganisationMemberRoleRoute = adminProcedure
|
||||
.input(ZUpdateOrganisationMemberRoleRequestSchema)
|
||||
.output(ZUpdateOrganisationMemberRoleResponseSchema)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
const { organisationId, userId, role } = input;
|
||||
|
||||
ctx.logger.info({
|
||||
input: {
|
||||
organisationId,
|
||||
userId,
|
||||
role,
|
||||
},
|
||||
});
|
||||
|
||||
const organisation = await prisma.organisation.findUnique({
|
||||
where: {
|
||||
id: organisationId,
|
||||
},
|
||||
include: {
|
||||
groups: {
|
||||
where: {
|
||||
type: OrganisationGroupType.INTERNAL_ORGANISATION,
|
||||
},
|
||||
},
|
||||
members: {
|
||||
where: {
|
||||
userId,
|
||||
},
|
||||
include: {
|
||||
organisationGroupMembers: {
|
||||
include: {
|
||||
group: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!organisation) {
|
||||
throw new AppError(AppErrorCode.NOT_FOUND, {
|
||||
message: 'Organisation not found',
|
||||
});
|
||||
}
|
||||
|
||||
const [member] = organisation.members;
|
||||
|
||||
if (!member) {
|
||||
throw new AppError(AppErrorCode.NOT_FOUND, {
|
||||
message: 'User is not a member of this organisation',
|
||||
});
|
||||
}
|
||||
|
||||
const currentOrganisationRole = getHighestOrganisationRoleInGroup(
|
||||
member.organisationGroupMembers.flatMap((member) => member.group),
|
||||
);
|
||||
|
||||
if (role === 'OWNER') {
|
||||
if (organisation.ownerUserId === userId) {
|
||||
throw new AppError(AppErrorCode.INVALID_REQUEST, {
|
||||
message: 'User is already the owner of this organisation',
|
||||
});
|
||||
}
|
||||
|
||||
const currentMemberGroup = organisation.groups.find(
|
||||
(group) => group.organisationRole === currentOrganisationRole,
|
||||
);
|
||||
|
||||
const adminGroup = organisation.groups.find(
|
||||
(group) => group.organisationRole === OrganisationMemberRole.ADMIN,
|
||||
);
|
||||
|
||||
if (!currentMemberGroup) {
|
||||
ctx.logger.error({
|
||||
message: '[CRITICAL]: Missing internal group',
|
||||
organisationId,
|
||||
userId,
|
||||
role: currentOrganisationRole,
|
||||
});
|
||||
|
||||
throw new AppError(AppErrorCode.UNKNOWN_ERROR, {
|
||||
message: 'Current member group not found',
|
||||
});
|
||||
}
|
||||
|
||||
if (!adminGroup) {
|
||||
ctx.logger.error({
|
||||
message: '[CRITICAL]: Missing internal group',
|
||||
organisationId,
|
||||
userId,
|
||||
targetRole: 'ADMIN',
|
||||
});
|
||||
|
||||
throw new AppError(AppErrorCode.UNKNOWN_ERROR, {
|
||||
message: 'Admin group not found',
|
||||
});
|
||||
}
|
||||
|
||||
await prisma.$transaction(async (tx) => {
|
||||
await tx.organisation.update({
|
||||
where: {
|
||||
id: organisationId,
|
||||
},
|
||||
data: {
|
||||
ownerUserId: userId,
|
||||
},
|
||||
});
|
||||
|
||||
if (currentOrganisationRole !== OrganisationMemberRole.ADMIN) {
|
||||
await tx.organisationGroupMember.delete({
|
||||
where: {
|
||||
organisationMemberId_groupId: {
|
||||
organisationMemberId: member.id,
|
||||
groupId: currentMemberGroup.id,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await tx.organisationGroupMember.create({
|
||||
data: {
|
||||
id: generateDatabaseId('group_member'),
|
||||
organisationMemberId: member.id,
|
||||
groupId: adminGroup.id,
|
||||
},
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const targetRole = role as OrganisationMemberRole;
|
||||
|
||||
if (currentOrganisationRole === targetRole) {
|
||||
throw new AppError(AppErrorCode.INVALID_REQUEST, {
|
||||
message: 'User already has this role',
|
||||
});
|
||||
}
|
||||
|
||||
if (userId === organisation.ownerUserId && targetRole !== OrganisationMemberRole.ADMIN) {
|
||||
throw new AppError(AppErrorCode.INVALID_REQUEST, {
|
||||
message: 'Organisation owner must be an admin. Transfer ownership first.',
|
||||
});
|
||||
}
|
||||
|
||||
const currentMemberGroup = organisation.groups.find(
|
||||
(group) => group.organisationRole === currentOrganisationRole,
|
||||
);
|
||||
|
||||
const newMemberGroup = organisation.groups.find(
|
||||
(group) => group.organisationRole === targetRole,
|
||||
);
|
||||
|
||||
if (!currentMemberGroup) {
|
||||
ctx.logger.error({
|
||||
message: '[CRITICAL]: Missing internal group',
|
||||
organisationId,
|
||||
userId,
|
||||
role: currentOrganisationRole,
|
||||
});
|
||||
|
||||
throw new AppError(AppErrorCode.UNKNOWN_ERROR, {
|
||||
message: 'Current member group not found',
|
||||
});
|
||||
}
|
||||
|
||||
if (!newMemberGroup) {
|
||||
ctx.logger.error({
|
||||
message: '[CRITICAL]: Missing internal group',
|
||||
organisationId,
|
||||
userId,
|
||||
targetRole,
|
||||
});
|
||||
|
||||
throw new AppError(AppErrorCode.UNKNOWN_ERROR, {
|
||||
message: 'New member group not found',
|
||||
});
|
||||
}
|
||||
|
||||
await prisma.$transaction(async (tx) => {
|
||||
await tx.organisationGroupMember.delete({
|
||||
where: {
|
||||
organisationMemberId_groupId: {
|
||||
organisationMemberId: member.id,
|
||||
groupId: currentMemberGroup.id,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
await tx.organisationGroupMember.create({
|
||||
data: {
|
||||
id: generateDatabaseId('group_member'),
|
||||
organisationMemberId: member.id,
|
||||
groupId: newMemberGroup.id,
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,30 @@
|
||||
import { OrganisationMemberRole } from '@prisma/client';
|
||||
import { z } from 'zod';
|
||||
|
||||
/**
|
||||
* Admin-only role selection that includes OWNER as a special case.
|
||||
* OWNER is not a database role but triggers ownership transfer.
|
||||
*/
|
||||
export const ZAdminRoleSelection = z.enum([
|
||||
'OWNER',
|
||||
OrganisationMemberRole.ADMIN,
|
||||
OrganisationMemberRole.MANAGER,
|
||||
OrganisationMemberRole.MEMBER,
|
||||
]);
|
||||
|
||||
export type TAdminRoleSelection = z.infer<typeof ZAdminRoleSelection>;
|
||||
|
||||
export const ZUpdateOrganisationMemberRoleRequestSchema = z.object({
|
||||
organisationId: z.string().min(1),
|
||||
userId: z.number().min(1),
|
||||
role: ZAdminRoleSelection,
|
||||
});
|
||||
|
||||
export const ZUpdateOrganisationMemberRoleResponseSchema = z.void();
|
||||
|
||||
export type TUpdateOrganisationMemberRoleRequest = z.infer<
|
||||
typeof ZUpdateOrganisationMemberRoleRequestSchema
|
||||
>;
|
||||
export type TUpdateOrganisationMemberRoleResponse = z.infer<
|
||||
typeof ZUpdateOrganisationMemberRoleResponseSchema
|
||||
>;
|
||||
Reference in New Issue
Block a user