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 UserInterface, PasswordAuthenticatedUserInterface
  13. {
  14.     public const ROLE_ALLOWED_PASSWORD = 'ROLE_ALLOWED_PASSWORD';
  15.     #[ORM\Id, ORM\Column(name: "id", type: "integer"), ORM\GeneratedValue(strategy: "AUTO")]
  16.     private int $id;
  17.     #[ORM\Column(name: "sso_identifier", type: "string", length: 64, unique: true)]
  18.     private string $ssoIdentifier;
  19.     #[ORM\Column(name: "email", type: "string", length: 64)]
  20.     private string $email;
  21.     #[ORM\Column(name: "name", type: "string", length: 32)]
  22.     private string $name;
  23.     #[ORM\Column(name: "roles", type: "simple_array")]
  24.     private array $roles;
  25.     #[ORM\Column(name: "local_password", type: "string", length: 64, nullable: true)]
  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", targetEntity: PersonalAccessToken::class, cascade: ["all"], orphanRemoval: true)]
  32.     private iterable $accessTokens;
  33.     public static function ssoUser(string $ssoIdentifier, string $email, string $name, array $roles, DateTimeImmutable $createdAt): self
  34.     {
  35.         return new self($ssoIdentifier, $email, $name, $roles, $createdAt);
  36.     }
  37.     private function __construct(string $ssoIdentifier, string $email, string $name, array $roles, DateTimeImmutable $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 $email, string $name, array $roles, DateTimeImmutable $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->getUserIdentifier();
  69.     }
  70.     public function getRoles(): array
  71.     {
  72.         return $this->roles;
  73.     }
  74.     public function isAllowedPasswordAuthentication(): bool
  75.     {
  76.         return in_array(self::ROLE_ALLOWED_PASSWORD, $this->roles, true);
  77.     }
  78.     public function getPassword(): ?string
  79.     {
  80.         if (!$this->isAllowedPasswordAuthentication()) {
  81.             return null;
  82.         }
  83.         return $this->localPassword;
  84.     }
  85.     public function getSalt(): ?string
  86.     {
  87.         return null;
  88.     }
  89.     public function changeLocalPassword(string $plainPassword, UserPasswordHasherInterface $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 $expiry, TokenValueGenerator $generator): PersonalAccessToken
  97.     {
  98.         $accessToken = new PersonalAccessToken($this, $generator->generate(), $displayName, $notBefore, $expiry);
  99.         $this->accessTokens->add($accessToken);
  100.         return $accessToken;
  101.     }
  102. }