Skip to content

Commit

Permalink
Merge branch '2' into 3
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Sep 17, 2024
2 parents 9750358 + a954c0e commit 9f50239
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 71 deletions.
63 changes: 37 additions & 26 deletions src/Tasks/LDAPGroupSyncTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Dev\BuildTask;
use SilverStripe\Dev\Deprecation;
use SilverStripe\LDAP\Services\LDAPService;
use SilverStripe\ORM\DB;
use SilverStripe\Security\Group;
Expand Down Expand Up @@ -84,27 +85,31 @@ public function run($request)
$group = new Group();
$group->GUID = $data['objectguid'];

$this->log(sprintf(
'Creating new Group (GUID: %s, sAMAccountName: %s)',
$data['objectguid'],
$data['samaccountname']
));
Deprecation::withNoReplacement(function () use ($data) {
$this->log(sprintf(
'Creating new Group (GUID: %s, sAMAccountName: %s)',
$data['objectguid'],
$data['samaccountname']
));
});
$created++;
} else {
$this->log(sprintf(
'Updating existing Group "%s" (ID: %s, GUID: %s, sAMAccountName: %s)',
$group->getTitle(),
$group->ID,
$data['objectguid'],
$data['samaccountname']
));
Deprecation::withNoReplacement(function () use ($group, $data) {
$this->log(sprintf(
'Updating existing Group "%s" (ID: %s, GUID: %s, sAMAccountName: %s)',
$group->getTitle(),
$group->ID,
$data['objectguid'],
$data['samaccountname']
));
});
$updated++;
}

try {
$this->ldapService->updateGroupFromLDAP($group, $data);
} catch (Exception $e) {
$this->log($e->getMessage());
Deprecation::withNoReplacement(fn() => $this->log($e->getMessage()));
continue;
}
}
Expand All @@ -116,11 +121,13 @@ public function run($request)
if (!isset($ldapGroups[$record['GUID']])) {
$group = Group::get()->byId($record['ID']);

$this->log(sprintf(
'Removing Group "%s" (GUID: %s) that no longer exists in LDAP.',
$group->Title,
$group->GUID
));
Deprecation::withNoReplacement(function () use ($group) {
$this->log(sprintf(
'Removing Group "%s" (GUID: %s) that no longer exists in LDAP.',
$group->Title,
$group->GUID
));
});

try {
// Cascade into mappings, just to clean up behind ourselves.
Expand All @@ -129,7 +136,7 @@ public function run($request)
}
$group->delete();
} catch (Exception $e) {
$this->log($e->getMessage());
Deprecation::withNoReplacement(fn() => $this->log($e->getMessage()));
continue;
}

Expand All @@ -142,22 +149,26 @@ public function run($request)

$end = time() - $start;

$this->log(sprintf(
'Done. Created %s records. Updated %s records. Deleted %s records. Duration: %s seconds',
$created,
$updated,
$deleted,
round($end ?? 0.0, 0)
));
Deprecation::withNoReplacement(function () use ($created, $updated, $deleted, $end) {
$this->log(sprintf(
'Done. Created %s records. Updated %s records. Deleted %s records. Duration: %s seconds',
$created,
$updated,
$deleted,
round($end ?? 0.0, 0)
));
});
}

/**
* Sends a message, formatted either for the CLI or browser
*
* @param string $message
* @deprecated 2.3.0 Will be replaced with new $output parameter in the run() method
*/
protected function log($message)
{
Deprecation::notice('2.3.0', 'Will be replaced with new $output parameter in the run() method');
$message = sprintf('[%s] ', date('Y-m-d H:i:s')) . $message;
echo Director::is_cli() ? ($message . PHP_EOL) : ($message . '<br>');
}
Expand Down
27 changes: 16 additions & 11 deletions src/Tasks/LDAPMemberSyncOneTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Dev\Deprecation;
use SilverStripe\LDAP\Services\LDAPService;

