vendor/crosiersource/crosierlib-base/src/Entity/Security/User.php line 68

Open in your IDE?
  1. <?php
  2. namespace CrosierSource\CrosierLibBaseBundle\Entity\Security;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  6. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  7. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\BooleanFilter;
  8. use CrosierSource\CrosierLibBaseBundle\Doctrine\Annotations\EntityHandler;
  9. use CrosierSource\CrosierLibBaseBundle\Doctrine\Annotations\NotUppercase;
  10. use CrosierSource\CrosierLibBaseBundle\Entity\EntityId;
  11. use CrosierSource\CrosierLibBaseBundle\Entity\EntityIdTrait;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Symfony\Component\Security\Core\User\UserInterface;
  15. use Symfony\Component\Serializer\Annotation\Groups;
  16. /**
  17.  * Entidade 'User'.
  18.  *
  19.  * @ApiResource(
  20.  *     normalizationContext={"groups"={"user","entityId"},"enable_max_depth"=true},
  21.  *     denormalizationContext={"groups"={"user","userPassword","entityId"},"enable_max_depth"=true},
  22.  *
  23.  *     itemOperations={
  24.  *          "get"={"path"="/sec/user/{id}", "security"="is_granted('ROLE_ADMIN') or object.owner == user"},
  25.  *          "put"={"path"="/sec/user/{id}", "security"="is_granted('ROLE_ADMIN') or object.owner == user"},
  26.  *          "delete"={"path"="/sec/user/{id}", "security"="is_granted('ROLE_ADMIN')"}
  27.  *     },
  28.  *     collectionOperations={
  29.  *          "get"={"path"="/sec/user", "security"="is_granted('ROLE_ADMIN')"},
  30.  *          "post"={"path"="/sec/user", "security"="is_granted('ROLE_ADMIN')"}
  31.  *     },
  32.  *
  33.  *     attributes={
  34.  *          "pagination_items_per_page"=10,
  35.  *          "formats"={"jsonld", "csv"={"text/csv"}}
  36.  *     }
  37.  * )
  38.  *
  39.  * @ApiFilter(SearchFilter::class, properties={
  40.  *     "username": "partial", 
  41.  *     "nome": "partial",
  42.  *     "email": "partial"
  43.  * })
  44.  * 
  45.  * @ApiFilter(BooleanFilter::class, properties={
  46.  *     "isActive"
  47.  * })
  48.  * 
  49.  * @ApiFilter(OrderFilter::class, properties={
  50.  *     "id", 
  51.  *     "username", 
  52.  *     "nome", 
  53.  *     "updated",
  54.  *     "isActive"
  55.  * }, arguments={"orderParameterName"="order"})
  56.  * 
  57.  * 
  58.  *
  59.  * @EntityHandler(entityHandlerClass="CrosierSource\CrosierLibBaseBundle\EntityHandler\Security\UserEntityHandler")
  60.  *
  61.  * @ORM\Entity(repositoryClass="CrosierSource\CrosierLibBaseBundle\Repository\Security\UserRepository")
  62.  * @ORM\Table(name="sec_user")
  63.  * @author Carlos Eduardo Pauluk
  64.  */
  65. class User implements EntityIdUserInterface\Serializable
  66. {
  67.     use EntityIdTrait;
  68.     /**
  69.      * @NotUppercase()
  70.      * @ORM\Column(name="username", type="string", length=90, nullable=false)
  71.      * @Groups("user")
  72.      * @var null|string
  73.      */
  74.     public ?string $username null;
  75.     /**
  76.      * @NotUppercase()
  77.      * @ORM\Column(name="password", type="string", length=90, nullable=false)
  78.      * @Groups("userPassword")
  79.      * @var null|string
  80.      */
  81.     public ?string $password null;
  82.     /**
  83.      * @NotUppercase()
  84.      * @ORM\Column(name="email", type="string", length=90, nullable=false)
  85.      * @Groups("user")
  86.      * @var null|string
  87.      */
  88.     public ?string $email null;
  89.     /**
  90.      * @ORM\Column(name="fone", type="string", length=90)
  91.      * @Groups("user")
  92.      * @var null|string
  93.      */
  94.     public ?string $fone null;
  95.     /**
  96.      * @ORM\Column(name="nome", type="string", length=90, nullable=false)
  97.      * @Groups("user")
  98.      * @var null|string
  99.      */
  100.     public ?string $nome null;
  101.     
  102.     /**
  103.      * @ORM\Column(name="descricao", type="string", length=255, nullable=true)
  104.      * @Groups("user")
  105.      * @var null|string
  106.      */
  107.     public ?string $descricao null;
  108.     /**
  109.      *
  110.      * @ORM\Column(name="ativo", type="boolean", nullable=false)
  111.      * @Groups("user")
  112.      * @var null|bool
  113.      */
  114.     public bool $isActive true;
  115.     /**
  116.      *
  117.      * @ORM\ManyToOne(targetEntity="CrosierSource\CrosierLibBaseBundle\Entity\Security\Group")
  118.      * @ORM\JoinColumn(name="group_id", nullable=true)
  119.      * @Groups("user")
  120.      *
  121.      * @var null|Group
  122.      */
  123.     public ?Group $group null;
  124.     /**
  125.      * Renomeei o atributo para poder funcionar corretamente com o security do Symfony.
  126.      *
  127.      * @ORM\ManyToMany(targetEntity="Role")
  128.      * @ORM\JoinTable(name="sec_user_role",
  129.      *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
  130.      *      inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")})
  131.      *
  132.      * @Groups("user")
  133.      */
  134.     public $userRoles null;
  135.     /**
  136.      * @NotUppercase()
  137.      * @ORM\Column(name="api_token", type="string", length=255, nullable=false)
  138.      * @Groups("userPassword")
  139.      * @var null|string
  140.      */
  141.     public ?string $apiToken null;
  142.     /**
  143.      * @ORM\Column(name="api_token_expires_at", type="datetime", nullable=false)
  144.      * @Groups("userPassword")
  145.      */
  146.     public ?\DateTime $apiTokenExpiresAt null;
  147.     /**
  148.      * @NotUppercase()
  149.      * @ORM\Column(name="token_recupsenha", type="string", length=36, nullable=true)
  150.      * @Groups("userPassword")
  151.      * @var null|string
  152.      */
  153.     public ?string $tokenRecupSenha null;
  154.     /**
  155.      * @ORM\Column(name="dt_valid_token_recupsenha", type="datetime", nullable=false)
  156.      * @Groups("userPassword")
  157.      */
  158.     public ?\DateTime $dtValidadeTokenRecupSenha null;
  159.     public function __construct()
  160.     {
  161.         $this->roles = new ArrayCollection();
  162.         $this->userRoles = new ArrayCollection();
  163.     }
  164.     public function getRoles()
  165.     {
  166.         $roles = array();
  167.         foreach ($this->userRoles as $role) {
  168.             $roles[] = $role->getRole();
  169.         }
  170.         return $roles;
  171.     }
  172.     public function addRole(Role $role)
  173.     {
  174.         if (!$this->userRoles->contains($role)) {
  175.             $this->userRoles[] = $role;
  176.         }
  177.         return $this;
  178.     }
  179.     public function serialize()
  180.     {
  181.         return serialize(array(
  182.             $this->id,
  183.             $this->username,
  184.             $this->password
  185.         ));
  186.     }
  187.     public function unserialize($serialized)
  188.     {
  189.         list ($this->id$this->username$this->password) = unserialize($serialized, [
  190.             'allowed_classes' => false
  191.         ]);
  192.     }
  193.     public function eraseCredentials()
  194.     {
  195.     }
  196.     public function getSalt()
  197.     {
  198.         return null;
  199.     }
  200.     /**
  201.      * @return string|null
  202.      */
  203.     public function getUsername(): ?string
  204.     {
  205.         return $this->username;
  206.     }
  207.     /**
  208.      * @return string|null
  209.      */
  210.     public function getPassword(): ?string
  211.     {
  212.         return $this->password;
  213.     }
  214.     /**
  215.      * @Groups("user")
  216.      * @return string|null
  217.      */
  218.     public function getDescricaoMontada(): ?string
  219.     {
  220.         $d $this->username ' - ' $this->nome;
  221.         if ($this->descricao) {
  222.             $d .= ' (' $this->descricao ')';
  223.         }
  224.         return $d;
  225.     }
  226. }