2026-02-04 23:31:53 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use MintyPHP\Buffer;
|
|
|
|
|
use MintyPHP\Router;
|
2026-02-24 08:49:40 +01:00
|
|
|
use MintyPHP\Service\Access\TenantAuthorizationPolicy;
|
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
|
|
|
use MintyPHP\Service\CustomField\TenantCustomFieldService;
|
2026-02-23 12:58:19 +01:00
|
|
|
use MintyPHP\Support\Flash;
|
|
|
|
|
use MintyPHP\Support\Guard;
|
2026-02-04 23:31:53 +01:00
|
|
|
Guard::requireLogin();
|
2026-03-04 15:56:58 +01:00
|
|
|
$request = requestInput();
|
2026-02-04 23:31:53 +01:00
|
|
|
|
|
|
|
|
$currentUserId = (int) ($_SESSION['user']['id'] ?? 0);
|
|
|
|
|
if ($currentUserId > 0) {
|
2026-03-04 15:56:58 +01:00
|
|
|
app(\MintyPHP\Service\Access\PermissionGateway::class)->getUserPermissions($currentUserId);
|
2026-02-04 23:31:53 +01:00
|
|
|
}
|
2026-03-04 15:56:58 +01:00
|
|
|
$userAccountService = app(\MintyPHP\Service\User\UserAccountService::class);
|
2026-02-04 23:31:53 +01:00
|
|
|
|
|
|
|
|
$uuid = trim((string) ($id ?? ''));
|
2026-03-04 15:56:58 +01:00
|
|
|
$tenant = $uuid !== '' ? app(\MintyPHP\Service\Tenant\TenantService::class)->findByUuid($uuid) : null;
|
2026-02-04 23:31:53 +01:00
|
|
|
if (!$tenant) {
|
|
|
|
|
Flash::error('Tenant not found', 'admin/tenants', 'tenant_not_found');
|
|
|
|
|
Router::redirect('admin/tenants');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$tenantId = (int) ($tenant['id'] ?? 0);
|
2026-03-04 15:56:58 +01:00
|
|
|
$tenantSsoService = app(\MintyPHP\Service\Auth\TenantSsoService::class);
|
|
|
|
|
$authService = app(\MintyPHP\Service\Auth\AuthService::class);
|
|
|
|
|
$authorizationService = app(\MintyPHP\Service\Access\AuthorizationService::class);
|
2026-02-24 08:49:40 +01:00
|
|
|
$contextDecision = $authorizationService->authorize(TenantAuthorizationPolicy::ABILITY_ADMIN_TENANTS_EDIT_CONTEXT, [
|
|
|
|
|
'actor_user_id' => $currentUserId,
|
|
|
|
|
'target_tenant_id' => $tenantId,
|
|
|
|
|
]);
|
|
|
|
|
if (!$contextDecision->isAllowed()) {
|
|
|
|
|
Router::redirect('error/forbidden');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$capabilities = $contextDecision->attribute('capabilities', []);
|
|
|
|
|
if (!is_array($capabilities)) {
|
|
|
|
|
$capabilities = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$canViewPage = (bool) ($capabilities['can_view_page'] ?? false);
|
|
|
|
|
$canUpdateTenant = (bool) ($capabilities['can_update_tenant'] ?? false);
|
|
|
|
|
$canManageCustomFields = (bool) ($capabilities['can_manage_custom_fields'] ?? false);
|
|
|
|
|
$canManageSso = (bool) ($capabilities['can_manage_sso'] ?? false);
|
2026-03-04 15:56:58 +01:00
|
|
|
$viewAuth['page'] = [
|
|
|
|
|
'can_update_tenant' => $canUpdateTenant,
|
|
|
|
|
'can_manage_custom_fields' => $canManageCustomFields,
|
|
|
|
|
'can_manage_sso' => $canManageSso,
|
|
|
|
|
];
|
2026-02-24 08:49:40 +01:00
|
|
|
if (!$canViewPage) {
|
|
|
|
|
Router::redirect('error/forbidden');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
$tenantCustomFieldService = app(TenantCustomFieldService::class);
|
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
|
|
|
$customFieldDefinitions = $canManageCustomFields
|
2026-03-04 15:56:58 +01:00
|
|
|
? $tenantCustomFieldService->listForTenant($tenantId)
|
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
|
|
|
: [];
|
2026-02-23 12:58:19 +01:00
|
|
|
$ssoConfig = $canManageSso ? $tenantSsoService->getTenantMicrosoftAuth($tenantId) : [];
|
|
|
|
|
$ssoUiState = $canManageSso ? $tenantSsoService->buildMicrosoftUiState($tenantId, $ssoConfig) : [];
|
2026-02-04 23:31:53 +01:00
|
|
|
$creatorId = (int) ($tenant['created_by'] ?? 0);
|
|
|
|
|
if ($creatorId > 0) {
|
2026-02-23 12:58:19 +01:00
|
|
|
$creator = $userAccountService->findById($creatorId);
|
2026-02-04 23:31:53 +01:00
|
|
|
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) {
|
2026-02-23 12:58:19 +01:00
|
|
|
$modifier = $userAccountService->findById($modifierId);
|
2026-02-04 23:31:53 +01:00
|
|
|
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) {
|
2026-02-23 12:58:19 +01:00
|
|
|
$statusChanger = $userAccountService->findById($statusChangedById);
|
2026-02-04 23:31:53 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
$errorBag = formErrors();
|
2026-02-04 23:31:53 +01:00
|
|
|
$errors = [];
|
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
|
|
|
$form = array_merge($tenant, $ssoConfig);
|
2026-02-04 23:31:53 +01:00
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
if ($request->hasBody('description')) {
|
|
|
|
|
$post = $request->bodyAll();
|
2026-02-24 08:49:40 +01:00
|
|
|
$submitDecision = $authorizationService->authorize(TenantAuthorizationPolicy::ABILITY_ADMIN_TENANTS_EDIT_SUBMIT, [
|
|
|
|
|
'actor_user_id' => $currentUserId,
|
|
|
|
|
'target_tenant_id' => $tenantId,
|
2026-03-04 15:56:58 +01:00
|
|
|
'input' => $post,
|
2026-02-24 08:49:40 +01:00
|
|
|
]);
|
|
|
|
|
if (!$submitDecision->isAllowed()) {
|
2026-02-04 23:31:53 +01:00
|
|
|
Router::redirect('error/forbidden');
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-02-24 08:49:40 +01:00
|
|
|
|
|
|
|
|
$submitCapabilities = $submitDecision->attribute('capabilities', []);
|
|
|
|
|
if (is_array($submitCapabilities)) {
|
|
|
|
|
$canUpdateTenant = (bool) ($submitCapabilities['can_update_tenant'] ?? $canUpdateTenant);
|
|
|
|
|
$canManageCustomFields = (bool) ($submitCapabilities['can_manage_custom_fields'] ?? $canManageCustomFields);
|
|
|
|
|
$canManageSso = (bool) ($submitCapabilities['can_manage_sso'] ?? $canManageSso);
|
2026-03-04 15:56:58 +01:00
|
|
|
$viewAuth['page'] = [
|
|
|
|
|
'can_update_tenant' => $canUpdateTenant,
|
|
|
|
|
'can_manage_custom_fields' => $canManageCustomFields,
|
|
|
|
|
'can_manage_sso' => $canManageSso,
|
|
|
|
|
];
|
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
|
|
|
}
|
2026-02-24 08:49:40 +01:00
|
|
|
if (!$canUpdateTenant) {
|
|
|
|
|
Router::redirect('error/forbidden');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
$result = app(\MintyPHP\Service\Tenant\TenantService::class)->updateFromAdmin($tenantId, $post, $currentUserId);
|
2026-02-04 23:31:53 +01:00
|
|
|
$form = $result['form'] ?? $form;
|
2026-03-04 15:56:58 +01:00
|
|
|
$errorBag->merge($result['errors'] ?? []);
|
2026-02-04 23:31:53 +01:00
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
if (($result['ok'] ?? false) && !$errorBag->hasAny()) {
|
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
|
|
|
if ($canManageSso) {
|
2026-03-04 15:56:58 +01:00
|
|
|
$ssoSaveResult = $tenantSsoService->saveTenantMicrosoftAuth($tenantId, $post);
|
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
|
|
|
if (!($ssoSaveResult['ok'] ?? false)) {
|
2026-03-04 15:56:58 +01:00
|
|
|
$errorBag->merge($ssoSaveResult['errors'] ?? [t('Tenant SSO settings could not be saved')]);
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
if (($result['ok'] ?? false) && !$errorBag->hasAny()) {
|
2026-02-04 23:31:53 +01:00
|
|
|
if ($currentUserId > 0) {
|
2026-03-04 15:56:58 +01:00
|
|
|
$authService->loadTenantDataIntoSession($currentUserId);
|
2026-02-04 23:31:53 +01:00
|
|
|
}
|
2026-03-04 15:56:58 +01:00
|
|
|
$action = (string) ($post['action'] ?? 'save');
|
2026-02-04 23:31:53 +01:00
|
|
|
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}");
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
if ($canManageSso) {
|
|
|
|
|
$form = array_merge($form, [
|
2026-03-04 15:56:58 +01:00
|
|
|
'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'] ?? '')),
|
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
|
|
|
]);
|
2026-03-04 15:56:58 +01:00
|
|
|
$ssoUiState = $tenantSsoService->buildMicrosoftUiState($tenantId, $post);
|
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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($canManageSso && empty($ssoUiState)) {
|
2026-02-23 12:58:19 +01:00
|
|
|
$ssoUiState = $tenantSsoService->buildMicrosoftUiState($tenantId, $form);
|
2026-02-04 23:31:53 +01:00
|
|
|
}
|
|
|
|
|
|
2026-03-04 15:56:58 +01:00
|
|
|
$validationSummaryErrors = $errorBag->toArray();
|
|
|
|
|
$errors = $errorBag->toFlatList();
|
2026-02-04 23:31:53 +01:00
|
|
|
Buffer::set('title', t('Edit tenant'));
|