mirror of
				https://github.com/kevinveenbirkenbach/infinito.git
				synced 2025-11-04 11:17:58 +00:00 
			
		
		
		
	Optimized draft for LawPermissionChecker
This commit is contained in:
		@@ -2,116 +2,117 @@
 | 
			
		||||
 | 
			
		||||
namespace App\Domain\LawManagement;
 | 
			
		||||
 | 
			
		||||
use App\Entity\Source\SourceInterface;
 | 
			
		||||
use PhpCollection\CollectionInterface;
 | 
			
		||||
use Doctrine\Common\Collections\ArrayCollection;
 | 
			
		||||
use App\Entity\Meta\RightInterface;
 | 
			
		||||
use Doctrine\Common\Collections\Collection;
 | 
			
		||||
use App\Entity\Meta\LawInterface;
 | 
			
		||||
use App\Domain\RightManagement\RightChecker;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @todo Implement checking by operation sources
 | 
			
		||||
 * @todo chek if recievers are still neccessary and if they should be implemented
 | 
			
		||||
 *
 | 
			
		||||
 * @author kevinfrantz
 | 
			
		||||
 */
 | 
			
		||||
class LawPermissionCheckerService implements LawPermissionCheckerServiceInterface
 | 
			
		||||
final class LawPermissionCheckerService implements LawPermissionCheckerServiceInterface
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * @var SourceInterface
 | 
			
		||||
     * @var LawInterface
 | 
			
		||||
     */
 | 
			
		||||
    private $clientSource;
 | 
			
		||||
    private $law;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @var SourceInterface
 | 
			
		||||
     */
 | 
			
		||||
    private $requestedSource;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @var
 | 
			
		||||
     */
 | 
			
		||||
    private $permissionType;
 | 
			
		||||
 | 
			
		||||
    private function getRightsByPermission(Collection $rights): Collection
 | 
			
		||||
    {
 | 
			
		||||
        $result = new ArrayCollection();
 | 
			
		||||
        /*
 | 
			
		||||
         * @var RightInterface
 | 
			
		||||
         */
 | 
			
		||||
        foreach ($rights as $right) {
 | 
			
		||||
            if ($right->getType() === $this->permissionType) {
 | 
			
		||||
                $result->add($right);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $result;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private function getRightsByClient(Collection $rights): Collection
 | 
			
		||||
    {
 | 
			
		||||
        $result = new ArrayCollection();
 | 
			
		||||
        /*
 | 
			
		||||
         * @var RightInterface
 | 
			
		||||
         */
 | 
			
		||||
        foreach ($rights as $right) {
 | 
			
		||||
            if ($right->getReciever() === $this->clientSource) {
 | 
			
		||||
                $result->add($right);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $result;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @todo Implement
 | 
			
		||||
     * @param Collection|RightInterface[] $rights
 | 
			
		||||
     * @param string                      $value
 | 
			
		||||
     * @param string                      $attribut
 | 
			
		||||
     *
 | 
			
		||||
     * @return CollectionInterface the sources to which the client belongs to
 | 
			
		||||
     * @return Collection|RightInterface[]
 | 
			
		||||
     */
 | 
			
		||||
    private function getAllClientMemberships(): Collection
 | 
			
		||||
    private function getFilteredRights(Collection $rights, string $value, string $attribut): Collection
 | 
			
		||||
    {
 | 
			
		||||
        $result = new ArrayCollection();
 | 
			
		||||
        foreach ($rights as $right) {
 | 
			
		||||
            if ($right->{'get'.$attribut}() === $value) {
 | 
			
		||||
                $result->add($right);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $result;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param Collection|RightInterface[] $rights
 | 
			
		||||
     * @param string                      $type
 | 
			
		||||
     *
 | 
			
		||||
     * @return Collection
 | 
			
		||||
     */
 | 
			
		||||
    private function getRightsByType(Collection $rights, string $type): Collection
 | 
			
		||||
    {
 | 
			
		||||
        return $this->getFilteredRights($rights, $type, 'Type');
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param Collection|RightInterface[] $rights
 | 
			
		||||
     * @param string                      $layer
 | 
			
		||||
     *
 | 
			
		||||
     * @return Collection
 | 
			
		||||
     */
 | 
			
		||||
    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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @todo Implement priority function for locking
 | 
			
		||||
     *
 | 
			
		||||
     * @param CollectionInterface $rights the rights which exist
 | 
			
		||||
     * @param CollectionInterface|RightInterface[] $rights
 | 
			
		||||
     *                                                     the rights which exist
 | 
			
		||||
     *
 | 
			
		||||
     * @return bool
 | 
			
		||||
     */
 | 
			
		||||
    private function isGranted(CollectionInterface $rights): bool
 | 
			
		||||
    private function isGranted(Collection $rights, RightInterface $client): bool
 | 
			
		||||
    {
 | 
			
		||||
        /*
 | 
			
		||||
         * @var RightInterface
 | 
			
		||||
         */
 | 
			
		||||
        foreach ($rights as $right) {
 | 
			
		||||
            if ($clientSources->contains($right)) {
 | 
			
		||||
            }
 | 
			
		||||
        if (0 === $rights->count()) {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
        $right = $rights[0];
 | 
			
		||||
        $rightChecker = new RightChecker($right);
 | 
			
		||||
 | 
			
		||||
        return $result;
 | 
			
		||||
        return $rightChecker->isGranted($client->getLayer(), $client->getType(), $client->getSource());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function checkPermission(): bool
 | 
			
		||||
    public function __construct(LawInterface $law)
 | 
			
		||||
    {
 | 
			
		||||
        $requestedSourceRights = $this->requestedSource->getLaw()->getRights();
 | 
			
		||||
        $rightsByPermission = $this->getRightsByPermission($requestedSourceRights);
 | 
			
		||||
        $rightsbyClient = $this->getRightsByClient($rightsByPermission);
 | 
			
		||||
        $this->law = $law;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setClientSource(SourceInterface $clientSource): void
 | 
			
		||||
    public function hasPermission(RightInterface $client): bool
 | 
			
		||||
    {
 | 
			
		||||
        $this->clientSource = $clientSource;
 | 
			
		||||
    }
 | 
			
		||||
        $rights = clone $this->law->getRights();
 | 
			
		||||
        $rights = $this->getRightsByType($rights, $client->getType());
 | 
			
		||||
        $rights = $this->getRightsByLayer($rights, $client->getLayer());
 | 
			
		||||
        $rights = $this->sortByPriority($rights);
 | 
			
		||||
 | 
			
		||||
    public function setRequestedSource(SourceInterface $requestedSource): void
 | 
			
		||||
    {
 | 
			
		||||
        $this->requestedSource = $requestedSource;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function setType(string $type): void
 | 
			
		||||
    {
 | 
			
		||||
        $this->permissionType = $type;
 | 
			
		||||
        return $this->isGranted($rights, $client);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@
 | 
			
		||||
 | 
			
		||||
namespace App\Domain\LawManagement;
 | 
			
		||||
 | 
			
		||||
use App\Entity\Source\SourceInterface;
 | 
			
		||||
use App\Entity\Meta\RightInterface;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Allows to check if a source has rights on a source.
 | 
			
		||||
@@ -11,16 +11,10 @@ use App\Entity\Source\SourceInterface;
 | 
			
		||||
 */
 | 
			
		||||
interface LawPermissionCheckerServiceInterface
 | 
			
		||||
{
 | 
			
		||||
    public function setRequestedSource(SourceInterface $requestedSource): void;
 | 
			
		||||
 | 
			
		||||
    public function setClientSource(SourceInterface $clientSource): void;
 | 
			
		||||
 | 
			
		||||
    public function checkPermission(): bool;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Sets the permission type.
 | 
			
		||||
     * Checks if the client has the right for executing.
 | 
			
		||||
     *
 | 
			
		||||
     * @param string $type
 | 
			
		||||
     * @return bool
 | 
			
		||||
     */
 | 
			
		||||
    public function setType(string $type): void;
 | 
			
		||||
    public function hasPermission(RightInterface $client): bool;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -5,6 +5,7 @@ namespace App\Domain\SourceManagement;
 | 
			
		||||
use Doctrine\Common\Collections\Collection;
 | 
			
		||||
use App\Entity\Source\SourceInterface;
 | 
			
		||||
use Doctrine\Common\Collections\ArrayCollection;
 | 
			
		||||
use App\Entity\Attribut\MemberRelationAttributInterface;
 | 
			
		||||
 | 
			
		||||
final class SourceMemberInformation implements SourceMemberInformationInterface
 | 
			
		||||
{
 | 
			
		||||
@@ -23,6 +24,9 @@ final class SourceMemberInformation implements SourceMemberInformationInterface
 | 
			
		||||
        $this->source = $source;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @param Collection|MemberRelationAttributInterface[] $members
 | 
			
		||||
     */
 | 
			
		||||
    private function itterateOverMembers(Collection $members): void
 | 
			
		||||
    {
 | 
			
		||||
        foreach ($members as $member) {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user