This commit is contained in:
2025-12-07 02:49:46 +01:00
parent 37318e69fb
commit 6414802ce8
7 changed files with 1190 additions and 9 deletions

View File

@@ -67,6 +67,9 @@ class AuthService
$colName = $authDb['col_name'] ?? 'name';
$colId = $authDb['col_id'] ?? 'id';
$colStatus = $authDb['col_status']?? null;
$colRole = $authDb['col_role'] ?? 'role';
$colCustomer = $authDb['customer_fk'] ?? 'customer_id';
$customerTable = $authDb['customer_table'] ?? null;
$activeValues = $authDb['active_values'] ?? ['active','1',1];
$table = $authDb['table'] ?? 'emailtemplate_users';
@@ -92,14 +95,38 @@ class AuthService
$this->fail('Invalid credentials', null, 401);
}
$customerId = isset($row[$colCustomer]) ? (int)$row[$colCustomer] : null;
$customerData = $customerId ? $this->fetchCustomerData($customerId, $customerTable, $authDb) : null;
$_SESSION['auth'] = [
'id' => $row[$colId] ?? null,
'name' => $row[$colName] ?? ($row[$colUser] ?? $identifier),
'email' => $row[$colUser] ?? $identifier,
'at' => time(),
'id' => $row[$colId] ?? null,
'name' => $row[$colName] ?? ($row[$colUser] ?? $identifier),
'email' => $row[$colUser] ?? $identifier,
'role' => $row[$colRole] ?? 'user',
'customer_id' => $customerId,
'customer' => $customerData,
'permissions' => [
'owner' => ($row[$colRole] ?? '') === 'owner',
],
'at' => time(),
];
$token = base64_encode(hash('sha256', ($_SESSION['auth']['id'] ?? $identifier).'|'.session_id(), true));
return ['user'=>$_SESSION['auth'], 'token'=>$token];
}
private function fetchCustomerData(?int $customerId, ?string $table, array $authDb): ?array
{
if (!$customerId || !$table) return null;
$cols = $authDb['customer_cols'] ?? [];
$select = ['`id`'];
foreach ($cols as $alias => $column) {
$select[] = sprintf('`%s` AS `%s`', $column, $alias);
}
$sql = sprintf('SELECT %s FROM `%s` WHERE `id` = :id LIMIT 1', implode(',', $select), $table);
$stmt = $this->pdo->prepare($sql);
$stmt->execute([':id' => $customerId]);
$row = $stmt->fetch();
return $row ?: null;
}
}