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