vendor/bigidea/identity-bundle/Entity/TeamMate.php line 15

Open in your IDE?
  1. <?php
  2. namespace BigIdea\IdentityBundle\Entity;
  3. use BigIdea\IdentityBundle\Service\TokenValueGenerator;
  4. use DateTimeImmutable;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  8. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. #[ORM\Entity]
  11. #[ORM\Table(name"identity_team_mates")]
  12. /** @final */ class TeamMate implements UserInterfacePasswordAuthenticatedUserInterface
  13. {
  14.     public const ROLE_ALLOWED_PASSWORD 'ROLE_ALLOWED_PASSWORD';
  15.     #[ORM\IdORM\Column(name"id"type"integer"), ORM\GeneratedValue(strategy"AUTO")]
  16.     private int $id;
  17.     #[ORM\Column(name"sso_identifier"type"string"length64uniquetrue)]
  18.     private string $ssoIdentifier;
  19.     #[ORM\Column(name"email"type"string"length64)]
  20.     private string $email;
  21.     #[ORM\Column(name"name"type"string"length32)]
  22.     private string $name;
  23.     #[ORM\Column(name"roles"type"simple_array")]
  24.     private array $roles;
  25.     #[ORM\Column(name"local_password"type"string"length64nullabletrue)]
  26.     private ?string $localPassword;
  27.     #[ORM\Column(name"created_at"type"datetimetz_immutable")]
  28.     private DateTimeImmutable $createdAt;
  29.     #[ORM\Column(name"updated_at"type"datetimetz_immutable")]
  30.     private DateTimeImmutable $updatedAt;
  31.     #[ORM\OneToMany(mappedBy"owner"targetEntityPersonalAccessToken::class, cascade: ["all"], orphanRemovaltrue)]
  32.     private iterable $accessTokens;
  33.     public static function ssoUser(string $ssoIdentifierstring $emailstring $name, array $rolesDateTimeImmutable $createdAt): self
  34.     {
  35.         return new self($ssoIdentifier$email$name$roles$createdAt);
  36.     }
  37.     private function __construct(string $ssoIdentifierstring $emailstring $name, array $rolesDateTimeImmutable $createdAt)
  38.     {
  39.         $this->ssoIdentifier $ssoIdentifier;
  40.         $this->email $email;
  41.         $this->name $name;
  42.         $this->roles $roles;
  43.         $this->localPassword null;
  44.         $this->createdAt $createdAt;
  45.         $this->updatedAt $createdAt;
  46.         $this->accessTokens = new ArrayCollection();
  47.     }
  48.     public function update(string $emailstring $name, array $rolesDateTimeImmutable $updatedAt): void
  49.     {
  50.         $this->email $email;
  51.         $this->name $name;
  52.         $this->roles $roles;
  53.         $this->updatedAt $updatedAt;
  54.     }
  55.     public function id(): int
  56.     {
  57.         return $this->id;
  58.     }
  59.     public function eraseCredentials(): void
  60.     {
  61.     }
  62.     public function getUserIdentifier(): string
  63.     {
  64.         return $this->ssoIdentifier;
  65.     }
  66.     public function getUsername(): string
  67.     {
  68.         return $this->email;
  69.     }
  70.     public function getSalt(): ?string
  71.     {
  72.         return null;
  73.     }
  74.     public function getRoles(): array
  75.     {
  76.         return $this->roles;
  77.     }
  78.     public function isAllowedPasswordAuthentication(): bool
  79.     {
  80.         return in_array(self::ROLE_ALLOWED_PASSWORD$this->rolestrue);
  81.     }
  82.     public function getPassword(): ?string
  83.     {
  84.         if (!$this->isAllowedPasswordAuthentication()) {
  85.             return null;
  86.         }
  87.         return $this->localPassword;
  88.     }
  89.     public function changeLocalPassword(string $plainPasswordUserPasswordHasherInterface $hasher): void
  90.     {
  91.         if (!$this->isAllowedPasswordAuthentication()) {
  92.             throw new \DomainException(sprintf('In order to set local password user must have role %s.'self::ROLE_ALLOWED_PASSWORD));
  93.         }
  94.         $this->localPassword $hasher->hashPassword($this$plainPassword);
  95.     }
  96.     public function createAccessToken(string $displayName, ?DateTimeImmutable $notBefore, ?DateTimeImmutable $expiryTokenValueGenerator $generator): PersonalAccessToken
  97.     {
  98.         $accessToken = new PersonalAccessToken($this$generator->generate(), $displayName$notBefore$expiry);
  99.         $this->accessTokens->add($accessToken);
  100.         return $accessToken;
  101.     }
  102. }