src/Services/Mission/MissionSecurityTools.php line 26

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Services/Mission/MissionSecurityTools.php
  4. //
  5. // Plan.io Task #4024
  6. // We need to test the fact that a Mission is shareable
  7. // inside voters and outside.
  8. // So in order to avoid duplicating code, have a special service
  9. // which deals with this (shit) :)
  10. //----------------------------------------------------------------------
  11. namespace App\Services\Mission;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Persistence\ManagerRegistry;
  14. use App\Entity\Access;
  15. use App\Entity\SocietyGroup;
  16. use App\Entity\Mission\Mission;
  17. use App\Entity\Planning\TaskShareData;
  18. use App\Services\LogTools;
  19. class MissionSecurityTools
  20. {
  21.     public function __construct(ManagerRegistry $doctrineLogTools $logTools)
  22.     {
  23.         $this->em $doctrine->getManager();
  24.         $this->logTools $logTools;
  25.     }
  26.     public function cannotBeShared(Mission $mission)
  27.     {
  28.         return !$this->canBeShared($mission);
  29.     }
  30.     public function canBeShared(Mission $mission)
  31.     {
  32.         if ($this->verifyCommonRestrictions($mission) === false)
  33.         {
  34.             return false;
  35.         }
  36.         // Cannot share a mission that is already shared
  37.         if ($mission->isShared())
  38.         {
  39.             return false;
  40.         }
  41.         // Plan.io Task #4071
  42.         // Deny sharing a parent mission with at least one child
  43.         if ($mission->hasChildren())
  44.         {
  45.             return false;
  46.             // Old code
  47.             // // Deny sharing a parent mission with at least one shared child
  48.             foreach ($mission->getChildren() as $child)
  49.             {
  50.                 if ($child->isShared())
  51.                 {
  52.                     return false;
  53.                 }
  54.             }
  55.         }
  56.         // Plan.io Task #4071
  57.         // Deny sharing a child mission if the parent is already shared
  58.         if ($mission->getParent() !== null)
  59.         {
  60.             if ($mission->getParent()->isShared())
  61.             {
  62.                 return false;
  63.             }
  64.         }
  65.         // If we are here all is good
  66.         return true;
  67.     }
  68.     // Used in MissionSearchController :: searchSelect_sharedTaskEmitterSide
  69.     public function canBeShared_sharedTask(Mission $missionTaskShareData $taskShareData)
  70.     {
  71.         if ($this->verifyCommonRestrictions($mission) === false)
  72.         {
  73.             return false;
  74.         }
  75.         $societyGroupReceiver $taskShareData->getSocietyGroupReceiver();
  76.         if ($societyGroupReceiver === null)
  77.         {
  78.             return false;
  79.         }
  80.         // Cannot share a mission that is already shared
  81.         // Exception : if it is shared with the taskShareData->societyGroupReceiver
  82.         $sharedWithReceiver $societyGroupReceiver->equals($mission->getSocietyGroupOwner());
  83.         if ($mission->isShared() && !$sharedWithReceiver)
  84.         {
  85.             return false;
  86.         }
  87.         // Plan.io Task #4071
  88.         // Deny sharing a parent mission with at least one shared child
  89.         if ($mission->hasChildren())
  90.         {
  91.             return false;
  92.             // Old code
  93.             // Deny sharing a parent mission with at least one shared child
  94.             foreach ($mission->getChildren() as $child)
  95.             {
  96.                 // Exception : if it is shared with the taskShareData->societyGroupReceiver
  97.                 $sharedWithReceiver $societyGroupReceiver->equals($child->getSocietyGroupOwner());
  98.                 if ($child->isShared() && !$sharedWithReceiver)
  99.                 {
  100.                     return false;
  101.                 }
  102.             }
  103.         }
  104.         // Plan.io Task #4071
  105.         // Deny sharing a child mission if the parent is already shared
  106.         if ($mission->getParent() !== null)
  107.         {
  108.             // Exception : if it is shared with the taskShareData->societyGroupReceiver
  109.             $sharedWithReceiver $societyGroupReceiver->equals($mission->getParent()->getSocietyGroupOwner());
  110.             if ($mission->getParent()->isShared() && !$sharedWithReceiver)
  111.             {
  112.                 return false;
  113.             }
  114.         }
  115.         
  116.         // If we are here all is good
  117.         return true;
  118.     }
  119.     protected function verifyCommonRestrictions(Mission $mission)
  120.     {
  121.         // Plan.io Task #3182 : Custom MissionStatus
  122.         // Deny sharing missions with custom statuses
  123.         if ($mission->hasCustomStatus())
  124.         {
  125.             return false;
  126.         }
  127.         // Deny sharing on missions that have already been synced with Rekto
  128.         if (!empty($mission->getRemoteMissionId()))
  129.         {
  130.             return false;
  131.         }
  132.         // Deny sharing on archivedRefused objects
  133.         if ($mission->isArchivedRefused())
  134.         {
  135.             return false;
  136.         }
  137.         // Plan.io Task #3304
  138.         // Deny sharing a mission for which the owner was paid
  139.         if ($mission->ownerWasPaid())
  140.         {
  141.             return false;
  142.         }
  143.         return true;
  144.     }
  145. }