Verified Commit 4c58fbbf authored by dockx thibault's avatar dockx thibault
Browse files

:art: refactor(audit) - standardize spacing and formatting in AutomaticGroups...

:art: refactor(audit) - standardize spacing and formatting in AutomaticGroups class for improved readability
parent dcc3580a
1 merge request!89Resolve "[Orchestrator] - Automatic Group Management Task"
Pipeline #32746 passed with stages
in 26 seconds
Showing with 43 additions and 43 deletions
+43 -43
...@@ -4,7 +4,7 @@ class AutomaticGroups implements EndpointInterface ...@@ -4,7 +4,7 @@ class AutomaticGroups implements EndpointInterface
{ {
private TaskGateway $gateway; private TaskGateway $gateway;
public function __construct(TaskGateway $gateway) public function __construct (TaskGateway $gateway)
{ {
$this->gateway = $gateway; $this->gateway = $gateway;
} }
...@@ -13,7 +13,7 @@ class AutomaticGroups implements EndpointInterface ...@@ -13,7 +13,7 @@ class AutomaticGroups implements EndpointInterface
* @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
{ {
return $this->gateway->getObjectTypeTask('Automatic Groups'); return $this->gateway->getObjectTypeTask('Automatic Groups');
} }
...@@ -22,7 +22,7 @@ class AutomaticGroups implements EndpointInterface ...@@ -22,7 +22,7 @@ class AutomaticGroups implements EndpointInterface
* @param array|null $data * @param array|null $data
* @return array * @return array
*/ */
public function processEndPointPost(array $data = NULL): array public function processEndPointPost (array $data = NULL): array
{ {
return []; return [];
} }
...@@ -32,7 +32,7 @@ class AutomaticGroups implements EndpointInterface ...@@ -32,7 +32,7 @@ class AutomaticGroups implements EndpointInterface
* @return array * @return array
* @throws Exception * @throws Exception
*/ */
public function processEndPointPatch(array $data = NULL): array public function processEndPointPatch (array $data = NULL): array
{ {
return $this->processAutomaticGroups($this->gateway->getObjectTypeTask('Automatic Groups')); return $this->processAutomaticGroups($this->gateway->getObjectTypeTask('Automatic Groups'));
} }
...@@ -41,22 +41,22 @@ class AutomaticGroups implements EndpointInterface ...@@ -41,22 +41,22 @@ class AutomaticGroups implements EndpointInterface
* @param array|null $data * @param array|null $data
* @return array * @return array
*/ */
public function processEndPointDelete(array $data = NULL): array public function processEndPointDelete (array $data = NULL): array
{ {
return []; return [];
} }
/** /**
* Process automatic group assignment tasks * Process automatic group assignment tasks
* *
* @param array $automaticGroupsTasks * @param array $automaticGroupsTasks
* @return array * @return array
* @throws Exception * @throws Exception
*/ */
public function processAutomaticGroups(array $automaticGroupsTasks): array public function processAutomaticGroups (array $automaticGroupsTasks): array
{ {
$result = []; $result = [];
if (empty($automaticGroupsTasks)) { if (empty($automaticGroupsTasks)) {
return ['No automatic groups tasks require processing.']; return ['No automatic groups tasks require processing.'];
} }
...@@ -69,26 +69,26 @@ class AutomaticGroups implements EndpointInterface ...@@ -69,26 +69,26 @@ class AutomaticGroups implements EndpointInterface
} }
// Get the DN of the user/group to process // Get the DN of the user/group to process
$userDn = $task['fdtasksgranulardn'][0] ?? null; $userDn = $task['fdtasksgranulardn'][0] ?? NULL;
if (empty($userDn)) { if (empty($userDn)) {
throw new Exception("Missing user DN in task"); throw new Exception("Missing user DN in task");
} }
// Get main task configuration // Get main task configuration
$mainTaskConfig = $this->getAutomaticGroupsMainTask($task['fdtasksgranularmaster'][0]); $mainTaskConfig = $this->getAutomaticGroupsMainTask($task['fdtasksgranularmaster'][0]);
// Get target group and resource/state criteria // Get target group and resource/state criteria
$targetGroup = $mainTaskConfig[0]['fdtasksautomaticgroupsofname'][0] ?? null; $targetGroup = $mainTaskConfig[0]['fdtasksautomaticgroupsofname'][0] ?? NULL;
$resource = $mainTaskConfig[0]['fdtasksautomaticgroupsresource'][0] ?? null; $resource = $mainTaskConfig[0]['fdtasksautomaticgroupsresource'][0] ?? NULL;
$state = $mainTaskConfig[0]['fdtasksautomaticgroupsstate'][0] ?? null; $state = $mainTaskConfig[0]['fdtasksautomaticgroupsstate'][0] ?? NULL;
$subState = $mainTaskConfig[0]['fdtasksautomaticgroupssubstate'][0] ?? null; $subState = $mainTaskConfig[0]['fdtasksautomaticgroupssubstate'][0] ?? NULL;
if (empty($targetGroup)) { if (empty($targetGroup)) {
throw new Exception("Missing target group in task configuration"); throw new Exception("Missing target group in task configuration");
} }
// Check if user meets the criteria (if resource/state specified) // Check if user meets the criteria (if resource/state specified)
$shouldAddToGroup = true; $shouldAddToGroup = TRUE;
if ($resource !== 'NONE' && !empty($resource) && !empty($state)) { if ($resource !== 'NONE' && !empty($resource) && !empty($state)) {
$userSupannState = $this->getUserSupannState($userDn); $userSupannState = $this->getUserSupannState($userDn);
$shouldAddToGroup = $this->checkUserSupannState($userSupannState, $resource, $state, $subState); $shouldAddToGroup = $this->checkUserSupannState($userSupannState, $resource, $state, $subState);
...@@ -116,11 +116,11 @@ class AutomaticGroups implements EndpointInterface ...@@ -116,11 +116,11 @@ class AutomaticGroups implements EndpointInterface
/** /**
* Get main task configuration * Get main task configuration
* *
* @param string $mainTaskDn * @param string $mainTaskDn
* @return array * @return array
*/ */
private function getAutomaticGroupsMainTask(string $mainTaskDn): array private function getAutomaticGroupsMainTask (string $mainTaskDn): array
{ {
return $this->gateway->getLdapTasks( return $this->gateway->getLdapTasks(
'(objectClass=fdTasksAutomaticGroups)', '(objectClass=fdTasksAutomaticGroups)',
...@@ -137,11 +137,11 @@ class AutomaticGroups implements EndpointInterface ...@@ -137,11 +137,11 @@ class AutomaticGroups implements EndpointInterface
/** /**
* Get user's Supann state * Get user's Supann state
* *
* @param string $userDn * @param string $userDn
* @return array * @return array
*/ */
private function getUserSupannState(string $userDn): array private function getUserSupannState (string $userDn): array
{ {
$result = $this->gateway->getLdapTasks( $result = $this->gateway->getLdapTasks(
'(objectClass=*)', '(objectClass=*)',
...@@ -156,17 +156,17 @@ class AutomaticGroups implements EndpointInterface ...@@ -156,17 +156,17 @@ class AutomaticGroups implements EndpointInterface
/** /**
* Check if user matches the required Supann state * Check if user matches the required Supann state
* *
* @param array $userSupannState * @param array $userSupannState
* @param string $resource * @param string $resource
* @param string $state * @param string $state
* @param string|null $subState * @param string|null $subState
* @return bool * @return bool
*/ */
private function checkUserSupannState(array $userSupannState, string $resource, string $state, ?string $subState): bool private function checkUserSupannState (array $userSupannState, string $resource, string $state, ?string $subState): bool
{ {
if (empty($userSupannState[0]['supannressourceetat'])) { if (empty($userSupannState[0]['supannressourceetat'])) {
return false; return FALSE;
} }
foreach ($userSupannState[0]['supannressourceetat'] as $value) { foreach ($userSupannState[0]['supannressourceetat'] as $value) {
...@@ -177,24 +177,24 @@ class AutomaticGroups implements EndpointInterface ...@@ -177,24 +177,24 @@ class AutomaticGroups implements EndpointInterface
} else { } else {
$expectedState = '{' . $resource . '}' . $state; $expectedState = '{' . $resource . '}' . $state;
} }
if ($value === $expectedState) { if ($value === $expectedState) {
return true; return TRUE;
} }
} }
return false; return FALSE;
} }
/** /**
* Add user to LDAP group * Add user to LDAP group
* *
* @param string $userDn * @param string $userDn
* @param string $groupDn * @param string $groupDn
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
private function addUserToGroup(string $userDn, string $groupDn): bool private function addUserToGroup (string $userDn, string $groupDn): bool
{ {
// Get current group members // Get current group members
$groupInfo = $this->gateway->getLdapTasks( $groupInfo = $this->gateway->getLdapTasks(
...@@ -206,23 +206,23 @@ class AutomaticGroups implements EndpointInterface ...@@ -206,23 +206,23 @@ class AutomaticGroups implements EndpointInterface
$this->gateway->unsetCountKeys($groupInfo); $this->gateway->unsetCountKeys($groupInfo);
$members = $groupInfo[0]['member'] ?? []; $members = $groupInfo[0]['member'] ?? [];
// If member is already in the group, nothing to do // If member is already in the group, nothing to do
if (in_array($userDn, $members)) { if (in_array($userDn, $members)) {
return true; return TRUE;
} }
// Add member to the group // Add member to the group
$members[] = $userDn; $members[] = $userDn;
$entry = ['member' => $members]; $entry = ['member' => $members];
// Update the group in LDAP // Update the group in LDAP
try { try {
$result = ldap_modify($this->gateway->ds, $groupDn, $entry); $result = ldap_modify($this->gateway->ds, $groupDn, $entry);
if (!$result) { if (!$result) {
throw new Exception("Failed to add $userDn to group $groupDn: " . ldap_error($this->gateway->ds)); throw new Exception("Failed to add $userDn to group $groupDn: " . ldap_error($this->gateway->ds));
} }
return true; return TRUE;
} catch (Exception $e) { } catch (Exception $e) {
throw new Exception("Error adding member to group: " . $e->getMessage()); throw new Exception("Error adding member to group: " . $e->getMessage());
} }
...@@ -230,13 +230,13 @@ class AutomaticGroups implements EndpointInterface ...@@ -230,13 +230,13 @@ class AutomaticGroups implements EndpointInterface
/** /**
* Remove user from LDAP group * Remove user from LDAP group
* *
* @param string $userDn * @param string $userDn
* @param string $groupDn * @param string $groupDn
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
private function removeUserFromGroup(string $userDn, string $groupDn): bool private function removeUserFromGroup (string $userDn, string $groupDn): bool
{ {
// Get current group members // Get current group members
$groupInfo = $this->gateway->getLdapTasks( $groupInfo = $this->gateway->getLdapTasks(
...@@ -248,29 +248,29 @@ class AutomaticGroups implements EndpointInterface ...@@ -248,29 +248,29 @@ class AutomaticGroups implements EndpointInterface
$this->gateway->unsetCountKeys($groupInfo); $this->gateway->unsetCountKeys($groupInfo);
$members = $groupInfo[0]['member'] ?? []; $members = $groupInfo[0]['member'] ?? [];
// If member is not in the group, nothing to do // If member is not in the group, nothing to do
if (!in_array($userDn, $members)) { if (!in_array($userDn, $members)) {
return true; return TRUE;
} }
// Remove member from the group // Remove member from the group
$members = array_diff($members, [$userDn]); $members = array_diff($members, [$userDn]);
// Groups must have at least one member, so check if this would empty the group // Groups must have at least one member, so check if this would empty the group
if (empty($members)) { if (empty($members)) {
return true; // Do nothing if it would empty the group return TRUE; // Do nothing if it would empty the group
} }
$entry = ['member' => $members]; $entry = ['member' => $members];
// Update the group in LDAP // Update the group in LDAP
try { try {
$result = ldap_modify($this->gateway->ds, $groupDn, $entry); $result = ldap_modify($this->gateway->ds, $groupDn, $entry);
if (!$result) { if (!$result) {
throw new Exception("Failed to remove $userDn from group $groupDn: " . ldap_error($this->gateway->ds)); throw new Exception("Failed to remove $userDn from group $groupDn: " . ldap_error($this->gateway->ds));
} }
return true; return TRUE;
} catch (Exception $e) { } catch (Exception $e) {
throw new Exception("Error removing member from group: " . $e->getMessage()); throw new Exception("Error removing member from group: " . $e->getMessage());
} }
......
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