1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2018, Roeland Jago Douma <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @author Roeland Jago Douma <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @license GNU AGPL version 3 or any later version |
8
|
|
|
* |
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
12
|
|
|
* License, or (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU Affero General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace OCA\Richdocuments\Db; |
25
|
|
|
|
26
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
27
|
|
|
use OCP\AppFramework\Db\Mapper; |
28
|
|
|
use OCP\AppFramework\Utility\ITimeFactory; |
29
|
|
|
use OCP\IDBConnection; |
30
|
|
|
use OCP\Security\ISecureRandom; |
31
|
|
|
|
32
|
|
View Code Duplication |
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
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.