Files
breadcrumb-the-shire/pages/admin/tenants/data().php
fs 25370a1a55 add composer-unused, comprehensive docs, and project restructure
- Add icanhazstring/composer-unused as dev dependency for dependency hygiene checks
- Add German documentation (docs/) covering architecture, conventions, workflows, and developer checklists
- Add API layer (ApiAuth, ApiBootstrap, ApiResponse), audit, scheduler, custom fields, and SSO services
- Add Microsoft OIDC SSO, API token management, and user lifecycle features
- Add swagger-ui vendor integration and OpenAPI spec
- Add production Docker setup and bin/ scripts
- Update composer dependencies, config, templates, and frontend assets throughout

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-22 15:27:35 +01:00

148 lines
5.8 KiB
PHP

<?php
use MintyPHP\Router;
use MintyPHP\Support\Guard;
use MintyPHP\Repository\Tenant\UserTenantRepository;
use MintyPHP\Repository\Tenant\TenantMicrosoftAuthRepository;
use MintyPHP\Service\Tenant\TenantService;
use MintyPHP\Service\Settings\SettingService;
use MintyPHP\Service\Access\PermissionService;
Guard::requireLogin();
Guard::requirePermissionOrForbidden(PermissionService::TENANTS_VIEW);
$limit = (int) ($_GET['limit'] ?? 10);
$offset = (int) ($_GET['offset'] ?? 0);
$search = trim((string) ($_GET['search'] ?? ''));
$order = (string) ($_GET['order'] ?? 'description');
$dir = (string) ($_GET['dir'] ?? 'asc');
$dir = strtolower($dir) === 'desc' ? 'desc' : 'asc';
$computedOrderKeys = ['theme', 'sso', 'active_users', 'inactive_users'];
$fetchRows = static function (array $tenantRows, array $themes, string $globalDefaultTheme): array {
$tenantIds = [];
foreach ($tenantRows as $tenantRow) {
$tenantId = (int) ($tenantRow['id'] ?? 0);
if ($tenantId > 0) {
$tenantIds[] = $tenantId;
}
}
$tenantIds = array_values(array_unique($tenantIds));
$tenantSsoMap = TenantMicrosoftAuthRepository::listByTenantIds($tenantIds);
$tenantUserCounts = UserTenantRepository::countUsersByTenantIds($tenantIds);
$tenantActiveUserCounts = UserTenantRepository::countActiveUsersByTenantIds($tenantIds);
$defaultTenantId = SettingService::getDefaultTenantId();
$rows = [];
foreach ($tenantRows as $row) {
$tenantId = (int) ($row['id'] ?? 0);
$tenantTheme = strtolower(trim((string) ($row['default_theme'] ?? '')));
$themeIsTenantOverride = $tenantTheme !== '' && isset($themes[$tenantTheme]);
$resolvedThemeKey = $themeIsTenantOverride ? $tenantTheme : $globalDefaultTheme;
$resolvedThemeLabel = (string) ($themes[$resolvedThemeKey] ?? ucfirst($resolvedThemeKey));
$themePolicyRaw = $row['allow_user_theme'] ?? null;
$themePolicyMode = 'inherit';
if ($themePolicyRaw !== null && $themePolicyRaw !== '') {
$normalized = strtolower(trim((string) $themePolicyRaw));
if (in_array($normalized, ['1', 'true', 'yes', 'on'], true)) {
$themePolicyMode = 'allow';
} elseif (in_array($normalized, ['0', 'false', 'no', 'off'], true)) {
$themePolicyMode = 'disallow';
}
}
$tenantSso = $tenantSsoMap[$tenantId] ?? null;
$ssoEnabled = !empty($tenantSso['enabled']);
$ssoMicrosoftOnly = $ssoEnabled && !empty($tenantSso['enforce_microsoft_login']);
$usersTotal = (int) ($tenantUserCounts[$tenantId] ?? 0);
$usersActive = (int) ($tenantActiveUserCounts[$tenantId] ?? 0);
$usersInactive = max(0, $usersTotal - $usersActive);
$rows[] = [
'id' => $row['id'] ?? null,
'uuid' => $row['uuid'] ?? '',
'is_default' => $tenantId > 0 && $defaultTenantId === $tenantId,
'description' => $row['description'] ?? '',
'status' => $row['status'] ?? 'active',
'theme' => [
'key' => $resolvedThemeKey,
'label' => t($resolvedThemeLabel),
'is_override' => $themeIsTenantOverride ? 1 : 0,
'policy_mode' => $themePolicyMode,
],
'sso' => [
'enabled' => $ssoEnabled ? 1 : 0,
'microsoft_only' => $ssoMicrosoftOnly ? 1 : 0,
],
'active_users' => $usersActive,
'inactive_users' => $usersInactive,
'created' => dt($row['created'] ?? ''),
'modified' => dt($row['modified'] ?? ''),
];
}
return $rows;
};
$themes = appThemes();
$globalDefaultTheme = SettingService::getAppTheme();
if ($globalDefaultTheme === null || !isset($themes[$globalDefaultTheme])) {
$envTheme = strtolower(trim((string) (getenv('APP_THEME') ?: 'light')));
$globalDefaultTheme = isset($themes[$envTheme]) ? $envTheme : 'light';
}
$result = TenantService::listPaged([
'limit' => $limit,
'offset' => $offset,
'search' => $search,
'order' => in_array($order, $computedOrderKeys, true) ? 'description' : $order,
'dir' => $dir,
]);
$rows = [];
$total = (int) ($result['total'] ?? 0);
if (in_array($order, $computedOrderKeys, true)) {
$fullResult = TenantService::listPaged([
'limit' => max(1, $total),
'offset' => 0,
'search' => $search,
'order' => 'description',
'dir' => 'asc',
]);
$rows = $fetchRows($fullResult['rows'] ?? [], $themes, $globalDefaultTheme);
usort($rows, static function (array $a, array $b) use ($order, $dir): int {
$valueA = '';
$valueB = '';
if ($order === 'theme') {
$valueA = strtolower((string) ($a['theme']['label'] ?? ''));
$valueB = strtolower((string) ($b['theme']['label'] ?? ''));
} elseif ($order === 'sso') {
$valueA = ((int) ($a['sso']['enabled'] ?? 0) * 10) + (int) ($a['sso']['microsoft_only'] ?? 0);
$valueB = ((int) ($b['sso']['enabled'] ?? 0) * 10) + (int) ($b['sso']['microsoft_only'] ?? 0);
} elseif ($order === 'inactive_users') {
$valueA = (int) ($a['inactive_users'] ?? 0);
$valueB = (int) ($b['inactive_users'] ?? 0);
} else {
$valueA = (int) ($a['active_users'] ?? 0);
$valueB = (int) ($b['active_users'] ?? 0);
}
$cmp = $valueA <=> $valueB;
if ($cmp === 0) {
$cmp = strcasecmp((string) ($a['description'] ?? ''), (string) ($b['description'] ?? ''));
}
return $dir === 'desc' ? -$cmp : $cmp;
});
$rows = array_slice($rows, $offset, $limit);
} else {
$rows = $fetchRows($result['rows'] ?? [], $themes, $globalDefaultTheme);
}
Router::json([
'data' => $rows,
'total' => $total,
]);