2026-04-21 13:43:16 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MintyPHP\Module\Helpdesk\Repository;
|
|
|
|
|
|
|
|
|
|
use MintyPHP\DB;
|
2026-04-21 14:04:21 +02:00
|
|
|
use MintyPHP\Repository\Support\RepositoryArrayHelper;
|
2026-04-21 13:43:16 +02:00
|
|
|
|
|
|
|
|
class DomainSecurityLevelRepository
|
|
|
|
|
{
|
2026-04-21 14:04:21 +02:00
|
|
|
private const TABLE = 'helpdesk_domain_security_levels';
|
2026-04-21 13:43:16 +02:00
|
|
|
private const ALLOWED_LEVELS = ['niedrig', 'normal', 'hoch', 'kritisch'];
|
|
|
|
|
|
|
|
|
|
public function findByTenantAndDomainNo(int $tenantId, string $domainNo): ?array
|
|
|
|
|
{
|
|
|
|
|
if ($domainNo === '') {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$row = DB::selectOne(
|
2026-04-21 14:04:21 +02:00
|
|
|
'SELECT id, tenant_id, domain_no, security_level, note, created_by, updated_by, created_at, updated_at FROM ' . self::TABLE . ' WHERE tenant_id = ? AND domain_no = ? LIMIT 1',
|
2026-04-21 13:43:16 +02:00
|
|
|
(string) $tenantId,
|
|
|
|
|
$domainNo
|
|
|
|
|
);
|
|
|
|
|
|
2026-04-21 14:04:21 +02:00
|
|
|
return RepositoryArrayHelper::unwrap(is_array($row) ? $row : null, self::TABLE);
|
2026-04-21 13:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-04-21 14:04:21 +02:00
|
|
|
* @api Called from DomainSecurityLevelService
|
2026-04-21 13:43:16 +02:00
|
|
|
* @param list<string> $domainNos
|
|
|
|
|
* @return array<string, string> Map of domain_no => security_level
|
|
|
|
|
*/
|
|
|
|
|
public function listLevelsByDomainNos(int $tenantId, array $domainNos): array
|
|
|
|
|
{
|
|
|
|
|
if ($domainNos === []) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$placeholders = implode(',', array_fill(0, count($domainNos), '?'));
|
2026-04-21 14:04:21 +02:00
|
|
|
$params = [(string) $tenantId, ...array_map('strval', $domainNos)];
|
2026-04-21 13:43:16 +02:00
|
|
|
|
|
|
|
|
$rows = DB::select(
|
2026-04-21 14:04:21 +02:00
|
|
|
'SELECT domain_no, security_level FROM ' . self::TABLE . ' WHERE tenant_id = ? AND domain_no IN (' . $placeholders . ')',
|
2026-04-21 13:43:16 +02:00
|
|
|
...$params
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$result = [];
|
2026-04-21 14:04:21 +02:00
|
|
|
foreach (RepositoryArrayHelper::unwrapList($rows, self::TABLE) as $row) {
|
|
|
|
|
$domainNo = (string) ($row['domain_no'] ?? '');
|
|
|
|
|
$level = (string) ($row['security_level'] ?? 'normal');
|
|
|
|
|
if ($domainNo !== '') {
|
|
|
|
|
$result[$domainNo] = in_array($level, self::ALLOWED_LEVELS, true) ? $level : 'normal';
|
2026-04-21 13:43:16 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-04-21 14:04:21 +02:00
|
|
|
* @api Called from DomainSecurityLevelService
|
2026-04-21 13:43:16 +02:00
|
|
|
* @param list<string> $domainNos
|
|
|
|
|
* @return array<string, array{level: string, note: string}> Map of domain_no => {level, note}
|
|
|
|
|
*/
|
|
|
|
|
public function listDetailsByDomainNos(int $tenantId, array $domainNos): array
|
|
|
|
|
{
|
|
|
|
|
if ($domainNos === []) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$placeholders = implode(',', array_fill(0, count($domainNos), '?'));
|
2026-04-21 14:04:21 +02:00
|
|
|
$params = [(string) $tenantId, ...array_map('strval', $domainNos)];
|
2026-04-21 13:43:16 +02:00
|
|
|
|
|
|
|
|
$rows = DB::select(
|
2026-04-21 14:04:21 +02:00
|
|
|
'SELECT domain_no, security_level, note FROM ' . self::TABLE . ' WHERE tenant_id = ? AND domain_no IN (' . $placeholders . ')',
|
2026-04-21 13:43:16 +02:00
|
|
|
...$params
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$result = [];
|
2026-04-21 14:04:21 +02:00
|
|
|
foreach (RepositoryArrayHelper::unwrapList($rows, self::TABLE) as $row) {
|
|
|
|
|
$domainNo = (string) ($row['domain_no'] ?? '');
|
|
|
|
|
$level = (string) ($row['security_level'] ?? 'normal');
|
|
|
|
|
$note = (string) ($row['note'] ?? '');
|
|
|
|
|
if ($domainNo !== '') {
|
|
|
|
|
$result[$domainNo] = [
|
|
|
|
|
'level' => in_array($level, self::ALLOWED_LEVELS, true) ? $level : 'normal',
|
|
|
|
|
'note' => $note,
|
|
|
|
|
];
|
2026-04-21 13:43:16 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Insert or update security level for a domain.
|
|
|
|
|
*
|
2026-04-21 14:04:21 +02:00
|
|
|
* @api Called from DomainSecurityLevelService
|
2026-04-21 13:43:16 +02:00
|
|
|
* @return bool True on success
|
|
|
|
|
*/
|
|
|
|
|
public function upsert(int $tenantId, string $domainNo, string $level, ?int $userId, ?string $note = null): bool
|
|
|
|
|
{
|
|
|
|
|
if ($domainNo === '' || !in_array($level, self::ALLOWED_LEVELS, true)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$existing = $this->findByTenantAndDomainNo($tenantId, $domainNo);
|
|
|
|
|
|
|
|
|
|
if ($existing !== null) {
|
|
|
|
|
$result = DB::update(
|
2026-04-21 14:04:21 +02:00
|
|
|
'UPDATE ' . self::TABLE . ' SET security_level = ?, note = ?, updated_by = ? WHERE tenant_id = ? AND domain_no = ?',
|
2026-04-21 13:43:16 +02:00
|
|
|
$level,
|
|
|
|
|
$note,
|
|
|
|
|
$userId !== null ? (string) $userId : null,
|
|
|
|
|
(string) $tenantId,
|
|
|
|
|
$domainNo
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return $result !== false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$result = DB::insert(
|
2026-04-21 14:04:21 +02:00
|
|
|
'INSERT INTO ' . self::TABLE . ' (tenant_id, domain_no, security_level, note, created_by, updated_by) VALUES (?, ?, ?, ?, ?, ?)',
|
2026-04-21 13:43:16 +02:00
|
|
|
(string) $tenantId,
|
|
|
|
|
$domainNo,
|
|
|
|
|
$level,
|
|
|
|
|
$note,
|
|
|
|
|
$userId !== null ? (string) $userId : null,
|
|
|
|
|
$userId !== null ? (string) $userId : null
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return $result !== false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @api Reserved for future cleanup jobs
|
|
|
|
|
*/
|
|
|
|
|
public function deleteByTenantAndDomainNo(int $tenantId, string $domainNo): bool
|
|
|
|
|
{
|
|
|
|
|
if ($domainNo === '') {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$result = DB::update(
|
2026-04-21 14:04:21 +02:00
|
|
|
'DELETE FROM ' . self::TABLE . ' WHERE tenant_id = ? AND domain_no = ?',
|
2026-04-21 13:43:16 +02:00
|
|
|
(string) $tenantId,
|
|
|
|
|
$domainNo
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return $result !== false;
|
|
|
|
|
}
|
|
|
|
|
}
|