moved PDO stuff to repository
Signed-off-by: tracer <tracer@24unix.net>
This commit is contained in:
parent
9f8d572244
commit
aec1a3e125
@ -16,126 +16,5 @@ class ApiKeys
|
||||
{}
|
||||
|
||||
|
||||
/**
|
||||
* @return array|false
|
||||
*/
|
||||
public function findAll(): bool|array
|
||||
{
|
||||
$sql = "
|
||||
SELECT id, name, api_token_prefix, api_token
|
||||
FROM " . DatabaseConnection::TABLE_USER;
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
||||
$statement->execute();
|
||||
return $statement->fetchAll(mode: PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $e) {
|
||||
exit($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Int $id
|
||||
*
|
||||
* @return array|false
|
||||
*/
|
||||
public function findByID(Int $id): bool|array
|
||||
{
|
||||
$sql = "
|
||||
SELECT name, api_token_prefix, api_token
|
||||
FROM " . DatabaseConnection::TABLE_USER . "
|
||||
WHERE id = :id;
|
||||
";
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
||||
$statement->bindParam(param: ':id', var: $id);
|
||||
$statement->execute();
|
||||
return $statement->fetch(mode: PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $e) {
|
||||
exit($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param String $prefix
|
||||
*
|
||||
* @return bool|array
|
||||
*/
|
||||
public function findByPrefix(String $prefix): bool|array
|
||||
{
|
||||
$sql = "
|
||||
SELECT name, api_token
|
||||
FROM " . DatabaseConnection::TABLE_USER . "
|
||||
WHERE api_token_prefix = :prefix";
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
||||
$statement->bindParam(param: ':prefix', var: $prefix);
|
||||
$statement->execute();
|
||||
return $statement->fetch(mode: PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $e) {
|
||||
exit($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array|void
|
||||
*/
|
||||
public function create(String $name = '')
|
||||
{
|
||||
$tokenPrefix = uniqid();
|
||||
$result['tokenPrefix'] = $tokenPrefix;
|
||||
try {
|
||||
$key = bin2hex(string: random_bytes(length: 24));
|
||||
$result['key'] = $key;
|
||||
} catch (Exception $e) {
|
||||
echo $e->getMessage() . PHP_EOL;
|
||||
exit(1);
|
||||
}
|
||||
$token = password_hash(password: $tokenPrefix . '.' . $key, algo: PASSWORD_ARGON2ID);
|
||||
|
||||
$sql = "
|
||||
INSERT INTO " . DatabaseConnection::TABLE_USER . " (name, api_token_prefix, api_token)
|
||||
VALUES (:name, :token_prefix, :token)";
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
||||
$statement->bindParam(param: ':token_prefix', var: $tokenPrefix);
|
||||
$statement->bindParam(param: ':token', var: $token);
|
||||
$statement->bindParam(param: ':name', var: $name);
|
||||
|
||||
$statement->execute();
|
||||
$result['row'] = $this->databaseConnection->getConnection()->lastInsertId();
|
||||
return $result;
|
||||
} catch (PDOException $e) {
|
||||
exit($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function delete($id): int
|
||||
{
|
||||
$sql = "
|
||||
DELETE FROM " . DatabaseConnection::TABLE_USER . "
|
||||
WHERE id = :id";
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
||||
$statement->bindParam(param: 'id', var: $id);
|
||||
$statement->execute();
|
||||
return $statement->rowCount();
|
||||
} catch (PDOException $e) {
|
||||
exit($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user