Deactivate roles
This commit is contained in:
parent
e87fdd32e4
commit
0a602fb52e
|
|
@ -112,9 +112,30 @@ readonly class UserOrganizationService
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set user organizations with roles, ensuring USER role is always present
|
||||
*
|
||||
* @param User $user
|
||||
* @param Organizations $organization
|
||||
* @param array $selectedRoles Array of role IDs to set for the user in the organization
|
||||
*/
|
||||
|
||||
public function setUserOrganizations(User $user, Organizations $organization, array $selectedRoles): void
|
||||
{
|
||||
$repo = $this->entityManager->getRepository(UsersOrganizations::class);
|
||||
$roleRepo = $this->entityManager->getRepository(Roles::class);
|
||||
|
||||
// Get the USER role entity
|
||||
$userRole = $roleRepo->findOneBy(['name' => 'USER']);
|
||||
if (!$userRole) {
|
||||
throw new \RuntimeException('USER role not found');
|
||||
}
|
||||
|
||||
// If USER role is not selected, deactivate all roles and return
|
||||
if (!in_array($userRole->getId(), $selectedRoles)) {
|
||||
$this->deactivateAllUserRoles($user, $organization);
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. Get all current UsersOrganizations for this user/org
|
||||
$currentUserOrgs = $repo->findBy([
|
||||
|
|
@ -128,23 +149,42 @@ readonly class UserOrganizationService
|
|||
$currentRolesMap[$uo->getRole()->getId()] = $uo;
|
||||
}
|
||||
|
||||
// 3. Add new roles that are selected but not present
|
||||
// 3. Check if we need to ensure USER role exists
|
||||
$hasNonUserRole = false;
|
||||
foreach ($selectedRoles as $roleId) {
|
||||
if ($roleId !== $userRole->getId()) {
|
||||
$hasNonUserRole = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 4. If we have non-USER roles, ensure USER role is present
|
||||
if ($hasNonUserRole && !in_array($userRole->getId(), $selectedRoles)) {
|
||||
$selectedRoles[] = $userRole->getId();
|
||||
}
|
||||
|
||||
// 5. Add new roles that are selected but not present
|
||||
foreach ($selectedRoles as $roleId) {
|
||||
if (!isset($currentRolesMap[$roleId])) {
|
||||
$roleEntity = $this->entityManager->getRepository(Roles::class)->find($roleId);
|
||||
$roleEntity = $roleRepo->find($roleId);
|
||||
if ($roleEntity) {
|
||||
$newUserOrganization = new UsersOrganizations();
|
||||
$newUserOrganization->setUsers($user);
|
||||
$newUserOrganization->setRole($roleEntity);
|
||||
$newUserOrganization->setOrganization($organization);
|
||||
$newUserOrganization->setIsActive(true); // Ensure new roles are active
|
||||
$this->entityManager->persist($newUserOrganization);
|
||||
}
|
||||
} else {
|
||||
// If role exists but was inactive, reactivate it
|
||||
$currentRolesMap[$roleId]->setIsActive(true);
|
||||
$this->entityManager->persist($currentRolesMap[$roleId]);
|
||||
}
|
||||
// Remove from map so we know which ones to delete later
|
||||
// Remove from map so we know which ones to deactivate later
|
||||
unset($currentRolesMap[$roleId]);
|
||||
}
|
||||
|
||||
// 4. Remove roles that are present but not selected (deactivate them)
|
||||
// 6. Remove roles that are present but not selected (deactivate them)
|
||||
foreach ($currentRolesMap as $uo) {
|
||||
$uo->setIsActive(false);
|
||||
$this->entityManager->persist($uo);
|
||||
|
|
@ -186,4 +226,24 @@ readonly class UserOrganizationService
|
|||
$this->entityManager->persist($uoEntity);
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deactivate all roles if role USER is deactivated
|
||||
* @param User $user
|
||||
* @param Organizations $organization
|
||||
* @return void
|
||||
*/
|
||||
public function deactivateAllUserRoles(User $user, Organizations $organization): void
|
||||
{
|
||||
$repo = $this->entityManager->getRepository(UsersOrganizations::class);
|
||||
$userOrganizations = $repo->findBy([
|
||||
'users' => $user,
|
||||
'organization' => $organization
|
||||
]);
|
||||
foreach ($userOrganizations as $uo) {
|
||||
$uo->setIsActive(false);
|
||||
$this->entityManager->persist($uo);
|
||||
}
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue