add permission check to invite service
This commit is contained in:
1 parent
4ecc737438
commit
b4c929963f
2 files changed
+23
-4
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import type { Invite } from "@/types/index.js";
|
||||
import type { Invite, UserRole } from "@/types/index.js";
|
||||
import { canManageRole } from "@/types/user.js";
|
||||
import type { IInvitesRepository } from "@/repositories/index.js";
|
||||
import { AppError } from "@/utils/AppError.js";
|
||||
|
||||
@@ -29,8 +30,23 @@ class InviteService {
|
||||
return InviteService.SERVICE_NAME;
|
||||
}
|
||||
|
||||
getInviteToken = async ({ invite, teamId }: { invite: Partial<Invite>; teamId: string }) => {
|
||||
getInviteToken = async ({ invite, teamId, userRoles }: { invite: Partial<Invite>; teamId: string; userRoles: UserRole[] }) => {
|
||||
invite.teamId = teamId;
|
||||
|
||||
const inviteRoles = invite.role ?? [];
|
||||
|
||||
for (const targetRole of inviteRoles) {
|
||||
const canManage = userRoles.some((actorRole) => canManageRole(actorRole, targetRole));
|
||||
if (!canManage) {
|
||||
throw new AppError({
|
||||
message: "You do not have permission to create this invite",
|
||||
service: SERVICE_NAME,
|
||||
method: "getInviteToken",
|
||||
status: 403,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const inviteToken = await this.invitesRepository.create(invite);
|
||||
return inviteToken;
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user