src/Entity/User.php line 71

  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\UserRepository;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  6. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. use ApiPlatform\Metadata\ApiResource;
  9. use ApiPlatform\Metadata\Get;
  10. use ApiPlatform\Metadata\GetCollection;
  11. use ApiPlatform\Metadata\Delete;
  12. use ApiPlatform\Metadata\Post;
  13. use ApiPlatform\Metadata\Put;
  14. use Symfony\Component\Serializer\Annotation\Groups;
  15. use App\Controller\Api\AdminController;
  16. use App\Controller\Api\AuthController;
  17. use App\Controller\Api\UserController;
  18. #[ORM\Entity(repositoryClassUserRepository::class)]
  19. #[UniqueEntity(fields: ['email'], message'There is already an account with this email')]
  20. #[ApiResource(
  21.     normalizationContext: ['groups' => ['user:read']],
  22.     denormalizationContext: ['groups' => ['user:write']],
  23.     operations: [
  24.         new Get(
  25.             name'auth_user',
  26.             routeName'api_auth_user',
  27.             controllerAuthController::class
  28.         ),
  29.         // new GetCollection(security: 'is_granted("ROLE_SUPER_ADMIN")'),
  30.         new GetCollection(
  31.             name'users',
  32.             routeName'api_users',
  33.             controllerUserController::class,
  34.             security'is_granted("ROLE_ADMIN")'
  35.         ),
  36.         new GetCollection(
  37.             name'users',
  38.             routeName'api_users_employees',
  39.             controllerUserController::class,
  40.             security'is_granted("ROLE_ADMIN")'
  41.         ),
  42.         new Delete(security'is_granted("ROLE_SUPER_ADMIN")'),
  43.         new Post(
  44.             name'user',
  45.             routeName'api_user',
  46.             controllerUserController::class
  47.         ),
  48.         new Post(
  49.             name'reset_token',
  50.             routeName'api_user_reset_token',
  51.             controllerAuthController::class
  52.         ),
  53.         new Post(
  54.             name'reset_password',
  55.             routeName'api_user_reset_password',
  56.             controllerAuthController::class
  57.         ),
  58.         new Post(security'is_granted("ROLE_SUPER_ADMIN") or is_granted("ROLE_ADMIN")'),
  59.         new Put(
  60.             name'user_edit',
  61.             routeName'api_user_edit',
  62.             controllerUserController::class,
  63.             security'is_granted("ROLE_SUPER_ADMIN") or is_granted("ROLE_ADMIN")'
  64.         )
  65.     ],
  66.     paginationEnabledfalse
  67. )]
  68. class User implements UserInterfacePasswordAuthenticatedUserInterface
  69. {
  70.     #[ORM\Id]
  71.     #[ORM\GeneratedValue]
  72.     #[ORM\Column]
  73.     #[Groups(['user:read''invoice:read''invoiceDetail:read'])]
  74.     private ?int $id null;
  75.     #[ORM\Column(length180uniquetrue)]
  76.     #[Groups(['user:read''user:write''invoice:read''invoiceDetail:read'])]
  77.     private ?string $email null;
  78.     #[ORM\Column]
  79.     #[Groups(['user:read''user:write''invoice:read''invoiceDetail:read'])]
  80.     private array $roles = [];
  81.     #[ORM\Column(type'integer')]
  82.     #[Groups(['user:read''user:write''invoice:read''invoiceDetail:read'])]
  83.     private ?int $status 1;
  84.     #[ORM\Column(type'text'length65535nullabletrue)]
  85.     private ?string $reset_password_token null;
  86.     #[ORM\OneToOne(targetEntityEmployee::class, inversedBy'user')]
  87.     #[ORM\JoinColumn(name'employee_id'referencedColumnName'id'nullabletrue)]
  88.     #[Groups(['user:read''user:write'])]
  89.     private ?Employee $employee null;
  90.     /**
  91.      * @var string The hashed password
  92.      */
  93.     #[ORM\Column]
  94.     #[Groups(['user:write'])]
  95.     private ?string $password null;
  96.     public function getId(): ?int
  97.     {
  98.         return $this->id;
  99.     }
  100.     public function getEmail(): ?string
  101.     {
  102.         return $this->email;
  103.     }
  104.     public function setEmail(string $email): self
  105.     {
  106.         $this->email $email;
  107.         return $this;
  108.     }
  109.     public function getStatus(): ?int
  110.     {
  111.         return $this->status;
  112.     }
  113.     public function setStatus(int $status): self
  114.     {
  115.         $this->status $status;
  116.         return $this;
  117.     }
  118.     public function getResetPasswordToken(): ?string
  119.     {
  120.         return $this->reset_password_token;
  121.     }
  122.     public function setResetPasswordToken(string $reset_password_token): self
  123.     {
  124.         $this->reset_password_token $reset_password_token;
  125.         return $this;
  126.     }
  127.     /**
  128.      * A visual identifier that represents this user.
  129.      *
  130.      * @see UserInterface
  131.      */
  132.     public function getUserIdentifier(): string
  133.     {
  134.         return (string) $this->email;
  135.     }
  136.     /**
  137.      * @see UserInterface
  138.      */
  139.     public function getRoles(): array
  140.     {
  141.         $roles $this->roles;
  142.         // guarantee every user at least has ROLE_USER
  143.         $roles[] = 'ROLE_USER';
  144.         return array_unique($roles);
  145.     }
  146.     public function setRoles(array $roles): self
  147.     {
  148.         $this->roles $roles;
  149.         return $this;
  150.     }
  151.     /**
  152.      * @see PasswordAuthenticatedUserInterface
  153.      */
  154.     public function getPassword(): string
  155.     {
  156.         return $this->password;
  157.     }
  158.     public function setHashedPassword(string $plainPassword): self
  159.     {
  160.         $this->password password_hash($plainPasswordPASSWORD_BCRYPT);
  161.         return $this;
  162.     }
  163.     public function setPassword(string $plainPassword): self
  164.     {
  165.         if (!password_get_info($plainPassword)['algo']) {
  166.             $this->setHashedPassword($plainPassword);
  167.         }
  168.         return $this;
  169.     }
  170.     public function getEmployee(): ?Employee
  171.     {
  172.         return $this->employee;
  173.     }
  174.     public function setEmployee(?Employee $employee): self
  175.     {
  176.         if ($employee === null && $this->employee !== null) {
  177.             $this->employee->setUser(null);
  178.         }
  179.         if ($employee !== null && $employee->getUser() !== $this) {
  180.             $employee->setUser($this);
  181.         }
  182.         $this->employee $employee;
  183.         return $this;
  184.     }
  185.     /**
  186.      * @see UserInterface
  187.      */
  188.     public function eraseCredentials()
  189.     {
  190.         // If you store any temporary, sensitive data on the user, clear it here
  191.         // $this->plainPassword = null;
  192.     }
  193. }