silverstripe /
silverstripe-ldap
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SilverStripe\LDAP\Tasks; |
||
| 4 | |||
| 5 | use Exception; |
||
| 6 | use SilverStripe\Control\HTTPRequest; |
||
| 7 | use SilverStripe\LDAP\Services\LDAPService; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Class LDAPMemberSyncOneTask |
||
| 11 | * @package SilverStripe\LDAP\Tasks |
||
| 12 | * |
||
| 13 | * Debug build task that can be used to sync a single member by providing their email address registered in LDAP. |
||
| 14 | * |
||
| 15 | * Usage: /dev/tasks/[email protected] |
||
| 16 | */ |
||
| 17 | class LDAPMemberSyncOneTask extends LDAPMemberSyncTask |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * {@inheritDoc} |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | private static $segment = 'LDAPMemberSyncOneTask'; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 24 | |||
| 25 | /** |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | private static $dependencies = [ |
||
|
0 ignored issues
–
show
|
|||
| 29 | 'LDAPService' => '%$' . LDAPService::class, |
||
| 30 | ]; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var LDAPService |
||
| 34 | */ |
||
| 35 | protected $ldapService; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @return string |
||
| 39 | */ |
||
| 40 | public function getTitle() |
||
| 41 | { |
||
| 42 | return _t(__CLASS__ . '.SYNCONETITLE', 'Sync single user from LDAP'); |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Syncs a single user based on the email address passed in the URL |
||
| 47 | * |
||
| 48 | * @param HTTPRequest $request |
||
| 49 | */ |
||
| 50 | public function run($request) |
||
| 51 | { |
||
| 52 | $email = $request->getVar('email'); |
||
| 53 | |||
| 54 | if (!$email) { |
||
| 55 | echo 'You must supply an email parameter to this method.', PHP_EOL; |
||
| 56 | exit; |
||
|
0 ignored issues
–
show
|
|||
| 57 | } |
||
| 58 | |||
| 59 | $user = $this->ldapService->getUserByEmail($email); |
||
| 60 | |||
| 61 | if (!$user) { |
||
|
0 ignored issues
–
show
The expression
$user of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using Loading history...
|
|||
| 62 | echo sprintf('No user found in LDAP for email %s', $email), PHP_EOL; |
||
| 63 | exit; |
||
|
0 ignored issues
–
show
|
|||
| 64 | } |
||
| 65 | |||
| 66 | $member = $this->findOrCreateMember($user); |
||
| 67 | |||
| 68 | // If member exists already, we're updating - otherwise we're creating |
||
| 69 | if ($member->exists()) { |
||
| 70 | $this->log(sprintf( |
||
| 71 | 'Updating existing Member %s: "%s" (ID: %s, SAM Account Name: %s)', |
||
| 72 | $user['objectguid'], |
||
| 73 | $member->getName(), |
||
| 74 | $member->ID, |
||
| 75 | $user['samaccountname'] |
||
| 76 | )); |
||
| 77 | } else { |
||
| 78 | $this->log(sprintf( |
||
| 79 | 'Creating new Member %s: "%s" (SAM Account Name: %s)', |
||
| 80 | $user['objectguid'], |
||
| 81 | $user['cn'], |
||
| 82 | $user['samaccountname'] |
||
| 83 | )); |
||
| 84 | } |
||
| 85 | |||
| 86 | $this->log('User data returned from LDAP follows:'); |
||
| 87 | $this->log(var_export($user)); |
||
|
0 ignored issues
–
show
Are you sure the usage of
var_export($user) is correct as it seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||
| 88 | |||
| 89 | try { |
||
| 90 | $this->ldapService->updateMemberFromLDAP($member, $user); |
||
| 91 | $this->log('Done!'); |
||
| 92 | } catch (Exception $e) { |
||
| 93 | $this->log($e->getMessage()); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param LDAPService $service |
||
| 99 | * @return $this |
||
| 100 | */ |
||
| 101 | public function setLDAPService(LDAPService $service) |
||
| 102 | { |
||
| 103 | $this->ldapService = $service; |
||
| 104 | return $this; |
||
| 105 | } |
||
| 106 | } |
||
| 107 |