Files
breadcrumb-the-shire/pages/admin/tenants/edit($id).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

137 lines
5.6 KiB
PHP

<?php
use MintyPHP\Buffer;
use MintyPHP\Support\Flash;
use MintyPHP\Support\Guard;
use MintyPHP\Router;
use MintyPHP\Service\Tenant\TenantService;
use MintyPHP\Service\User\UserService;
use MintyPHP\Service\Settings\SettingService;
use MintyPHP\Service\Access\PermissionService;
use MintyPHP\Service\CustomField\TenantCustomFieldService;
use MintyPHP\Service\Auth\TenantSsoService;
Guard::requireLogin();
Guard::requirePermissionOrForbidden(PermissionService::TENANTS_VIEW);
$currentUserId = (int) ($_SESSION['user']['id'] ?? 0);
if ($currentUserId > 0) {
PermissionService::getUserPermissions($currentUserId);
}
$uuid = trim((string) ($id ?? ''));
$tenant = $uuid !== '' ? TenantService::findByUuid($uuid) : null;
if (!$tenant) {
Flash::error('Tenant not found', 'admin/tenants', 'tenant_not_found');
Router::redirect('admin/tenants');
}
$tenantId = (int) ($tenant['id'] ?? 0);
$canManageCustomFields = PermissionService::userHas($currentUserId, PermissionService::CUSTOM_FIELDS_MANAGE);
$canManageSso = PermissionService::userHas($currentUserId, PermissionService::TENANTS_SSO_MANAGE);
$customFieldDefinitions = $canManageCustomFields
? TenantCustomFieldService::listForTenant($tenantId)
: [];
$ssoConfig = $canManageSso ? TenantSsoService::getTenantMicrosoftAuth($tenantId) : [];
$ssoUiState = $canManageSso ? TenantSsoService::buildMicrosoftUiState($tenantId, $ssoConfig) : [];
$creatorId = (int) ($tenant['created_by'] ?? 0);
if ($creatorId > 0) {
$creator = UserService::findById($creatorId);
if ($creator) {
$creatorName = trim(($creator['first_name'] ?? '') . ' ' . ($creator['last_name'] ?? ''));
$tenant['created_by_label'] = $creatorName !== '' ? $creatorName : ($creator['email'] ?? '');
$tenant['created_by_uuid'] = $creator['uuid'] ?? null;
}
}
$modifierId = (int) ($tenant['modified_by'] ?? 0);
if ($modifierId > 0) {
$modifier = UserService::findById($modifierId);
if ($modifier) {
$modifierName = trim(($modifier['first_name'] ?? '') . ' ' . ($modifier['last_name'] ?? ''));
$tenant['modified_by_label'] = $modifierName !== '' ? $modifierName : ($modifier['email'] ?? '');
$tenant['modified_by_uuid'] = $modifier['uuid'] ?? null;
}
}
$statusChangedById = (int) ($tenant['status_changed_by'] ?? 0);
if ($statusChangedById > 0) {
$statusChanger = UserService::findById($statusChangedById);
if ($statusChanger) {
$statusChangerName = trim(($statusChanger['first_name'] ?? '') . ' ' . ($statusChanger['last_name'] ?? ''));
$tenant['status_changed_by_label'] = $statusChangerName !== '' ? $statusChangerName : ($statusChanger['email'] ?? '');
$tenant['status_changed_by_uuid'] = $statusChanger['uuid'] ?? null;
}
}
$errors = [];
$form = array_merge($tenant, $ssoConfig);
if (isset($_POST['description'])) {
if (!PermissionService::userHas($currentUserId, PermissionService::TENANTS_UPDATE)) {
Router::redirect('error/forbidden');
return;
}
$ssoFields = [
'microsoft_enabled',
'enforce_microsoft_login',
'sync_profile_on_login',
'sync_profile_fields',
'entra_tenant_id',
'allowed_domains',
'use_shared_app',
'client_id_override',
'client_secret_override',
'clear_client_secret_override',
];
foreach ($ssoFields as $ssoField) {
if (array_key_exists($ssoField, $_POST) && !$canManageSso) {
Router::redirect('error/forbidden');
return;
}
}
$result = TenantService::updateFromAdmin($tenantId, $_POST, $currentUserId);
$form = $result['form'] ?? $form;
$errors = $result['errors'] ?? [];
if ($result['ok'] ?? false) {
if ($canManageSso) {
$ssoSaveResult = TenantSsoService::saveTenantMicrosoftAuth($tenantId, $_POST);
if (!($ssoSaveResult['ok'] ?? false)) {
$errors = array_merge($errors, $ssoSaveResult['errors'] ?? [t('Tenant SSO settings could not be saved')]);
}
}
}
if (($result['ok'] ?? false) && !$errors) {
if ($currentUserId > 0) {
\MintyPHP\Service\Auth\AuthService::loadTenantDataIntoSession($currentUserId);
}
$action = (string) ($_POST['action'] ?? 'save');
if ($action === 'save_close') {
Flash::success('Tenant updated', 'admin/tenants', 'tenant_updated');
Router::redirect('admin/tenants');
} else {
Flash::success('Tenant updated', "admin/tenants/edit/{$uuid}", 'tenant_updated');
Router::redirect("admin/tenants/edit/{$uuid}");
}
}
if ($canManageSso) {
$form = array_merge($form, [
'microsoft_enabled' => !empty($_POST['microsoft_enabled']) ? '1' : '0',
'enforce_microsoft_login' => !empty($_POST['enforce_microsoft_login']) ? '1' : '0',
'sync_profile_on_login' => !empty($_POST['sync_profile_on_login']) ? '1' : '0',
'sync_profile_fields_list' => TenantSsoService::normalizeProfileSyncFields($_POST['sync_profile_fields'] ?? []),
'entra_tenant_id' => trim((string) ($_POST['entra_tenant_id'] ?? '')),
'allowed_domains' => trim((string) ($_POST['allowed_domains'] ?? '')),
'use_shared_app' => !empty($_POST['use_shared_app']) ? '1' : '0',
'client_id_override' => trim((string) ($_POST['client_id_override'] ?? '')),
]);
$ssoUiState = TenantSsoService::buildMicrosoftUiState($tenantId, $_POST);
}
}
if ($canManageSso && empty($ssoUiState)) {
$ssoUiState = TenantSsoService::buildMicrosoftUiState($tenantId, $form);
}
Buffer::set('title', t('Edit tenant'));