diff --git a/module/VuFind/src/VuFind/Db/Row/AuthHash.php b/module/VuFind/src/VuFind/Db/Row/AuthHash.php deleted file mode 100644 index de5a60da0ac..00000000000 --- a/module/VuFind/src/VuFind/Db/Row/AuthHash.php +++ /dev/null @@ -1,195 +0,0 @@ - - * @author Ere Maijala - * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License - * @link https://vufind.org Main Site - */ - -namespace VuFind\Db\Row; - -use DateTime; -use VuFind\Db\Entity\AuthHashEntityInterface; -use VuFind\Db\Service\DbServiceAwareInterface; -use VuFind\Db\Service\DbServiceAwareTrait; - -/** - * Row Definition for auth_hash - * - * @category VuFind - * @package Db_Row - * @author Demian Katz - * @author Ere Maijala - * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License - * @link https://vufind.org Main Site - * - * @property int $id - * @property string $session_id - * @property string $hash - * @property string $type - * @property string $data - * @property string $created - */ -class AuthHash extends RowGateway implements AuthHashEntityInterface, DbServiceAwareInterface -{ - use \VuFind\Db\Table\DbTableAwareTrait; - use DbServiceAwareTrait; - - /** - * Constructor - * - * @param \Laminas\Db\Adapter\Adapter $adapter Database adapter - */ - public function __construct($adapter) - { - parent::__construct('id', 'auth_hash', $adapter); - } - - /** - * Get identifier (returns null for an uninitialized or non-persisted object). - * - * @return ?int - */ - public function getId(): ?int - { - return $this->id ?? null; - } - - /** - * Get PHP session id string. - * - * @return ?string - */ - public function getSessionId(): ?string - { - return $this->session_id ?? null; - } - - /** - * Set PHP session id string. - * - * @param ?string $sessionId PHP Session id string - * - * @return static - */ - public function setSessionId(?string $sessionId): static - { - $this->session_id = $sessionId; - return $this; - } - - /** - * Get hash value. - * - * @return string - */ - public function getHash(): string - { - return $this->hash ?? ''; - } - - /** - * Set hash value. - * - * @param string $hash Hash Value - * - * @return static - */ - public function setHash(string $hash): static - { - $this->hash = $hash; - return $this; - } - - /** - * Get type of hash. - * - * @return ?string - */ - public function getHashType(): ?string - { - return $this->type ?? null; - } - - /** - * Set type of hash. - * - * @param ?string $type Hash Type - * - * @return static - */ - public function setHashType(?string $type): static - { - $this->type = $type; - return $this; - } - - /** - * Get data. - * - * @return ?string - */ - public function getData(): ?string - { - return $this->__get('data'); - } - - /** - * Set data. - * - * @param ?string $data Data - * - * @return static - */ - public function setData(?string $data): static - { - $this->__set('data', $data); - return $this; - } - - /** - * Get created date. - * - * @return DateTime - */ - public function getCreated(): DateTime - { - return DateTime::createFromFormat('Y-m-d H:i:s', $this->created); - } - - /** - * Set created date. - * - * @param DateTime $dateTime Created date - * - * @return static - */ - public function setCreated(DateTime $dateTime): static - { - $this->created = $dateTime->format('Y-m-d H:i:s'); - return $this; - } -} diff --git a/module/VuFind/src/VuFind/Db/Row/PluginManager.php b/module/VuFind/src/VuFind/Db/Row/PluginManager.php index 9913894c141..d2ca7cb318c 100644 --- a/module/VuFind/src/VuFind/Db/Row/PluginManager.php +++ b/module/VuFind/src/VuFind/Db/Row/PluginManager.php @@ -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, diff --git a/module/VuFind/src/VuFind/Db/Service/AccessTokenService.php b/module/VuFind/src/VuFind/Db/Service/AccessTokenService.php index 62026a0144b..81f593eeaf5 100644 --- a/module/VuFind/src/VuFind/Db/Service/AccessTokenService.php +++ b/module/VuFind/src/VuFind/Db/Service/AccessTokenService.php @@ -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. @@ -47,11 +45,8 @@ */ class AccessTokenService extends AbstractDbService implements AccessTokenServiceInterface, - Feature\DeleteExpiredInterface, - LoggerAwareInterface + Feature\DeleteExpiredInterface { - use LoggerAwareTrait; - /** * Create an access_token entity object. * diff --git a/module/VuFind/src/VuFind/Db/Service/AuthHashService.php b/module/VuFind/src/VuFind/Db/Service/AuthHashService.php index 68d40f93ed9..da9ff8af280 100644 --- a/module/VuFind/src/VuFind/Db/Service/AuthHashService.php +++ b/module/VuFind/src/VuFind/Db/Service/AuthHashService.php @@ -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. @@ -45,11 +44,8 @@ */ class AuthHashService extends AbstractDbService implements AuthHashServiceInterface, - DbTableAwareInterface, Feature\DeleteExpiredInterface { - use DbTableAwareTrait; - /** * Create an auth_hash entity object. * @@ -57,7 +53,8 @@ class AuthHashService extends AbstractDbService implements */ public function createEntity(): AuthHashEntityInterface { - return $this->getDbTable('AuthHash')->createRow(); + $class = $this->getEntityClass(AuthHash::class); + return new $class(); } /** @@ -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(); } /** @@ -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; } /** @@ -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; } /** @@ -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(); } } diff --git a/module/VuFind/src/VuFind/Db/Table/AuthHash.php b/module/VuFind/src/VuFind/Db/Table/AuthHash.php deleted file mode 100644 index baf35c287f6..00000000000 --- a/module/VuFind/src/VuFind/Db/Table/AuthHash.php +++ /dev/null @@ -1,123 +0,0 @@ - - * @author Ere Maijala - * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License - * @link https://vufind.org Main Page - */ - -namespace VuFind\Db\Table; - -use Laminas\Db\Adapter\Adapter; -use VuFind\Db\Row\RowGateway; - -/** - * Table Definition for auth_hash - * - * @category VuFind - * @package Db_Table - * @author Demian Katz - * @author Ere Maijala - * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License - * @link https://vufind.org Main Site - */ -class AuthHash extends Gateway -{ - use ExpirationTrait; - - public const TYPE_EMAIL = 'email'; // EmailAuthenticator - - /** - * Constructor - * - * @param Adapter $adapter Database adapter - * @param PluginManager $tm Table manager - * @param array $cfg Laminas configuration - * @param RowGateway $rowObj Row prototype object (null for default) - * @param string $table Name of database table to interface with - */ - public function __construct( - Adapter $adapter, - PluginManager $tm, - $cfg, - ?RowGateway $rowObj = null, - $table = 'auth_hash' - ) { - parent::__construct($adapter, $tm, $cfg, $rowObj, $table); - } - - /** - * Retrieve an object from the database based on hash and type; create a new - * row if no existing match is found. - * - * @param string $hash Hash - * @param string $type Hash type - * @param bool $create Should we create rows that don't already exist? - * - * @return ?\VuFind\Db\Row\AuthHash - */ - public function getByHashAndType($hash, $type, $create = true) - { - $row = $this->select(['hash' => $hash, 'type' => $type])->current(); - if ($create && empty($row)) { - $row = $this->createRow(); - $row->hash = $hash; - $row->type = $type; - $row->created = date('Y-m-d H:i:s'); - } - return $row; - } - - /** - * Retrieve last object from the database based on session id. - * - * @param string $sessionId Session ID - * - * @return ?\VuFind\Db\Row\AuthHash - */ - public function getLatestBySessionId($sessionId) - { - $callback = function ($select) use ($sessionId) { - $select->where->equalTo('session_id', $sessionId); - $select->order('created DESC'); - }; - return $this->select($callback)->current(); - } - - /** - * Update the select statement to find records to delete. - * - * @param Select $select Select clause - * @param string $dateLimit Date threshold of an "expired" record in format - * 'Y-m-d H:i:s'. - * - * @return void - */ - protected function expirationCallback($select, $dateLimit) - { - $select->where->lessThan('created', $dateLimit); - } -} diff --git a/module/VuFind/src/VuFind/Db/Table/PluginManager.php b/module/VuFind/src/VuFind/Db/Table/PluginManager.php index 6327e2374f3..df3a4399b0f 100644 --- a/module/VuFind/src/VuFind/Db/Table/PluginManager.php +++ b/module/VuFind/src/VuFind/Db/Table/PluginManager.php @@ -46,7 +46,6 @@ class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager * @var array */ protected $aliases = [ - 'authhash' => AuthHash::class, 'externalsession' => ExternalSession::class, 'logintoken' => LoginToken::class, 'ratings' => Ratings::class, @@ -62,7 +61,6 @@ class PluginManager extends \VuFind\ServiceManager\AbstractPluginManager * @var array */ protected $factories = [ - AuthHash::class => GatewayFactory::class, ExternalSession::class => GatewayFactory::class, LoginToken::class => GatewayFactory::class, Ratings::class => GatewayFactory::class,