Deactivate user
This commit is contained in:
parent
52f3d2a3de
commit
7b7f58363a
|
|
@ -28,7 +28,7 @@ class UserController extends AbstractController
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly EntityManagerInterface $entityManager,
|
private readonly EntityManagerInterface $entityManager,
|
||||||
private readonly UserService $userService,
|
private readonly UserService $userService,
|
||||||
private readonly ActionService $actionService, private readonly UserOrganizationAppService $userOrganizationAppService,
|
private readonly ActionService $actionService, private readonly UserOrganizationAppService $userOrganizationAppService, private readonly UserOrganizationService $userOrganizationService,
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
@ -91,7 +91,7 @@ class UserController extends AbstractController
|
||||||
}
|
}
|
||||||
$uoa = $this->entityManager->getRepository(UserOrganizatonApp::class)->findBy(['userOrganization' => $uo, 'isActive' => true]);
|
$uoa = $this->entityManager->getRepository(UserOrganizatonApp::class)->findBy(['userOrganization' => $uo, 'isActive' => true]);
|
||||||
$uoa = $this->userOrganizationAppService->groupUserOrganizationAppsByApplication($uoa);
|
$uoa = $this->userOrganizationAppService->groupUserOrganizationAppsByApplication($uoa);
|
||||||
$this->actionService->createAction("View user information", $user, null, $user->getUserIdentifier());
|
$this->actionService->createAction("View user information", $actingUser, null, $user->getUserIdentifier());
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
//ignore
|
//ignore
|
||||||
}
|
}
|
||||||
|
|
@ -130,7 +130,7 @@ class UserController extends AbstractController
|
||||||
$user->setModifiedAt(new \DateTimeImmutable('now'));
|
$user->setModifiedAt(new \DateTimeImmutable('now'));
|
||||||
$this->entityManager->persist($user);
|
$this->entityManager->persist($user);
|
||||||
$this->entityManager->flush();
|
$this->entityManager->flush();
|
||||||
$this->actionService->createAction("Edit user information", $user, null, $user->getUserIdentifier());
|
$this->actionService->createAction("Edit user information", $actingUser, null, $user->getUserIdentifier());
|
||||||
|
|
||||||
return $this->redirectToRoute('user_show', ['id' => $user->getId()]);
|
return $this->redirectToRoute('user_show', ['id' => $user->getId()]);
|
||||||
}
|
}
|
||||||
|
|
@ -173,7 +173,7 @@ class UserController extends AbstractController
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
$this->actionService->createAction("Create new user", $user, null, $user->getUserIdentifier());
|
$this->actionService->createAction("Create new user", $actingUser, null, $user->getUserIdentifier());
|
||||||
}
|
}
|
||||||
$this->entityManager->flush();
|
$this->entityManager->flush();
|
||||||
|
|
||||||
|
|
@ -188,4 +188,27 @@ class UserController extends AbstractController
|
||||||
}
|
}
|
||||||
throw $this->createAccessDeniedException(self::ACCESS_DENIED);
|
throw $this->createAccessDeniedException(self::ACCESS_DENIED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[Route('/deactivate/{id}', name: 'deactivate', methods: ['GET', 'POST'])]
|
||||||
|
public function deactivate(int $id): Response
|
||||||
|
{
|
||||||
|
$this->denyAccessUnlessGranted('ROLE_ADMIN');
|
||||||
|
$actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier());
|
||||||
|
if ($this->userService->hasAccessTo($actingUser, true)) {
|
||||||
|
$user = $this->entityManager->getRepository(User::class)->find($id);
|
||||||
|
if (!$user) {
|
||||||
|
throw $this->createNotFoundException(self::NOT_FOUND);
|
||||||
|
}
|
||||||
|
$user->setIsActive(false);
|
||||||
|
$user->setModifiedAt(new \DateTimeImmutable('now'));
|
||||||
|
$this->userOrganizationService->deactivateAllUserOrganizationLinks($user, $actingUser);
|
||||||
|
$this->entityManager->persist($user);
|
||||||
|
$this->entityManager->flush();
|
||||||
|
$this->actionService->createAction("Deactivate user", $actingUser, null, $user->getUserIdentifier());
|
||||||
|
|
||||||
|
return $this->redirectToRoute('user_index');
|
||||||
|
}
|
||||||
|
|
||||||
|
throw $this->createAccessDeniedException(self::ACCESS_DENIED);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,19 @@
|
||||||
namespace App\Service;
|
namespace App\Service;
|
||||||
|
|
||||||
use App\Entity\UserOrganizatonApp;
|
use App\Entity\UserOrganizatonApp;
|
||||||
|
use App\Entity\UsersOrganizations;
|
||||||
|
use App\Service\ActionService;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
|
|
||||||
class UserOrganizationAppService
|
class UserOrganizationAppService
|
||||||
{
|
{
|
||||||
|
public function __construct(private readonly EntityManagerInterface $entityManager, private readonly ActionService $actionService)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Groups UserOrganizationApp entities by their associated Application.
|
||||||
|
*
|
||||||
* @param UserOrganizatonApp[] $userOrgApps
|
* @param UserOrganizatonApp[] $userOrgApps
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
|
|
@ -37,4 +46,20 @@ class UserOrganizationAppService
|
||||||
return array_values($grouped);
|
return array_values($grouped);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deactivates all the UserOrganizationApp entities for a given UserOrganization.
|
||||||
|
*
|
||||||
|
* @param UsersOrganizations $userOrganization
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function deactivateAllUserOrganizationsAppLinks(UsersOrganizations $userOrganization): void
|
||||||
|
{
|
||||||
|
$uoas = $this->entityManager->getRepository(UserOrganizatonApp::class)->findBy(['userOrganization' => $userOrganization, 'isActive' => true]);
|
||||||
|
foreach ($uoas as $uoa) {
|
||||||
|
$uoa->setIsActive(false);
|
||||||
|
$this->actionService->createAction("Deactivate UOA link", $userOrganization->getUsers(),
|
||||||
|
$userOrganization->getOrganization(), "App: " . $uoa->getApplication()->getName() . ", Role: " . $uoa->getRole()->getName());
|
||||||
|
$this->entityManager->persist($uoa);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,15 +3,11 @@
|
||||||
namespace App\Service;
|
namespace App\Service;
|
||||||
|
|
||||||
use App\Entity\Actions;
|
use App\Entity\Actions;
|
||||||
use App\Entity\Apps;
|
|
||||||
use App\Entity\Organizations;
|
|
||||||
use App\Entity\Roles;
|
|
||||||
use App\Entity\User;
|
use App\Entity\User;
|
||||||
use App\Service\ActionService;
|
|
||||||
use App\Service\UserService;
|
|
||||||
use App\Entity\UsersOrganizations;
|
use App\Entity\UsersOrganizations;
|
||||||
|
use App\Service\ActionService;
|
||||||
|
use \App\Service\UserOrganizationAppService;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Symfony\Bundle\SecurityBundle\Security;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Service pour la gestion des organisations d'utilisateurs.
|
* Service pour la gestion des organisations d'utilisateurs.
|
||||||
|
|
@ -21,12 +17,26 @@ readonly class UserOrganizationService
|
||||||
{
|
{
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private EntityManagerInterface $entityManager,
|
private userOrganizationAppService $userOrganizationAppService, private EntityManagerInterface $entityManager, private ActionService $actionService,
|
||||||
private UserService $userService,
|
|
||||||
private ActionService $actionService,
|
|
||||||
private Security $security
|
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deactive all user organization links.
|
||||||
|
*
|
||||||
|
* @param User $user
|
||||||
|
* @param User $actingUser
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function deactivateAllUserOrganizationLinks(User $user, User $actingUser): void{
|
||||||
|
$uos = $this->entityManager->getRepository(UsersOrganizations::class)->findBy(['users' => $user, 'isActive' => true]);
|
||||||
|
foreach ($uos as $uo) {
|
||||||
|
$this->userOrganizationAppService->deactivateAllUserOrganizationsAppLinks($uo);
|
||||||
|
$uo->setIsActive(false);
|
||||||
|
$this->entityManager->persist($uo);
|
||||||
|
$this->actionService->createAction("Deactivate UO link", $actingUser, $uo->getOrganization(), $uo->getOrganization()->getName() );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -79,15 +79,16 @@ class UserService
|
||||||
* Check if the user have the rights to access the page
|
* Check if the user have the rights to access the page
|
||||||
*
|
*
|
||||||
* @param User $user
|
* @param User $user
|
||||||
|
* @param bool $skipSelfCheck
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function hasAccessTo(User $user): bool
|
public function hasAccessTo(User $user, bool $skipSelfCheck = false): bool
|
||||||
{
|
{
|
||||||
if ($user->getUserIdentifier() === $this->security->getUser()->getUserIdentifier()) {
|
if (!$skipSelfCheck && $user->getUserIdentifier() === $this->security->getUser()->getUserIdentifier()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
$userOrganization = $this->entityManager->getRepository(UserOrganizatonApp::class)->findBy(['user' => $user]);
|
$userOrganization = $this->entityManager->getRepository(UsersOrganizations::class)->findBy(['users' => $user]);
|
||||||
if ($userOrganization) {
|
if ($userOrganization) {
|
||||||
foreach ($userOrganization as $uo) {
|
foreach ($userOrganization as $uo) {
|
||||||
if ($this->isAdminOfOrganization($uo)) {
|
if ($this->isAdminOfOrganization($uo)) {
|
||||||
|
|
@ -115,8 +116,8 @@ class UserService
|
||||||
public function isAdminOfOrganization(UsersOrganizations $usersOrganizations): bool
|
public function isAdminOfOrganization(UsersOrganizations $usersOrganizations): bool
|
||||||
{
|
{
|
||||||
$actingUser = $this->getUserByIdentifier($this->security->getUser()->getUserIdentifier());
|
$actingUser = $this->getUserByIdentifier($this->security->getUser()->getUserIdentifier());
|
||||||
$uo = $this->entityManager->getRepository(UsersOrganizations::class)->findOneBy(['user' => $actingUser]);
|
$uo = $this->entityManager->getRepository(UsersOrganizations::class)->findOneBy(['users' => $actingUser]);
|
||||||
$roleAdmin = $this->entityManager->getRepository(Roles::class)->findOneBy(['role' => 'ADMIN']);
|
$roleAdmin = $this->entityManager->getRepository(Roles::class)->findOneBy(['name' => 'ADMIN']);
|
||||||
if ($uo) {
|
if ($uo) {
|
||||||
$uoa = $this->entityManager->getRepository(UserOrganizatonApp::class)->findOneBy(['userOrganization' => $uo,
|
$uoa = $this->entityManager->getRepository(UserOrganizatonApp::class)->findOneBy(['userOrganization' => $uo,
|
||||||
'role' => $roleAdmin,
|
'role' => $roleAdmin,
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
{% if is_granted("ROLE_ADMIN") %}
|
{% if is_granted("ROLE_ADMIN") %}
|
||||||
<div class="col d-flex justify-content-between align-items-center ">
|
<div class="col d-flex justify-content-between align-items-center ">
|
||||||
<h1 class="mb-4">Gestion Utilisateur</h1>
|
<h1 class="mb-4">Gestion Utilisateur</h1>
|
||||||
{# <a href="{{ path('user_deactivate', {'id': user.id}) }}" class="btn btn-danger">Désactiver</a> #}
|
<a href="{{ path('user_deactivate', {'id': user.id}) }}" class="btn btn-danger">Désactiver</a>
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% include 'user/userInformation.html.twig' %}
|
{% include 'user/userInformation.html.twig' %}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue