2025-01-31 18:20:13 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-08-12 14:36:18 +03:00
|
|
|
namespace openvk\Web\Models\Entities;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2020-08-12 14:36:18 +03:00
|
|
|
use openvk\Web\Models\RowModel;
|
|
|
|
use openvk\Web\Models\Repositories\Users;
|
|
|
|
use Nette\InvalidStateException as ISE;
|
|
|
|
|
|
|
|
class APIToken extends RowModel
|
|
|
|
{
|
|
|
|
protected $tableName = "api_tokens";
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getUser(): User
|
2020-08-12 14:36:18 +03:00
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
return (new Users())->get($this->getRecord()->user);
|
2020-08-12 14:36:18 +03:00
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getSecret(): string
|
2020-08-12 14:36:18 +03:00
|
|
|
{
|
|
|
|
return $this->getRecord()->secret;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getFormattedToken(): string
|
2020-08-12 14:36:18 +03:00
|
|
|
{
|
|
|
|
return $this->getId() . "-" . chunk_split($this->getSecret(), 8, "-") . "jill";
|
|
|
|
}
|
2022-12-17 02:03:02 +03:00
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
public function getPlatform(): ?string
|
2022-12-17 02:03:02 +03:00
|
|
|
{
|
|
|
|
return $this->getRecord()->platform;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function isRevoked(): bool
|
2020-08-12 14:36:18 +03:00
|
|
|
{
|
|
|
|
return $this->isDeleted();
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function setUser(User $user): void
|
2020-08-12 14:36:18 +03:00
|
|
|
{
|
|
|
|
$this->stateChanges("user", $user->getId());
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function setSecret(string $secret): void
|
2020-08-12 14:36:18 +03:00
|
|
|
{
|
|
|
|
throw new ISE("Setting secret manually is prohbited");
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function revoke(): void
|
2020-08-12 14:36:18 +03:00
|
|
|
{
|
|
|
|
$this->delete();
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function save(?bool $log = false): void
|
2020-08-12 14:36:18 +03:00
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
if (is_null($this->getRecord())) {
|
2020-08-12 14:36:18 +03:00
|
|
|
$this->stateChanges("secret", bin2hex(openssl_random_pseudo_bytes(36)));
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|
|
|
|
|
2020-08-12 14:36:18 +03:00
|
|
|
parent::save();
|
|
|
|
}
|
|
|
|
}
|