Update frontend login page.jsx และ backend
This commit is contained in:
@@ -1,4 +1,9 @@
|
||||
import { Role, Permission, UserRole, RolePermission } from '../db/sequelize.js';
|
||||
// FILE: src/middleware/permissions.js
|
||||
// Permission calculation and enrichment middleware
|
||||
// - Computes effective permissions for a user based on their roles
|
||||
// - Attaches permissions to req.user.permissions
|
||||
|
||||
import { Role, Permission, UserRole, RolePermission } from "../db/sequelize.js";
|
||||
|
||||
/**
|
||||
* คืนชุด permission (string[]) ของ user_id
|
||||
@@ -6,16 +11,16 @@ import { Role, Permission, UserRole, RolePermission } from '../db/sequelize.js';
|
||||
export async function computeEffectivePermissions(user_id) {
|
||||
// ดึง roles ของผู้ใช้
|
||||
const userRoles = await UserRole.findAll({ where: { user_id } });
|
||||
const roleIds = userRoles.map(r => r.role_id);
|
||||
const roleIds = userRoles.map((r) => r.role_id);
|
||||
if (!roleIds.length) return [];
|
||||
|
||||
// ดึง permission ผ่าน role_permissions
|
||||
const rp = await RolePermission.findAll({ where: { role_id: roleIds } });
|
||||
const permIds = [...new Set(rp.map(x => x.permission_id))];
|
||||
const permIds = [...new Set(rp.map((x) => x.permission_id))];
|
||||
if (!permIds.length) return [];
|
||||
|
||||
const perms = await Permission.findAll({ where: { permission_id: permIds } });
|
||||
return [...new Set(perms.map(p => p.permission_name))];
|
||||
return [...new Set(perms.map((p) => p.permission_name))];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user