/**
Expand Down Expand Up @@ -67,13 +68,15 @@ public function run($request)

// If member exists already, we're updating - otherwise we're creating
if ($member->exists()) {
$this->log(sprintf(
'Updating existing Member %s: "%s" (ID: %s, SAM Account Name: %s)',
$user['objectguid'],
$member->getName(),
$member->ID,
$user['samaccountname']
));
Deprecation::withNoReplacement(function () use ($user, $member) {
$this->log(sprintf(
'Updating existing Member %s: "%s" (ID: %s, SAM Account Name: %s)',
$user['objectguid'],
$member->getName(),
$member->ID,
$user['samaccountname']
));
});
} else {
$this->log(sprintf(
'Creating new Member %s: "%s" (SAM Account Name: %s)',
Expand All @@ -83,14 +86,16 @@ public function run($request)
));
}

$this->log('User data returned from LDAP follows:');
$this->log(var_export($user));
Deprecation::withNoReplacement(function () use ($user) {
$this->log('User data returned from LDAP follows:');
$this->log(var_export($user));
});

try {
$this->ldapService->updateMemberFromLDAP($member, $user);
$this->log('Done!');
Deprecation::withNoReplacement(fn() => $this->log('Done!'));
} catch (Exception $e) {
$this->log($e->getMessage());
Deprecation::withNoReplacement(fn() => $this->log($e->getMessage()));
}
}

Expand Down
65 changes: 38 additions & 27 deletions src/Tasks/LDAPMemberSyncTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\BuildTask;
use SilverStripe\Dev\Deprecation;
use SilverStripe\LDAP\Services\LDAPService;
use SilverStripe\ORM\DB;
use SilverStripe\Security\Member;
Expand Down Expand Up @@ -82,29 +83,33 @@ public function run($request)
// If member exists already, we're updating - otherwise we're creating
if ($member->exists()) {
$updated++;
$this->log(sprintf(
'Updating existing Member %s: "%s" (ID: %s, SAM Account Name: %s)',
$data['objectguid'],
$member->getName(),
$member->ID,
$data['samaccountname']
));
Deprecation::withNoReplacement(function () use ($data, $member) {
$this->log(sprintf(
'Updating existing Member %s: "%s" (ID: %s, SAM Account Name: %s)',
$data['objectguid'],
$member->getName(),
$member->ID,
$data['samaccountname']
));
});
} else {
$created++;
$this->log(sprintf(
'Creating new Member %s: "%s" (SAM Account Name: %s)',
$data['objectguid'],
$data['cn'],
$data['samaccountname']
));
Deprecation::withNoReplacement(function () use ($data) {
$this->log(sprintf(
'Creating new Member %s: "%s" (SAM Account Name: %s)',
$data['objectguid'],
$data['cn'],
$data['samaccountname']
));
});
}

// Sync attributes from LDAP to the Member record. This will also write the Member record.
// this is also responsible for putting the user into mapped groups
try {
$this->ldapService->updateMemberFromLDAP($member, $data);
} catch (Exception $e) {
$this->log($e->getMessage());
Deprecation::withNoReplacement(fn() => $this->log($e->getMessage()));
continue;
}
}
Expand All @@ -116,16 +121,18 @@ public function run($request)
$member = Member::get()->byId($record['ID']);

if (!isset($users[$record['GUID']])) {
$this->log(sprintf(
'Removing Member "%s" (GUID: %s) that no longer exists in LDAP.',
$member->getName(),
$member->GUID
));
Deprecation::withNoReplacement(function () use ($member) {
$this->log(sprintf(
'Removing Member "%s" (GUID: %s) that no longer exists in LDAP.',
$member->getName(),
$member->GUID
));
});

try {
$member->delete();
} catch (Exception $e) {
$this->log($e->getMessage());
Deprecation::withNoReplacement(fn() => $this->log($e->getMessage()));
continue;
}

Expand All @@ -138,22 +145,26 @@ public function run($request)

$end = time() - $start;

$this->log(sprintf(
'Done. Created %s records. Updated %s records. Deleted %s records. Duration: %s seconds',
$created,
$updated,
$deleted,
round($end ?? 0.0, 0)
));
Deprecation::withNoReplacement(function () use ($created, $updated, $deleted, $end) {
$this->log(sprintf(
'Done. Created %s records. Updated %s records. Deleted %s records. Duration: %s seconds',
$created,
$updated,
$deleted,
round($end ?? 0.0, 0)
));
});
}

/**
* Sends a message, formatted either for the CLI or browser
*
* @param string $message
* @deprecated 2.3.0 Will be replaced with new $output parameter in the run() method
*/
protected function log($message)
{
Deprecation::notice('2.3.0', 'Will be replaced with new $output parameter in the run() method');
$message = sprintf('[%s] ', date('Y-m-d H:i:s')) . $message;
echo Director::is_cli() ? ($message . PHP_EOL) : ($message . '<br>');
}
Expand Down
21 changes: 14 additions & 7 deletions src/Tasks/LDAPMigrateExistingMembersTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Convert;
use SilverStripe\Dev\BuildTask;
use SilverStripe\Dev\Deprecation;
use SilverStripe\LDAP\Services\LDAPService;
use SilverStripe\Security\Member;

Expand Down Expand Up @@ -73,26 +74,32 @@ public function run($request)

$count++;

$this->log(sprintf(
'Migrated Member %s (ID: %s, Email: %s)',
$member->getName(),
$member->ID,
$member->Email
));
Deprecation::withNoReplacement(function () use ($member) {
$this->log(sprintf(
'Migrated Member %s (ID: %s, Email: %s)',
$member->getName(),
$member->ID,
$member->Email
));
});
}

$end = time() - $start;

$this->log(sprintf('Done. Migrated %s Member records. Duration: %s seconds', $count, round($end ?? 0.0, 0)));
Deprecation::withNoReplacement(function () use ($count, $end) {
$this->log(sprintf('Done. Migrated %s Member records. Duration: %s seconds', $count, round($end ?? 0.0, 0)));
});
}

/**
* Sends a message, formatted either for the CLI or browser
*
* @param string $message
* @deprecated 2.3.0 Will be replaced with new $output parameter in the run() method
*/
protected function log($message)
{
Deprecation::notice('2.3.0', 'Will be replaced with new $output parameter in the run() method');
$message = sprintf('[%s] ', date('Y-m-d H:i:s')) . $message;
echo Director::is_cli() ? ($message . PHP_EOL) : ($message . '<br>');
}
Expand Down

0 comments on commit 9f50239

Please sign in to comment.