src/App/Entity/OAuth2/AbstractToken.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\Entity\OAuth2;
  3. use App\Entity\Content\Common\DiscriminatorInterface;
  4. use App\Entity\Content\Common\DiscriminatorTrait;
  5. use App\Entity\OAuth2\Admin\AbstractAdminToken;
  6. use App\Entity\OAuth2\Admin\AdminAccessToken;
  7. use App\Entity\OAuth2\Admin\AdminRefreshToken;
  8. use App\Entity\OAuth2\App\AbstractAppToken;
  9. use App\Entity\OAuth2\App\AppAccessToken;
  10. use App\Entity\OAuth2\App\AppRefreshToken;
  11. use App\Entity\Shared\UlidIdentifiableInterface;
  12. use App\Entity\Shared\UlidIdentifiableTrait;
  13. use App\Service\OAuth2\AppScopeRepository;
  14. use Cms\CoreBundle\Model\Interfaces\Timestampable\TimestampableInterface;
  15. use Cms\CoreBundle\Model\Interfaces\Timestampable\TimestampableTrait;
  16. use Cms\TenantBundle\Model\TenantableInterface;
  17. use Cms\TenantBundle\Model\TenantableTrait;
  18. use DateTimeImmutable;
  19. use Doctrine\ORM\Mapping as ORM;
  20. use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
  21. use League\OAuth2\Server\Entities\ClientEntityInterface;
  22. use League\OAuth2\Server\Entities\ScopeEntityInterface;
  23. /**
  24.  * @ORM\Entity(
  25.  *     repositoryClass = "App\Doctrine\Repository\OAuth2\TokenRepository",
  26.  * )
  27.  * @ORM\InheritanceType("SINGLE_TABLE")
  28.  * @ORM\DiscriminatorColumn(
  29.  *     name = DiscriminatorInterface::COLUMN__NAME,
  30.  *     type = DiscriminatorInterface::COLUMN__TYPE,
  31.  *     length = 64,
  32.  * )
  33.  * @ORM\DiscriminatorMap(AbstractToken::DISCRS)
  34.  * @ORM\Table(
  35.  *     name = "sys__oauth2__token",
  36.  *     indexes = {
  37.  *         @ORM\Index(
  38.  *             name = "idx__discr",
  39.  *             columns = {
  40.  *                 "discr",
  41.  *             },
  42.  *         ),
  43.  *     },
  44.  * )
  45.  */
  46. abstract class AbstractToken
  47.     implements
  48.         TenantableInterface,
  49.         UlidIdentifiableInterface,
  50.         TimestampableInterface,
  51.         DiscriminatorInterface
  52. {
  53.     public const DISCRS = [
  54.         //AbstractAdminToken::DISCR => AbstractAdminToken::class,
  55.         AdminAccessToken::DISCR => AdminAccessToken::class,
  56.         AdminRefreshToken::DISCR => AdminRefreshToken::class,
  57.         //AbstractAppToken::DISCR => AbstractAppToken::class,
  58.         AppAccessToken::DISCR => AppAccessToken::class,
  59.         AppRefreshToken::DISCR => AppRefreshToken::class,
  60.     ];
  61.     public const DISCR null;
  62.     use TenantableTrait;
  63.     use UlidIdentifiableTrait;
  64.     use TimestampableTrait;
  65.     use DiscriminatorTrait;
  66.     /**
  67.      * @var AbstractToken|null
  68.      *
  69.      * @ORM\ManyToOne(
  70.      *     targetEntity = AbstractToken::class,
  71.      * )
  72.      * @ORM\JoinColumn(
  73.      *     name = "parent",
  74.      *     referencedColumnName = "id",
  75.      *     nullable = true,
  76.      *     onDelete = "CASCADE",
  77.      * )
  78.      */
  79.     protected ?AbstractToken $parent null;
  80.     /**
  81.      * @ORM\Column(
  82.      *     type = "string",
  83.      *     nullable = false,
  84.      * )
  85.      */
  86.     protected ?string $identifier null;
  87.     /**
  88.      * @var string|null
  89.      *
  90.      * @ORM\Column(
  91.      *     type = "string",
  92.      *     nullable = false,
  93.      * )
  94.      */
  95.     protected ?string $client null;
  96.     /**
  97.      * @var array|string[]
  98.      *
  99.      * @ORM\Column(
  100.      *     type = "simple_array",
  101.      *     nullable = true,
  102.      * )
  103.      */
  104.     protected ?array $scopes null;
  105.     /**
  106.      * @var string|null
  107.      *
  108.      * @ORM\Column(
  109.      *     type = "string",
  110.      *     nullable = false,
  111.      * )
  112.      */
  113.     protected ?string $userIdentifier null;
  114.     /**
  115.      * @var DateTimeImmutable|null
  116.      *
  117.      * @ORM\Column(
  118.      *     type = "datetime_immutable",
  119.      *     nullable = false,
  120.      * )
  121.      */
  122.     protected ?DateTimeImmutable $expiryDateTime null;
  123.     /**
  124.      * {@inheritdoc}
  125.      */
  126.     public function getIdentifier(): string
  127.     {
  128.         return $this->identifier;
  129.     }
  130.     /**
  131.      * {@inheritdoc}
  132.      */
  133.     public function setIdentifier($identifier): void
  134.     {
  135.         $this->identifier $identifier;
  136.     }
  137.     /**
  138.      * {@inheritdoc}
  139.      */
  140.     public function setAccessToken(AccessTokenEntityInterface $accessToken): void
  141.     {
  142.         if ( ! $accessToken instanceof self) {
  143.             throw new \LogicException();
  144.         }
  145.         $this->parent $accessToken;
  146.     }
  147.     /**
  148.      * {@inheritdoc}
  149.      */
  150.     public function getAccessToken(): ?AccessTokenEntityInterface
  151.     {
  152.         if ($this->parent && ! $this->parent instanceof AccessTokenEntityInterface) {
  153.             throw new \LogicException();
  154.         }
  155.         return $this->parent;
  156.     }
  157.     /**
  158.      * {@inheritdoc}
  159.      */
  160.     public function getScopes(): array
  161.     {
  162.         return array_map(
  163.             static function (string $identifier) {
  164.                 return AppScopeRepository::getInstance()->getScopeEntityByIdentifier($identifier);
  165.             },
  166.             $this->scopes,
  167.         );
  168.     }
  169.     /**
  170.      * {@inheritdoc}
  171.      */
  172.     public function addScope(ScopeEntityInterface $scope): void
  173.     {
  174.         if ( ! is_array($this->scopes)) {
  175.             $this->scopes = [];
  176.         }
  177.         if ( ! in_array($scope$this->scopestrue)) {
  178.             $this->scopes[] = $scope->getIdentifier();
  179.         }
  180.     }
  181.     /**
  182.      * {@inheritdoc}
  183.      */
  184.     public function getExpiryDateTime(): DateTimeImmutable
  185.     {
  186.         return $this->expiryDateTime;
  187.     }
  188.     /**
  189.      * {@inheritdoc}
  190.      */
  191.     public function setExpiryDateTime(DateTimeImmutable $dateTime): void
  192.     {
  193.         $this->expiryDateTime $dateTime;
  194.     }
  195.     /**
  196.      * {@inheritdoc}
  197.      */
  198.     public function getUserIdentifier(): ?string
  199.     {
  200.         return $this->userIdentifier;
  201.     }
  202.     /**
  203.      * {@inheritdoc}
  204.      */
  205.     public function setUserIdentifier($identifier): void
  206.     {
  207.         $this->userIdentifier $identifier;
  208.     }
  209.     /**
  210.      * {@inheritdoc}
  211.      */
  212.     public function setClient(ClientEntityInterface $client): void
  213.     {
  214.         $this->client $client->getIdentifier();
  215.     }
  216. }