/home/techb158/workloadmatch.com/api
Edit: /home/techb158/workloadmatch.com/api/Auth.php (4504B)
['Admin_ID', 'admin_profile'],
'master_profile' => ['Master_ID', 'master_profile'],
'manager_profile' => ['Manager_ID', 'manager_profile'],
'teacher_profile' => ['Teacher_ID', 'teacher_profile'],
];
if (!isset($tableMap[$userType])) {
return null;
}
[$idCol, $table] = $tableMap[$userType];
$stmt = $mysqli->prepare("SELECT * FROM $table WHERE $idCol = ? LIMIT 1");
if (!$stmt) return null;
$stmt->bind_param('s', $userId);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
$stmt->close();
if (!$user) return null;
// Verify login string
$loginCheck = hash('sha512', $user['Password'] . $_SERVER['HTTP_USER_AGENT']);
if ($loginCheck !== $_SESSION['login_string']) {
return null;
}
// Check if user is disabled
if (isset($user['User_Access']) && (int)$user['User_Access'] !== 1) {
return null;
}
self::$currentUser = $user;
return $user;
}
public static function requireLogin(): array
{
$user = self::getUser();
if (!$user) {
Response::unauthorized('Authentication required. Please log in.');
}
return $user;
}
public static function requireRole(string $role): array
{
$user = self::requireLogin();
if ($_SESSION['User_type'] !== $role) {
Response::forbidden("Access denied. Requires role: $role");
}
return $user;
}
public static function requireAnyRole(array $roles): array
{
$user = self::requireLogin();
if (!in_array($_SESSION['User_type'], $roles)) {
Response::forbidden('Access denied. Insufficient permissions.');
}
return $user;
}
public static function getUserType(): string
{
return $_SESSION['User_type'] ?? '';
}
public static function getUserId(): ?string
{
return $_SESSION['user_id'] ?? null;
}
public static function getEffectiveManagerId(): int
{
if (!empty($_SESSION['manage_as'])) {
return (int)$_SESSION['manage_as'];
}
return (int)($_SESSION['user_id'] ?? 0);
}
public static function isMasterActingAsManager(): bool
{
return self::getUserType() === 'master_profile' && !empty($_SESSION['manage_as']);
}
public static function getRoleDir(): string
{
$dirMap = [
'master_profile' => 'Master',
'manager_profile' => 'Manager',
'teacher_profile' => 'Teacher',
'admin_profile' => 'Admin',
];
$type = self::getUserType();
$dir = $dirMap[$type] ?? '';
if ($type === 'master_profile' && !empty($_SESSION['manage_as'])) {
$dir = 'Manager';
}
return $dir;
}
public static function hasPermission(mysqli $mysqli, string $module, string $action): bool
{
$userType = $_SESSION['User_type'] ?? '';
$roleId = $_SESSION['role_id'] ?? null;
if ($userType === 'admin_profile') return true;
if (!$roleId) return false;
$actionCol = 'can_' . $action;
$stmt = $mysqli->prepare("SELECT $actionCol FROM role_permissions WHERE role_id = ? AND module = ? LIMIT 1");
if ($stmt) {
$stmt->bind_param('is', $roleId, $module);
$stmt->execute();
$stmt->bind_result($allowed);
$stmt->fetch();
$stmt->close();
return (bool)$allowed;
}
return false;
}
public static function requirePermission(mysqli $mysqli, string $module, string $action): void
{
if (!self::hasPermission($mysqli, $module, $action)) {
Response::forbidden("Permission denied: $module/$action");
}
}
}