{'get'.$attribut}() === $value) { $result->add($right); } } return $result; } /** * @param Collection|RightInterface[] $rights * @param string $type * * @return Collection|RightInterface[] */ private function getRightsByType(Collection $rights, string $type): Collection { return $this->getFilteredRights($rights, $type, 'Type'); } /** * @param Collection|RightInterface[] $rights * @param SourceInterface $reciever * * @return Collection|RightInterface[] */ private function getRightsByReciever(Collection $rights, SourceInterface $reciever): Collection { $result = new ArrayCollection(); foreach ($rights as $right) { if ($right->getReciever() === $reciever || $this->memberExist($right, $reciever)) { $result->add($right); } } return $result; } /** * @todo Implement! * * @param RightInterface $right * @param SourceInterface $recieverSource * * @return bool */ private function memberExist(RightInterface $right, SourceInterface $recieverSource): bool { $rightMemberInformation = new SourceMemberInformation($right->getReciever()); $rightMemberSources = $rightMemberInformation->getAllMembers(); foreach ($rightMemberSources as $memberSource) { if ($memberSource === $recieverSource) { return true; } } return false; } /** * @param Collection|RightInterface[] $rights * @param string $layer * * @return Collection|RightInterface[] */ private function getRightsByLayer(Collection $rights, string $layer): Collection { return $this->getFilteredRights($rights, $layer, 'Layer'); } /** * @todo seems like this can be solved on a nicer way * * @param Collection|RightInterface[] $rights * * @return Collection|RightInterface[] */ private function sortByPriority(Collection $rights): Collection { $iterator = $rights->getIterator(); $iterator->uasort(function ($first, $second) { return (int) $first->getPriority() > (int) $second->getPriority() ? 1 : -1; }); $sorted = new ArrayCollection(); foreach ($iterator as $right) { $sorted->add($right); } return $sorted; } /** * @param CollectionInterface|RightInterface[] $rights * the rights which exist * * @return bool */ private function isGranted(Collection $rights, RightInterface $client): bool { if (0 === $rights->count()) { return false; } $right = $rights[0]; $rightChecker = new RightChecker($right); return $rightChecker->isGranted($client->getLayer(), $client->getType(), $client->getReciever()); } public function __construct(LawInterface $law) { $this->law = $law; } public function hasPermission(RightInterface $clientRight): bool { $rights = clone $this->law->getRights(); $rights = $this->getRightsByType($rights, $clientRight->getType()); $rights = $this->getRightsByLayer($rights, $clientRight->getLayer()); $rights = $this->getRightsByReciever($rights, $clientRight->getReciever()); $rights = $this->sortByPriority($rights); return $this->isGranted($rights, $clientRight); } }