![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/domains/lavocat.quebec/private_html/scripts/ |
#!/usr/bin/env ts-node
// Simple test for impersonation permission logic
function isEffectiveAdmin(session: any): boolean {
if (!session || !session.user) return false;
// If impersonating, check original user's role
if (session.user.isImpersonating && session.user.originalUser) {
const originalRole = session.user.originalUser.role;
return originalRole === 'SUPERADMIN' || originalRole === 'ADMIN';
}
// Otherwise check current user's role
const role = session.user.role;
return role === 'SUPERADMIN' || role === 'ADMIN';
}
function isEffectiveSuperAdmin(session: any): boolean {
if (!session || !session.user) return false;
// If impersonating, check original user's role
if (session.user.isImpersonating && session.user.originalUser) {
return session.user.originalUser.role === 'SUPERADMIN';
}
// Otherwise check current user's role
return session.user.role === 'SUPERADMIN';
}
// Test cases for impersonation permission logic
function testImpersonationPermissions() {
console.log('🧪 Testing Impersonation Permission Logic\n');
// Test Case 1: SuperAdmin not impersonating
const superAdminSession = {
user: {
id: '1',
email: 'admin@example.com',
role: 'SUPERADMIN',
isImpersonating: false
}
};
console.log('Test 1: SuperAdmin not impersonating');
console.log(' isEffectiveAdmin:', isEffectiveAdmin(superAdminSession));
console.log(' isEffectiveSuperAdmin:', isEffectiveSuperAdmin(superAdminSession));
console.log(' Expected: true, true\n');
// Test Case 2: SuperAdmin impersonating a Lawyer
const superAdminImpersonatingLawyer = {
user: {
id: '2',
email: 'lawyer@example.com',
role: 'LAWYER',
isImpersonating: true,
originalUser: {
id: '1',
email: 'admin@example.com',
role: 'SUPERADMIN'
}
}
};
console.log('Test 2: SuperAdmin impersonating Lawyer');
console.log(' isEffectiveAdmin:', isEffectiveAdmin(superAdminImpersonatingLawyer));
console.log(' isEffectiveSuperAdmin:', isEffectiveSuperAdmin(superAdminImpersonatingLawyer));
console.log(' Expected: true, true\n');
// Test Case 3: Admin not impersonating
const adminSession = {
user: {
id: '3',
email: 'admin2@example.com',
role: 'ADMIN',
isImpersonating: false
}
};
console.log('Test 3: Admin not impersonating');
console.log(' isEffectiveAdmin:', isEffectiveAdmin(adminSession));
console.log(' isEffectiveSuperAdmin:', isEffectiveSuperAdmin(adminSession));
console.log(' Expected: true, false\n');
// Test Case 4: Admin impersonating a Client
const adminImpersonatingClient = {
user: {
id: '4',
email: 'client@example.com',
role: 'CLIENT',
isImpersonating: true,
originalUser: {
id: '3',
email: 'admin2@example.com',
role: 'ADMIN'
}
}
};
console.log('Test 4: Admin impersonating Client');
console.log(' isEffectiveAdmin:', isEffectiveAdmin(adminImpersonatingClient));
console.log(' isEffectiveSuperAdmin:', isEffectiveSuperAdmin(adminImpersonatingClient));
console.log(' Expected: true, false\n');
// Test Case 5: Lawyer not impersonating
const lawyerSession = {
user: {
id: '5',
email: 'lawyer2@example.com',
role: 'LAWYER',
isImpersonating: false
}
};
console.log('Test 5: Lawyer not impersonating');
console.log(' isEffectiveAdmin:', isEffectiveAdmin(lawyerSession));
console.log(' isEffectiveSuperAdmin:', isEffectiveSuperAdmin(lawyerSession));
console.log(' Expected: false, false\n');
// Test Case 6: Lawyer impersonating someone (should not happen, but test anyway)
const lawyerImpersonating = {
user: {
id: '6',
email: 'client2@example.com',
role: 'CLIENT',
isImpersonating: true,
originalUser: {
id: '5',
email: 'lawyer2@example.com',
role: 'LAWYER'
}
}
};
console.log('Test 6: Lawyer impersonating Client (should not happen)');
console.log(' isEffectiveAdmin:', isEffectiveAdmin(lawyerImpersonating));
console.log(' isEffectiveSuperAdmin:', isEffectiveSuperAdmin(lawyerImpersonating));
console.log(' Expected: false, false\n');
console.log('✅ All tests completed!');
}
// Run the tests
testImpersonationPermissions();