1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2021 Daniel Rudolf <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @author Daniel Rudolf <[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\Polls\Command\Share; |
25
|
|
|
|
26
|
|
|
use OC\Core\Command\Base; |
|
|
|
|
27
|
|
|
use OCA\Polls\Db\Poll; |
28
|
|
|
use OCA\Polls\Db\Share; |
29
|
|
|
use OCA\Polls\Model\Contact; |
30
|
|
|
use OCA\Polls\Model\Email; |
31
|
|
|
use OCA\Polls\Model\GenericUser; |
32
|
|
|
use OCA\Polls\Model\Group; |
33
|
|
|
use OCA\Polls\Model\User; |
34
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
35
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
36
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
37
|
|
|
use Symfony\Component\Console\Input\InputOption; |
38
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
39
|
|
|
|
40
|
|
|
class Remove extends Base { |
41
|
|
|
use TShareCommand; |
42
|
|
|
|
43
|
|
|
protected function configure(): void { |
44
|
|
|
$this |
45
|
|
|
->setName('polls:share:remove') |
46
|
|
|
->setDescription('Remove user invitations from a poll') |
47
|
|
|
->addArgument( |
48
|
|
|
'id', |
49
|
|
|
InputArgument::REQUIRED, |
50
|
|
|
'ID of the poll to remove invitations from' |
51
|
|
|
)->addOption( |
52
|
|
|
'user', |
53
|
|
|
null, |
54
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
55
|
|
|
'Removes invitation of the given users from the poll' |
56
|
|
|
)->addOption( |
57
|
|
|
'group', |
58
|
|
|
null, |
59
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
60
|
|
|
'Removes invitations for all members of the given groups from the poll' |
61
|
|
|
)->addOption( |
62
|
|
|
'email', |
63
|
|
|
null, |
64
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
65
|
|
|
'Removes invitations for all users with the given e-mail addresses from the poll' |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int { |
70
|
|
|
$pollId = $input->getArgument('id'); |
71
|
|
|
$users = $input->getOption('user'); |
72
|
|
|
$groups = $input->getOption('group'); |
73
|
|
|
$emails = $input->getOption('email'); |
74
|
|
|
|
75
|
|
|
try { |
76
|
|
|
$poll = $this->pollMapper->find($pollId); |
|
|
|
|
77
|
|
|
} catch (DoesNotExistException $e) { |
78
|
|
|
$output->writeln('<error>Poll not found.</error>'); |
79
|
|
|
return 1; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->removeUsers($poll, $users); |
|
|
|
|
83
|
|
|
$this->removeGroups($poll, $groups); |
|
|
|
|
84
|
|
|
$this->removeEmails($poll, $emails); |
|
|
|
|
85
|
|
|
|
86
|
|
|
$output->writeln('<info>Poll invitations successfully revoked.</info>'); |
87
|
|
|
return 0; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param Poll $poll |
92
|
|
|
* @param string[] $userIds |
93
|
|
|
*/ |
94
|
|
|
private function removeUsers(Poll $poll, array $userIds): void { |
95
|
|
|
foreach ($this->getUserShares($poll) as $share) { |
96
|
|
|
if (in_array($share->getUserId(), $userIds, true)) { |
97
|
|
|
$this->shareService->delete($share->getToken()); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param Poll $poll |
104
|
|
|
* @param string[] $groupIds |
105
|
|
|
*/ |
106
|
|
|
private function removeGroups(Poll $poll, array $groupIds): void { |
107
|
|
|
foreach ($this->getGroupShares($poll) as $share) { |
108
|
|
|
if (in_array($share->getUserId(), $groupIds, true)) { |
109
|
|
|
$this->shareService->delete($share->getToken()); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param Poll $poll |
116
|
|
|
* @param string[] $emails |
117
|
|
|
*/ |
118
|
|
|
private function removeEmails(Poll $poll, array $emails): void { |
119
|
|
|
foreach ($this->getEmailShares($poll) as $share) { |
120
|
|
|
if (in_array($share->getEmailAddress(), $emails, true)) { |
121
|
|
|
$this->shareService->delete($share->getToken()); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param Poll $poll |
128
|
|
|
* @return Share[] |
129
|
|
|
*/ |
130
|
|
|
private function getUserShares(Poll $poll): array |
131
|
|
|
{ |
132
|
|
|
$shares = $this->shareMapper->findByPoll($poll->getId()); |
133
|
|
|
return array_values(array_filter($shares, static function (Share $share): bool { |
134
|
|
|
return ($share->getType() === User::TYPE); |
135
|
|
|
})); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param Poll $poll |
140
|
|
|
* @return Share[] |
141
|
|
|
*/ |
142
|
|
|
private function getGroupShares(Poll $poll): array |
143
|
|
|
{ |
144
|
|
|
$shares = $this->shareMapper->findByPoll($poll->getId()); |
145
|
|
|
return array_values(array_filter($shares, static function (Share $share): bool { |
146
|
|
|
return ($share->getType() === Group::TYPE); |
147
|
|
|
})); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param Poll $poll |
152
|
|
|
* @return Share[] |
153
|
|
|
*/ |
154
|
|
|
private function getEmailShares(Poll $poll): array |
155
|
|
|
{ |
156
|
|
|
$shares = $this->shareMapper->findByPoll($poll->getId()); |
157
|
|
|
return array_values(array_filter($shares, static function (Share $share): bool { |
158
|
|
|
if (($share->getType() === GenericUser::TYPE) && $share->getEmailAddress()) { |
159
|
|
|
return true; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return (($share->getType() === Email::TYPE) || ($share->getType() === Contact::TYPE)); |
163
|
|
|
})); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths