1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverLeague\Console\Command\Member; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Security\Group; |
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Change a member's assigned groups |
13
|
|
|
* |
14
|
|
|
* @package silverstripe-console |
15
|
|
|
* @author Robbie Averill <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class ChangeGroupsCommand extends AbstractMemberCommand |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* {@inheritDoc} |
21
|
|
|
*/ |
22
|
|
|
protected function configure() |
23
|
|
|
{ |
24
|
2 |
|
$this |
25
|
|
|
->setName('member:change-groups') |
26
|
|
|
->setDescription("Change a member's groups") |
27
|
2 |
|
->addArgument('email', InputArgument::OPTIONAL, 'Email address'); |
28
|
2 |
|
} |
29
|
2 |
|
|
30
|
2 |
|
/** |
31
|
|
|
* {@inheritDoc} |
32
|
|
|
*/ |
33
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
34
|
|
|
{ |
35
|
2 |
|
$member = $this->getMember($input, $output); |
36
|
|
|
if (!$member) { |
37
|
2 |
|
return; |
38
|
2 |
|
} |
39
|
2 |
|
|
40
|
1 |
|
if ($member->Groups()->count()) { |
41
|
1 |
|
$output->writeln( |
42
|
|
|
'Member <info>' . $member->Email . '</info> is already in the following groups (will be overwritten):' |
43
|
|
|
); |
44
|
1 |
|
$output->writeln(' ' . implode(', ', $member->Groups()->column('Code'))); |
45
|
|
|
$output->writeln(''); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$allGroups = Group::get()->column('Code'); |
49
|
|
|
$question = new ChoiceQuestion('Select the groups to add this Member to', $allGroups); |
50
|
|
|
$question->setMultiselect(true); |
51
|
|
|
|
52
|
1 |
|
$newGroups = $this->getHelper('question')->ask($input, $output, $question); |
|
|
|
|
53
|
1 |
|
|
54
|
1 |
|
$output->writeln('Adding <info>' . $member->Email . '</info> to groups: ' . implode(', ', $newGroups)); |
55
|
|
|
// $member->Groups()->removeAll(); |
56
|
1 |
|
foreach ($newGroups as $group) { |
57
|
|
|
$member->addToGroupByCode($group); |
58
|
1 |
|
} |
59
|
|
|
|
60
|
1 |
|
$output->writeln('<info>Groups updated.</info>'); |
61
|
1 |
|
} |
62
|
|
|
} |
63
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: