1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverLeague\Console\Command\Member; |
4
|
|
|
|
5
|
|
|
use SilverLeague\Console\Command\SilverStripeCommand; |
6
|
|
|
use SilverStripe\Security\Group; |
7
|
|
|
use SilverStripe\Security\Member; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Change a member's assigned groups |
15
|
|
|
* |
16
|
|
|
* @package silverstripe-console |
17
|
|
|
* @author Robbie Averill <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class ChangeGroupsCommand extends SilverStripeCommand |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* {@inheritDoc} |
23
|
|
|
*/ |
24
|
2 |
|
protected function configure() |
25
|
|
|
{ |
26
|
|
|
$this |
27
|
2 |
|
->setName('member:change-groups') |
28
|
2 |
|
->setDescription("Change a member's groups") |
29
|
2 |
|
->addArgument('email', InputArgument::OPTIONAL, 'Email address'); |
30
|
2 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritDoc} |
34
|
|
|
*/ |
35
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
36
|
|
|
{ |
37
|
2 |
|
$email = $this->getOrAskForArgument($input, $output, 'email', 'Enter email address: '); |
38
|
2 |
|
$member = Member::get()->filter('email', $email)->first(); |
39
|
2 |
|
if (!$member) { |
40
|
1 |
|
$output->writeln('<error>Member with email "' . $email . '" was not found.'); |
41
|
1 |
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
if ($member->Groups()->count()) { |
45
|
|
|
$output->writeln( |
46
|
|
|
'Member <info>' . $email . '</info> is already in the following groups (will be overwritten):' |
47
|
|
|
); |
48
|
|
|
$output->writeln(' ' . implode(', ', $member->Groups()->column('Code'))); |
49
|
|
|
$output->writeln(''); |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
$allGroups = Group::get()->column('Code'); |
53
|
1 |
|
$question = new ChoiceQuestion('Select the groups to add this Member to', $allGroups); |
54
|
1 |
|
$question->setMultiselect(true); |
55
|
|
|
|
56
|
1 |
|
$newGroups = $this->getHelper('question')->ask($input, $output, $question); |
|
|
|
|
57
|
|
|
|
58
|
1 |
|
$output->writeln('Adding <info>' . $email . '</info> to groups: ' . implode(', ', $newGroups)); |
59
|
|
|
// $member->Groups()->removeAll(); |
|
|
|
|
60
|
1 |
|
foreach ($newGroups as $group) { |
61
|
1 |
|
$member->addToGroupByCode($group); |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
$output->writeln('<info>Groups updated.</info>'); |
65
|
1 |
|
} |
66
|
|
|
} |
67
|
|
|
|
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: