src/Entity/User.php line 52

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use JsonException;
  4. use Lexik\Bundle\JWTAuthenticationBundle\Security\User\JWTUserInterface;
  5. use Symfony\Component\HttpClient\HttpClient;
  6. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  7. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  8. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  9. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  10. /**
  11.  * @ORM\Entity
  12.  * @ORM\Table(name="user")
  13.  */
  14. final class User implements JWTUserInterface
  15. {
  16.     /**
  17.      * @ORM\Id
  18.      * @ORM\GeneratedValue
  19.      * @ORM\Column(type="integer")
  20.      */
  21.     private int $id;
  22.     /**
  23.      * @ORM\Column(type="string", unique=true)
  24.      */
  25.     private string $username;
  26.     /**
  27.      * @ORM\Column(type="string", unique=true)
  28.      */
  29.     private string $email;
  30.     /**
  31.      * @ORM\Column(type="string")
  32.      */
  33.     private string $password;
  34.     /**
  35.      * @ORM\Column(type="json_array")
  36.      */
  37.     private array $roles;
  38.     /**
  39.      * @ORM\Column(type="string", unique=true)
  40.      */
  41.     private string $apiToken;
  42.     /**
  43.      * @param string $username
  44.      * @param array $roles
  45.      * @param string $email
  46.      * @param string $password
  47.      */
  48.     public function __construct(string $username, array $rolesstring $emailstring $password)
  49.     {
  50.         $this->username $username;
  51.         $this->roles    $roles;
  52.         $this->email    $email;
  53.         $this->password $password;
  54.     }
  55.     /**
  56.      * @param $username
  57.      * @param array $payload
  58.      *
  59.      * @return User
  60.      */
  61.     public static function createFromPayload($username, array $payload): User
  62.     {
  63.         return new self(
  64.             $username,
  65.             $payload['roles'],
  66.             $payload['email'],
  67.             $payload['password']
  68.         );
  69.     }
  70.     /**
  71.      * @return int
  72.      */
  73.     public function getId(): int
  74.     {
  75.         return $this->id;
  76.     }
  77.     /**
  78.      * @return string
  79.      */
  80.     public function getUsername(): string
  81.     {
  82.         return $this->username;
  83.     }
  84.     /**
  85.      * @param $username
  86.      *
  87.      * @return $this
  88.      */
  89.     public function setUsername($username): User
  90.     {
  91.         $this->username $username;
  92.         return $this;
  93.     }
  94.     /**
  95.      * @return string|null
  96.      */
  97.     public function getEmail(): ?string
  98.     {
  99.         return $this->email;
  100.     }
  101.     /**
  102.      * @param $email
  103.      *
  104.      * @return $this
  105.      */
  106.     public function setEmail($email): User
  107.     {
  108.         $this->email $email;
  109.         return $this;
  110.     }
  111.     /**
  112.      * @return string|null
  113.      */
  114.     public function getPassword(): ?string
  115.     {
  116.         return $this->password;
  117.     }
  118.     /**
  119.      * @param $password
  120.      *
  121.      * @return $this
  122.      */
  123.     public function setPassword($password): User
  124.     {
  125.         $this->password $password;
  126.         return $this;
  127.     }
  128.     /**
  129.      * Returns the roles or permissions granted to the user for security.
  130.      */
  131.     public function getRoles(): array
  132.     {
  133.         $roles $this->roles;
  134.         if (empty($roles)) {
  135.             $roles[] = 'User';
  136.         }
  137.         return array_unique($roles);
  138.     }
  139.     /**
  140.      * @param $roles
  141.      *
  142.      * @return $this
  143.      */
  144.     public function setRoles($roles): User
  145.     {
  146.         $this->roles $roles;
  147.         return $this;
  148.     }
  149.     /**
  150.      * Returns the salt that was originally used to encode the password.
  151.      */
  152.     public function getSalt(): User
  153.     {
  154.         return $this;
  155.     }
  156.     /**
  157.      * Removes sensitive data from the user.
  158.      */
  159.     public function eraseCredentials(): void
  160.     {
  161.         // if you had a plainPassword property, you'd nullify it here
  162.         // $this->plainPassword = null;
  163.     }
  164.     /**
  165.      * @param string $apiToken
  166.      *
  167.      * @return User
  168.      */
  169.     public function setApiToken(string $apiToken): User
  170.     {
  171.         $this->apiToken $apiToken;
  172.         return $this;
  173.     }
  174.     /**
  175.      * @return string
  176.      */
  177.     public function getApiToken(): string
  178.     {
  179.         return $this->apiToken;
  180.     }
  181.     /**
  182.      * Returns the identifier for this user (e.g. its username or email address).
  183.      */
  184.     public function getUserIdentifier(): string
  185.     {
  186.         return $this->email;
  187.     }
  188.     /**
  189.      * @param string $apiUrl
  190.      * @param string $username
  191.      * @param string $password
  192.      * @param string $clientId
  193.      * @param string $secretKey
  194.      * @param string $method
  195.      * @param string $contentType
  196.      * @param string $role
  197.      *
  198.      * @return array
  199.      * @throws JsonException
  200.      * @throws TransportExceptionInterface
  201.      */
  202.     public function callUserApi(
  203.         string $apiUrl,
  204.         string $username,
  205.         string $password,
  206.         string $clientId,
  207.         string $secretKey,
  208.         string $method 'POST',
  209.         string $contentType 'application/x-www-form-urlencoded',
  210.         string $role 'User'
  211.     ): array {
  212.         $params = [
  213.             'role'          => $role,
  214.             'client_id'     => $clientId,
  215.             'client_secret' => $secretKey,
  216.             'grant_type'    => 'facticity',
  217.             'username'      => $username,
  218.             'password'      => $password,
  219.         ];
  220.         $http_client HttpClient::create([ 'headers' => [ 'Content-Type: ' $contentType ] ]);
  221.         $response    $http_client->request(
  222.             $method,
  223.             $apiUrl,
  224.             [ 'body' => $params ]
  225.         );
  226.         try {
  227.             $content json_decode($response->getContent(), true512JSON_THROW_ON_ERROR);
  228.             if (array_key_exists('error'$content)) {
  229.                 return [ false$content['error'] ];
  230.             }
  231.             return $content;
  232.         } catch (
  233.         ClientExceptionInterface RedirectionExceptionInterface |
  234.         ServerExceptionInterface TransportExceptionInterface $e
  235.         ) {
  236.             return [ false$e->getMessage() ];
  237.         }
  238.     }
  239.     /**
  240.      * @param string $apiUrl
  241.      * @param array $params
  242.      * @param string $method
  243.      * @param string $contentType
  244.      *
  245.      * @return array
  246.      * @throws JsonException
  247.      * @throws TransportExceptionInterface
  248.      */
  249.     public function callDataApi(
  250.         string $apiUrl,
  251.         array $params,
  252.         string $method 'POST',
  253.         string $contentType 'application/x-www-form-urlencoded'
  254.     ): array {
  255.         $http_client HttpClient::create([ 'headers' => [ 'Content-Type: ' $contentType ] ]);
  256.         $response    $http_client->request(
  257.             $method,
  258.             $apiUrl,
  259.             [ 'body' => $params ]
  260.         );
  261.         try {
  262.             $content json_decode($response->getContent(), true512JSON_THROW_ON_ERROR);
  263.             if (array_key_exists('error'$content)) {
  264.                 return [ false$content['error'] ];
  265.             }
  266.             return $content;
  267.         } catch (
  268.             ClientExceptionInterface RedirectionExceptionInterface |
  269.             ServerExceptionInterface TransportExceptionInterface $e
  270.         ) {
  271.             return [ false$e->getMessage() ];
  272.         }
  273.     }
  274. }