major update

This commit is contained in:
2026-03-04 15:56:58 +01:00
parent 16759a2732
commit 8f4dd5840d
478 changed files with 24313 additions and 8201 deletions

View File

@@ -7,6 +7,8 @@ use MintyPHP\Repository\Support\RepoQuery;
class ApiAuditLogRepository
{
private const FILTER_OPTIONS_LIMIT_MAX = 200;
public function create(array $data): int|false
{
$id = DB::insert(
@@ -39,8 +41,16 @@ class ApiAuditLogRepository
$method = strtoupper(trim((string) ($filters['method'] ?? '')));
$createdFrom = trim((string) ($filters['created_from'] ?? ''));
$createdTo = trim((string) ($filters['created_to'] ?? ''));
$tenantId = (int) ($filters['tenant_id'] ?? 0);
$userId = (int) ($filters['user_id'] ?? 0);
$tenantIds = array_slice(
RepoQuery::normalizeIdList($filters['tenant_ids'] ?? ''),
0,
self::FILTER_OPTIONS_LIMIT_MAX
);
$userIds = array_slice(
RepoQuery::normalizeIdList($filters['user_ids'] ?? ''),
0,
self::FILTER_OPTIONS_LIMIT_MAX
);
[$limit, $offset] = RepoQuery::sanitizeLimitOffset($filters, 20, 1, 200, 0);
[$order, $dir] = RepoQuery::sanitizeOrder(
@@ -86,13 +96,13 @@ class ApiAuditLogRepository
$where[] = 'api_audit_log.created_at <= ?';
$params[] = $createdTo . ' 23:59:59';
}
if ($tenantId > 0) {
$where[] = 'api_audit_log.tenant_id = ?';
$params[] = (string) $tenantId;
if ($tenantIds !== []) {
$where[] = 'api_audit_log.tenant_id in (???)';
$params[] = $tenantIds;
}
if ($userId > 0) {
$where[] = 'api_audit_log.user_id = ?';
$params[] = (string) $userId;
if ($userIds !== []) {
$where[] = 'api_audit_log.user_id in (???)';
$params[] = $userIds;
}
$whereSql = $where ? (' where ' . implode(' and ', $where)) : '';
@@ -147,6 +157,109 @@ class ApiAuditLogRepository
];
}
public function listFilterOptions(int $limit = self::FILTER_OPTIONS_LIMIT_MAX): array
{
$limit = max(1, min(self::FILTER_OPTIONS_LIMIT_MAX, $limit));
$methodRows = DB::select(
'select
api_audit_log.method,
max(api_audit_log.created_at) as last_used_at
from api_audit_log
where api_audit_log.method is not null
and api_audit_log.method <> \'\'
group by api_audit_log.method
order by last_used_at desc
limit ?',
(string) $limit
);
$methods = [];
if (is_array($methodRows)) {
foreach ($methodRows as $row) {
$flat = self::flattenRow($row);
$method = strtoupper(trim((string) ($flat['method'] ?? '')));
if ($method === '') {
continue;
}
$methods[] = $method;
}
}
$methods = array_values(array_unique($methods));
$userRows = DB::select(
'select
api_audit_log.user_id,
max(api_audit_log.created_at) as last_used_at,
max(users.id) as user_exists_id,
max(users.display_name) as user_display_name,
max(users.email) as user_email
from api_audit_log
left join users on users.id = api_audit_log.user_id
where api_audit_log.user_id is not null
and api_audit_log.user_id > 0
group by api_audit_log.user_id
order by last_used_at desc
limit ?',
(string) $limit
);
$users = [];
if (is_array($userRows)) {
foreach ($userRows as $row) {
$flat = self::flattenRow($row);
$id = (int) ($flat['user_id'] ?? 0);
if ($id <= 0) {
continue;
}
$users[] = [
'id' => $id,
'display_name' => trim((string) ($flat['user_display_name'] ?? '')),
'email' => trim((string) ($flat['user_email'] ?? '')),
'exists' => (int) ($flat['user_exists_id'] ?? 0) > 0,
];
}
}
$tenantRows = DB::select(
'select
api_audit_log.tenant_id,
max(api_audit_log.created_at) as last_used_at,
max(tenants.id) as tenant_exists_id,
max(tenants.description) as tenant_description
from api_audit_log
left join tenants on tenants.id = api_audit_log.tenant_id
where api_audit_log.tenant_id is not null
and api_audit_log.tenant_id > 0
group by api_audit_log.tenant_id
order by last_used_at desc
limit ?',
(string) $limit
);
$tenants = [];
if (is_array($tenantRows)) {
foreach ($tenantRows as $row) {
$flat = self::flattenRow($row);
$id = (int) ($flat['tenant_id'] ?? 0);
if ($id <= 0) {
continue;
}
$tenants[] = [
'id' => $id,
'description' => trim((string) ($flat['tenant_description'] ?? '')),
'exists' => (int) ($flat['tenant_exists_id'] ?? 0) > 0,
];
}
}
return [
'methods' => $methods,
'users' => $users,
'tenants' => $tenants,
];
}
public function find(int $id): ?array
{
$row = DB::selectOne(
@@ -219,4 +332,25 @@ class ApiAuditLogRepository
return $log;
}
/**
* @return array<string, mixed>
*/
private static function flattenRow(mixed $row): array
{
if (!is_array($row)) {
return [];
}
$flat = [];
foreach ($row as $key => $value) {
if (is_array($value)) {
$flat = array_merge($flat, $value);
} elseif (is_string($key)) {
$flat[$key] = $value;
}
}
return $flat;
}
}