refactor(actions): deduplicate audit metadata and grid user count enrichment
This commit is contained in:
@@ -29,24 +29,17 @@ $result = app(\MintyPHP\Service\Org\DepartmentService::class)->listPaged([
|
||||
|
||||
$settingsDefaultsGateway = app(\MintyPHP\Service\Settings\SettingsDefaultsGateway::class);
|
||||
$defaultDepartmentId = $settingsDefaultsGateway->getDefaultDepartmentId();
|
||||
$departmentIds = [];
|
||||
foreach ($result['rows'] as $departmentRow) {
|
||||
$departmentId = (int) ($departmentRow['id'] ?? 0);
|
||||
if ($departmentId > 0) {
|
||||
$departmentIds[] = $departmentId;
|
||||
}
|
||||
}
|
||||
$departmentIds = array_values(array_unique($departmentIds));
|
||||
$userDepartmentRepository = app(\MintyPHP\Repository\Org\UserDepartmentRepository::class);
|
||||
$departmentUserCounts = $userDepartmentRepository->countUsersByDepartmentIds($departmentIds);
|
||||
$departmentActiveUserCounts = $userDepartmentRepository->countActiveUsersByDepartmentIds($departmentIds);
|
||||
$userCounts = app(\MintyPHP\Service\Data\GridUserCountEnricher::class)->computeCounts(
|
||||
$result['rows'],
|
||||
$userDepartmentRepository->countUsersByDepartmentIds(...),
|
||||
$userDepartmentRepository->countActiveUsersByDepartmentIds(...)
|
||||
);
|
||||
|
||||
$rows = [];
|
||||
foreach ($result['rows'] as $row) {
|
||||
$departmentId = (int) ($row['id'] ?? 0);
|
||||
$usersTotal = (int) ($departmentUserCounts[$departmentId] ?? 0);
|
||||
$usersActive = (int) ($departmentActiveUserCounts[$departmentId] ?? 0);
|
||||
$usersInactive = max(0, $usersTotal - $usersActive);
|
||||
$counts = $userCounts[$departmentId] ?? ['active_users' => 0, 'inactive_users' => 0];
|
||||
$rows[] = [
|
||||
'id' => $row['id'] ?? null,
|
||||
'uuid' => $row['uuid'] ?? '',
|
||||
@@ -55,8 +48,8 @@ foreach ($result['rows'] as $row) {
|
||||
'code' => $row['code'] ?? '',
|
||||
'cost_center' => $row['cost_center'] ?? '',
|
||||
'active' => $row['active'] ?? 1,
|
||||
'active_users' => $usersActive,
|
||||
'inactive_users' => $usersInactive,
|
||||
'active_users' => $counts['active_users'],
|
||||
'inactive_users' => $counts['inactive_users'],
|
||||
'tenants' => $row['tenant_labels'] ?? [],
|
||||
'created' => dt($row['created'] ?? ''),
|
||||
'modified' => dt($row['modified'] ?? ''),
|
||||
|
||||
@@ -19,7 +19,6 @@ $currentUserId = (int) ($session['user']['id'] ?? 0);
|
||||
if ($currentUserId > 0) {
|
||||
app(\MintyPHP\Service\Access\PermissionService::class)->getUserPermissions($currentUserId);
|
||||
}
|
||||
$userAccountService = app(\MintyPHP\Service\User\UserAccountService::class);
|
||||
$departmentService = app(\MintyPHP\Service\Org\DepartmentService::class);
|
||||
$tenantService = app(\MintyPHP\Service\Tenant\TenantService::class);
|
||||
$directoryScopeGateway = app(\MintyPHP\Service\Tenant\TenantScopeService::class);
|
||||
@@ -62,24 +61,7 @@ if (!$canViewPage) {
|
||||
return;
|
||||
}
|
||||
|
||||
$creatorId = (int) ($department['created_by'] ?? 0);
|
||||
if ($creatorId > 0) {
|
||||
$creator = $userAccountService->findById($creatorId);
|
||||
if ($creator) {
|
||||
$creatorName = trim(($creator['first_name'] ?? '') . ' ' . ($creator['last_name'] ?? ''));
|
||||
$department['created_by_label'] = $creatorName !== '' ? $creatorName : ($creator['email'] ?? '');
|
||||
$department['created_by_uuid'] = $creator['uuid'] ?? null;
|
||||
}
|
||||
}
|
||||
$modifierId = (int) ($department['modified_by'] ?? 0);
|
||||
if ($modifierId > 0) {
|
||||
$modifier = $userAccountService->findById($modifierId);
|
||||
if ($modifier) {
|
||||
$modifierName = trim(($modifier['first_name'] ?? '') . ' ' . ($modifier['last_name'] ?? ''));
|
||||
$department['modified_by_label'] = $modifierName !== '' ? $modifierName : ($modifier['email'] ?? '');
|
||||
$department['modified_by_uuid'] = $modifier['uuid'] ?? null;
|
||||
}
|
||||
}
|
||||
app(\MintyPHP\Service\Audit\AuditMetadataEnricher::class)->enrich($department);
|
||||
|
||||
$errorBag = formErrors();
|
||||
$errors = [];
|
||||
|
||||
@@ -22,25 +22,18 @@ $result = app(\MintyPHP\Service\Access\RoleService::class)->listPaged([
|
||||
$settingsDefaultsGateway = app(\MintyPHP\Service\Settings\SettingsDefaultsGateway::class);
|
||||
$defaultRoleId = $settingsDefaultsGateway->getDefaultRoleId();
|
||||
$rolePermissionRepository = app(\MintyPHP\Repository\Access\RolePermissionRepository::class);
|
||||
$roleIds = [];
|
||||
foreach ($result['rows'] as $roleRow) {
|
||||
$roleId = (int) ($roleRow['id'] ?? 0);
|
||||
if ($roleId > 0) {
|
||||
$roleIds[] = $roleId;
|
||||
}
|
||||
}
|
||||
$roleIds = array_values(array_unique($roleIds));
|
||||
$userRoleRepository = app(\MintyPHP\Repository\Access\UserRoleRepository::class);
|
||||
$roleUserCounts = $userRoleRepository->countUsersByRoleIds($roleIds);
|
||||
$roleActiveUserCounts = $userRoleRepository->countActiveUsersByRoleIds($roleIds);
|
||||
$rolePermissionCounts = $rolePermissionRepository->countPermissionsByRoleIds($roleIds);
|
||||
$userCounts = app(\MintyPHP\Service\Data\GridUserCountEnricher::class)->computeCounts(
|
||||
$result['rows'],
|
||||
$userRoleRepository->countUsersByRoleIds(...),
|
||||
$userRoleRepository->countActiveUsersByRoleIds(...)
|
||||
);
|
||||
$rolePermissionCounts = $rolePermissionRepository->countPermissionsByRoleIds(array_keys($userCounts));
|
||||
|
||||
$rows = [];
|
||||
foreach ($result['rows'] as $row) {
|
||||
$roleId = (int) ($row['id'] ?? 0);
|
||||
$usersTotal = (int) ($roleUserCounts[$roleId] ?? 0);
|
||||
$usersActive = (int) ($roleActiveUserCounts[$roleId] ?? 0);
|
||||
$usersInactive = max(0, $usersTotal - $usersActive);
|
||||
$counts = $userCounts[$roleId] ?? ['active_users' => 0, 'inactive_users' => 0];
|
||||
$rows[] = [
|
||||
'id' => $row['id'] ?? null,
|
||||
'uuid' => $row['uuid'] ?? '',
|
||||
@@ -48,8 +41,8 @@ foreach ($result['rows'] as $row) {
|
||||
'description' => $row['description'] ?? '',
|
||||
'code' => $row['code'] ?? '',
|
||||
'active' => (int) ($row['active'] ?? 1),
|
||||
'active_users' => $usersActive,
|
||||
'inactive_users' => $usersInactive,
|
||||
'active_users' => $counts['active_users'],
|
||||
'inactive_users' => $counts['inactive_users'],
|
||||
'permission_count' => (int) ($rolePermissionCounts[$roleId] ?? 0),
|
||||
'created' => dt($row['created'] ?? ''),
|
||||
'modified' => dt($row['modified'] ?? ''),
|
||||
|
||||
@@ -20,8 +20,6 @@ $permissionRepository = app(\MintyPHP\Repository\Access\PermissionRepository::cl
|
||||
$rolePermissionRepository = app(\MintyPHP\Repository\Access\RolePermissionRepository::class);
|
||||
$userRoleRepository = app(\MintyPHP\Repository\Access\UserRoleRepository::class);
|
||||
$userWriteRepository = app(\MintyPHP\Repository\User\UserWriteRepository::class);
|
||||
$userAccountService = app(\MintyPHP\Service\User\UserAccountService::class);
|
||||
|
||||
$uuid = trim((string) ($id ?? ''));
|
||||
$editTarget = requestPathWithReturnTarget("admin/roles/edit/{$uuid}", $returnTarget);
|
||||
$role = $uuid !== '' ? app(\MintyPHP\Service\Access\RoleService::class)->findByUuid($uuid) : null;
|
||||
@@ -42,24 +40,7 @@ $capabilities = $contextDecision->attribute('capabilities', []);
|
||||
$canUpdateRole = is_array($capabilities) ? (bool) ($capabilities['can_edit_role'] ?? false) : false;
|
||||
$canDeleteRole = is_array($capabilities) ? (bool) ($capabilities['can_delete_role'] ?? false) : false;
|
||||
|
||||
$creatorId = (int) ($role['created_by'] ?? 0);
|
||||
if ($creatorId > 0) {
|
||||
$creator = $userAccountService->findById($creatorId);
|
||||
if ($creator) {
|
||||
$creatorName = trim(($creator['first_name'] ?? '') . ' ' . ($creator['last_name'] ?? ''));
|
||||
$role['created_by_label'] = $creatorName !== '' ? $creatorName : ($creator['email'] ?? '');
|
||||
$role['created_by_uuid'] = $creator['uuid'] ?? null;
|
||||
}
|
||||
}
|
||||
$modifierId = (int) ($role['modified_by'] ?? 0);
|
||||
if ($modifierId > 0) {
|
||||
$modifier = $userAccountService->findById($modifierId);
|
||||
if ($modifier) {
|
||||
$modifierName = trim(($modifier['first_name'] ?? '') . ' ' . ($modifier['last_name'] ?? ''));
|
||||
$role['modified_by_label'] = $modifierName !== '' ? $modifierName : ($modifier['email'] ?? '');
|
||||
$role['modified_by_uuid'] = $modifier['uuid'] ?? null;
|
||||
}
|
||||
}
|
||||
app(\MintyPHP\Service\Audit\AuditMetadataEnricher::class)->enrich($role);
|
||||
|
||||
$errorBag = formErrors();
|
||||
$errors = [];
|
||||
|
||||
@@ -25,23 +25,19 @@ $tenantAvatarService = app(\MintyPHP\Service\Tenant\TenantAvatarService::class);
|
||||
$settingsDefaultsGateway = app(\MintyPHP\Service\Settings\SettingsDefaultsGateway::class);
|
||||
$settingsAppGateway = app(\MintyPHP\Service\Settings\SettingsAppGateway::class);
|
||||
|
||||
$gridUserCountEnricher = app(\MintyPHP\Service\Data\GridUserCountEnricher::class);
|
||||
$fetchRows = static function (
|
||||
array $tenantRows,
|
||||
array $themes,
|
||||
string $globalDefaultTheme
|
||||
) use ($userTenantRepository, $tenantMicrosoftAuthRepository, $tenantAvatarService, $settingsDefaultsGateway): array {
|
||||
$tenantIds = [];
|
||||
foreach ($tenantRows as $tenantRow) {
|
||||
$tenantId = (int) ($tenantRow['id'] ?? 0);
|
||||
if ($tenantId > 0) {
|
||||
$tenantIds[] = $tenantId;
|
||||
}
|
||||
}
|
||||
$tenantIds = array_values(array_unique($tenantIds));
|
||||
|
||||
) use ($userTenantRepository, $tenantMicrosoftAuthRepository, $tenantAvatarService, $settingsDefaultsGateway, $gridUserCountEnricher): array {
|
||||
$userCounts = $gridUserCountEnricher->computeCounts(
|
||||
$tenantRows,
|
||||
$userTenantRepository->countUsersByTenantIds(...),
|
||||
$userTenantRepository->countActiveUsersByTenantIds(...)
|
||||
);
|
||||
$tenantIds = array_keys($userCounts);
|
||||
$tenantSsoMap = $tenantMicrosoftAuthRepository->listByTenantIds($tenantIds);
|
||||
$tenantUserCounts = $userTenantRepository->countUsersByTenantIds($tenantIds);
|
||||
$tenantActiveUserCounts = $userTenantRepository->countActiveUsersByTenantIds($tenantIds);
|
||||
|
||||
$defaultTenantId = $settingsDefaultsGateway->getDefaultTenantId();
|
||||
$rows = [];
|
||||
@@ -67,9 +63,7 @@ $fetchRows = static function (
|
||||
$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);
|
||||
$counts = $userCounts[$tenantId] ?? ['active_users' => 0, 'inactive_users' => 0];
|
||||
$tenantStatus = TenantStatus::normalizeOr((string) ($row['status'] ?? ''), TenantStatus::Active);
|
||||
$tenantUuid = (string) ($row['uuid'] ?? '');
|
||||
|
||||
@@ -91,8 +85,8 @@ $fetchRows = static function (
|
||||
'enabled' => $ssoEnabled ? 1 : 0,
|
||||
'microsoft_only' => $ssoMicrosoftOnly ? 1 : 0,
|
||||
],
|
||||
'active_users' => $usersActive,
|
||||
'inactive_users' => $usersInactive,
|
||||
'active_users' => $counts['active_users'],
|
||||
'inactive_users' => $counts['inactive_users'],
|
||||
'created' => dt($row['created'] ?? ''),
|
||||
'modified' => dt($row['modified'] ?? ''),
|
||||
'has_avatar' => $tenantUuid !== '' && $tenantAvatarService->hasAvatar($tenantUuid),
|
||||
|
||||
@@ -19,8 +19,6 @@ $currentUserId = (int) ($session['user']['id'] ?? 0);
|
||||
if ($currentUserId > 0) {
|
||||
app(\MintyPHP\Service\Access\PermissionService::class)->getUserPermissions($currentUserId);
|
||||
}
|
||||
$userAccountService = app(\MintyPHP\Service\User\UserAccountService::class);
|
||||
|
||||
$uuid = trim((string) ($id ?? ''));
|
||||
$editTarget = requestPathWithReturnTarget("admin/tenants/edit/{$uuid}", $returnTarget);
|
||||
$tenant = $uuid !== '' ? app(\MintyPHP\Service\Tenant\TenantService::class)->findByUuid($uuid) : null;
|
||||
@@ -69,33 +67,7 @@ $customFieldDefinitions = $canManageCustomFields
|
||||
: [];
|
||||
$ssoConfig = $canManageSso ? $tenantSsoService->getTenantMicrosoftAuth($tenantId) : [];
|
||||
$ssoUiState = $canManageSso ? $tenantSsoService->buildMicrosoftUiState($tenantId, $ssoConfig) : [];
|
||||
$creatorId = (int) ($tenant['created_by'] ?? 0);
|
||||
if ($creatorId > 0) {
|
||||
$creator = $userAccountService->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 = $userAccountService->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 = $userAccountService->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;
|
||||
}
|
||||
}
|
||||
app(\MintyPHP\Service\Audit\AuditMetadataEnricher::class)->enrich($tenant, ['created_by', 'modified_by', 'status_changed_by']);
|
||||
|
||||
$errorBag = formErrors();
|
||||
$errors = [];
|
||||
|
||||
@@ -87,33 +87,7 @@ if (!$canViewPage) {
|
||||
}
|
||||
|
||||
if ($canViewUserMeta || $canViewUserAudit) {
|
||||
$creatorId = (int) ($user['created_by'] ?? 0);
|
||||
if ($creatorId > 0) {
|
||||
$creator = $userAccountService->findById($creatorId);
|
||||
if ($creator) {
|
||||
$creatorName = trim(($creator['first_name'] ?? '') . ' ' . ($creator['last_name'] ?? ''));
|
||||
$user['created_by_label'] = $creatorName !== '' ? $creatorName : ($creator['email'] ?? '');
|
||||
$user['created_by_uuid'] = $creator['uuid'] ?? null;
|
||||
}
|
||||
}
|
||||
$modifierId = (int) ($user['modified_by'] ?? 0);
|
||||
if ($modifierId > 0) {
|
||||
$modifier = $userAccountService->findById($modifierId);
|
||||
if ($modifier) {
|
||||
$modifierName = trim(($modifier['first_name'] ?? '') . ' ' . ($modifier['last_name'] ?? ''));
|
||||
$user['modified_by_label'] = $modifierName !== '' ? $modifierName : ($modifier['email'] ?? '');
|
||||
$user['modified_by_uuid'] = $modifier['uuid'] ?? null;
|
||||
}
|
||||
}
|
||||
$activeChangedById = (int) ($user['active_changed_by'] ?? 0);
|
||||
if ($activeChangedById > 0) {
|
||||
$activeChanger = $userAccountService->findById($activeChangedById);
|
||||
if ($activeChanger) {
|
||||
$activeChangerName = trim(($activeChanger['first_name'] ?? '') . ' ' . ($activeChanger['last_name'] ?? ''));
|
||||
$user['active_changed_by_label'] = $activeChangerName !== '' ? $activeChangerName : ($activeChanger['email'] ?? '');
|
||||
$user['active_changed_by_uuid'] = $activeChanger['uuid'] ?? null;
|
||||
}
|
||||
}
|
||||
app(\MintyPHP\Service\Audit\AuditMetadataEnricher::class)->enrich($user, ['created_by', 'modified_by', 'active_changed_by']);
|
||||
}
|
||||
|
||||
$errorBag = formErrors();
|
||||
|
||||
Reference in New Issue
Block a user