Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auth hash service doctrine #33

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 0 additions & 195 deletions module/VuFind/src/VuFind/Db/Row/AuthHash.php

This file was deleted.

1 change: 0 additions & 1 deletion module/VuFind/src/VuFind/Db/Row/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager
* @var array
*/
protected $factories = [
AuthHash::class => RowGatewayFactory::class,
ExternalSession::class => RowGatewayFactory::class,
LoginToken::class => RowGatewayFactory::class,
Ratings::class => RowGatewayFactory::class,
Expand Down
7 changes: 1 addition & 6 deletions module/VuFind/src/VuFind/Db/Service/AccessTokenService.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@
namespace VuFind\Db\Service;

use DateTime;
use Laminas\Log\LoggerAwareInterface;
use VuFind\Db\Entity\AccessToken;
use VuFind\Db\Entity\AccessTokenEntityInterface;
use VuFind\Db\Entity\User;
use VuFind\Log\LoggerAwareTrait;

/**
* Database service for access tokens.
Expand All @@ -47,11 +45,8 @@
*/
class AccessTokenService extends AbstractDbService implements
AccessTokenServiceInterface,
Feature\DeleteExpiredInterface,
LoggerAwareInterface
Feature\DeleteExpiredInterface
{
use LoggerAwareTrait;

/**
* Create an access_token entity object.
*
Expand Down
55 changes: 45 additions & 10 deletions module/VuFind/src/VuFind/Db/Service/AuthHashService.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@
namespace VuFind\Db\Service;

use DateTime;
use VuFind\Db\Entity\AuthHash;
use VuFind\Db\Entity\AuthHashEntityInterface;
use VuFind\Db\Table\DbTableAwareInterface;
use VuFind\Db\Table\DbTableAwareTrait;

/**
* Database service for auth_hash table.
Expand All @@ -45,19 +44,17 @@
*/
class AuthHashService extends AbstractDbService implements
AuthHashServiceInterface,
DbTableAwareInterface,
Feature\DeleteExpiredInterface
{
use DbTableAwareTrait;

/**
* Create an auth_hash entity object.
*
* @return AuthHashEntityInterface
*/
public function createEntity(): AuthHashEntityInterface
{
return $this->getDbTable('AuthHash')->createRow();
$class = $this->getEntityClass(AuthHash::class);
return new $class();
}

/**
Expand All @@ -69,8 +66,12 @@ public function createEntity(): AuthHashEntityInterface
*/
public function deleteAuthHash(AuthHashEntityInterface|int $authHashOrId): void
{
$dql = 'DELETE FROM ' . $this->getEntityClass(AuthHash::class) . ' ah '
. 'WHERE ah.id = :id';
$query = $this->entityManager->createQuery($dql);
$authHashId = $authHashOrId instanceof AuthHashEntityInterface ? $authHashOrId->getId() : $authHashOrId;
$this->getDbTable('AuthHash')->delete(['id' => $authHashId]);
$query->setParameter('id', $authHashId);
$query->execute();
}

/**
Expand All @@ -85,7 +86,22 @@ public function deleteAuthHash(AuthHashEntityInterface|int $authHashOrId): void
*/
public function getByHashAndType(string $hash, string $type, bool $create = true): ?AuthHashEntityInterface
{
return $this->getDbTable('AuthHash')->getByHashAndType($hash, $type, $create);
$dql = 'SELECT ah '
. 'FROM ' . $this->getEntityClass(AuthHash::class) . ' ah '
. 'WHERE ah.hash = :hash '
. 'AND ah.type = :type';
$query = $this->entityManager->createQuery($dql);
$query->setParameters(compact('hash', 'type'));
$result = $query->getOneOrNullResult();
if ($result === null && $create) {
$result = $this->createEntity()
->setHash($hash)
->setHashType($type)
->setCreated(new DateTime());
$this->persistEntity($result);
}

return $result;
}

/**
Expand All @@ -97,7 +113,14 @@ public function getByHashAndType(string $hash, string $type, bool $create = true
*/
public function getLatestBySessionId(string $sessionId): ?AuthHashEntityInterface
{
return $this->getDbTable('AuthHash')->getLatestBySessionId($sessionId);
$dql = 'SELECT ah '
. 'FROM ' . $this->getEntityClass(AuthHash::class) . ' ah '
. 'WHERE ah.sessionId = :sessionId '
. 'ORDER BY ah.created DESC';
$query = $this->entityManager->createQuery($dql);
$query->setParameter('sessionId', $sessionId);
$result = $query->getOneOrNullResult();
return $result;
}

/**
Expand All @@ -110,6 +133,18 @@ public function getLatestBySessionId(string $sessionId): ?AuthHashEntityInterfac
*/
public function deleteExpired(DateTime $dateLimit, ?int $limit = null): int
{
return $this->getDbTable('AuthHash')->deleteExpired($dateLimit->format('Y-m-d H:i:s'), $limit);
$subQueryBuilder = $this->entityManager->createQueryBuilder();
$subQueryBuilder->select('ah.id')
->from($this->getEntityClass(AuthHashEntityInterface::class), 'ah')
->where('ah.created < :dateLimit')
->setParameter('dateLimit', $dateLimit->format('Y-m-d H:i:s'));
if ($limit) {
$subQueryBuilder->setMaxResults($limit);
}
$queryBuilder = $this->entityManager->createQueryBuilder();
$queryBuilder->delete($this->getEntityClass(AuthHashEntityInterface::class), 'ah')
->where('ah.id IN (:hashes)')
->setParameter('hashes', $subQueryBuilder->getQuery()->getResult());
return $queryBuilder->getQuery()->execute();
}
}
Loading
Loading