Fused activate and deactived user route
This commit is contained in:
parent
f6a2159177
commit
f0ae5a8c8a
|
|
@ -203,14 +203,16 @@ export default class extends Controller {
|
|||
e.preventDefault();
|
||||
const userId = target.getAttribute('data-id');
|
||||
if (confirm('Voulez-vous vraiment désactiver cet utilisateur ?')) {
|
||||
const formData = new FormData();
|
||||
formData.append('status', 'deactivate');
|
||||
|
||||
fetch(`/user/deactivate/${userId}`, {
|
||||
fetch(`/user/activeStatus/${userId}`, {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
headers: {'X-Requested-With': 'XMLHttpRequest'}
|
||||
})
|
||||
.then(async (response) => {
|
||||
if (response.ok) {
|
||||
// Option 1: update row status and re-render to switch icon
|
||||
const data = cell.getRow().getData();
|
||||
data.statut = false;
|
||||
cell.getRow().reformat();
|
||||
|
|
@ -228,9 +230,12 @@ export default class extends Controller {
|
|||
e.preventDefault();
|
||||
const userId = target.getAttribute('data-id');
|
||||
if (confirm('Voulez-vous réactiver cet utilisateur ?')) {
|
||||
const formData = new FormData();
|
||||
formData.append('status','activate');
|
||||
|
||||
fetch(`/user/activate/${userId}`, {
|
||||
fetch(`/user/activeStatus/${userId}`, {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
headers: {'X-Requested-With': 'XMLHttpRequest'}
|
||||
})
|
||||
.then(async (response) => {
|
||||
|
|
|
|||
|
|
@ -281,54 +281,45 @@ class UserController extends AbstractController
|
|||
}
|
||||
}
|
||||
|
||||
//TODO : MONOLOG
|
||||
#[Route('/deactivate/{id}', name: 'deactivate', methods: ['GET', 'POST'])]
|
||||
public function deactivate(int $id): Response
|
||||
|
||||
#[Route('/activeStatus/{id}', name: 'active_status', methods: ['GET', 'POST'])]
|
||||
public function activeStatus(int $id, Request $request): JsonResponse
|
||||
{
|
||||
$this->denyAccessUnlessGranted('ROLE_ADMIN');
|
||||
$actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier());
|
||||
if ($this->userService->hasAccessTo($actingUser, true)) {
|
||||
$user = $this->userRepository->find($id);
|
||||
if (!$user) {
|
||||
throw $this->createNotFoundException(self::NOT_FOUND);
|
||||
}
|
||||
$user->setIsActive(false);
|
||||
$user->setModifiedAt(new \DateTimeImmutable('now'));
|
||||
$this->userOrganizationService->deactivateAllUserOrganizationLinks($actingUser, $user);
|
||||
if ($this->userService->isUserConnected($user->getUserIdentifier())) {
|
||||
$this->userService->revokeUserTokens($user->getUserIdentifier());
|
||||
}
|
||||
$this->entityManager->persist($user);
|
||||
$this->entityManager->flush();
|
||||
$this->actionService->createAction("Deactivate user", $actingUser, null, $user->getUserIdentifier());
|
||||
try{
|
||||
if ($this->userService->hasAccessTo($actingUser, true)) {
|
||||
$user = $this->userRepository->find($id);
|
||||
if (!$user) {
|
||||
throw $this->createNotFoundException(self::NOT_FOUND);
|
||||
}
|
||||
$status = $request->get('status');
|
||||
if ($status === 'deactivate') {
|
||||
$user->setIsActive(false);
|
||||
$this->userOrganizationService->deactivateAllUserOrganizationLinks($actingUser, $user);
|
||||
if ($this->userService->isUserConnected($user->getUserIdentifier())) {
|
||||
$this->userService->revokeUserTokens($user->getUserIdentifier());
|
||||
}
|
||||
$user->setModifiedAt(new \DateTimeImmutable('now'));
|
||||
$this->entityManager->persist($user);
|
||||
$this->entityManager->flush();
|
||||
$this->logger->notice("User deactivated " . $user->getUserIdentifier());
|
||||
$this->actionService->createAction("Deactivate user", $actingUser, null, $user->getUserIdentifier());
|
||||
return new JsonResponse(['status' => 'deactivated'], Response::HTTP_OK);
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('user_index');
|
||||
if ($status === 'activate') {
|
||||
$user->setIsActive(true);
|
||||
$user->setModifiedAt(new \DateTimeImmutable('now'));
|
||||
$this->logger->notice("User activated " . $user->getUserIdentifier());
|
||||
$this->actionService->createAction("Activate user", $actingUser, null, $user->getUserIdentifier());
|
||||
return new JsonResponse(['status' => 'activated'], Response::HTTP_OK);
|
||||
}
|
||||
}
|
||||
}catch (\Exception $e){
|
||||
$this->logger->error($e->getMessage());
|
||||
}
|
||||
|
||||
throw $this->createAccessDeniedException(self::ACCESS_DENIED);
|
||||
}
|
||||
|
||||
//TODO : MONOLOG
|
||||
#[Route('/activate/{id}', name: 'activate', methods: ['GET', 'POST'])]
|
||||
public function activate(int $id): Response
|
||||
{
|
||||
$this->denyAccessUnlessGranted('ROLE_ADMIN');
|
||||
$actingUser = $this->userService->getUserByIdentifier($this->getUser()->getUserIdentifier());
|
||||
if ($this->userService->hasAccessTo($actingUser, true)) {
|
||||
$user = $this->userRepository->find($id);
|
||||
if (!$user) {
|
||||
throw $this->createNotFoundException(self::NOT_FOUND);
|
||||
}
|
||||
$user->setIsActive(true);
|
||||
$user->setModifiedAt(new \DateTimeImmutable('now'));
|
||||
$this->entityManager->persist($user);
|
||||
$this->entityManager->flush();
|
||||
$this->actionService->createAction("Activate user", $actingUser, null, $user->getUserIdentifier());
|
||||
|
||||
return $this->redirectToRoute('user_index');
|
||||
}
|
||||
|
||||
throw $this->createAccessDeniedException(self::ACCESS_DENIED);
|
||||
throw $this->createNotFoundException(self::NOT_FOUND);
|
||||
}
|
||||
|
||||
//TODO : MONOLOG
|
||||
|
|
|
|||
Loading…
Reference in New Issue