Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Deprecate API that will be removed #91

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading