src/Security/Voter/DemandVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Company;
  4. use App\Entity\CompanyDemand;
  5. use App\Entity\Demand;
  6. use App\Entity\User;
  7. use Doctrine\Common\Collections\Collection;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. class DemandVoter extends Voter
  11. {
  12. protected function supports(string $attribute, $subject): bool
  13. {
  14. // replace with your own logic
  15. // https://symfony.com/doc/current/security/voters.html
  16. return in_array($attribute, ['DEMAND_SHOW', 'DEMAND_UPLOAD', 'DEMAND_UPDATE', 'INTERVENTION_SHOW', 'ESTIMATE_SHOW'], true) && $subject instanceof Demand;
  17. }
  18. protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
  19. {
  20. $user = $token->getUser();
  21. // if the user is anonymous, do not grant access
  22. if (!$user instanceof User) {
  23. return false;
  24. }
  25. /** @var Demand $demand */
  26. $demand = $subject;
  27. /** @var Collection<CompanyDemand> $companyDemandAssigned */
  28. $companyDemandAssigned = $demand->getCompanyDemands()->filter(function (CompanyDemand $companyDemand) use ($user): bool {
  29. return $companyDemand->getCompany() === $user->getCompanies()->first();
  30. });
  31. $companyDemandSelected = $demand->getCompanyDemands()->filter(function (CompanyDemand $companyDemand) use ($user): bool {
  32. return $companyDemand->getCompany() === $user->getCompanies()->first() && $companyDemand->getSelected() === true;
  33. })->first();
  34. // ... (check conditions and return true to grant permission) ...
  35. switch ($attribute) {
  36. case 'DEMAND_SHOW':
  37. return $demand->getUser() === $user;
  38. case 'DEMAND_UPLOAD':
  39. // logic to determine if the user can EDIT
  40. // return true or false
  41. if ($demand->getUser() === $user) {
  42. if (
  43. $demand->getStatus() <= Demand::IN_PENDING_STATUS ||
  44. $demand->getStatus() === Demand::ON_GOING_STATUS ||
  45. $demand->getStatus() === Demand::COMPANY_POSTED_FEEDBACK_STATUS ||
  46. $demand->getStatus() === Demand::COMPLETE_STATUS
  47. ) {
  48. return true;
  49. }
  50. }
  51. if (in_array('ROLE_ADMIN', $user->getRoles(), true)) {
  52. return true;
  53. }
  54. if ($companyDemandAssigned->first()->getCompany()?->getUser() === $user) {
  55. if (
  56. $demand->getStatus() === Demand::ON_GOING_STATUS ||
  57. $demand->getStatus() === Demand::INTERVENTION_CONFIRMATION_STATUS ||
  58. $demand->getStatus() === Demand::SELECTED_COMPANY_STATUS ||
  59. $demand->getStatus() === Demand::USER_POSTED_FEEDBACK_STATUS ||
  60. $demand->getStatus() === Demand::COMPLETE_STATUS
  61. ) {
  62. return true;
  63. }
  64. }
  65. // no break
  66. case 'DEMAND_UPDATE':
  67. // logic to determine if the user can VIEW
  68. // return true or false
  69. return $demand->getUser() === $user && $demand->getStatus() <= Demand::IN_PENDING_STATUS;
  70. case 'INTERVENTION_SHOW':
  71. //Si la company est assigner a la demande
  72. if (!$companyDemandAssigned->isEmpty()) {
  73. //Si la company est selectionner
  74. if (!empty($companyDemandSelected)) {
  75. //Si le status de la demande est superieur à "En cours"
  76. if (
  77. $demand->getStatus() >= Demand::ON_GOING_STATUS
  78. ) {
  79. return true;
  80. }
  81. } else {
  82. //Si les status de la demande sont "Confirmation d'intervention" ou "Company sélectionner"
  83. if (
  84. $demand->getStatus() === Demand::INTERVENTION_CONFIRMATION_STATUS ||
  85. $demand->getStatus() === Demand::SELECTED_COMPANY_STATUS
  86. ) {
  87. return true;
  88. }
  89. }
  90. }
  91. }
  92. return false;
  93. }
  94. }