|
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
|
|
|
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
|
|
|
* @return Direct |
|
57
|
|
|
*/ |
|
58
|
|
|
public function newDirect($uid, $fileid) { |
|
59
|
|
|
$direct = new Direct(); |
|
60
|
|
|
$direct->setUid($uid); |
|
61
|
|
|
$direct->setFileid($fileid); |
|
62
|
|
|
$direct->setToken($this->random->generate(64, ISecureRandom::CHAR_DIGITS . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER)); |
|
63
|
|
|
$direct->setTimestamp($this->timeFactory->getTime()); |
|
64
|
|
|
|
|
65
|
|
|
$direct = $this->insert($direct); |
|
66
|
|
|
return $direct; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $token |
|
71
|
|
|
* @return Direct |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getByToken($token) { |
|
74
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
75
|
|
|
$qb->select('*') |
|
76
|
|
|
->from('richdocuments_direct') |
|
77
|
|
|
->where($qb->expr()->eq('token', $qb->createNamedParameter($token))); |
|
78
|
|
|
|
|
79
|
|
|
$cursor = $qb->execute(); |
|
80
|
|
|
$row = $cursor->fetch(); |
|
81
|
|
|
$cursor->closeCursor(); |
|
82
|
|
|
|
|
83
|
|
|
//There can only be one as the token is unique |
|
84
|
|
|
if ($row === false) { |
|
85
|
|
|
throw new DoesNotExistException('Could not find token.'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$direct = Direct::fromRow($row); |
|
89
|
|
|
|
|
90
|
|
|
if (($direct->getTimestamp() + self::tokenLifeTime) < $this->timeFactory->getTime()) { |
|
91
|
|
|
$this->delete($direct); |
|
92
|
|
|
throw new DoesNotExistException('Could not find token.'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $direct; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|