module = $module; $this->resourceMetadataFactory = $resourceMetadataFactory; } /** * @inheritDoc */ protected function supports(string $attribute, $subject) { if (!in_array($attribute, [self::HAVING_MODULE])) { return false; } return true; } /** * @param string $attribute * @param mixed $subject * @param TokenInterface $token * @return bool * @throws \ApiPlatform\Core\Exception\ResourceClassNotFoundException */ protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool { if (!$subject->attributes->get('_api_resource_class') || !$resourceMetadata = $this->resourceMetadataFactory->create($subject->attributes->get('_api_resource_class'))) { throw new AccessDeniedHttpException(sprintf('Missing resource class')); } $module = $this->module->getModuleByResourceName($resourceMetadata->getShortName()); //Check if there is a module for this entity : eq configuration problem if (null === $module) { throw new AccessDeniedHttpException(sprintf('There no module for the entity (%s) !', $resourceMetadata->getShortName())); } /** @var Access $currentAccess */ $currentAccess = $token->getUser(); /** @var Organization $organization */ $organization = $currentAccess->getOrganization(); return $this->isOrganizationHaveThisModule($organization, $module); } /** * Test si l'organisation possède le module parmis les modules possédés via le produit souscrit, les options souscrites * ou les modules possédées via des conditions particulières (isCmf par exemple) * * @param Organization $organization * @param string $module * @return bool */ private function isOrganizationHaveThisModule(Organization $organization, string $module): bool{ $organizationModules = $this->module->getOrganizationModules($organization); return in_array($module, $organizationModules); } }