renamed ApiUser to ApiKeys

Signed-off-by: tracer <tracer@24unix.net>
This commit is contained in:
tracer 2022-01-23 16:36:04 +01:00
parent 4f02c68cc5
commit 69659bb33f

View File

@ -9,7 +9,7 @@ use PDOException;
/** /**
* *
*/ */
class ApiUsers class ApiKeys
{ {
public function __construct(private DatabaseConnection $databaseConnection) public function __construct(private DatabaseConnection $databaseConnection)
{} {}
@ -21,7 +21,7 @@ class ApiUsers
public function findAll(): bool|array public function findAll(): bool|array
{ {
$sql = " $sql = "
SELECT id, api_token_prefix, api_token SELECT id, name, api_token_prefix, api_token
FROM " . DatabaseConnection::TABLE_USER; FROM " . DatabaseConnection::TABLE_USER;
try { try {
@ -41,7 +41,7 @@ class ApiUsers
public function findByID(Int $id): bool|array public function findByID(Int $id): bool|array
{ {
$sql = " $sql = "
SELECT api_token_prefix, api_token SELECT name, api_token_prefix, api_token
FROM " . DatabaseConnection::TABLE_USER . " FROM " . DatabaseConnection::TABLE_USER . "
WHERE id = :id; WHERE id = :id;
"; ";
@ -65,7 +65,7 @@ class ApiUsers
public function findByPrefix(String $prefix): bool|array public function findByPrefix(String $prefix): bool|array
{ {
$sql = " $sql = "
SELECT api_token SELECT name, api_token
FROM " . DatabaseConnection::TABLE_USER . " FROM " . DatabaseConnection::TABLE_USER . "
WHERE api_token_prefix = :prefix"; WHERE api_token_prefix = :prefix";
@ -83,7 +83,7 @@ class ApiUsers
/** /**
* @return array|void * @return array|void
*/ */
public function create() public function create(String $name = '')
{ {
$tokenPrefix = uniqid(); $tokenPrefix = uniqid();
$result['tokenPrefix'] = $tokenPrefix; $result['tokenPrefix'] = $tokenPrefix;
@ -97,13 +97,15 @@ class ApiUsers
$token = password_hash(password: $tokenPrefix . '.' . $key, algo: PASSWORD_ARGON2ID); $token = password_hash(password: $tokenPrefix . '.' . $key, algo: PASSWORD_ARGON2ID);
$sql = " $sql = "
INSERT INTO " . DatabaseConnection::TABLE_USER . " (api_token_prefix, api_token) INSERT INTO " . DatabaseConnection::TABLE_USER . " (name, api_token_prefix, api_token)
VALUES (:token_prefix, :token)"; VALUES (:token_prefix, :token)";
try { try {
$statement = $this->databaseConnection->getConnection()->prepare($sql); $statement = $this->databaseConnection->getConnection()->prepare($sql);
$statement->bindParam(param: ':token_prefix', var: $tokenPrefix); $statement->bindParam(param: ':token_prefix', var: $tokenPrefix);
$statement->bindParam(param: ':token', var: $token); $statement->bindParam(param: ':token', var: $token);
$statement->bindParam(param: ':name', var: $name);
$statement->execute(); $statement->execute();
$result['row'] = $this->databaseConnection->getConnection()->lastInsertId(); $result['row'] = $this->databaseConnection->getConnection()->lastInsertId();
return $result; return $result;