|
2 | 2 |
|
3 | 3 | namespace Webkul\UVDesk\CoreFrameworkBundle\Controller;
|
4 | 4 |
|
5 |
| -use Webkul\UVDesk\CoreFrameworkBundle\Entity\User; |
6 | 5 | use Symfony\Component\HttpFoundation\Request;
|
7 | 6 | use Symfony\Component\HttpFoundation\Response;
|
8 |
| -use Webkul\UVDesk\CoreFrameworkBundle\Entity\SavedFilters; |
| 7 | +use Symfony\Component\HttpFoundation\JsonResponse; |
9 | 8 | use Symfony\Component\EventDispatcher\GenericEvent;
|
10 | 9 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
11 |
| -use Webkul\UVDesk\CoreFrameworkBundle\Workflow\Events as CoreWorkflowEvents; |
12 | 10 | use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
|
13 | 11 | use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
14 |
| -use Webkul\UVDesk\CoreFrameworkBundle\Services\UserService; |
15 | 12 | use Symfony\Contracts\Translation\TranslatorInterface;
|
16 | 13 | use Symfony\Component\Filesystem\Filesystem as Fileservice;
|
17 | 14 | use Symfony\Component\DependencyInjection\ContainerInterface;
|
| 15 | +use Webkul\UVDesk\CoreFrameworkBundle\Entity\User; |
| 16 | +use Webkul\UVDesk\CoreFrameworkBundle\Entity\SavedFilters; |
| 17 | +use Webkul\UVDesk\CoreFrameworkBundle\Workflow\Events as CoreWorkflowEvents; |
| 18 | +use Webkul\UVDesk\CoreFrameworkBundle\Services\UserService; |
18 | 19 |
|
19 | 20 | class AccountXHR extends AbstractController
|
20 | 21 | {
|
@@ -45,52 +46,61 @@ public function listAgentsXHR(Request $request, ContainerInterface $container)
|
45 | 46 |
|
46 | 47 | public function deleteAgent(Request $request)
|
47 | 48 | {
|
48 |
| - if($request->getMethod() == "DELETE") { |
49 |
| - $em = $this->getDoctrine()->getManager(); |
50 |
| - $id = $request->query->get('id'); |
51 |
| - /* |
52 |
| - Original Code: $user = $em->getRepository('WebkulUserBundle:User')->findUserByCompany($id,$company->getId()); |
53 |
| - Using findUserByCompany() won't execute the UserListener, so user roles won't be set and user with ROLE_SUPER_ADMIN can be deleted as a result. |
54 |
| - To trigger UserListener to set roles, you need to only select 'u' instead of both 'u, dt' in query select clause. |
55 |
| - Doing this here instead of directly making changes to userRepository->findUserByCompany(). |
56 |
| - */ |
57 |
| - $user = $em->createQuery('SELECT u FROM UVDeskCoreFrameworkBundle:User u JOIN u.userInstance userInstance WHERE u.id = :userId AND userInstance.supportRole != :roles') |
58 |
| - ->setParameter('userId', $id) |
59 |
| - ->setParameter('roles', 4) |
60 |
| - ->getOneOrNullResult(); |
61 |
| - |
62 |
| - if ($user) { |
63 |
| - if($user->getAgentInstance()->getSupportRole() != "ROLE_SUPER_ADMIN") { |
64 |
| - |
65 |
| - // Trigger agent delete event |
66 |
| - $event = new GenericEvent(CoreWorkflowEvents\Agent\Delete::getId(), [ |
67 |
| - 'entity' => $user, |
68 |
| - ]); |
69 |
| - |
70 |
| - $this->eventDispatcher->dispatch($event, 'uvdesk.automation.workflow.execute'); |
71 |
| - |
72 |
| - // Removing profile image from physical path |
73 |
| - $fileService = new Fileservice; |
74 |
| - if ($user->getAgentInstance()->getProfileImagePath()) { |
75 |
| - $fileService->remove($this->getParameter('kernel.project_dir').'/public'.$user->getAgentInstance()->getProfileImagePath()); |
76 |
| - } |
77 |
| - |
78 |
| - $this->userService->removeAgent($user); |
79 |
| - |
80 |
| - $json['alertClass'] = 'success'; |
81 |
| - $json['alertMessage'] = $this->translator->trans('Success ! Agent removed successfully.'); |
82 |
| - } else { |
83 |
| - $json['alertClass'] = 'warning'; |
84 |
| - $json['alertMessage'] = $this->translator->trans("Warning ! You are allowed to remove account owner's account."); |
| 49 | + if ($request->getMethod() != "DELETE") { |
| 50 | + return new JsonResponse([ |
| 51 | + 'alertClass' => 'warning', |
| 52 | + 'alertMessage' => $this->translator->trans("How did you land here?"), |
| 53 | + ], 404); |
| 54 | + } |
| 55 | + |
| 56 | + $id = $request->query->get('id'); |
| 57 | + $entityManager = $this->getDoctrine()->getManager(); |
| 58 | + |
| 59 | + /* |
| 60 | + Original Code: $user = $em->getRepository('WebkulUserBundle:User')->findUserByCompany($id,$company->getId()); |
| 61 | + Using findUserByCompany() won't execute the UserListener, so user roles won't be set and user with ROLE_SUPER_ADMIN can be deleted as a result. |
| 62 | + To trigger UserListener to set roles, you need to only select 'u' instead of both 'u, dt' in query select clause. |
| 63 | + Doing this here instead of directly making changes to userRepository->findUserByCompany(). |
| 64 | + */ |
| 65 | + $user = $entityManager->createQueryBuilder() |
| 66 | + ->select('u') |
| 67 | + ->from(User::class, 'u') |
| 68 | + ->leftJoin('u.userInstance', 'userInstance') |
| 69 | + ->where('u.id = :userId')->setParameter('userId', $id) |
| 70 | + ->andWhere('userInstance.supportRole != :roles')->setParameter('roles', 4) |
| 71 | + ->getOneOrNullResult(1) |
| 72 | + ; |
| 73 | + |
| 74 | + if ($user) { |
| 75 | + if ($user->getAgentInstance()->getSupportRole() != "ROLE_SUPER_ADMIN") { |
| 76 | + // Trigger agent delete event |
| 77 | + $event = new GenericEvent(CoreWorkflowEvents\Agent\Delete::getId(), [ |
| 78 | + 'entity' => $user, |
| 79 | + ]); |
| 80 | + |
| 81 | + $this->eventDispatcher->dispatch($event, 'uvdesk.automation.workflow.execute'); |
| 82 | + |
| 83 | + // Removing profile image from physical path |
| 84 | + $fileService = new Fileservice; |
| 85 | + |
| 86 | + if ($user->getAgentInstance()->getProfileImagePath()) { |
| 87 | + $fileService->remove($this->getParameter('kernel.project_dir'). '/public' . $user->getAgentInstance()->getProfileImagePath()); |
85 | 88 | }
|
| 89 | + |
| 90 | + $this->userService->removeAgent($user); |
| 91 | + |
| 92 | + $json['alertClass'] = 'success'; |
| 93 | + $json['alertMessage'] = $this->translator->trans('Success ! Agent removed successfully.'); |
86 | 94 | } else {
|
87 |
| - $json['alertClass'] = 'danger'; |
88 |
| - $json['alertMessage'] = $this->translator->trans('Error ! Invalid user id.'); |
| 95 | + $json['alertClass'] = 'warning'; |
| 96 | + $json['alertMessage'] = $this->translator->trans("Warning ! You are allowed to remove account owner's account."); |
89 | 97 | }
|
| 98 | + } else { |
| 99 | + $json['alertClass'] = 'danger'; |
| 100 | + $json['alertMessage'] = $this->translator->trans('Error ! Invalid user id.'); |
90 | 101 | }
|
91 |
| - $response = new Response(json_encode($json)); |
92 |
| - $response->headers->set('Content-Type', 'application/json'); |
93 |
| - return $response; |
| 102 | + |
| 103 | + return new JsonResponse($json); |
94 | 104 | }
|
95 | 105 |
|
96 | 106 | public function savedFiltersXHR(Request $request)
|
|
0 commit comments