|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Circles - Bring cloud-users closer together. |
|
4
|
|
|
* |
|
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
6
|
|
|
* later. See the COPYING file. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Maxence Lange <[email protected]> |
|
9
|
|
|
* @copyright 2017 |
|
10
|
|
|
* @license GNU AGPL version 3 or any later version |
|
11
|
|
|
* |
|
12
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
13
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
14
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
15
|
|
|
* License, or (at your option) any later version. |
|
16
|
|
|
* |
|
17
|
|
|
* This program is distributed in the hope that it will be useful, |
|
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20
|
|
|
* GNU Affero General Public License for more details. |
|
21
|
|
|
* |
|
22
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
namespace OCA\Circles\Db; |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
use daita\MySmallPhpTools\Traits\TArrayTools; |
|
32
|
|
|
use daita\MySmallPhpTools\Traits\TStringTools; |
|
33
|
|
|
use OCA\Circles\Exceptions\TokenDoesNotExistException; |
|
34
|
|
|
use OCA\Circles\Model\Member; |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Class SharesRequest |
|
39
|
|
|
* |
|
40
|
|
|
* @package OCA\Circles\Db |
|
41
|
|
|
*/ |
|
42
|
|
|
class SharesRequest extends SharesRequestBuilder { |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
use TArrayTools; |
|
46
|
|
|
use TStringTools; |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* remove shares from a member to a circle |
|
51
|
|
|
* |
|
52
|
|
|
* @param Member $member |
|
53
|
|
|
*/ |
|
54
|
|
View Code Duplication |
public function removeSharesFromMember(Member $member) { |
|
|
|
|
|
|
55
|
|
|
$qb = $this->getSharesDeleteSql(); |
|
56
|
|
|
$expr = $qb->expr(); |
|
57
|
|
|
|
|
58
|
|
|
$andX = $expr->andX(); |
|
59
|
|
|
$andX->add($expr->eq('share_with', $qb->createNamedParameter($member->getCircleId()))); |
|
60
|
|
|
$andX->add($expr->eq('uid_initiator', $qb->createNamedParameter($member->getUserId()))); |
|
61
|
|
|
$qb->andWhere($andX); |
|
62
|
|
|
|
|
63
|
|
|
$qb->execute(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* remove shares from a member to a circle |
|
69
|
|
|
* |
|
70
|
|
|
* @param int $shareId |
|
71
|
|
|
* |
|
72
|
|
|
* @return string |
|
73
|
|
|
* @throws TokenDoesNotExistException |
|
74
|
|
|
*/ |
|
75
|
|
View Code Duplication |
public function getTokenByShareId(int $shareId) { |
|
|
|
|
|
|
76
|
|
|
$qb = $this->getSharesSelectSql(); |
|
77
|
|
|
$this->limitToId($qb, $shareId); |
|
78
|
|
|
|
|
79
|
|
|
$cursor = $qb->execute(); |
|
80
|
|
|
$data = $cursor->fetch(); |
|
81
|
|
|
$cursor->closeCursor(); |
|
82
|
|
|
if ($data === false) { |
|
83
|
|
|
throw new TokenDoesNotExistException('Unknown share token'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $this->get('token', $data, 'notfound'); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string $circleUniqueId |
|
92
|
|
|
*/ |
|
93
|
|
|
public function shuffleTokensFromCircle(string $circleUniqueId) { |
|
94
|
|
|
$shares = $this->getSharesForCircle($circleUniqueId); |
|
95
|
|
|
foreach ($shares as $share) { |
|
96
|
|
|
$this->shuffleTokenByShareId($this->getInt('id', $share, 0)); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param int $shareId |
|
103
|
|
|
*/ |
|
104
|
|
|
public function shuffleTokenByShareId(int $shareId) { |
|
105
|
|
|
$qb = $this->getSharesUpdateSql(); |
|
106
|
|
|
$qb->set('token', $qb->createNamedParameter($this->uuid(15))); |
|
107
|
|
|
|
|
108
|
|
|
$this->limitToId($qb, $shareId); |
|
109
|
|
|
|
|
110
|
|
|
$qb->execute(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param string $circleId |
|
116
|
|
|
* |
|
117
|
|
|
* @return array |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getSharesForCircle(string $circleId) { |
|
120
|
|
|
$qb = $this->getSharesSelectSql(); |
|
121
|
|
|
|
|
122
|
|
|
$this->limitToShareWith($qb, $circleId); |
|
123
|
|
|
$this->limitToShareType($qb, self::SHARE_TYPE); |
|
124
|
|
|
|
|
125
|
|
|
$shares = []; |
|
126
|
|
|
$cursor = $qb->execute(); |
|
127
|
|
|
while ($data = $cursor->fetch()) { |
|
128
|
|
|
$shares[] = $data; |
|
129
|
|
|
} |
|
130
|
|
|
$cursor->closeCursor(); |
|
131
|
|
|
|
|
132
|
|
|
return $shares; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
|
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.