diff --git a/.gitignore b/.gitignore index a47460047af34a4d26d521bacc1cfa163414722f..84558c9e883637214bf1f2aff7e1c8002c04b140 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,9 @@ .vendor/ .composer.lock +filelist +phpstan.neon +.idea/fusiondirectory-orchestrator.iml +.idea/modules.xml +.idea/php.xml +.idea/vcs.xml +.idea/codeStyles/codeStyleConfig.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100755 index 0000000000000000000000000000000000000000..13566b81b018ad684f3a35fee301741b2734c8f4 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/library/TaskController.php b/library/TaskController.php index 849f5b65c2971b92097a9098e764bcca0987b2cb..2b4ddf358822be6d5f43bc08220b66007114b39f 100644 --- a/library/TaskController.php +++ b/library/TaskController.php @@ -55,7 +55,7 @@ class TaskController switch ($objectType) { case $objectType: if (class_exists($objectType)) { - $endpoint = new $objectType; + $endpoint = new $objectType($this->gateway); $result = $endpoint->processEndPointGet(); } break; diff --git a/plugins/tasks/Archive.php b/plugins/tasks/Archive.php new file mode 100644 index 0000000000000000000000000000000000000000..2ef3e2f08d4c90222b5bb9b3d42882d13ed8199a --- /dev/null +++ b/plugins/tasks/Archive.php @@ -0,0 +1,137 @@ +<?php + +use FusionDirectory\Rest\WebServiceCall; + +class Archive implements EndpointInterface +{ + private TaskGateway $gateway; + + public function __construct (TaskGateway $gateway) + { + $this->gateway = $gateway; + } + + /** + * @return array + * Part of the interface of orchestrator plugin to treat GET method + */ + public function processEndPointGet (): array + { + // Retrieve tasks of type 'archive' + return $this->gateway->getObjectTypeTask('archive'); + } + + /** + * @param array|null $data + * @return array + * @throws Exception + * Note: Part of the interface of orchestrator plugin to treat PATCH method + */ + public function processEndPointPatch (array $data = NULL): array + { + $result = []; + $archiveTasks = $this->gateway->getObjectTypeTask('archive'); + + // Initialize the WebServiceCall object for login + $webServiceCall = new WebServiceCall($_ENV['FUSION_DIRECTORY_API_URL'] . '/login', 'POST'); + $webServiceCall->setCurlSettings(); // Perform login and set the token + + foreach ($archiveTasks as $task) { + try { + if (!$this->gateway->statusAndScheduleCheck($task)) { + // Skip this task if it does not meet the status and schedule criteria + continue; + } + + // Receive null or 'toBeArchived' + $supannState = $this->getUserSupannAccountStatus($task['fdtasksgranulardn'][0]); + + if ($supannState !== 'toBeArchived') { + // 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."; + $this->gateway->removeSubTask($task['dn']); + continue; + } + + // Set the archive endpoint and method using the same WebServiceCall object + $archiveUrl = $_ENV['FUSION_DIRECTORY_API_URL'] . '/archive/user/' . rawurlencode($task['fdtasksgranulardn'][0]); + $webServiceCall->setCurlSettings($archiveUrl, NULL, 'POST'); // Update settings for the archive request + $response = $webServiceCall->execute(); + + // Check if the HTTP status code is 204 + if ($webServiceCall->getHttpStatusCode() === 204) { + $result[$task['dn']]['result'] = "User successfully archived."; + $this->gateway->updateTaskStatus($task['dn'], $task['cn'][0], '2'); + } else { + throw new Exception("Unexpected HTTP status code: " . $webServiceCall->getHttpStatusCode()); + } + } catch (Exception $e) { + $result[$task['dn']]['result'] = "Error archiving user: " . $e->getMessage(); + $this->gateway->updateTaskStatus($task['dn'], $task['cn'][0], $e->getMessage()); + } + } + + return $result; + } + + /** + * @param array|null $data + * @return array + * Note: Part of the interface of orchestrator plugin to treat POST method + */ + public function processEndPointPost (array $data = NULL): array + { + return []; + } + + /** + * @param array|null $data + * @return array + * Note: Part of the interface of orchestrator plugin to treat DELETE method + */ + public function processEndPointDelete (array $data = NULL): array + { + return []; + } + + /** + * Retrieve the supannAccountStatus of a user + * @param string $userDn + * @return string|null + */ + private function getUserSupannAccountStatus (string $userDn): ?string + { + $supannState = $this->gateway->getLdapTasks( + '(objectClass=supannPerson)', + ['supannRessourceEtatDate'], + '', + $userDn + ); + + if ($this->hasToBeArchived($supannState)) { + return 'toBeArchived'; + } + + return NULL; + } + + private function hasToBeArchived (array $supannState): bool + { + if (!isset($supannState[0]['supannressourceetatdate']) || !is_array($supannState[0]['supannressourceetatdate'])) { + return FALSE; + } + + foreach ($supannState[0]['supannressourceetatdate'] as $key => $value) { + // Skip non-numeric keys (e.g., 'count') + if (!is_numeric($key)) { + continue; + } + + if (strpos($value, '{COMPTE}I:toBeArchived') !== FALSE) { + return TRUE; + } + } + + return FALSE; + } +} \ No newline at end of file