Commit 6c968043 authored by Laurent Lecluse's avatar Laurent Lecluse
Browse files

Typage fort

parent 97c67868
Loading
Loading
Loading
Loading
Loading
+20 −107
Original line number Diff line number Diff line
@@ -21,29 +21,18 @@ abstract class AbstractAssertion implements AssertionInterface
    use UserContextServiceAwareTrait;
    use AuthorizeServiceAwareTrait;

    /**
     * @var Acl
     */
    private $acl;
    private Acl $acl;

    /**
     * @var RoleInterface
     */
    private $role = false;
    private RoleInterface|bool|null $role = false;

    /**
     * @var MvcEvent
     */
    private $mvcEvent;
    private MvcEvent $mvcEvent;



    /**
     * !!!! Pour éviter l'erreur "Serialization of 'Closure' is not allowed"... !!!!
     *
     * @return array
     */
    public function __sleep()
    public function __sleep(): array
    {
        return [];
    }
@@ -56,15 +45,8 @@ abstract class AbstractAssertion implements AssertionInterface
     * This method is passed the ACL, Role, Resource, and privilege to which the authorization query applies. If the
     * $role, $this->resource, or $privilege parameters are null, it means that the query applies to all Roles, Resources, or
     * privileges, respectively.
     *
     * @param  Acl               $acl
     * @param  RoleInterface     $role
     * @param  ResourceInterface $resource
     * @param  string            $privilege
     *
     * @return bool
     */
    public final function assert(Acl $acl, RoleInterface $role = null, ResourceInterface $resource = null, $privilege = null)
    public final function assert(Acl $acl, ?RoleInterface $role = null, string|ResourceInterface|null $resource = null, ?string $privilege = null): bool
    {
        $this->setAcl($acl);
        $this->setRole($role);
@@ -97,23 +79,14 @@ abstract class AbstractAssertion implements AssertionInterface



    /**
     * @param string|ResourceInterface $resource
     * @param string                   $privilege
     *
     * @return bool
     */
    public function isAllowed($resource, $privilege = null)
    public function isAllowed(string|ResourceInterface $resource, ?string $privilege = null): bool
    {
        return $this->getServiceAuthorize()->isAllowed($resource, $privilege);
    }



    /**
     * @return Acl
     */
    public function getAcl()
    public function getAcl(): Acl
    {
        if (!$this->acl) {
            $this->acl = $this->getServiceAuthorize()->getAcl();
@@ -124,12 +97,7 @@ abstract class AbstractAssertion implements AssertionInterface



    /**
     * @param Acl $acl
     *
     * @return AbstractAssertion
     */
    public function setAcl(Acl $acl = null)
    public function setAcl(?Acl $acl = null): self
    {
        $this->acl = $acl;

@@ -138,10 +106,7 @@ abstract class AbstractAssertion implements AssertionInterface



    /**
     * @return RoleInterface
     */
    public function getRole()
    public function getRole(): ?RoleInterface
    {
        if (false === $this->role) {
            $sUserContext = $this->serviceUserContext;
@@ -155,12 +120,7 @@ abstract class AbstractAssertion implements AssertionInterface



    /**
     * @param RoleInterface $role
     *
     * @return AbstractAssertion
     */
    public function setRole(RoleInterface $role = null)
    public function setRole(?RoleInterface $role = null): self
    {
        $this->role = $role;

@@ -169,12 +129,7 @@ abstract class AbstractAssertion implements AssertionInterface



    /**
     * @param string $resource
     *
     * @return boolean
     */
    private function detectPrivilege($resource = null)
    private function detectPrivilege(string|ResourceInterface $resource = null): bool
    {
        if ($resource instanceof ResourceInterface) $resource = $resource->getResourceId();

@@ -183,25 +138,14 @@ abstract class AbstractAssertion implements AssertionInterface



    /**
     * @param string $privilege
     * @param string $subPrivilege
     *
     * @return boolean
     */
    protected function assertPrivilege($privilege, $subPrivilege = null)
    protected function assertPrivilege(string $privilege, ?string $subPrivilege = null): bool
    {
        return true;
    }



    /**
     * @param string $resource
     *
     * @return boolean
     */
    private function detectController($resource = null)
    private function detectController(string|ResourceInterface|null $resource = null): bool
    {
        if ($resource instanceof ResourceInterface) $resource = $resource->getResourceId();

@@ -213,33 +157,21 @@ abstract class AbstractAssertion implements AssertionInterface
    /**
     * Ititialisation des paramètres de l'assertion (si nécessaire)
     */
    public function init()
    public function init(): void
    {

    }



    /**
     * @param string $controller
     * @param string $action
     * @param string $privilege
     *
     * @return boolean
     */
    protected function assertController($controller, $action = null, $privilege = null)
    protected function assertController(string $controller, ?string $action = null, ?string $privilege = null): bool
    {
        return true;
    }



    /**
     * @param string $resource
     *
     * @return boolean
     */
    private function detectEntity($resource = null)
    private function detectEntity(string|ResourceInterface|null $resource = null): bool
    {
        return
            is_object($resource)
@@ -248,26 +180,14 @@ abstract class AbstractAssertion implements AssertionInterface



    /**
     * @param ResourceInterface $entity
     * @param string            $privilege
     *
     * @return boolean
     */
    protected function assertEntity(ResourceInterface $entity, $privilege = null)
    protected function assertEntity(ResourceInterface $entity, ?string $privilege = null): bool
    {
        return true;
    }



    /**
     * @param ResourceInterface $resource
     * @param string            $privilege
     *
     * @return boolean
     */
    protected function assertOther(ResourceInterface $resource = null, $privilege = null)
    protected function assertOther(string|ResourceInterface|null $resource = null, ?string $privilege = null): bool
    {
        return true;
    }
@@ -277,12 +197,8 @@ abstract class AbstractAssertion implements AssertionInterface
    /**
     * Parcours la liste des résultats des assertions transmises (liste de booleans)
     * Si l'une d'entres elles est fausse alors false est retourné. true sinon.
     *
     * @param array $assertions
     *
     * @return bool
     */
    protected function asserts($assertions)
    protected function asserts(array $assertions): bool
    {
        if (!is_array($assertions)) {
            $assertions = [$assertions];
@@ -304,10 +220,7 @@ abstract class AbstractAssertion implements AssertionInterface



    /**
     * @return MvcEvent
     */
    protected function getMvcEvent()
    protected function getMvcEvent(): MvcEvent
    {
        return $this->mvcEvent;
    }