Authentication/src/FuzeWorks/Authentication/Drivers/PdoSessionsModelDriver.php

205 lines
6.9 KiB
PHP

<?php
/**
* FuzeWorks Authentication Plugin.
*
* The FuzeWorks PHP FrameWork
*
* Copyright (C) 2013 - 2023 i15
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @author i15
* @copyright Copyright (C) 2013 - 2023, i15. (https://i15.nl)
* @license https://opensource.org/licenses/MIT MIT License
*
* @since Version 1.3.0
*
* @version Version 1.3.0
*/
namespace FuzeWorks\Authentication\Drivers;
use FuzeWorks\Authentication\Exceptions\AuthenticationException;
use FuzeWorks\Authentication\Model\Session;
use FuzeWorks\Authentication\Model\Users;
use FuzeWorks\Authentication\SessionsModelDriverInterface;
use FuzeWorks\DatabaseEngine\iDatabaseEngine;
use FuzeWorks\DatabaseEngine\PDOEngine;
use FuzeWorks\Factory;
use FuzeWorks\ObjectStorage\ObjectStorageComponent;
use PDO;
class PdoSessionsModelDriver implements SessionsModelDriverInterface
{
/** @var PDOEngine $engine */
protected iDatabaseEngine $engine;
protected Users $usersModel;
protected bool $schema;
public function __construct(iDatabaseEngine $engine, Users $usersModel)
{
$this->engine = $engine;
$this->usersModel = $usersModel;
// Load object storage
/** @var ObjectStorageComponent $storage */
$storage = Factory::getInstance("storage");
$cache = $storage->getCache();
// Lookup schema
$schema = $cache->get("authPdoSessionsSchema");
if (is_null($schema))
{
$this->loadSchema();
$cache->set("authPdoSessionsSchema", true);
}
$this->schema = true;
}
private function loadSchema(): bool
{
// Check if the sessions table exists
$statement = $this->engine->prepare("SELECT * FROM information_schema.tables WHERE `table_name` = :table_name LIMIT 1");
$statement->execute([':table_name' => 'sessions']);
if ($statement->rowCount() !== 1 && !$this->populateSchema())
throw new AuthenticationException("Could not initiate Authentication. Database schema is missing.");
return true;
}
private function populateSchema(): bool
{
// @todo Implement schema populator
return false;
}
public function createSession(Session $session): bool
{
$insert = [
"sessionKey" => $session->sessionKey,
"user" => $session->user->id,
"expiryDate" => $session->expiryDate,
"expiryThreshold" => $session->expiryThreshold,
"persistent" => (int) $session->persistent,
"active" => (int) $session->active,
"context" => json_encode($session->context)
];
// Perform the insert
$statement = $this->engine->prepare("INSERT INTO sessions (sessionKey, user, expiryDate, expiryThreshold, persistent, active, context) VALUES (:sessionKey, :user, :expiryDate, :expiryThreshold, :persistent, :active, :context)");
$statement->execute($insert);
return $statement->rowCount() === 1;
}
public function readSessions(array $filter): array
{
// Prepare the where statement
if (empty($filter))
$where = '';
else
{
$whereKeys = [];
foreach ($filter as $filterKey => $filterVal)
$whereKeys[] = $filterKey . '=:' . $filterKey;
$where = 'WHERE ' . implode(' AND ', $whereKeys);
}
// Prepare the statement
$sql = "SELECT * FROM sessions $where";
$statement = $this->engine->prepare($sql);
// And execute the query
foreach ($filter as $key => $val)
{
if (is_null($val))
$statement->bindValue(':' . $key, $val, PDO::PARAM_NULL);
else
$statement->bindValue(':' . $key, $val);
}
$statement->execute();
// And return the results
$sessions = [];
while ($result = $statement->fetch(PDO::FETCH_OBJ))
{
// Load variables
$sessionKey = $result->sessionKey;
$user = $this->usersModel->getUserByUid($result->user);
$expiryDate = $result->expiryDate;
$expiryThreshold = $result->expiryThreshold;
$persistent = $result->persistent;
$active = $result->active;
$context = json_decode($result->context, true);
$sessions[] = new Session($user, $sessionKey, $expiryDate, $expiryThreshold, $context, $persistent, $active);
}
return $sessions;
}
public function updateSessions(array $sessions): bool
{
// Prepare statement
$statement = $this->engine->prepare("UPDATE sessions SET user=:user, expiryDate=:expiryDate, expiryThreshold=:expiryThreshold, persistent=:persistent, active=:active, context=:context WHERE sessionKey=:sessionKey");
// Loop over sessions
foreach ($sessions as $session)
{
$update = [
"user" => $session->user->id,
"expiryDate" => $session->expiryDate,
"expiryThreshold" => $session->expiryThreshold,
"persistent" => (int) $session->persistent,
"active" => (int) $session->active,
"context" => json_encode($session->context),
"sessionKey" => $session->sessionKey
];
// Perform the update
$statement->execute($update);
if ($statement->rowCount() !== 1)
return false;
}
return true;
}
public function deleteSessions(array $sessions): bool
{
// Prepare statement
$statement = $this->engine->prepare("DELETE FROM sessions WHERE sessionKey=:sessionKey");
// Loop over sessions
foreach ($sessions as $session)
{
// Perform the delete
$statement->execute([':sessionKey' => $session->sessionKey]);
if ($statement->rowCount() !== 1)
return false;
}
return true;
}
}