1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverLeague\Console\Command\Member; |
4
|
|
|
|
5
|
|
|
use SilverLeague\Console\Command\SilverStripeCommand; |
6
|
|
|
use SilverStripe\Security\Member; |
7
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
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\Question; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Create a new member, and optionally add them to groups and roles. |
15
|
|
|
* |
16
|
|
|
* @package silverstripe-console |
17
|
|
|
* @author Robbie Averill <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class CreateCommand extends SilverStripeCommand |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* {@inheritDoc} |
23
|
|
|
*/ |
24
|
3 |
|
protected function configure() |
25
|
|
|
{ |
26
|
|
|
$this |
27
|
3 |
|
->setName('member:create') |
28
|
3 |
|
->setDescription('Create a new member, and optionally add them to groups') |
29
|
3 |
|
->addArgument('email', InputArgument::OPTIONAL, 'Email address') |
30
|
3 |
|
->addArgument('username', InputArgument::OPTIONAL, 'Username') |
31
|
3 |
|
->addArgument('password', InputArgument::OPTIONAL, 'Password') |
32
|
3 |
|
->addArgument('firstname', InputArgument::OPTIONAL, 'First name') |
33
|
3 |
|
->addArgument('surname', InputArgument::OPTIONAL, 'Surname'); |
34
|
3 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritDoc} |
38
|
|
|
*/ |
39
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
40
|
|
|
{ |
41
|
|
|
$data = [ |
42
|
1 |
|
'Email' => $this->getOrAskForArgument($input, $output, 'email', 'Email address: '), |
43
|
1 |
|
'Password' => $this->getOrAskForArgument($input, $output, 'password', 'Password: '), |
44
|
1 |
|
'FirstName' => $this->getOrAskForArgument($input, $output, 'firstname', 'First name: '), |
45
|
1 |
|
'Surname' => $this->getOrAskForArgument($input, $output, 'surname', 'Surname: ') |
46
|
|
|
]; |
47
|
1 |
|
if (empty($data['Email']) || empty($data['Password'])) { |
48
|
|
|
$output->writeln('<error>Please enter an email address and password.</error>'); |
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
// Check for existing member |
53
|
1 |
|
$member = Member::get()->filter(['Email' => $data['Email']])->first(); |
54
|
1 |
|
if ($member) { |
55
|
|
|
$output->writeln('<error>Member already exists with email address: ' . $data['Email']); |
56
|
1 |
|
return; |
57
|
|
|
} |
58
|
1 |
|
|
59
|
|
|
$member = Member::create(); |
60
|
1 |
|
foreach ($data as $key => $value) { |
61
|
1 |
|
$member->setField($key, $value); |
62
|
|
|
} |
63
|
|
|
$member->write(); |
64
|
|
|
|
65
|
|
|
$output->writeln('<info>Member created.</info>'); |
66
|
|
|
|
67
|
|
|
$setGroups = new Question('Do you want to assign groups now? ', 'yes'); |
68
|
|
|
if ($this->getHelper('question')->ask($input, $output, $setGroups) === 'yes') { |
|
|
|
|
69
|
|
|
$command = $this->getApplication()->find('member:change-groups'); |
70
|
|
|
$command->run( |
71
|
1 |
|
new ArrayInput([ |
72
|
|
|
'command' => 'member:change-groups', |
73
|
|
|
'email' => $data['Email'] |
74
|
|
|
]), |
75
|
|
|
$output |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
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: