From aec1a3e125a7ac66ed3682ae274aa1dcc134d74a Mon Sep 17 00:00:00 2001 From: tracer Date: Mon, 31 Jan 2022 21:01:00 +0100 Subject: [PATCH] moved PDO stuff to repository Signed-off-by: tracer --- src/Controller/ApiKeys.php | 121 ------------------------------------- 1 file changed, 121 deletions(-) diff --git a/src/Controller/ApiKeys.php b/src/Controller/ApiKeys.php index c22b1fa..e887b14 100644 --- a/src/Controller/ApiKeys.php +++ b/src/Controller/ApiKeys.php @@ -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()); - } - } } \ No newline at end of file