major update
This commit is contained in:
@@ -4,6 +4,7 @@ namespace MintyPHP\Service\User;
|
||||
|
||||
use MintyPHP\Repository\Access\UserRoleRepository;
|
||||
use MintyPHP\Repository\Org\UserDepartmentRepository;
|
||||
use MintyPHP\Repository\Support\DatabaseSessionRepository;
|
||||
use MintyPHP\Repository\Tenant\UserTenantRepository;
|
||||
use MintyPHP\Repository\User\UserWriteRepository;
|
||||
|
||||
@@ -15,7 +16,8 @@ class UserAssignmentService
|
||||
private readonly UserRoleRepository $userRoleRepository,
|
||||
private readonly UserDepartmentRepository $userDepartmentRepository,
|
||||
private readonly UserDirectoryGateway $directoryGateway,
|
||||
private readonly UserPermissionGateway $permissionGateway
|
||||
private readonly UserPermissionGateway $permissionGateway,
|
||||
private readonly DatabaseSessionRepository $databaseSessionRepository
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -224,6 +226,38 @@ class UserAssignmentService
|
||||
$this->userWriteRepository->bumpAuthzVersion($userId);
|
||||
}
|
||||
|
||||
public function syncAllAssignments(int $userId, array $tenantIds, array $roleIds, array $departmentIds): bool
|
||||
{
|
||||
if ($userId <= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$transactionStarted = false;
|
||||
try {
|
||||
$this->databaseSessionRepository->beginTransaction();
|
||||
$transactionStarted = true;
|
||||
|
||||
$ok = $this->syncTenants($userId, $tenantIds, false)
|
||||
&& $this->syncRoles($userId, $roleIds, false)
|
||||
&& $this->syncDepartments($userId, $departmentIds, false);
|
||||
if (!$ok) {
|
||||
$this->rollbackQuietly();
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->bumpAuthzVersion($userId);
|
||||
$this->databaseSessionRepository->commitTransaction();
|
||||
$transactionStarted = false;
|
||||
|
||||
return true;
|
||||
} catch (\Throwable $exception) {
|
||||
if ($transactionStarted) {
|
||||
$this->rollbackQuietly();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function normalizeIdInput($value): array
|
||||
{
|
||||
$raw = is_array($value) ? $value : [$value];
|
||||
@@ -271,4 +305,13 @@ class UserAssignmentService
|
||||
}
|
||||
return $ids;
|
||||
}
|
||||
|
||||
private function rollbackQuietly(): void
|
||||
{
|
||||
try {
|
||||
$this->databaseSessionRepository->rollbackTransaction();
|
||||
} catch (\Throwable $exception) {
|
||||
// no-op
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user