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\Exceptions\ShareAlreadyExistsException; |
29
|
|
|
use OCA\Polls\Model\Email; |
30
|
|
|
use OCA\Polls\Model\Group; |
31
|
|
|
use OCA\Polls\Model\User; |
32
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
33
|
|
|
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext; |
|
|
|
|
34
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
35
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
36
|
|
|
use Symfony\Component\Console\Input\InputOption; |
37
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
38
|
|
|
|
39
|
|
|
class Add extends Base { |
40
|
|
|
use TShareCommand; |
41
|
|
|
|
42
|
|
|
protected function configure(): void { |
43
|
|
|
$this |
44
|
|
|
->setName('polls:share:add') |
45
|
|
|
->setDescription('Invites users to a poll') |
46
|
|
|
->addArgument( |
47
|
|
|
'id', |
48
|
|
|
InputArgument::REQUIRED, |
49
|
|
|
'ID of the poll to invite users to' |
50
|
|
|
)->addOption( |
51
|
|
|
'user', |
52
|
|
|
null, |
53
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
54
|
|
|
'Invites the given users to the poll' |
55
|
|
|
)->addOption( |
56
|
|
|
'group', |
57
|
|
|
null, |
58
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
59
|
|
|
'Invites all members of the given groups to the poll' |
60
|
|
|
)->addOption( |
61
|
|
|
'email', |
62
|
|
|
null, |
63
|
|
|
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
64
|
|
|
'Sends invitation e-mails to the given addresses to participate in the poll' |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int { |
69
|
|
|
$pollId = $input->getArgument('id'); |
70
|
|
|
$users = $input->getOption('user'); |
71
|
|
|
$groups = $input->getOption('group'); |
72
|
|
|
$emails = $input->getOption('email'); |
73
|
|
|
|
74
|
|
|
try { |
75
|
|
|
$poll = $this->pollMapper->find($pollId); |
|
|
|
|
76
|
|
|
} catch (DoesNotExistException $e) { |
77
|
|
|
$output->writeln('<error>Poll not found.</error>'); |
78
|
|
|
return 1; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->inviteUsers($poll, $users); |
|
|
|
|
82
|
|
|
$this->inviteGroups($poll, $groups); |
|
|
|
|
83
|
|
|
$this->inviteEmails($poll, $emails); |
|
|
|
|
84
|
|
|
|
85
|
|
|
$output->writeln('<info>Users successfully invited to poll.</info>'); |
86
|
|
|
return 0; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param Poll $poll |
91
|
|
|
* @param string[] $userIds |
92
|
|
|
*/ |
93
|
|
|
private function inviteUsers(Poll $poll, array $userIds): void { |
94
|
|
|
foreach ($userIds as $userId) { |
95
|
|
|
try { |
96
|
|
|
$share = $this->shareService->add($poll->getId(), User::TYPE, $userId); |
97
|
|
|
$this->shareService->sendInvitation($share->getToken()); |
98
|
|
|
} catch (ShareAlreadyExistsException $e) { |
99
|
|
|
// silently ignore already existing shares |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param Poll $poll |
106
|
|
|
* @param string[] $groupIds |
107
|
|
|
*/ |
108
|
|
|
private function inviteGroups(Poll $poll, array $groupIds): void { |
109
|
|
|
foreach ($groupIds as $groupId) { |
110
|
|
|
try { |
111
|
|
|
$share = $this->shareService->add($poll->getId(), Group::TYPE, $groupId); |
112
|
|
|
$this->shareService->sendInvitation($share->getToken()); |
113
|
|
|
} catch (ShareAlreadyExistsException $e) { |
114
|
|
|
// silently ignore already existing shares |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param Poll $poll |
121
|
|
|
* @param string[] $emails |
122
|
|
|
*/ |
123
|
|
|
private function inviteEmails(Poll $poll, array $emails): void { |
124
|
|
|
foreach ($emails as $email) { |
125
|
|
|
try { |
126
|
|
|
$share = $this->shareService->add($poll->getId(), Email::TYPE, $email); |
127
|
|
|
$this->shareService->sendInvitation($share->getToken()); |
128
|
|
|
} catch (ShareAlreadyExistsException $e) { |
129
|
|
|
// silently ignore already existing shares |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function completeOptionValues($optionName, CompletionContext $context) { |
135
|
|
|
switch ($optionName) { |
136
|
|
|
case 'user': |
137
|
|
|
return $this->completeUserValues($context); |
138
|
|
|
|
139
|
|
|
case 'group': |
140
|
|
|
return $this->completeGroupValues($context); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return parent::completeOptionValues($optionName, $context); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
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