src/Entity/UserApi.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use AdScore\Common\StrUtils;
  4. use App\Component\Serializer\Normalizer\ProxyAwareObjectNormalizer;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\Serializer\Annotation as SA;
  7. /**
  8.  * UserApi
  9.  *
  10.  * @ORM\Table(name="users_api")
  11.  * @ORM\Entity
  12.  */
  13. class UserApi extends User
  14. {
  15.     public const KEY_TYPE = [
  16.         'static' => 'static',
  17.     ];
  18.     public const SERIALIZATION_GROUP = [
  19.         UserApi::class,
  20.         User::class,
  21.         UserAllowedClient::class,
  22.         Role::class,
  23.     ];
  24.     public const SERIALIZATION_CONTEXT = [
  25.         'read' => ['groups' => ['user_general''user_details''user_api'], ProxyAwareObjectNormalizer::RESOLVE_PROXY_CLASSES => self::SERIALIZATION_GROUP ],
  26.         'create' => ['groups' => ['user_general''user_details''user_api''user_api_create'], ProxyAwareObjectNormalizer::RESOLVE_PROXY_CLASSES => self::SERIALIZATION_GROUP ],
  27.     ];
  28.     /**
  29.      * @var string|null
  30.      *
  31.      * @ORM\Column(name="description", type="string", length=255, nullable=true)
  32.      * @SA\Groups({"user_api"})
  33.      */
  34.     private $description;
  35.     /**
  36.      * @var string
  37.      *
  38.      * @SA\Ignore
  39.      * @ORM\Column(name="`key`", type="text", length=65535, nullable=false)
  40.      */
  41.     private $key;
  42.     /**
  43.      * @var string
  44.      *
  45.      * @ORM\Column(name="key_type", type="string", length=0, nullable=false)
  46.      * @SA\Groups({"user_api"})
  47.      */
  48.     private $keyType;
  49.     /**
  50.      * @var string|null
  51.      * @SA\Groups({"user_api_create"})
  52.      * @SA\SerializedName("password")
  53.      */
  54.     private $plainKey null;
  55.     /**
  56.      * @var string|null
  57.      * @SA\Groups({"user_api_create"})
  58.      * @SA\SerializedName("auth_header")
  59.      */
  60.     private $authHeader null;
  61.     public function __construct(
  62.         Account $account,
  63.         ?string  $description null,
  64.         ?array   $ipAddress null,
  65.         ?string $key null,
  66.         string  $keyType self::KEY_TYPE['static'],
  67.         string  $roleName Role::ROLES['owner']
  68.     ) {
  69.         $password $key ?? StrUtils::generateRandomPassword(
  70.             StrUtils::PASSWORD_DEFAULT_LENGTH
  71.         );
  72.         parent::__construct($account$roleNameUser::SENTRY_LOGGING['off']);
  73.         $this->setDescription($description)
  74.             ->setPlainKey($password)
  75.             ->setKeyType($keyType);
  76.         if (!empty($ipAddress)) {
  77.             foreach ($ipAddress as $ip) {
  78.                 $allowed = new UserAllowedClient();
  79.                 $allowed->setIpAddress($ip)
  80.                     ->setUser($this);
  81.             }
  82.         }
  83.     }
  84.     public function getDescription(): ?string
  85.     {
  86.         return $this->description;
  87.     }
  88.     public function setDescription(?string $description): self
  89.     {
  90.         $this->description $description;
  91.         return $this;
  92.     }
  93.     public function getKey(): ?string
  94.     {
  95.         return $this->key;
  96.     }
  97.     public function setKey(string $key): self
  98.     {
  99.         $this->key $key;
  100.         return $this;
  101.     }
  102.     public function getKeyType(): ?string
  103.     {
  104.         return $this->keyType;
  105.     }
  106.     public function setKeyType(string $keyType): self
  107.     {
  108.         $this->keyType $keyType;
  109.         return $this;
  110.     }
  111.     /**
  112.      * @return mixed
  113.      */
  114.     public function getPlainKey()
  115.     {
  116.         return $this->plainKey;
  117.     }
  118.     /**
  119.      * @param mixed $plainKey
  120.      */
  121.     public function setPlainKey($plainKey): self
  122.     {
  123.         $this->plainKey $plainKey;
  124.         $this->setKey(StrUtils::hashPassword($plainKey));
  125.         return $this;
  126.     }
  127.     /**
  128.      * @return string|null
  129.      */
  130.     public function getAuthHeader(): ?string
  131.     {
  132.         return "Authorization: Basic " . \base64_encode($this->getId(). ':' $this->plainKey);
  133.     }
  134. }