54 lines
2.5 KiB
JavaScript
54 lines
2.5 KiB
JavaScript
"use strict";
|
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
};
|
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.TourRoleGuard = void 0;
|
|
const common_1 = require("@nestjs/common");
|
|
const prisma_service_1 = require("./prisma.service");
|
|
let TourRoleGuard = class TourRoleGuard {
|
|
constructor(prisma) {
|
|
this.prisma = prisma;
|
|
}
|
|
async canActivate(context) {
|
|
const request = context.switchToHttp().getRequest();
|
|
const user = request.user;
|
|
const tourId = parseInt(request.params.id || request.params.tourId);
|
|
const path = request.url;
|
|
if (!user || isNaN(tourId)) {
|
|
throw new common_1.ForbiddenException("Thông tin xác thực hoặc mã Tour không hợp lệ.");
|
|
}
|
|
const membership = await this.prisma.tourMember.findUnique({
|
|
where: {
|
|
tourId_userId: {
|
|
tourId: tourId,
|
|
userId: user.id,
|
|
},
|
|
},
|
|
});
|
|
if (!membership) {
|
|
throw new common_1.ForbiddenException("Bạn không phải là thành viên của tour này.");
|
|
}
|
|
request.tourMembership = membership;
|
|
const role = membership.role;
|
|
const isPlanPath = path.includes('/plans');
|
|
const isExpensePath = path.includes('/expenses');
|
|
if ((role === 'MEMBER_PHOTO_ONLY' || role === 'VIEWER_EXTERNAL') &&
|
|
(isPlanPath || isExpensePath)) {
|
|
throw new common_1.ForbiddenException("Bạn không có quyền xem kế hoạch và chi phí của tour này.");
|
|
}
|
|
return true;
|
|
}
|
|
};
|
|
exports.TourRoleGuard = TourRoleGuard;
|
|
exports.TourRoleGuard = TourRoleGuard = __decorate([
|
|
(0, common_1.Injectable)(),
|
|
__metadata("design:paramtypes", [prisma_service_1.PrismaService])
|
|
], TourRoleGuard);
|
|
//# sourceMappingURL=rbac.middleware.js.map
|