Migrate away from legacy mapper

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2022-10-12 15:35:24 +02:00
parent 479bdb0999
commit c395fca3c0
No known key found for this signature in database
GPG key ID: 4C614C6ED2CDE6DF
4 changed files with 49 additions and 69 deletions

View file

@ -24,20 +24,21 @@
namespace OCA\Richdocuments\Db;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\Mapper;
use OCP\AppFramework\Db\QBMapper;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\Security\ISecureRandom;
class AssetMapper extends Mapper {
/** @var int Limetime of a token is 10 minutes */
public const tokenLifeTime = 600;
/**
* @method findEntity(IQueryBuilder $query): Asset
*/
class AssetMapper extends QBMapper {
/** @var int Lifetime of a token is 10 minutes */
public const TOKEN_TTL = 600;
/** @var ISecureRandom */
private $random;
/** @var ITimeFactory */
private $time;
private ISecureRandom $random;
private ITimeFactory $time;
public function __construct(IDBConnection $db, ISecureRandom $random, ITimeFactory $timeFactory) {
parent::__construct($db, 'richdocuments_assets', Asset::class);
@ -58,39 +59,31 @@ class AssetMapper extends Mapper {
$asset->setTimestamp($this->time->getTime());
$asset->setToken($this->random->generate(64, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS));
$asset = $this->insert($asset);
return $asset;
return $this->insert($asset);
}
/**
* @param $token
* @return Asset
* @throws DoesNotExistException
*/
public function getAssetByToken($token) {
public function getAssetByToken(string $token): Asset {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from('richdocuments_assets')
->where($qb->expr()->eq('token', $qb->createNamedParameter($token)));
$cursor = $qb->execute();
$data = $cursor->fetch();
$cursor->closeCursor();
try {
$asset = $this->findEntity($qb);
// Check the token lifetime
if ($asset->getTimestamp() + self::TOKEN_TTL < $this->time->getTime()) {
$this->delete($asset);
throw new DoesNotExistException('No asset for token found');
}
//There can only be one row since it is a unqiue field
if ($data === false) {
throw new DoesNotExistException('No asset for token found');
return $asset;
} catch (\Exception $e) {
}
$asset = Asset::fromRow($data);
// Check the token lifetime
if ($asset->getTimestamp() + self::tokenLifeTime < $this->time->getTime()) {
$this->delete($asset);
throw new DoesNotExistException('No asset for token found');
}
return $asset;
throw new DoesNotExistException('No asset for token found');
}
}

View file

@ -24,20 +24,17 @@
namespace OCA\Richdocuments\Db;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\Mapper;
use OCP\AppFramework\Db\QBMapper;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IDBConnection;
use OCP\Security\ISecureRandom;
class DirectMapper extends Mapper {
/** @var int Limetime of a token is 10 minutes */
public const tokenLifeTime = 600;
class DirectMapper extends QBMapper {
/** @var int Lifetime of a token is 10 minutes */
public const TOKEN_TTL = 600;
/** @var ISecureRandom */
protected $random;
/**@var ITimeFactory */
protected $timeFactory;
protected ISecureRandom $random;
protected ITimeFactory $timeFactory;
public function __construct(IDBConnection $db,
ISecureRandom $random,
@ -65,36 +62,29 @@ class DirectMapper extends Mapper {
$direct->setInitiatorHost($initiatorHost);
$direct->setInitiatorToken($initiatorToken);
$direct = $this->insert($direct);
return $direct;
return $this->insert($direct);
}
/**
* @param string $token
* @return Direct
* @throws DoesNotExistException
*/
public function getByToken($token) {
public function getByToken(string $token): Direct {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from('richdocuments_direct')
->where($qb->expr()->eq('token', $qb->createNamedParameter($token)));
$cursor = $qb->execute();
$row = $cursor->fetch();
$cursor->closeCursor();
try {
$direct = $this->findEntity($qb);
if (($direct->getTimestamp() + self::TOKEN_TTL) < $this->timeFactory->getTime()) {
$this->delete($direct);
throw new DoesNotExistException('Could not find token.');
}
//There can only be one as the token is unique
if ($row === false) {
throw new DoesNotExistException('Could not find token.');
return $direct;
} catch (\Exception $e) {
}
$direct = Direct::fromRow($row);
if (($direct->getTimestamp() + self::tokenLifeTime) < $this->timeFactory->getTime()) {
$this->delete($direct);
throw new DoesNotExistException('Could not find token.');
}
return $direct;
throw new DoesNotExistException('No asset for token found');
}
}

View file

@ -139,18 +139,18 @@ class Wopi extends Entity implements \JsonSerializable {
protected $tokenType = 0;
public function __construct() {
$this->addType('owner_uid', 'string');
$this->addType('editor_uid', 'string');
$this->addType('ownerUid', 'string');
$this->addType('editorUid', 'string');
$this->addType('fileid', 'int');
$this->addType('version', 'int');
$this->addType('canwrite', 'bool');
$this->addType('server_host', 'string');
$this->addType('serverHost', 'string');
$this->addType('token', 'string');
$this->addType('expiry', 'int');
$this->addType('guest_displayname', 'string');
$this->addType('guestDisplayname', 'string');
$this->addType('templateDestination', 'int');
$this->addType('templateId', 'int');
$this->addType('hide_download', 'bool');
$this->addType('hideDownload', 'bool');
$this->addType('direct', 'bool');
$this->addType('tokenType', 'int');
}

View file

@ -25,14 +25,14 @@ namespace OCA\Richdocuments\Db;
use OCA\Richdocuments\AppConfig;
use OCA\Richdocuments\Exceptions\ExpiredTokenException;
use OCA\Richdocuments\Exceptions\UnknownTokenException;
use OCP\AppFramework\Db\Mapper;
use OCP\AppFramework\Db\QBMapper;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\ILogger;
use OCP\Security\ISecureRandom;
class WopiMapper extends Mapper {
class WopiMapper extends QBMapper {
/** @var ISecureRandom */
private $random;
@ -110,10 +110,7 @@ class WopiMapper extends Mapper {
'tokenType' => Wopi::TOKEN_TYPE_INITIATOR
]);
/** @var Wopi $wopi */
$wopi = $this->insert($wopi);
return $wopi;
return $this->insert($wopi);
}
/**
@ -175,7 +172,7 @@ class WopiMapper extends Mapper {
private function calculateNewTokenExpiry(): int {
return $this->timeFactory->getTime() + (int) $this->appConfig->getAppValue('token_ttl');
}
/**
* @param int|null $limit
* @param int|null $offset