Skip to content

Commit

Permalink
NEW Add AbstractMemberCommand and implement into existing member comm…
Browse files Browse the repository at this point in the history
…ands
  • Loading branch information
robbieaverill committed Apr 30, 2018
1 parent 50c7f92 commit e170561
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 46 deletions.
43 changes: 43 additions & 0 deletions src/Command/Member/AbstractMemberCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace SilverLeague\Console\Command\Member;

use SilverLeague\Console\Command\SilverStripeCommand;
use SilverStripe\Security\Member;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Provides a base for member related commands, lookups etc
*
* @package silverstripe-console
* @author Robbie Averill <robbie@averill.co.nz>
*/
abstract class AbstractMemberCommand extends SilverStripeCommand
{
/**
* Get a member by the provided email address, output an error message if not found
*
* @param InputInterface $input
* @param OutputInterface $output
* @return Member|false
*/
protected function getMember(InputInterface $input, OutputInterface $output)
{
$email = $this->getOrAskForArgument($input, $output, 'email', 'Enter email address: ');
if (empty($email)) {
$output->writeln('<error>Please enter an email address.</error>');
return false;
}

/** @var Member $member */
$member = Member::get()->filter('email', $email)->first();
if (!$member) {
$output->writeln('<error>Member with email "' . $email . '" was not found.');
return false;
}

return $member;
}
}

12 changes: 4 additions & 8 deletions src/Command/Member/ChangeGroupsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace SilverLeague\Console\Command\Member;

use SilverLeague\Console\Command\SilverStripeCommand;
use SilverStripe\Security\Group;
use SilverStripe\Security\Member;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -16,7 +14,7 @@
* @package silverstripe-console
* @author Robbie Averill <robbie@averill.co.nz>
*/
class ChangeGroupsCommand extends SilverStripeCommand
class ChangeGroupsCommand extends AbstractMemberCommand
{
/**
* {@inheritDoc}
Expand All @@ -34,16 +32,14 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$email = $this->getOrAskForArgument($input, $output, 'email', 'Enter email address: ');
$member = Member::get()->filter('email', $email)->first();
$member = $this->getMember($input, $output);
if (!$member) {
$output->writeln('<error>Member with email "' . $email . '" was not found.');
return;
}

if ($member->Groups()->count()) {
$output->writeln(
'Member <info>' . $email . '</info> is already in the following groups (will be overwritten):'
'Member <info>' . $member->Email . '</info> is already in the following groups (will be overwritten):'
);
$output->writeln(' ' . implode(', ', $member->Groups()->column('Code')));
$output->writeln('');
Expand All @@ -55,7 +51,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$newGroups = $this->getHelper('question')->ask($input, $output, $question);

$output->writeln('Adding <info>' . $email . '</info> to groups: ' . implode(', ', $newGroups));
$output->writeln('Adding <info>' . $member->Email . '</info> to groups: ' . implode(', ', $newGroups));
// $member->Groups()->removeAll();
foreach ($newGroups as $group) {
$member->addToGroupByCode($group);
Expand Down
8 changes: 2 additions & 6 deletions src/Command/Member/ChangePasswordCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace SilverLeague\Console\Command\Member;

use SilverLeague\Console\Command\SilverStripeCommand;
use SilverStripe\ORM\ValidationResult;
use SilverStripe\Security\Member;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -15,7 +13,7 @@
* @package silverstripe-console
* @author Robbie Averill <robbie@averill.co.nz>
*/
class ChangePasswordCommand extends SilverStripeCommand
class ChangePasswordCommand extends AbstractMemberCommand
{
/**
* {@inheritDoc}
Expand All @@ -41,10 +39,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
return;
}

/** @var Member $member */
$member = Member::get()->filter('email', $email)->first();
$member = $this->getMember($input, $output);
if (!$member) {
$output->writeln('<error>Member with email "' . $email . '" was not found.');
return;
}

Expand Down
7 changes: 7 additions & 0 deletions src/Command/Member/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
return;
}

// Check for existing member
$member = Member::get()->filter(['Email' => $data['Email']])->first();
if ($member) {
$output->writeln('<error>Member already exists with email address: ' . $data['Email']);
return;
}

$member = Member::create();
foreach ($data as $key => $value) {
$member->setField($key, $value);
Expand Down
15 changes: 3 additions & 12 deletions src/Command/Member/LockCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace SilverLeague\Console\Command\Member;

use SilverLeague\Console\Command\SilverStripeCommand;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\Security\Member;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -15,7 +14,7 @@
* @package silverstripe-console
* @author Robbie Averill <robbie@averill.co.nz>
*/
class LockCommand extends SilverStripeCommand
class LockCommand extends AbstractMemberCommand
{
protected function configure()
{
Expand All @@ -27,16 +26,8 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$email = $this->getOrAskForArgument($input, $output, 'email', 'Enter email address: ');
if (empty($email)) {
$output->writeln('<error>Please enter an email address.</error>');
return;
}

/** @var Member $member */
$member = Member::get()->filter('email', $email)->first();
$member = $this->getMember($input, $output);
if (!$member) {
$output->writeln('<error>Member with email "' . $email . '" was not found.');
return;
}

Expand All @@ -45,7 +36,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$member->FailedLoginCount = 0;
$member->write();

$output->writeln('Member <info>' . $email . '</info> locked for <info>' . $lockoutMins . ' mins.</info>');
$output->writeln('Member <info>' . $member->Email . '</info> locked for <info>' . $lockoutMins . ' mins.</info>');
}
}

17 changes: 3 additions & 14 deletions src/Command/Member/UnlockCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace SilverLeague\Console\Command\Member;

use SilverLeague\Console\Command\SilverStripeCommand;
use SilverStripe\ORM\FieldType\DBDatetime;
use SilverStripe\Security\Member;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -15,7 +12,7 @@
* @package silverstripe-console
* @author Robbie Averill <robbie@averill.co.nz>
*/
class UnlockCommand extends SilverStripeCommand
class UnlockCommand extends AbstractMemberCommand
{
protected function configure()
{
Expand All @@ -27,24 +24,16 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$email = $this->getOrAskForArgument($input, $output, 'email', 'Enter email address: ');
if (empty($email)) {
$output->writeln('<error>Please enter an email address.</error>');
return;
}

/** @var Member $member */
$member = Member::get()->filter('email', $email)->first();
$member = $this->getMember($input, $output);
if (!$member) {
$output->writeln('<error>Member with email "' . $email . '" was not found.');
return;
}

$member->LockedOutUntil = null;
$member->FailedLoginCount = 0;
$member->write();

$output->writeln('Member <info>' . $email . '</info> unlocked.');
$output->writeln('Member <info>' . $member->Email . '</info> unlocked.');
}
}

17 changes: 11 additions & 6 deletions tests/Command/Member/CreateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,19 @@ public function testExecute()

$this->command->getApplication()->getHelperSet()->set($questionHelper, 'question');

$tester = $this->executeTest(
[
'email' => 'unittest@example.com',
'password' => 'OpenSe$am3!'
]
);
$tester = $this->executeTest([
'email' => 'unittest@example.com',
'password' => 'OpenSe$am3!',
]);
$output = $tester->getDisplay();
$this->assertContains('Member created', $output);

$tester = $this->executeTest([
'email' => 'unittest@example.com',
'password' => 'OpenSe$am3!',
]);
$output = $tester->getDisplay();
$this->assertContains('Member already exists', $output);
}

/**
Expand Down

0 comments on commit e170561

Please sign in to comment.