690417:1538 Refactor Work flow ADR-021

This commit is contained in:
2026-04-17 15:38:20 +07:00
parent 6d45bdaeb5
commit 3a5fc8d4af
23 changed files with 892 additions and 135 deletions
@@ -10,7 +10,7 @@ import {
NotFoundException,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { DataSource, Repository } from 'typeorm';
import { WorkflowInstance } from '../entities/workflow-instance.entity';
import { UserService } from '../../../modules/user/user.service';
import type { RequestWithUser } from '../../../common/interfaces/request-with-user.interface';
@@ -18,10 +18,12 @@ import type { RequestWithUser } from '../../../common/interfaces/request-with-us
/**
* WorkflowTransitionGuard — ตรวจสอบสิทธิ์ 4 ระดับก่อนอนุญาตให้เปลี่ยนสถานะ Workflow
*
* Level 1: system.manage_all (Superadmin) → ผ่านทันที
* Level 2: organization.manage_users + สังกัดองค์กรเดียวกับเอกสาร → ผ่าน
* Level 3: Assigned Handler (context.assignedUserId === req.user.user_id) → ผ่าน
* Level 4: ผู้ใช้ทั่วไป → ForbiddenException
* Level 1: system.manage_all (Superadmin) → ผ่านทันที
* Level 2: organization.manage_users + สังกัดองค์กรเดียวกับเอกสาร → ผ่าน
* Level 2.5: [C3] contract_organizations membership — ถ้า instance.contractId ถูกตั้ง
* และ User ไม่อยู่ใน contract นั้น → ForbiddenException (cross-contract block)
* Level 3: Assigned Handler (context.assignedUserId === req.user.user_id) → ผ่าน
* Level 4: ผู้ใช้ทั่วไป → ForbiddenException
*/
@Injectable()
export class WorkflowTransitionGuard implements CanActivate {
@@ -30,7 +32,8 @@ export class WorkflowTransitionGuard implements CanActivate {
constructor(
@InjectRepository(WorkflowInstance)
private readonly instanceRepo: Repository<WorkflowInstance>,
private readonly userService: UserService
private readonly userService: UserService,
private readonly dataSource: DataSource
) {}
async canActivate(context: ExecutionContext): Promise<boolean> {
@@ -67,6 +70,35 @@ export class WorkflowTransitionGuard implements CanActivate {
return true;
}
// Level 2.5: [C3] Contract Membership check — ถ้า instance ผูกกับ contract ใด
// User ต้องสังกัดองค์กรที่อยู่ใน contract นั้น ป้องกัน cross-contract access
if (instance.contractId !== null && instance.contractId !== undefined) {
const userOrgId = user.primaryOrganizationId;
if (!userOrgId) {
this.logger.warn(
`No org for User ${user.user_id} attempting contract-scoped workflow ${instanceId}`
);
throw new ForbiddenException({
userMessage:
'คุณไม่ได้สังกัดองค์กรใด ไม่สามารถดำเนินการในสัญญานี้ได้',
recoveryAction: 'ติดต่อ Admin เพื่อกำหนดองค์กร',
});
}
const rows = await this.dataSource.query<[{ cnt: string }]>(
'SELECT COUNT(*) AS cnt FROM contract_organizations WHERE contract_id = ? AND organization_id = ?',
[instance.contractId, userOrgId]
);
if (Number(rows[0]?.cnt ?? 0) === 0) {
this.logger.warn(
`Cross-contract access attempt: User ${user.user_id} (Org ${userOrgId}) on Contract ${instance.contractId} Instance ${instanceId}`
);
throw new ForbiddenException({
userMessage: 'คุณไม่มีสิทธิ์เข้าถึง Workflow ของสัญญานี้',
recoveryAction: 'ตรวจสอบสิทธิ์กับ Project Admin',
});
}
}
// Level 3: Assigned Handler — User นี้ถูก Assign มาให้ทำ Step นี้โดยตรง
const assignedUserId = instance.context?.assignedUserId as
| number