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
|
|
|
use PhpParser\Node\Scalar\MagicConst\Dir; |
32
|
|
|
|
33
|
|
View Code Duplication |
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
|
|
|
|
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.