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,5 +1,6 @@
|
|||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, NextFunction } from "express";
|
||||||
import { inviteBodyValidation, inviteVerificationBodyValidation } from "@/validation/joi.js";
|
import { inviteBodyValidation, inviteVerificationBodyValidation } from "@/validation/joi.js";
|
||||||
|
import { requireTeamId, requireUserRoles } from "@/controllers/controllerUtils.js";
|
||||||
const SERVICE_NAME = "inviteController";
|
const SERVICE_NAME = "inviteController";
|
||||||
|
|
||||||
class InviteController {
|
class InviteController {
|
||||||
@@ -14,12 +15,14 @@ class InviteController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getInviteToken = async (req: Request, res: Response, next: NextFunction) => {
|
getInviteToken = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
|
console.log(req.body);
|
||||||
try {
|
try {
|
||||||
|
const teamId = requireTeamId(req.user?.teamId);
|
||||||
|
const userRoles = requireUserRoles(req.user?.role);
|
||||||
const invite = req.body;
|
const invite = req.body;
|
||||||
const teamId = req?.user?.teamId;
|
|
||||||
invite.teamId = teamId;
|
invite.teamId = teamId;
|
||||||
await inviteBodyValidation.validateAsync(invite);
|
await inviteBodyValidation.validateAsync(invite);
|
||||||
const inviteToken = await this.inviteService.getInviteToken({ invite, teamId });
|
const inviteToken = await this.inviteService.getInviteToken({ invite, teamId, userRoles });
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Invite token generated successfully",
|
msg: "Invite token generated successfully",
|
||||||
|
|||||||
@@ -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 type { IInvitesRepository } from "@/repositories/index.js";
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
|
|
||||||
@@ -29,8 +30,23 @@ class InviteService {
|
|||||||
return InviteService.SERVICE_NAME;
|
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;
|
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);
|
const inviteToken = await this.invitesRepository.create(invite);
|
||||||
return inviteToken;
|
return inviteToken;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in new issue
Block a user