bundles/Suzuki/AccountBundle/Controller/AccountController.php line 23

Open in your IDE?
  1. <?php
  2. namespace Suzuki\AccountBundle\Controller;
  3. use Exception;
  4. use Sonata\AdminBundle\Controller\CRUDController;
  5. use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
  6. use Suzuki\AccountBundle\Entity\DealerShip;
  7. use Suzuki\AccountBundle\Entity\User;
  8. use Suzuki\CoreBundle\Helpers\CoreHelper;
  9. use Suzuki\SettingBundle\Constants\Constants;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  14. use function in_array;
  15. /**
  16.  * Class AccountController
  17.  *
  18.  * @package Suzuki\AccountBundle\Controller
  19.  */
  20. class AccountController extends CRUDController
  21. {
  22.     /**
  23.      * @param int|string|null $id
  24.      *
  25.      * @return RedirectResponse|Response
  26.      */
  27.     public function deleteAction($id)
  28.     {
  29.         $request $this->getRequest();
  30.         $id      $request->get($this->admin->getIdParameter());
  31.         $object  $this->admin->getObject($id);
  32.         if ( !$object) {
  33.             throw $this->createNotFoundException(
  34.                 sprintf(
  35.                     'unable to find the object with id: %s',
  36.                     $id
  37.                 )
  38.             );
  39.         }
  40.         $currentUserId $this->getUser()->getId(); // ID of the current user
  41.         if ($currentUserId == $id) {
  42.             $this->addFlash(
  43.                 'sonata_flash_error',
  44.                 'You cannot delete your own account.'
  45.             );
  46.             return $this->redirectTo($object);
  47.         }
  48.         return parent::deleteAction($id);
  49.     }
  50.     /**
  51.      * @param ProxyQueryInterface $query
  52.      *
  53.      * @return RedirectResponse
  54.      */
  55.     public function batchActionDelete(ProxyQueryInterface $query)
  56.     {
  57.         $currentUserId $this->getUser()->getId(); // ID of the current user
  58.         $selectedUsers $query->execute();
  59.         foreach ($selectedUsers as $selectedUser) {
  60.             if ($selectedUser->getId() == $currentUserId) {
  61.                 $this->addFlash(
  62.                     'sonata_flash_error',
  63.                     'You cannot delete your own account.'
  64.                 );
  65.                 return new RedirectResponse(
  66.                     $this->admin->generateUrl(
  67.                         'list',
  68.                         ['filter' => $this->admin->getFilterParameters()]
  69.                     )
  70.                 );
  71.             }
  72.         }
  73.         return parent::batchActionDelete($query);
  74.     }
  75.     /**
  76.      * @param null $id
  77.      *
  78.      * @return RedirectResponse|Response
  79.      * @throws Exception
  80.      */
  81.     public function editAction($id NULL)
  82.     {
  83.         $user         $this->getUser();
  84.         $coreHelper   = new CoreHelper();
  85.         $isAdmin      in_array(Constants::ROLE_ADMIN$user->getRoles());
  86.         $isSuperAdmin in_array(Constants::ROLE_SUPER_ADMIN$user->getRoles());
  87.         $em           $this->container->get('doctrine.orm.entity_manager');
  88.         $userRepo     $em->getRepository(User::class);
  89.         $targetUser   $userRepo->find($id);
  90.         if ($isAdmin && in_array(Constants::ROLE_SUPER_ADMIN$targetUser->getRoles())) {
  91.             throw new AccessDeniedHttpException("Access Denied");
  92.         }
  93.         if ( !$isSuperAdmin && !$isAdmin) {
  94.             $isAccess 1;
  95.             switch (TRUE) {
  96.                 case $id == $user->getID():
  97.                     $isAccess 1;
  98.                     break;
  99.                 case in_array(
  100.                     Constants::ROLE_REGION_MANAGER,
  101.                     $user->getRoles()
  102.                 ):
  103.                     if ( !empty($user->getRegion())) {
  104.                         $isAccess count(
  105.                             $this->getDoctrine()
  106.                                  ->getRepository(User::class)
  107.                                  ->checkAccessEditForm(
  108.                                      $id,
  109.                                      $user->getRegion()
  110.                                           ->getID()
  111.                                  )
  112.                         );
  113.                     }
  114.                     break;
  115.                 case in_array(Constants::ROLE_DEALER$user->getRoles()):
  116.                     if ( !empty($user->getDealerShip())) {
  117.                         $isAccess count(
  118.                             $this->getDoctrine()
  119.                                  ->getRepository(User::class)
  120.                                  ->checkAccessEditForm(
  121.                                      $id,
  122.                                      NULL,
  123.                                      $coreHelper->getAllIdByObject(
  124.                                          $user,
  125.                                          'getDealerShip'
  126.                                      )
  127.                                  )
  128.                         );
  129.                     }
  130.                     break;
  131.                 case in_array(Constants::ROLE_SELLER$user->getRoles()):
  132.                     $isAccess 0;
  133.                     break;
  134.                 default:
  135.                     $isAccess 0;
  136.             }
  137.             if ($isAccess == 0) {
  138.                 throw new AccessDeniedHttpException("Access Denied");
  139.             }
  140.         }
  141.         return parent::editAction($id);
  142.     }
  143.     /**
  144.      * @param Request $request
  145.      *
  146.      * @return Response
  147.      */
  148.     public function getDealerShipByRegionAction(Request $request)
  149.     {
  150.         $html             "";
  151.         $regionId         $request->request->get('regionId');
  152.         $userId           $request->request->get('userId');
  153.         $dealerShip       $this->getDoctrine()
  154.                                  ->getRepository(DealerShip::class)
  155.                                  ->getDealerShipEqualRegionVente($regionId);
  156.         $dealerShipByUser $this->getDoctrine()
  157.                                  ->getRepository(DealerShip::class)
  158.                                  ->getDealerShipByUser($userId);
  159.         $dealerShipByUser array_column($dealerShipByUser'id');
  160.         foreach ($dealerShip as $item) {
  161.             $title 'Suzuki - ' .
  162.                      $item->getReasonSocial() .
  163.                      ', ' .
  164.                      trim($item->getAdr3() . ' ' $item->getAdr4() . ' ' $item->getAdr4()) .
  165.                      ', ' .
  166.                      $item->getCodePostal() . ' ' $item->getVille();
  167.             $html .= '<option ' . (in_array(
  168.                     $item->getId(),
  169.                     $dealerShipByUser
  170.                 ) ? 'selected="selected"' "") . ' value="' $item->getId() . '" >' $title '</option>';
  171.         }
  172.         return new Response($html200);
  173.     }
  174. }