src/Application/Internit/ContentBundle/Controller/CRUD.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Application\Internit\ContentBundle\Controller;
  3. use App\Application\Internit\ContentBundle\Controller\FileUploader;
  4. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  9. use App\Application\Internit\RealEstateBundle\Entity\Realty;
  10. use App\Application\Internit\RealEstateBundle\Entity\RealEstate;
  11. use Sonata\AdminBundle\Controller\CRUDController;
  12. use App\Application\Internit\RealEstateBundle\Entity\File;
  13. use Doctrine\Common\Collections\ArrayCollection;
  14. use Symfony\Component\HttpFoundation\File\UploadedFile;
  15. use App\Application\Internit\RealEstateBundle\Entity\Media;
  16. class CRUD extends CRUDController
  17. {
  18.     public $bundle '';
  19.     protected $allActionRoles    = array('ROLE_MASTER_ADMIN''ROLE_CONSTRUTORA''ROLE_IMOBILIARIA''ROLE_CORRETOR''ROLE_AGENCIA');
  20.     protected $adminActionRoles  = array('ROLE_MASTER_ADMIN');
  21.     protected $createActionRoles = array('ROLE_MASTER_ADMIN');
  22.     protected $editActionRoles   = array('ROLE_MASTER_ADMIN');
  23.     protected $deleteActionRoles = array('ROLE_MASTER_ADMIN');
  24.     protected $listActionRoles   = array('ROLE_MASTER_ADMIN');
  25.     protected $showActionRoles   = array('ROLE_MASTER_ADMIN');
  26.     public function validateAccess(){
  27.     }
  28.     public function getUrlMedia($media$format)
  29.     {        
  30.         $mimes = new \Mimey\MimeTypes;
  31.         return "thumb_".$media->getId()."_".$media->getContext()."_".$format.".".pathinfo($media->getProviderReference(), PATHINFO_EXTENSION);
  32.     }
  33.     public function deleteAction($id)
  34.     {
  35.         if(!$this->permissionByRole($this->deleteActionRoles$this->getUser()))
  36.             return $this->renderWithExtraParams("@ApplicationInternit/ContentBundle/Resources/views/permission.html.twig");
  37.         $object $this->admin->getObject($id);
  38.         //dump($object);
  39.         $this->admin->delete($object);
  40.         return $this->listAction();
  41.     }
  42.     public function createAction()
  43.     {
  44.         if(!$this->permissionByRole($this->createActionRoles$this->getUser()))
  45.             return $this->renderWithExtraParams("@ApplicationInternit/ContentBundle/Resources/views/permission.html.twig");
  46.         //return parent::createAction();             
  47.         $request $this->getRequest();
  48.         // the key used to lookup the template
  49.         $templateKey 'edit';
  50.         $this->admin->checkAccess('create');
  51.         
  52.         $class = new \ReflectionClass($this->admin->hasActiveSubClass() ? $this->admin->getActiveSubClass() : $this->admin->getClass());
  53.         
  54.         $newObject $this->admin->getNewInstance();
  55.         $preResponse $this->preCreate($request$newObject);
  56.         if (null !== $preResponse) {
  57.             return $preResponse;
  58.         }
  59.         $this->admin->setSubject($newObject);
  60.         $form $this->admin->getForm();
  61.        
  62.         $form->setData($newObject);
  63.         $form->handleRequest($request);
  64.         
  65.         if ($form->isSubmitted()) {
  66.             
  67.             $isFormValid $form->isValid();            
  68.             
  69.             // persist if the form was valid and if in preview mode the preview was approved
  70.             if ($isFormValid && (!$this->isInPreviewMode() || $this->isPreviewApproved())) {
  71.                 $this->persistImageApresentation($form);
  72.                 $submittedObject $form->getData();
  73.                 $this->admin->setSubject($submittedObject);
  74.                 $this->admin->checkAccess('create'$submittedObject);                
  75.                 try {
  76.                     
  77.                     $newObject $this->admin->create($submittedObject);
  78.                     if ($this->isXmlHttpRequest()) {
  79.                         return $this->renderJson([
  80.                             'result' => 'ok',
  81.                             'objectId' => $this->admin->getNormalizedIdentifier($newObject),
  82.                             'objectName' => $this->escapeHtml($this->admin->toString($newObject)),
  83.                         ], 200, []);
  84.                     }
  85.                     $this->get('session')->getFlashBag()->set('flash_create_success''Mensagem enviada com sucesso');
  86.                                         
  87.                     // redirect to edit mode
  88.                     return $this->redirectTo($newObject);
  89.                 } catch (ModelManagerException $e) {
  90.                     $this->handleModelManagerException($e);
  91.                     $isFormValid false;
  92.                 }
  93.             }
  94.             
  95.             // show an error message if the form failed validation
  96.             if (!$isFormValid) {
  97.                 if (!$this->isXmlHttpRequest()) 
  98.                 {        
  99.                     $errors = array();
  100.                     foreach ($form->getErrors(true) as $error) {
  101.                         $errors[] = $error->getMessage();
  102.                     } 
  103.                       
  104.                     $this->get('session')->getFlashBag()->set('flash_create_error'implode('<br>'$errors));
  105.                 }
  106.             } elseif ($this->isPreviewRequested()) {
  107.                 // pick the preview template if the form was valid and preview was requested
  108.                 $templateKey 'preview';
  109.                 $this->admin->getShow();
  110.             }
  111.         }
  112.         $formView $form->createView();
  113.     
  114.         return $this->renderWithExtraParams($this->base."create.html.twig", [
  115.             'form' => $formView
  116.         ], null);   
  117.     }
  118.     public function listAction($order = array('id' => 'DESC'))
  119.     {
  120.         if(!$this->permissionByRole($this->listActionRoles$this->getUser()))
  121.             return $this->renderWithExtraParams("@ApplicationInternit/ContentBundle/Resources/views/permission.html.twig");
  122.         /*
  123.         if (!$this->get('security.authorization_checker')->isGranted('ROLE_IMOBILIARIA')) 
  124.         {
  125.             $realEstates = $this->get('security.token_storage')->getToken()->getUser()->getRealEstates();
  126.             $arrayRealEstates = array();
  127.             foreach($realEstates as $r)
  128.             {
  129.                 $arrayRealEstates[] = $r->getId();
  130.             }
  131.             
  132.             //var_dump("im.id = ".implode(' or im.id = ', $arrayRealEstates));exit;
  133.             $where = "im.id = ".implode(' or im.id = ', $arrayRealEstates); 
  134.             //ver todos os seus corretores
  135.         }*/
  136.         $where = array();
  137.         //var_dump($this->get('security.token_storage')->getToken()->getUser()->getRoles());
  138.         //Edit
  139.         /*if (in_array('ROLE_CORRETOR', $this->get('security.token_storage')->getToken()->getUser()->getRoles()))
  140.             $where = array('user'=>$this->get('security.token_storage')->getToken()->getUser()->getid());*/
  141.         $data $this->getDoctrine()->getRepository($this->bundle)->findBy($where$order);
  142.         return $this->renderWithExtraParams($this->base."list.html.twig", [
  143.             'action' => 'list',
  144.             'datas' => $data,
  145.             'order' => $order
  146.         ], null);       
  147.     }
  148.     public function showAction($id null)
  149.     {
  150.         if(!$this->permissionByRole($this->showActionRoles$this->getUser()))
  151.             return $this->renderWithExtraParams("@ApplicationInternit/ContentBundle/Resources/views/permission.html.twig");
  152.         $request $this->getRequest();
  153.         $id $request->get($this->admin->getIdParameter());        
  154.         $data $this->getDoctrine()->getRepository($this->bundle)->find($id);
  155.         $template $this->base.'show.html.twig';
  156.         return $this->renderWithExtraParams($template, [
  157.             'action' => 'show',
  158.             'data' => $data
  159.             ,
  160.         ], null);
  161.     }
  162.     public function editAction($id null)
  163.     {
  164.         if(!$this->permissionByRole($this->editActionRoles$this->getUser()))
  165.             return $this->renderWithExtraParams("@ApplicationInternit/ContentBundle/Resources/views/permission.html.twig");
  166.         //var_dump($_REQUEST);exit;
  167.        // $realty = new Realty();
  168.         $request $this->getRequest();        
  169.         $templateKey 'edit';
  170.         $id $request->get($this->admin->getIdParameter());       
  171.         $existingObject $this->admin->getObject($id);
  172.         
  173.         //var_dump($existingObject->getPlainPassword());exit;
  174.         
  175.         if (!$existingObject) {
  176.             throw $this->createNotFoundException(sprintf('unable to find the object with id: %s'$id));
  177.         }
  178.         
  179.         $form $this->admin->getForm();
  180.         //var_dump($form['rgDoc']); exit;
  181.         
  182.         $form->setData($existingObject);
  183.         //var_dump($request);exit;
  184.         $form->handleRequest($request);
  185.         //var_dump($form->getData()->getPassword());exit;
  186.         //var_dump($form['realestates']);exit;
  187.        
  188.         if ($form->isSubmitted()) {
  189.             
  190.             $isFormValid $form->isValid();
  191.             
  192.            
  193.             // persist if the form was valid and if in preview mode the preview was approved
  194.             //if ($isFormValid && (!$this->isInPreviewMode() || $this->isPreviewApproved())) {                
  195.                 
  196.                 //dump($form);
  197.                 //exit;
  198.                 //##########UPLOAD##########   
  199.                
  200.                 if(isset($form['gallerysFiles']) || isset($form['files']) || isset($form['gallerys']) || isset($form['imageApresentation']))
  201.                 {     
  202.                     if ($form['gallerysFiles']) 
  203.                     {                        
  204.                         //$fileUploader = $this->get('admin.upload.file.service');   
  205.                         $type 'application/pdf, text/plain';
  206.                         $name 'files';                     
  207.                         $i 0;                    
  208.                         
  209.                         foreach($form['gallerysFiles'] as $bf)
  210.                         {  
  211.                             $files = new ArrayCollection();
  212.                             if($bf[$name])
  213.                             {      
  214.                                 $FILE = array();           
  215.                                 foreach($bf[$name]->getData() as $fil)
  216.                                 {      
  217.                                     
  218.                                     $FILE['name'] = $fil->getClientOriginalName();
  219.                                     $FILE['type'] = $fil->getClientMimeType();
  220.                                     $FILE['tmp_name'] = $fil->getPathName();
  221.                                     $FILE['error'] = $fil->getError();
  222.                                     $FILE['size'] = $fil->getSize();
  223.                                    //dump($name."_".$i);exit;
  224.                                     $file $this->multiupload($FILE$name."_".$i$type);
  225.                                     //$brochureFileName = $fileUploader->upload($fil, $this->upload_folder_file);                                                                                                       
  226.                                                                        
  227.                                     $med = new Media();
  228.                                     $med->setGalleryFile($bf->getData());
  229.                                     $med->setName($fil->getClientOriginalName());
  230.                                     $med->setDescription(null);
  231.                                     $med->setEnabled(1);
  232.                                     $med->setProviderName('sonata.media.provider.file');
  233.                                     $med->setProviderStatus(1);
  234.                                     $med->setProviderMetadata(["filename"=>$fil->getClientOriginalName()]);
  235.                                     $med->setWidth(300);
  236.                                     $med->setHeight(100);
  237.                                     $med->setLength(1544);
  238.                                     $med->setProviderReference($file[0]['name']);      
  239.                                     $med->setContentType($fil->getClientMimeType());      
  240.                                     $med->setContext('default'); 
  241.                                     $files[] = $med;      
  242.                                     unset($FILE);
  243.                                     
  244.                                 }                                                              
  245.                             }
  246.                             
  247.                             $bf->getData()->setMedias($files); 
  248.                             unset($files);
  249.                             $i++;
  250.                         }
  251.                                                 
  252.                     }
  253.                     if ($form['gallerys']) 
  254.                     {                        
  255.                         //$fileUploader = $this->get('admin.upload.file.service');  
  256.                         $type 'image/*';
  257.                         $name 'medias';                     
  258.                         $i 0;
  259.                         foreach($form['gallerys'] as $bf)
  260.                         {  
  261.                             $files = new ArrayCollection();
  262.                             if($bf[$name])
  263.                             {          
  264.                                 $FILE = array();    
  265.                                                      
  266.                                 foreach($bf[$name]->getData() as $fil)
  267.                                 {     
  268.                                     $FILE['name'] = $fil->getClientOriginalName();
  269.                                     $FILE['type'] = $fil->getClientMimeType();
  270.                                     $FILE['tmp_name'] = $fil->getPathName();
  271.                                     $FILE['error'] = $fil->getError();
  272.                                     $FILE['size'] = $fil->getSize();
  273.                                    
  274.                                    
  275.                                     $media $this->multiupload($FILE$name."_".$i$type);
  276.                                     
  277.                                     //dump($media);exit;
  278.                                     //dump($_POST);exit;
  279.                                     //$brochureFileName = $fileUploader->upload($fil, $this->upload_folder_image);                                                                                                       
  280.                                     //dump($brochureFileName);exit;
  281.                                     
  282.                                     $med = new Media();
  283.                                     $med->setGallery($bf->getData());
  284.                                     $med->setName($fil->getClientOriginalName());
  285.                                     $med->setDescription(null);
  286.                                     $med->setEnabled(1);
  287.                                     $med->setProviderName('sonata.media.provider.image');
  288.                                     $med->setProviderStatus(1);
  289.                                     $med->setProviderMetadata(["filename"=>$fil->getClientOriginalName()]);
  290.                                     $med->setWidth(300);
  291.                                     $med->setHeight(100);
  292.                                     $med->setLength(1544);
  293.                                     $med->setProviderReference($media[0]['name']);      
  294.                                     $med->setContentType($fil->getClientMimeType());      
  295.                                     $med->setContext('default'); 
  296.                                     $files[] = $med
  297.                                     unset($FILE);  
  298.                                     
  299.                                 }                                             
  300.                             }
  301.                             
  302.                             $bf->getData()->setMedias($files); 
  303.                             unset($files);
  304.                             $i++;
  305.                         }
  306.                                                 
  307.                     }
  308.                     $this->persistImageApresentation($form);
  309.                     //exit;
  310.                     /*if ($form['gallerys']) 
  311.                     {                        
  312.                         $fileUploader = $this->get('admin.upload.file.service');                       
  313.                         var_dump($form['gallerys']->getData()->first());
  314.                         foreach($form['gallerys'] as $bf)
  315.                         {  
  316.                             $files = new ArrayCollection();
  317.                             if($bf['medias'])
  318.                             {          
  319.                                                                    
  320.                                 foreach($bf['medias']->getData() as $fil)
  321.                                 {                                                                       
  322.                                     $brochureFileName = $fileUploader->upload($fil, $this->upload_folder_file);                                                                                                        
  323.                                                                         
  324.                                     $med = new Media();
  325.                                     $med->setName($fil->getClientOriginalName());
  326.                                     $med->setDescription(null);
  327.                                     $med->setEnabled(1);
  328.                                     $med->setProviderName('sonata.media.provider.image');
  329.                                     $med->setProviderStatus(1);
  330.                                     $med->setProviderMetadata(["filename"=>$fil->getClientOriginalName()]);
  331.                                     $med->setWidth(300);
  332.                                     $med->setHeight(100);
  333.                                     $med->setLength(1544);
  334.                                     $med->setProviderReference($brochureFileName);      
  335.                                     $med->setContentType($fil->getClientMimeType());      
  336.                                     $med->setContext('default'); 
  337.                                     $files[] = $med;  
  338.                                                                      
  339.                                     
  340.                                 }
  341.                                 
  342.                                 exit;
  343.                                $bf->getData()->setMedias($files);                                 
  344.                             }
  345.                         }
  346.                                                 
  347.                     }*/
  348.                     
  349.                     
  350.                     //exit; 
  351.                 }
  352.                
  353.                 //##########UPLOAD##########       
  354.                          
  355.                 $submittedObject $form->getData();
  356.                 
  357.                 $this->admin->setSubject($submittedObject);
  358.                 //
  359.                 //dump($submittedObject);exit;
  360.                 try {
  361.                     $existingObject $this->admin->update($submittedObject);
  362.                     //var_dump($existingObject->getPassword());exit;
  363.                    
  364.                     $this->get('session')->getFlashBag()->set('flash_create_success''Mensagem enviada com sucesso');
  365.                     // redirect to edit mode
  366.                     return $this->redirectTo($existingObject);exit;
  367.                 } catch (ModelManagerException $e) {
  368.                     $this->handleModelManagerException($e);
  369.                     $isFormValid false;
  370.                 } catch (LockException $e) {
  371.                     $this->get('session')->getFlashBag()->set('flash_create_error''Mensagem enviada com sucesso');
  372.                 }
  373.             //}
  374.             // show an error message if the form failed validation
  375.             if (!$isFormValid) {
  376.                 if (!$this->isXmlHttpRequest()) {
  377.                     $this->addFlash(
  378.                         'sonata_flash_error',
  379.                         $this->trans(
  380.                             'flash_edit_error',
  381.                             ['%name%' => $this->escapeHtml($this->admin->toString($existingObject))],
  382.                             'SonataAdminBundle'
  383.                         )
  384.                     );
  385.                 }
  386.             } elseif ($this->isPreviewRequested()) {
  387.                 // enable the preview template if the form was valid and preview was requested
  388.                 $templateKey 'preview';
  389.                 $this->admin->getShow();
  390.             }
  391.         }
  392.         $formView $form->createView();
  393.         return $this->renderWithExtraParams($this->base."edit.html.twig", [
  394.             'action' => 'edit',
  395.             'form' => $formView
  396.         ], null);
  397.     }
  398.     public function persistImageApresentation($form$type 'image/*'$name 'imageApresentation')
  399.     {
  400.         if (isset($form[$name]) && !empty($form[$name])) 
  401.         {                                                
  402.             //$fileUploader = $this->get('admin.upload.file.service');  
  403.                   
  404.             $i 0;
  405.             /*foreach($form['imageApresentation'] as $bf)
  406.             { */ 
  407.                
  408.                 $bf $form[$name];
  409.                 $files = new ArrayCollection();
  410.                 if(!empty($bf->getData()))
  411.                 {
  412.                               
  413.                     $FILE = array();    
  414.                                          
  415.                     foreach($bf->getData() as $fil)
  416.                     {     
  417.                         $FILE['name'] = $fil->getClientOriginalName();
  418.                         $FILE['type'] = $fil->getClientMimeType();
  419.                         $FILE['tmp_name'] = $fil->getPathName();
  420.                         $FILE['error'] = $fil->getError();
  421.                         $FILE['size'] = $fil->getSize();
  422.                        
  423.                         $media $this->multiupload($FILE$name."_".$i$type);
  424.                         //dump($media);exit;
  425.                         //dump($_POST);exit;
  426.                         //$brochureFileName = $fileUploader->upload($fil, $this->upload_folder_image);                                                                                                       
  427.                         //dump($brochureFileName);exit;
  428.                         
  429.                         $med = new Media();
  430.                         $med->setImageApresentation($form->getData());
  431.                         $med->setName($fil->getClientOriginalName());
  432.                         $med->setDescription(null);
  433.                         $med->setEnabled(1);
  434.                         $med->setProviderName('sonata.media.provider.image');
  435.                         $med->setProviderStatus(1);
  436.                         $med->setProviderMetadata(["filename"=>$fil->getClientOriginalName()]);
  437.                         $med->setWidth(300);
  438.                         $med->setHeight(100);
  439.                         $med->setLength(1544);
  440.                         $med->setProviderReference($media[0]['name']);      
  441.                         $med->setContentType($fil->getClientMimeType());      
  442.                         $med->setContext('default'); 
  443.                         $files[] = $med
  444.                         unset($FILE);  
  445.                         
  446.                     }    
  447.                     $form->getData()->setImageApresentations($files); 
  448.                     unset($files);
  449.                     $i++;                                                          
  450.                 }
  451.             //}
  452.                                     
  453.         }
  454.     }
  455.     public function upload($brochureFile)
  456.     {
  457.         $originalFilename pathinfo($brochureFile->getClientOriginalName(), PATHINFO_FILENAME);
  458.         // this is needed to safely include the file name as part of the URL
  459.         $safeFilename transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()'$originalFilename);
  460.         $newFilename $safeFilename.'-'.uniqid().'.'.$brochureFile->guessExtension();
  461.         
  462.         // Move the file to the directory where brochures are stored
  463.         try {            
  464.             $brochureFile->move(
  465.                 $this->getParameter('realty_directory'),
  466.                 $newFilename
  467.             );
  468.         } catch (FileException $e) {
  469.             // ... handle exception if something happens during file upload
  470.         }        
  471.         return $newFilename;
  472.     }
  473.     public function multiupload($FILE$name$type)
  474.     {
  475.     
  476.         $n explode('_',$name);
  477.        
  478.         $folder $n[0];
  479.         //dump($folder);exit;
  480.         // define uploads path
  481.         $uploadDir 'upload/realty/'.$folder.'/';
  482.         $thumbsDir $uploadDir 'thumbs/';
  483.        
  484.         //dump($name);
  485.         
  486.         // initialize FileUploader
  487.         $FileUploader = new FileUploader($name, array(
  488.             'limit' => null,
  489.             'maxSize' => null,
  490.             'extensions' => [$type],
  491.             'uploadDir' => $uploadDir,
  492.             'title' => 'name',
  493.             '$_FILE' => $FILE,
  494.             'editor' => array(
  495.                 'maxWidth' => 1280,
  496.                 'maxHeight' => 720,
  497.                 'crop' => false,
  498.                 'quality' => 90
  499.             )
  500.         ));
  501.         //dump($FileUploader);exit;
  502.         
  503.        
  504.         // unlink the files
  505.         // !important only for preloaded files
  506.         // you will need to give the array with appendend files in 'files' option of the FileUploader
  507.         foreach($FileUploader->getRemovedFiles('file') as $key=>$value) {
  508.             
  509.             $file $uploadDir $value['name']; 
  510.             $thumb $thumbsDir $value['name'];
  511.             
  512.             if (is_file($file))
  513.                 unlink($file);
  514.             if (is_file($thumb))
  515.                 unlink($thumb);
  516.         }
  517.         
  518.         // call to upload the files
  519.         $data $FileUploader->upload();
  520.         
  521.         
  522.         
  523.         
  524.         // echo '<pre>';
  525.         // print_r($FileUploader);
  526.         // echo '</pre>';
  527.         // exit;
  528.         //dump($data);exit;
  529.         
  530.         
  531.         // if uploaded and success
  532.         if($data['isSuccess'] && count($data['files']) > 0) {
  533.             // get uploaded files
  534.             $uploadedFiles $data['files'];
  535.             
  536.             // create thumbnails
  537.             if (!is_dir($thumbsDir))
  538.                 mkdir($thumbsDir);
  539.             foreach($uploadedFiles as $item) {
  540.                 FileUploader::resize($filename $item['file'], $width 100$height 100$destination $thumbsDir $item['name'], $crop false$quality 100);
  541.             }
  542.         }
  543.         
  544.         // if warnings
  545.         if($data['hasWarnings']) {
  546.             // get warnings
  547.             $warnings $data['warnings'];
  548.             
  549.             echo '<pre>';
  550.             print_r($warnings);
  551.             echo '</pre>';
  552.             exit;
  553.         }
  554.         
  555.         
  556.         // get the fileList
  557.         $fileList $FileUploader->getFileList();
  558.         
  559.         // // show
  560.         // echo '<pre>';
  561.         // print_r($fileList);
  562.         // echo '</pre>';
  563.         // exit;
  564.         return $fileList;
  565.     }
  566.     public function getRouterName(){
  567.         $router $this->get("router");
  568.         $router $router->match($this->getRequest()->getPathInfo());
  569.         return $router['_route'];
  570.     }
  571.     public function routesNeedPermission(){
  572.         return [
  573.             'admin_internit_realestate_realty_listMirror',
  574.         ];
  575.     }
  576.     public function userPermissionByModule($user){
  577.         dump($user);
  578.         dump($this->getUserModulos($user));
  579.         /** Super Admiministrador ? */
  580.         if( $this->isMasterAdmin($user->getRoles()) )
  581.             return true;
  582.         /** Validação por Rota */
  583.         $validateByRoute in_array$this->getRouterName(), $this->routesNeedPermission() );
  584.         if( $validateByRoute && in_array$this->getRouterName(), $this->getUserModulos($user) ) )
  585.             return true;
  586.         /** Validação por Pacote/Bundle */
  587.         if( $this->getUserModulos($user) && in_array($this->bundle$this->getUserModulos($user)) )
  588.             return true;
  589.         return false;
  590.     }
  591.     public function getUserModulos($user){
  592.         switch ($user->getRoles()[0]) {
  593.             case 'ROLE_CONSTRUTORA':
  594.                 return $user->getBuilder()->getModulos();
  595.                 break;
  596.             case 'ROLE_IMOBILIARIA':
  597.                 return $user->getRealEstates()[0]->getModulosImobiliaria();
  598.                 break;
  599.             case 'ROLE_CORRETOR':
  600.                 return $user->getRealEstates()[0]->getModulosCorretor();
  601.                 break;
  602.             case 'ROLE_AGENCIA':
  603.                 return $user->getAgency()->getModulos();
  604.                 break;
  605.         }
  606.         return [];
  607.     }
  608.     public function permissionByRole(array $modulesName$user)
  609.     {
  610.         if( $this->isMasterAdmin($user->getRoles()) )
  611.             return true;
  612.         if( !$this->userPermissionByModule($user) )
  613.             return false;
  614.         foreach ($modulesName as $module)
  615.             if( in_array($module$user->getRoles()) )
  616.                 //dump($user);
  617.                 return true;
  618.         return false;
  619.     }
  620.     public function isMasterAdmin($userRoles){
  621.         $masterAdmin = [
  622.             "ROLE_SUPER_ADMIN",
  623.             "ROLE_CORRETOR",
  624.             "ROLE_IMOBILIARIA",
  625.             "ROLE_CONSTRUTORA",
  626.             "ROLE_AGENCIA",
  627.             "ROLE_USER"
  628.         ];
  629.         $isMaster 0;
  630.         foreach ($masterAdmin as $module)
  631.             if( in_array($module$userRoles) )
  632.                 $isMaster ++;
  633.         if($isMaster == 6)
  634.             return true;
  635.         return false;
  636.     }
  637. }