major update
This commit is contained in:
@@ -3,40 +3,41 @@
|
||||
namespace MintyPHP\Service\Access;
|
||||
|
||||
use MintyPHP\Repository\Access\RoleRepository;
|
||||
use MintyPHP\Service\Audit\SystemAuditService;
|
||||
use MintyPHP\Service\Directory\DirectorySettingsGateway;
|
||||
use MintyPHP\Service\Settings\SettingServicesFactory;
|
||||
|
||||
class RoleService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ?RoleRepository $roleRepository = null,
|
||||
private readonly ?DirectorySettingsGateway $settingsGateway = null
|
||||
private readonly RoleRepository $roleRepository,
|
||||
private readonly DirectorySettingsGateway $settingsGateway,
|
||||
private readonly SystemAuditService $systemAuditService
|
||||
) {
|
||||
}
|
||||
|
||||
public function list(): array
|
||||
{
|
||||
return $this->roleRepository()->list();
|
||||
return $this->roleRepository->list();
|
||||
}
|
||||
|
||||
public function listActive(): array
|
||||
{
|
||||
return $this->roleRepository()->listActive();
|
||||
return $this->roleRepository->listActive();
|
||||
}
|
||||
|
||||
public function listPaged(array $options): array
|
||||
{
|
||||
return $this->roleRepository()->listPaged($options);
|
||||
return $this->roleRepository->listPaged($options);
|
||||
}
|
||||
|
||||
public function findByUuid(string $uuid): ?array
|
||||
{
|
||||
return $this->roleRepository()->findByUuid($uuid);
|
||||
return $this->roleRepository->findByUuid($uuid);
|
||||
}
|
||||
|
||||
public function findById(int $id): ?array
|
||||
{
|
||||
return $this->roleRepository()->find($id);
|
||||
return $this->roleRepository->find($id);
|
||||
}
|
||||
|
||||
public function createFromAdmin(array $input, int $currentUserId = 0): array
|
||||
@@ -48,7 +49,7 @@ class RoleService
|
||||
return ['ok' => false, 'errors' => $errors, 'form' => $form];
|
||||
}
|
||||
|
||||
$createdId = $this->roleRepository()->create([
|
||||
$createdId = $this->roleRepository->create([
|
||||
'description' => $form['description'],
|
||||
'code' => $form['code'] ?: null,
|
||||
'active' => $form['active'],
|
||||
@@ -59,11 +60,20 @@ class RoleService
|
||||
return ['ok' => false, 'errors' => [t('Role can not be created')], 'form' => $form];
|
||||
}
|
||||
|
||||
$createdRole = $this->roleRepository()->find((int) $createdId);
|
||||
$createdRole = $this->roleRepository->find((int) $createdId);
|
||||
$uuid = $createdRole['uuid'] ?? null;
|
||||
if (!empty($input['is_default'])) {
|
||||
$this->settingsGateway()->setDefaultRoleId((int) $createdId);
|
||||
$this->settingsGateway->setDefaultRoleId((int) $createdId);
|
||||
}
|
||||
|
||||
$this->systemAuditService->record('admin.roles.create', 'success', [
|
||||
'actor_user_id' => $currentUserId > 0 ? $currentUserId : null,
|
||||
'target_type' => 'role',
|
||||
'target_id' => (int) $createdId,
|
||||
'target_uuid' => is_string($uuid) ? $uuid : '',
|
||||
'metadata' => ['active' => $form['active']],
|
||||
]);
|
||||
|
||||
return ['ok' => true, 'form' => $form, 'uuid' => $uuid, 'id' => (int) $createdId];
|
||||
}
|
||||
|
||||
@@ -76,7 +86,9 @@ class RoleService
|
||||
return ['ok' => false, 'errors' => $errors, 'form' => $form];
|
||||
}
|
||||
|
||||
$updated = $this->roleRepository()->update($roleId, [
|
||||
$existingBefore = $this->roleRepository->find($roleId) ?? [];
|
||||
|
||||
$updated = $this->roleRepository->update($roleId, [
|
||||
'description' => $form['description'],
|
||||
'code' => $form['code'] ?: null,
|
||||
'active' => $form['active'],
|
||||
@@ -87,6 +99,15 @@ class RoleService
|
||||
return ['ok' => false, 'errors' => [t('Role can not be updated')], 'form' => $form];
|
||||
}
|
||||
|
||||
$this->systemAuditService->record('admin.roles.update', 'success', [
|
||||
'actor_user_id' => $currentUserId > 0 ? $currentUserId : null,
|
||||
'target_type' => 'role',
|
||||
'target_id' => $roleId,
|
||||
'target_uuid' => (string) ($existingBefore['uuid'] ?? ''),
|
||||
'before' => ['active' => $existingBefore['active'] ?? null],
|
||||
'after' => ['active' => $form['active']],
|
||||
]);
|
||||
|
||||
return ['ok' => true, 'form' => $form];
|
||||
}
|
||||
|
||||
@@ -97,7 +118,7 @@ class RoleService
|
||||
return ['ok' => false, 'status' => 404, 'error' => 'not_found'];
|
||||
}
|
||||
|
||||
$role = $this->roleRepository()->findByUuid($uuid);
|
||||
$role = $this->roleRepository->findByUuid($uuid);
|
||||
if (!$role || !isset($role['id'])) {
|
||||
return ['ok' => false, 'status' => 404, 'error' => 'not_found'];
|
||||
}
|
||||
@@ -106,11 +127,17 @@ class RoleService
|
||||
return ['ok' => false, 'status' => 403, 'error' => 'admin_role_protected'];
|
||||
}
|
||||
|
||||
$deleted = $this->roleRepository()->delete((int) $role['id']);
|
||||
$deleted = $this->roleRepository->delete((int) $role['id']);
|
||||
if (!$deleted) {
|
||||
return ['ok' => false, 'status' => 500, 'error' => 'delete_failed'];
|
||||
}
|
||||
|
||||
$this->systemAuditService->record('admin.roles.delete', 'success', [
|
||||
'target_type' => 'role',
|
||||
'target_id' => (int) $role['id'],
|
||||
'target_uuid' => (string) ($role['uuid'] ?? ''),
|
||||
]);
|
||||
|
||||
return ['ok' => true, 'role' => $role];
|
||||
}
|
||||
|
||||
@@ -130,7 +157,7 @@ class RoleService
|
||||
$errors[] = t('Description cannot be empty');
|
||||
}
|
||||
$code = $form['code'] ?? '';
|
||||
if ($code !== '' && $this->roleRepository()->existsByCode($code, $excludeId)) {
|
||||
if ($code !== '' && $this->roleRepository->existsByCode($code, $excludeId)) {
|
||||
$errors[] = t('Role code already exists');
|
||||
}
|
||||
return $errors;
|
||||
@@ -145,19 +172,4 @@ class RoleService
|
||||
return 1;
|
||||
}
|
||||
|
||||
private function roleRepository(): RoleRepository
|
||||
{
|
||||
return $this->roleRepository ?? new RoleRepository();
|
||||
}
|
||||
|
||||
private function settingsGateway(): DirectorySettingsGateway
|
||||
{
|
||||
if ($this->settingsGateway instanceof DirectorySettingsGateway) {
|
||||
return $this->settingsGateway;
|
||||
}
|
||||
|
||||
return new DirectorySettingsGateway(
|
||||
(new SettingServicesFactory())->createSettingGateway()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user