| @@ 32-96 (lines=65) @@ | ||
| 29 | use OCP\IDBConnection; |
|
| 30 | use OCP\Security\ISecureRandom; |
|
| 31 | ||
| 32 | class AssetMapper extends Mapper { |
|
| 33 | /** @var int Limetime of a token is 10 minutes */ |
|
| 34 | const tokenLifeTime = 600; |
|
| 35 | ||
| 36 | /** @var ISecureRandom */ |
|
| 37 | private $random; |
|
| 38 | ||
| 39 | /** @var ITimeFactory */ |
|
| 40 | private $time; |
|
| 41 | ||
| 42 | public function __construct(IDBConnection $db, ISecureRandom $random, ITimeFactory $timeFactory) { |
|
| 43 | parent::__construct($db, 'richdocuments_assets', Asset::class); |
|
| 44 | ||
| 45 | $this->random = $random; |
|
| 46 | $this->time = $timeFactory; |
|
| 47 | } |
|
| 48 | ||
| 49 | /** |
|
| 50 | * @param $uid |
|
| 51 | * @param $fileid |
|
| 52 | * @return Asset |
|
| 53 | */ |
|
| 54 | public function newAsset($uid, $fileid) { |
|
| 55 | $asset = new Asset(); |
|
| 56 | $asset->setUid($uid); |
|
| 57 | $asset->setFileid($fileid); |
|
| 58 | $asset->setTimestamp($this->time->getTime()); |
|
| 59 | $asset->setToken($this->random->generate(64, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS)); |
|
| 60 | ||
| 61 | $asset = $this->insert($asset); |
|
| 62 | return $asset; |
|
| 63 | } |
|
| 64 | ||
| 65 | ||
| 66 | /** |
|
| 67 | * @param $token |
|
| 68 | * @return Asset |
|
| 69 | * @throws DoesNotExistException |
|
| 70 | */ |
|
| 71 | public function getAssetByToken($token) { |
|
| 72 | $qb = $this->db->getQueryBuilder(); |
|
| 73 | $qb->select('*') |
|
| 74 | ->from('richdocuments_assets') |
|
| 75 | ->where($qb->expr()->eq('token', $qb->createNamedParameter($token))); |
|
| 76 | ||
| 77 | $cursor = $qb->execute(); |
|
| 78 | $data = $cursor->fetch(); |
|
| 79 | $cursor->closeCursor(); |
|
| 80 | ||
| 81 | //There can only be one row since it is a unqiue field |
|
| 82 | if ($data === false) { |
|
| 83 | throw new DoesNotExistException('No asset for token found'); |
|
| 84 | } |
|
| 85 | ||
| 86 | $asset = Asset::fromRow($data); |
|
| 87 | ||
| 88 | // Check the token lifetime |
|
| 89 | if ($asset->getTimestamp() + self::tokenLifeTime < $this->time->getTime()) { |
|
| 90 | $this->delete($asset); |
|
| 91 | throw new DoesNotExistException('No asset for token found'); |
|
| 92 | } |
|
| 93 | ||
| 94 | return $asset; |
|
| 95 | } |
|
| 96 | } |
|
| 97 | ||
| @@ 33-99 (lines=67) @@ | ||
| 30 | use OCP\Security\ISecureRandom; |
|
| 31 | use PhpParser\Node\Scalar\MagicConst\Dir; |
|
| 32 | ||
| 33 | class DirectMapper extends Mapper { |
|
| 34 | ||
| 35 | /** @var int Limetime of a token is 10 minutes */ |
|
| 36 | const tokenLifeTime = 600; |
|
| 37 | ||
| 38 | /** @var ISecureRandom */ |
|
| 39 | protected $random; |
|
| 40 | ||
| 41 | /**@var ITimeFactory */ |
|
| 42 | protected $timeFactory; |
|
| 43 | ||
| 44 | public function __construct(IDBConnection $db, |
|
| 45 | ISecureRandom $random, |
|
| 46 | ITimeFactory $timeFactory) { |
|
| 47 | parent::__construct($db, 'richdocuments_direct', Direct::class); |
|
| 48 | ||
| 49 | $this->random = $random; |
|
| 50 | $this->timeFactory = $timeFactory; |
|
| 51 | } |
|
| 52 | ||
| 53 | /** |
|
| 54 | * @param string $uid |
|
| 55 | * @param int $fileid |
|
| 56 | * @param int $destination |
|
| 57 | * @return Direct |
|
| 58 | */ |
|
| 59 | public function newDirect($uid, $fileid, $destination = null) { |
|
| 60 | $direct = new Direct(); |
|
| 61 | $direct->setUid($uid); |
|
| 62 | $direct->setFileid($fileid); |
|
| 63 | $direct->setToken($this->random->generate(64, ISecureRandom::CHAR_DIGITS . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER)); |
|
| 64 | $direct->setTimestamp($this->timeFactory->getTime()); |
|
| 65 | $direct->setTemplateDestination($destination); |
|
| 66 | ||
| 67 | $direct = $this->insert($direct); |
|
| 68 | return $direct; |
|
| 69 | } |
|
| 70 | ||
| 71 | /** |
|
| 72 | * @param string $token |
|
| 73 | * @return Direct |
|
| 74 | */ |
|
| 75 | public function getByToken($token) { |
|
| 76 | $qb = $this->db->getQueryBuilder(); |
|
| 77 | $qb->select('*') |
|
| 78 | ->from('richdocuments_direct') |
|
| 79 | ->where($qb->expr()->eq('token', $qb->createNamedParameter($token))); |
|
| 80 | ||
| 81 | $cursor = $qb->execute(); |
|
| 82 | $row = $cursor->fetch(); |
|
| 83 | $cursor->closeCursor(); |
|
| 84 | ||
| 85 | //There can only be one as the token is unique |
|
| 86 | if ($row === false) { |
|
| 87 | throw new DoesNotExistException('Could not find token.'); |
|
| 88 | } |
|
| 89 | ||
| 90 | $direct = Direct::fromRow($row); |
|
| 91 | ||
| 92 | if (($direct->getTimestamp() + self::tokenLifeTime) < $this->timeFactory->getTime()) { |
|
| 93 | $this->delete($direct); |
|
| 94 | throw new DoesNotExistException('Could not find token.'); |
|
| 95 | } |
|
| 96 | ||
| 97 | return $direct; |
|
| 98 | } |
|
| 99 | } |
|
| 100 | ||