Commit 2e1430b2 authored by dockx thibault's avatar dockx thibault
Browse files

Merge branch '74-orchestrator-automatic-archiving-based-on-supann-resource-states' into 'dev'

Resolve "[Orchestrator] - Automatic Archiving Based on SupAnn Resource States"

See merge request !82
1 merge request!82Resolve "[Orchestrator] - Automatic Archiving Based on SupAnn Resource States"
Pipeline #32579 passed with stages
in 17 seconds
Showing with 107 additions and 59 deletions
+107 -59
...@@ -8,33 +8,33 @@ class Archive implements EndpointInterface ...@@ -8,33 +8,33 @@ class Archive implements EndpointInterface
public function __construct (TaskGateway $gateway) public function __construct (TaskGateway $gateway)
{ {
$this->gateway = $gateway; $this->gateway = $gateway;
} }
/** /**
* @return array * @return array
* Part of the interface of orchestrator plugin to treat GET method * Part of the interface of orchestrator plugin to treat GET method
*/ */
public function processEndPointGet (): array public function processEndPointGet (): array
{ {
// Retrieve tasks of type 'archive' // Retrieve tasks of type 'archive'
return $this->gateway->getObjectTypeTask('archive'); return $this->gateway->getObjectTypeTask('archive');
} }
/** /**
* @param array|null $data * @param array|null $data
* @return array * @return array
* @throws Exception * @throws Exception
* Note: Part of the interface of orchestrator plugin to treat PATCH method * Note: Part of the interface of orchestrator plugin to treat PATCH method
*/ */
public function processEndPointPatch (array $data = NULL): array public function processEndPointPatch (array $data = NULL): array
{ {
$result = []; $result = [];
$archiveTasks = $this->gateway->getObjectTypeTask('archive'); $archiveTasks = $this->gateway->getObjectTypeTask('archive');
// Initialize the WebServiceCall object for login // Initialize the WebServiceCall object for login
$webServiceCall = new WebServiceCall($_ENV['FUSION_DIRECTORY_API_URL'] . '/login', 'POST'); $webServiceCall = new WebServiceCall($_ENV['FUSION_DIRECTORY_API_URL'] . '/login', 'POST');
$webServiceCall->setCurlSettings(); // Perform login and set the token $webServiceCall->setCurlSettings(); // Perform login and set the token
foreach ($archiveTasks as $task) { foreach ($archiveTasks as $task) {
try { try {
...@@ -43,95 +43,143 @@ class Archive implements EndpointInterface ...@@ -43,95 +43,143 @@ class Archive implements EndpointInterface
continue; continue;
} }
// Receive null or 'toBeArchived' // Retrieve the desired supann status from the main task
$supannState = $this->getUserSupannAccountStatus($task['fdtasksgranulardn'][0]); $desiredSupannStatus = $this->getArchiveTaskBehaviorFromMainTask($task['fdtasksgranularmaster'][0]);
if ($supannState !== 'toBeArchived') { // Retrieve the current supann status of the user
$currentSupannStatus = $this->getUserSupannAccountStatus($task['fdtasksgranulardn'][0]);
// Check if the current supann status matches the desired status
if (!$this->isSupannStatusMatching($desiredSupannStatus, $currentSupannStatus)) {
// The task does not meet the criteria for archiving and can therefore be suppressed // The task does not meet the criteria for archiving and can therefore be suppressed
$result[$task['dn']]['result'] = "User does not meet the criteria for archiving."; $result[$task['dn']]['result'] = "User does not meet the criteria for archiving.";
$this->gateway->removeSubTask($task['dn']); $this->gateway->removeSubTask($task['dn']);
continue; continue;
} }
// Set the archive endpoint and method using the same WebServiceCall object // Set the archive endpoint and method using the same WebServiceCall object
$archiveUrl = $_ENV['FUSION_DIRECTORY_API_URL'] . '/archive/user/' . rawurlencode($task['fdtasksgranulardn'][0]); $archiveUrl = $_ENV['FUSION_DIRECTORY_API_URL'] . '/archive/user/' . rawurlencode($task['fdtasksgranulardn'][0]);
$webServiceCall->setCurlSettings($archiveUrl, NULL, 'POST'); // Update settings for the archive request $webServiceCall->setCurlSettings($archiveUrl, NULL, 'POST'); // Update settings for the archive request
$response = $webServiceCall->execute(); $response = $webServiceCall->execute();
// Check if the HTTP status code is 204 // Check if the HTTP status code is 204
if ($webServiceCall->getHttpStatusCode() === 204) { if ($webServiceCall->getHttpStatusCode() === 204) {
$result[$task['dn']]['result'] = "User successfully archived."; $result[$task['dn']]['result'] = "User " . $task['fdtasksgranulardn'][0] . " successfully archived.";
$this->gateway->updateTaskStatus($task['dn'], $task['cn'][0], '2'); $this->gateway->updateTaskStatus($task['dn'], $task['cn'][0], '2');
} else { } else {
throw new Exception("Unexpected HTTP status code: " . $webServiceCall->getHttpStatusCode()); throw new Exception("Unexpected HTTP status code: " . $webServiceCall->getHttpStatusCode());
} }
} catch (Exception $e) { } catch (Exception $e) {
$result[$task['dn']]['result'] = "Error archiving user: " . $e->getMessage(); $result[$task['dn']]['result'] = "Error archiving user: " . $e->getMessage();
$this->gateway->updateTaskStatus($task['dn'], $task['cn'][0], $e->getMessage()); $this->gateway->updateTaskStatus($task['dn'], $task['cn'][0], $e->getMessage());
} }
} }
return $result; return $result;
} }
/** /**
* @param array|null $data * @param array|null $data
* @return array * @return array
* Note: Part of the interface of orchestrator plugin to treat POST method * Note: Part of the interface of orchestrator plugin to treat POST method
*/ */
public function processEndPointPost (array $data = NULL): array public function processEndPointPost (array $data = NULL): array
{ {
return []; return [];
} }
/** /**
* @param array|null $data * @param array|null $data
* @return array * @return array
* Note: Part of the interface of orchestrator plugin to treat DELETE method * Note: Part of the interface of orchestrator plugin to treat DELETE method
*/ */
public function processEndPointDelete (array $data = NULL): array public function processEndPointDelete (array $data = NULL): array
{ {
return []; return [];
} }
/** /**
* Retrieve the supannAccountStatus of a user * Retrieve the supannAccountStatus of a user
* @param string $userDn * @param string $userDn
* @return string|null * @return array
*/ */
private function getUserSupannAccountStatus (string $userDn): ?string private function getUserSupannAccountStatus (string $userDn): array
{ {
$supannState = $this->gateway->getLdapTasks( return $this->gateway->getLdapTasks(
'(objectClass=supannPerson)', '(objectClass=supannPerson)',
['supannRessourceEtatDate'], ['supannRessourceEtatDate'],
'', '',
$userDn $userDn
); );
}
if ($this->hasToBeArchived($supannState)) { /**
return 'toBeArchived'; * @param string $taskDN
} * @return array
* Note: Retrieve the desired supann status from the main task attributes.
return NULL; */
private function getArchiveTaskBehaviorFromMainTask (string $taskDN): array
{
return $this->gateway->getLdapTasks(
'(objectClass=*)',
['fdArchiveTaskResource', 'fdArchiveTaskState', 'fdArchiveTaskSubState'],
'',
$taskDN
);
} }
private function hasToBeArchived (array $supannState): bool /**
* @param array $desiredStatus
* @param array $currentStatus
* @return bool
* Note: Compare the desired supann status with the current status to determine if they match.
*/
private function isSupannStatusMatching (array $desiredStatus, array $currentStatus): bool
{ {
if (!isset($supannState[0]['supannressourceetatdate']) || !is_array($supannState[0]['supannressourceetatdate'])) { if (empty($currentStatus[0]['supannressourceetatdate'])) {
return FALSE; return FALSE;
} }
foreach ($supannState[0]['supannressourceetatdate'] as $key => $value) { // Extract the desired attributes
// Skip non-numeric keys (e.g., 'count') $desiredAttributes = $this->extractDesiredAttributes($desiredStatus);
if (!$desiredAttributes['resource'] || !$desiredAttributes['state']) {
return FALSE;
}
// Check if any of the current supannressourceetatdate values match the desired attributes
foreach ($currentStatus[0]['supannressourceetatdate'] as $key => $resource) {
if (!is_numeric($key)) { if (!is_numeric($key)) {
continue; continue;
} }
if (strpos($value, '{COMPTE}I:toBeArchived') !== FALSE) { if ($this->doesResourceMatch($resource, $desiredAttributes)) {
return TRUE; return TRUE;
} }
} }
return FALSE; return FALSE;
} }
private function extractDesiredAttributes (array $desiredStatus): array
{
return [
'resource' => $desiredStatus[0]['fdarchivetaskresource'][0] ?? NULL,
'state' => $desiredStatus[0]['fdarchivetaskstate'][0] ?? NULL,
'substate' => $desiredStatus[0]['fdarchivetasksubstate'][0] ?? NULL,
];
}
private function doesResourceMatch (string $resource, array $desiredAttributes): bool
{
// Extract parts from the resource string
$parts = explode(':', $resource);
$resourcePart = str_replace(['{', '}'], '', $parts[0]);
$substatePart = $parts[1] ?? '';
$resourceMatch = $resourcePart === $desiredAttributes['resource'] . $desiredAttributes['state'];
$substateMatch = empty($desiredAttributes['substate']) || $substatePart === $desiredAttributes['substate'];
return $resourceMatch && $substateMatch;
}
} }
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment