refactor: fix 7 CRUD system inconsistencies for robustness and maintainability
- Add user-assignment dependency check to DepartmentService.deleteByUuid (409 when users assigned)
- Standardize POST detection to isMethod('POST') in 10 create/edit actions
- Align RoleService code uniqueness to warning pattern (matching departments)
- Normalize repository create() return types from mixed to int|false (3 repos + 3 interfaces)
- Add JSON response branches to 4 non-user delete actions
- Document delete authorization ordering pattern
- Decompose AdminSettingsService.updateFromAdmin into domain-scoped private methods
Workflow: .agents/runs/CRUD-CONSISTENCY-001/
Reviewed by: Code Reviewer (pass), Security Reviewer (pass), Acceptance Tester (pass)
Quality gates: QG-001 PHPUnit pass, QG-002 PHPStan pass, QG-003 Architecture pass
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -43,10 +43,11 @@ class RoleService
|
||||
public function createFromAdmin(array $input, int $currentUserId = 0): array
|
||||
{
|
||||
$form = $this->sanitizeBase($input);
|
||||
$errors = $this->validateBase($form, 0);
|
||||
$errors = $this->validateBase($form);
|
||||
$warnings = $this->warningsForCode($form, 0);
|
||||
|
||||
if ($errors) {
|
||||
return ['ok' => false, 'errors' => $errors, 'form' => $form];
|
||||
return ['ok' => false, 'errors' => $errors, 'warnings' => $warnings, 'form' => $form];
|
||||
}
|
||||
|
||||
$createdId = $this->roleRepository->create([
|
||||
@@ -74,16 +75,17 @@ class RoleService
|
||||
'metadata' => ['active' => $form['active']],
|
||||
]);
|
||||
|
||||
return ['ok' => true, 'form' => $form, 'uuid' => $uuid, 'id' => (int) $createdId];
|
||||
return ['ok' => true, 'form' => $form, 'warnings' => $warnings, 'uuid' => $uuid, 'id' => (int) $createdId];
|
||||
}
|
||||
|
||||
public function updateFromAdmin(int $roleId, array $input, int $currentUserId = 0): array
|
||||
{
|
||||
$form = $this->sanitizeBase($input);
|
||||
$errors = $this->validateBase($form, $roleId);
|
||||
$errors = $this->validateBase($form);
|
||||
$warnings = $this->warningsForCode($form, $roleId);
|
||||
|
||||
if ($errors) {
|
||||
return ['ok' => false, 'errors' => $errors, 'form' => $form];
|
||||
return ['ok' => false, 'errors' => $errors, 'warnings' => $warnings, 'form' => $form];
|
||||
}
|
||||
|
||||
$existingBefore = $this->roleRepository->find($roleId) ?? [];
|
||||
@@ -108,7 +110,7 @@ class RoleService
|
||||
'after' => ['active' => $form['active']],
|
||||
]);
|
||||
|
||||
return ['ok' => true, 'form' => $form];
|
||||
return ['ok' => true, 'form' => $form, 'warnings' => $warnings];
|
||||
}
|
||||
|
||||
public function deleteByUuid(string $uuid): array
|
||||
@@ -150,17 +152,25 @@ class RoleService
|
||||
];
|
||||
}
|
||||
|
||||
private function validateBase(array $form, int $excludeId): array
|
||||
private function validateBase(array $form): array
|
||||
{
|
||||
$errors = [];
|
||||
if ($form['description'] === '') {
|
||||
$errors[] = t('Description cannot be empty');
|
||||
}
|
||||
return $errors;
|
||||
}
|
||||
|
||||
// Duplicate codes are a warning, not a hard error — codes are optional identifiers,
|
||||
// not unique keys, so duplicates are allowed but surfaced to the user.
|
||||
private function warningsForCode(array $form, int $excludeId): array
|
||||
{
|
||||
$warnings = [];
|
||||
$code = $form['code'] ?? '';
|
||||
if ($code !== '' && $this->roleRepository->existsByCode($code, $excludeId)) {
|
||||
$errors[] = t('Role code already exists');
|
||||
$warnings[] = t('Role code already exists');
|
||||
}
|
||||
return $errors;
|
||||
return $warnings;
|
||||
}
|
||||
|
||||
private function normalizeActive($value): int
|
||||
|
||||
Reference in New Issue
Block a user