update hak akses dan perbaikan api pembuatan tiket

This commit is contained in:
Fanrouver
2026-04-20 09:42:03 +07:00
parent 94ff9f55d9
commit 2bf00eff3c
38 changed files with 2054 additions and 2552 deletions

No files matched your search

+33 -7
View File
@@ -73,21 +73,41 @@ export default defineEventHandler(async (event) => {
idTokenPayload?.groups ||
[];
// Determine tipeUser from groups or roles if possible
// You can customize this mapping based on your business logic
// Build user data object
let tipeUser = '';
if (Array.isArray(groups) && groups.length > 0) {
// Extract tipeUser from groups path (e.g., "/Instalasi STIM/Devops/Superadmin" -> "Superadmin")
// Check if tipeUser exists in database
try {
const Database = (await import('better-sqlite3')).default;
const { join } = await import('path');
const { existsSync } = await import('fs');
const dbPath = join(process.cwd(), 'data', 'users.db');
if (existsSync(dbPath)) {
const db = new Database(dbPath);
const dbUser = db.prepare('SELECT tipeUser FROM users WHERE id = ?').get(idTokenPayload?.sub || session.user?.id) as any;
if (dbUser && dbUser.tipeUser) {
tipeUser = dbUser.tipeUser;
console.log(`✅ Using tipeUser from database: ${tipeUser}`);
}
db.close();
}
} catch (dbError) {
console.warn('⚠️ Failed to fetch tipeUser from database:', dbError);
}
// Fallback to groups if not in database
if (!tipeUser && Array.isArray(groups) && groups.length > 0) {
const { slugify } = await import('~/server/utils/userSync');
const lastGroup = groups[groups.length - 1];
if (typeof lastGroup === 'string') {
const parts = lastGroup.split('/').filter(Boolean);
if (parts.length > 0) {
tipeUser = parts[parts.length - 1]; // Get last part of path
tipeUser = slugify(parts[parts.length - 1]);
}
}
}
// Build user data object
const userData = {
id: idTokenPayload?.sub || session.user?.id,
namaLengkap: idTokenPayload?.name ||
@@ -105,13 +125,19 @@ export default defineEventHandler(async (event) => {
accountRoles: Array.isArray(accountRoles) ? accountRoles : [],
resourceRoles: Array.isArray(resourceRoles) ? resourceRoles : [],
groups: Array.isArray(groups) ? groups : [],
tipeUser: tipeUser, // Extracted from groups or empty
tipeUser: tipeUser,
lastLogin: null, // Will be set on sync
// Include full token payloads for reference
idTokenPayload,
accessTokenPayload,
};
// IMPORTANT: Inject tipeUser as a role so checkPageAccess/useHakAkses can find it
if (tipeUser && !userData.roles.includes(tipeUser)) {
userData.roles.push(tipeUser);
console.log(`🛡️ Injected tipeUser '${tipeUser}' as a role for access control`);
}
console.log("✅ Current user data extracted from JWT");
return userData;
} catch (parseError: any) {