-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API Update API to reflect changes to CLI interaction
- Loading branch information
1 parent
9750358
commit 93bab52
Showing
7 changed files
with
97 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,24 +3,23 @@ | |
namespace SilverStripe\LDAP\Tasks; | ||
|
||
use Exception; | ||
use SilverStripe\Control\HTTPRequest; | ||
use SilverStripe\PolyExecution\PolyOutput; | ||
use SilverStripe\LDAP\Services\LDAPService; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
|
||
/** | ||
* Class LDAPMemberSyncOneTask | ||
* @package SilverStripe\LDAP\Tasks | ||
* | ||
* Debug build task that can be used to sync a single member by providing their email address registered in LDAP. | ||
* | ||
* Usage: /dev/tasks/LDAPMemberSyncOneTask?mail[email protected] | ||
* Usage: sake tasks:LDAPMemberSyncOneTask --email[email protected] | ||
*/ | ||
class LDAPMemberSyncOneTask extends LDAPMemberSyncTask | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
* @var string | ||
*/ | ||
private static $segment = 'LDAPMemberSyncOneTask'; | ||
protected static string $commandName = 'LDAPMemberSyncOneTask'; | ||
|
||
/** | ||
* @var array | ||
|
@@ -34,64 +33,58 @@ class LDAPMemberSyncOneTask extends LDAPMemberSyncTask | |
*/ | ||
protected $ldapService; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getTitle() | ||
public function getTitle(): string | ||
{ | ||
return _t(__CLASS__ . '.SYNCONETITLE', 'Sync single user from LDAP'); | ||
} | ||
|
||
/** | ||
* Syncs a single user based on the email address passed in the URL | ||
* | ||
* @param HTTPRequest $request | ||
*/ | ||
public function run($request) | ||
protected function execute(InputInterface $input, PolyOutput $output): int | ||
{ | ||
$email = $request->getVar('email'); | ||
$email = $input->getOption('email'); | ||
|
||
if (!$email) { | ||
echo 'You must supply an email parameter to this method.', PHP_EOL; | ||
exit; | ||
$output->writeln('<error>You must supply an email address.</>'); | ||
return Command::INVALID; | ||
} | ||
|
||
$user = $this->ldapService->getUserByEmail($email); | ||
|
||
if (!$user) { | ||
echo sprintf('No user found in LDAP for email %s', $email), PHP_EOL; | ||
exit; | ||
$output->writeln(sprintf('<error>No user found in LDAP for email %s</>', $email)); | ||
return Command::FAILURE; | ||
} | ||
|
||
$member = $this->findOrCreateMember($user); | ||
|
||
// If member exists already, we're updating - otherwise we're creating | ||
if ($member->exists()) { | ||
$this->log(sprintf( | ||
$output->writeln(sprintf( | ||
'Updating existing Member %s: "%s" (ID: %s, SAM Account Name: %s)', | ||
$user['objectguid'], | ||
$member->getName(), | ||
$member->ID, | ||
$user['samaccountname'] | ||
)); | ||
} else { | ||
$this->log(sprintf( | ||
$output->writeln(sprintf( | ||
'Creating new Member %s: "%s" (SAM Account Name: %s)', | ||
$user['objectguid'], | ||
$user['cn'], | ||
$user['samaccountname'] | ||
)); | ||
} | ||
|
||
$this->log('User data returned from LDAP follows:'); | ||
$this->log(var_export($user)); | ||
$output->writeln('User data returned from LDAP follows:'); | ||
$output->writeln(var_export($user, true)); | ||
|
||
try { | ||
$this->ldapService->updateMemberFromLDAP($member, $user); | ||
$this->log('Done!'); | ||
} catch (Exception $e) { | ||
$this->log($e->getMessage()); | ||
$output->writeln('<error>' . $e->getMessage() . '</>'); | ||
return Command::FAILURE; | ||
} | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
/** | ||
|
@@ -103,4 +96,11 @@ public function setLDAPService(LDAPService $service) | |
$this->ldapService = $service; | ||
return $this; | ||
} | ||
|
||
public function getOptions(): array | ||
{ | ||
return [ | ||
new InputOption('email', null, InputOption::VALUE_REQUIRED, 'Email address of the member to sync (required)'), | ||
]; | ||
} | ||
} |
Oops, something went wrong.