From 247a3c2786d9774965451b7e67401027bab674a1 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 17 May 2024 12:58:37 -0700 Subject: [PATCH 01/18] 'active' attribute in LDAP not mapped correctly to 'enabled' attribute at ecloud --- lib/Listeners/UserChangedListener.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/Listeners/UserChangedListener.php b/lib/Listeners/UserChangedListener.php index 48d2ddb9..49afac84 100644 --- a/lib/Listeners/UserChangedListener.php +++ b/lib/Listeners/UserChangedListener.php @@ -58,6 +58,29 @@ class UserChangedListener implements IEventListener { $this->updateAttributesInLDAP($username, $recoveryEmailAttribute); } + + /** @var mixed $oldValue */ + $oldValue = $event->getOldValue(); + /** @var mixed $value */ + $value = $event->getValue(); + if ($feature === 'enabled'){ + if($value === true && $oldValue === false){ + $this->logger->info('Enabling an user', ['event' => $event]); + $userEnableAttributes = [ + 'active' => 'TRUE', + 'mailActive' => 'TRUE', + ]; + $this->updateAttributesInLDAP($username, $userEnableAttributes); + } + if($value === false && $oldValue === true){ + $this->logger->info('Disabling an user', ['event' => $event]); + $userEnableAttributes = [ + 'active' => 'FALSE', + 'mailActive' => 'FALSE', + ]; + $this->updateAttributesInLDAP($username, $userEnableAttributes); + } + } } private function updateQuota(string $username, string $backend, int $quotaInBytes) { -- GitLab From 7b86df558c9f26a3ac27c5252e187aafb79f76a3 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 17 May 2024 13:00:52 -0700 Subject: [PATCH 02/18] php fixer --- lib/Listeners/UserChangedListener.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Listeners/UserChangedListener.php b/lib/Listeners/UserChangedListener.php index 49afac84..4994c1be 100644 --- a/lib/Listeners/UserChangedListener.php +++ b/lib/Listeners/UserChangedListener.php @@ -63,8 +63,8 @@ class UserChangedListener implements IEventListener { $oldValue = $event->getOldValue(); /** @var mixed $value */ $value = $event->getValue(); - if ($feature === 'enabled'){ - if($value === true && $oldValue === false){ + if ($feature === 'enabled') { + if($value === true && $oldValue === false) { $this->logger->info('Enabling an user', ['event' => $event]); $userEnableAttributes = [ 'active' => 'TRUE', @@ -72,7 +72,7 @@ class UserChangedListener implements IEventListener { ]; $this->updateAttributesInLDAP($username, $userEnableAttributes); } - if($value === false && $oldValue === true){ + if($value === false && $oldValue === true) { $this->logger->info('Disabling an user', ['event' => $event]); $userEnableAttributes = [ 'active' => 'FALSE', -- GitLab From 1da83df183b93db199e38bc87cd925085e32c088 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 17 May 2024 13:35:43 -0700 Subject: [PATCH 03/18] Added listener and command --- appinfo/info.xml | 1 + lib/Command/MapActiveAttributetoLDAP.php | 68 ++++++++++++++++++++++++ lib/Listeners/UserChangedListener.php | 26 ++++----- lib/Service/UserService.php | 11 ++++ 4 files changed, 89 insertions(+), 17 deletions(-) create mode 100644 lib/Command/MapActiveAttributetoLDAP.php diff --git a/appinfo/info.xml b/appinfo/info.xml index 351006aa..91b23d65 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -27,5 +27,6 @@ OCA\EcloudAccounts\Command\Migrate2FASecrets OCA\EcloudAccounts\Command\MigrateWebmailAddressbooks + OCA\EcloudAccounts\Command\MapActiveAttributetoLDAP diff --git a/lib/Command/MapActiveAttributetoLDAP.php b/lib/Command/MapActiveAttributetoLDAP.php new file mode 100644 index 00000000..31274db4 --- /dev/null +++ b/lib/Command/MapActiveAttributetoLDAP.php @@ -0,0 +1,68 @@ +userManager = $userManager; + $this->userService = $userService; + parent::__construct(); + } + + protected function configure(): void { + $this + ->setName(Application::APP_ID.':map-active-attribute-to-ldap') + ->setDescription('Map Active attribute to LDAP'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + try { + $this->userManager->callForSeenUsers(function (IUser $user) { + if ($this->isUserValid($user)) { + if ($user->isEnabled()) { + $userEnableAttributes = [ + 'active' => 'TRUE', + 'mailActive' => 'TRUE', + ]; + } else { + $userEnableAttributes = [ + 'active' => 'FALSE', + 'mailActive' => 'FALSE', + ]; + } + $username = $user->getUID(); + $this->userService->updateAttributesInLDAP($username, $userEnableAttributes); + } + }); + return 0; + } catch (\Exception $e) { + $this->commandOutput->writeln($e->getMessage()); + return 1; + } + } + /** + * validate user + * + * @param IUser $user + */ + private function isUserValid(?IUser $user) : bool { + if (!($user instanceof IUser)) { + return false; + } + return true; + } +} diff --git a/lib/Listeners/UserChangedListener.php b/lib/Listeners/UserChangedListener.php index 4994c1be..8636ce15 100644 --- a/lib/Listeners/UserChangedListener.php +++ b/lib/Listeners/UserChangedListener.php @@ -7,6 +7,7 @@ namespace OCA\EcloudAccounts\Listeners; use Exception; use OCA\EcloudAccounts\Db\MailboxMapper; use OCA\EcloudAccounts\Service\LDAPConnectionService; +use OCA\EcloudAccounts\Service\UserService; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; use OCP\ILogger; @@ -26,11 +27,14 @@ class UserChangedListener implements IEventListener { private $mailboxMapper; - public function __construct(Util $util, LDAPConnectionService $LDAPConnectionService, ILogger $logger, MailboxMapper $mailboxMapper) { + private $userService; + + public function __construct(Util $util, LDAPConnectionService $LDAPConnectionService, ILogger $logger, MailboxMapper $mailboxMapper, UserService $userService) { $this->util = $util; $this->ldapConnectionService = $LDAPConnectionService; $this->mailboxMapper = $mailboxMapper; $this->logger = $logger; + $this->userService = $userService; } public function handle(Event $event): void { @@ -56,7 +60,7 @@ class UserChangedListener implements IEventListener { 'recoveryMailAddress' => $recoveryEmail ]; - $this->updateAttributesInLDAP($username, $recoveryEmailAttribute); + $this->userService->updateAttributesInLDAP($username, $recoveryEmailAttribute); } /** @var mixed $oldValue */ @@ -70,7 +74,7 @@ class UserChangedListener implements IEventListener { 'active' => 'TRUE', 'mailActive' => 'TRUE', ]; - $this->updateAttributesInLDAP($username, $userEnableAttributes); + $this->userService->updateAttributesInLDAP($username, $userEnableAttributes); } if($value === false && $oldValue === true) { $this->logger->info('Disabling an user', ['event' => $event]); @@ -78,7 +82,7 @@ class UserChangedListener implements IEventListener { 'active' => 'FALSE', 'mailActive' => 'FALSE', ]; - $this->updateAttributesInLDAP($username, $userEnableAttributes); + $this->userService->updateAttributesInLDAP($username, $userEnableAttributes); } } } @@ -92,22 +96,10 @@ class UserChangedListener implements IEventListener { $quotaAttribute = [ 'quota' => $quotaInBytes ]; - $this->updateAttributesInLDAP($username, $quotaAttribute); + $this->userService->updateAttributesInLDAP($username, $quotaAttribute); } } catch (Exception $e) { $this->logger->error("Error setting quota for user $username " . $e->getMessage()); } } - - private function updateAttributesInLDAP(string $username, array $attributes) { - if ($this->ldapConnectionService->isLDAPEnabled()) { - $conn = $this->ldapConnectionService->getLDAPConnection(); - $userDn = $this->ldapConnectionService->username2dn($username); - - if (!ldap_modify($conn, $userDn, $attributes)) { - throw new Exception('Could not modify user entry at LDAP server!'); - } - $this->ldapConnectionService->closeLDAPConnection($conn); - } - } } diff --git a/lib/Service/UserService.php b/lib/Service/UserService.php index a347655c..fb673a3f 100644 --- a/lib/Service/UserService.php +++ b/lib/Service/UserService.php @@ -520,6 +520,17 @@ class UserService { throw new AddUsernameToCommonStoreException("Error adding username '$username' to common data store."); } } + public function updateAttributesInLDAP(string $username, array $attributes) { + if ($this->LDAPConnectionService->isLDAPEnabled()) { + $conn = $this->LDAPConnectionService->getLDAPConnection(); + $userDn = $this->LDAPConnectionService->username2dn($username); + + if (!ldap_modify($conn, $userDn, $attributes)) { + throw new Exception('Could not modify user entry at LDAP server!'); + } + $this->LDAPConnectionService->closeLDAPConnection($conn); + } + } private function getDefaultQuota() { return $this->config->getSystemValueInt('default_quota_in_megabytes', 1024); } -- GitLab From 02f06c8f98844dcce9893c5b48eff5c8fc54f721 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 17 May 2024 13:37:00 -0700 Subject: [PATCH 04/18] Added listener and command --- lib/Command/MapActiveAttributetoLDAP.php | 6 +++--- lib/Listeners/UserChangedListener.php | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/Command/MapActiveAttributetoLDAP.php b/lib/Command/MapActiveAttributetoLDAP.php index 31274db4..f8cb5a95 100644 --- a/lib/Command/MapActiveAttributetoLDAP.php +++ b/lib/Command/MapActiveAttributetoLDAP.php @@ -34,18 +34,18 @@ class MapActiveAttributetoLDAP extends Command { $this->userManager->callForSeenUsers(function (IUser $user) { if ($this->isUserValid($user)) { if ($user->isEnabled()) { - $userEnableAttributes = [ + $userActiveAttributes = [ 'active' => 'TRUE', 'mailActive' => 'TRUE', ]; } else { - $userEnableAttributes = [ + $userActiveAttributes = [ 'active' => 'FALSE', 'mailActive' => 'FALSE', ]; } $username = $user->getUID(); - $this->userService->updateAttributesInLDAP($username, $userEnableAttributes); + $this->userService->updateAttributesInLDAP($username, $userActiveAttributes); } }); return 0; diff --git a/lib/Listeners/UserChangedListener.php b/lib/Listeners/UserChangedListener.php index 8636ce15..47348f12 100644 --- a/lib/Listeners/UserChangedListener.php +++ b/lib/Listeners/UserChangedListener.php @@ -70,19 +70,19 @@ class UserChangedListener implements IEventListener { if ($feature === 'enabled') { if($value === true && $oldValue === false) { $this->logger->info('Enabling an user', ['event' => $event]); - $userEnableAttributes = [ + $userActiveAttributes = [ 'active' => 'TRUE', 'mailActive' => 'TRUE', ]; - $this->userService->updateAttributesInLDAP($username, $userEnableAttributes); + $this->userService->updateAttributesInLDAP($username, $userActiveAttributes); } if($value === false && $oldValue === true) { $this->logger->info('Disabling an user', ['event' => $event]); - $userEnableAttributes = [ + $userActiveAttributes = [ 'active' => 'FALSE', 'mailActive' => 'FALSE', ]; - $this->userService->updateAttributesInLDAP($username, $userEnableAttributes); + $this->userService->updateAttributesInLDAP($username, $userActiveAttributes); } } } -- GitLab From 7578c01757c015b4929315822a85a1196274fc22 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 17 May 2024 13:46:12 -0700 Subject: [PATCH 05/18] added exception --- lib/Service/UserService.php | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/Service/UserService.php b/lib/Service/UserService.php index fb673a3f..226da8e3 100644 --- a/lib/Service/UserService.php +++ b/lib/Service/UserService.php @@ -522,13 +522,22 @@ class UserService { } public function updateAttributesInLDAP(string $username, array $attributes) { if ($this->LDAPConnectionService->isLDAPEnabled()) { - $conn = $this->LDAPConnectionService->getLDAPConnection(); - $userDn = $this->LDAPConnectionService->username2dn($username); - - if (!ldap_modify($conn, $userDn, $attributes)) { - throw new Exception('Could not modify user entry at LDAP server!'); + try { + $conn = $this->LDAPConnectionService->getLDAPConnection(); + $userDn = $this->LDAPConnectionService->username2dn($username); + + if ($userDn === false) { + throw new Exception('Could not find DN for username: ' . $username); + } + if (!ldap_modify($conn, $userDn, $attributes)) { + throw new Exception('Could not modify user entry at LDAP server!'); + } + $this->LDAPConnectionService->closeLDAPConnection($conn); + } catch (Exception $e) { + // Handle the exception or log it as needed + $this->logger->error('LDAP operation failed', ['exception' => $e]); + throw $e; // Re-throw the exception if you want it to propagate } - $this->LDAPConnectionService->closeLDAPConnection($conn); } } private function getDefaultQuota() { -- GitLab From 430032de5a49f517fbe084adde91c083b49437ec Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 17 May 2024 13:48:54 -0700 Subject: [PATCH 06/18] added exception --- lib/Command/MapActiveAttributetoLDAP.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Command/MapActiveAttributetoLDAP.php b/lib/Command/MapActiveAttributetoLDAP.php index f8cb5a95..9d3720d2 100644 --- a/lib/Command/MapActiveAttributetoLDAP.php +++ b/lib/Command/MapActiveAttributetoLDAP.php @@ -31,6 +31,7 @@ class MapActiveAttributetoLDAP extends Command { protected function execute(InputInterface $input, OutputInterface $output): int { try { + $this->commandOutput = $output; $this->userManager->callForSeenUsers(function (IUser $user) { if ($this->isUserValid($user)) { if ($user->isEnabled()) { -- GitLab From e8b3a13681b81a794c0c5f9e7371a8b2ec252af2 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 17 May 2024 13:53:27 -0700 Subject: [PATCH 07/18] Logger added --- lib/Service/UserService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Service/UserService.php b/lib/Service/UserService.php index 226da8e3..42d45a14 100644 --- a/lib/Service/UserService.php +++ b/lib/Service/UserService.php @@ -527,7 +527,7 @@ class UserService { $userDn = $this->LDAPConnectionService->username2dn($username); if ($userDn === false) { - throw new Exception('Could not find DN for username: ' . $username); + $this->logger->error('Could not find DN for username: ' . $username); } if (!ldap_modify($conn, $userDn, $attributes)) { throw new Exception('Could not modify user entry at LDAP server!'); -- GitLab From 0feb705e2c5abcc89a3a180d4e32b0e94af340b8 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 17 May 2024 13:57:44 -0700 Subject: [PATCH 08/18] Changes in userservice --- lib/Service/UserService.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/Service/UserService.php b/lib/Service/UserService.php index 42d45a14..1454e457 100644 --- a/lib/Service/UserService.php +++ b/lib/Service/UserService.php @@ -528,9 +528,11 @@ class UserService { if ($userDn === false) { $this->logger->error('Could not find DN for username: ' . $username); + return; } if (!ldap_modify($conn, $userDn, $attributes)) { - throw new Exception('Could not modify user entry at LDAP server!'); + $this->logger->error('Could not modify user '.$username.' entry at LDAP server!'); + return; } $this->LDAPConnectionService->closeLDAPConnection($conn); } catch (Exception $e) { -- GitLab From eb4adacac2854e6c487890410aa7af6d0f83ab68 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 17 May 2024 18:09:42 -0700 Subject: [PATCH 09/18] added const --- lib/Listeners/UserChangedListener.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/Listeners/UserChangedListener.php b/lib/Listeners/UserChangedListener.php index 47348f12..0d9ff9cc 100644 --- a/lib/Listeners/UserChangedListener.php +++ b/lib/Listeners/UserChangedListener.php @@ -19,6 +19,8 @@ class UserChangedListener implements IEventListener { private const RECOVERY_EMAIL_FEATURE = 'recovery-email'; + private const ENABLED_FEATURE = 'enabled'; + private $util; private $logger; @@ -67,7 +69,7 @@ class UserChangedListener implements IEventListener { $oldValue = $event->getOldValue(); /** @var mixed $value */ $value = $event->getValue(); - if ($feature === 'enabled') { + if ($feature === self::ENABLED_FEATURE) { if($value === true && $oldValue === false) { $this->logger->info('Enabling an user', ['event' => $event]); $userActiveAttributes = [ -- GitLab From 5f25b851dd4e5dcdd883e7c7becefc3794a146b1 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 17 May 2024 18:22:07 -0700 Subject: [PATCH 10/18] Changed code structure --- lib/Command/MapActiveAttributetoLDAP.php | 14 ++------------ lib/Listeners/UserChangedListener.php | 23 +++-------------------- lib/Service/UserService.php | 7 +++++++ 3 files changed, 12 insertions(+), 32 deletions(-) diff --git a/lib/Command/MapActiveAttributetoLDAP.php b/lib/Command/MapActiveAttributetoLDAP.php index 9d3720d2..7e628725 100644 --- a/lib/Command/MapActiveAttributetoLDAP.php +++ b/lib/Command/MapActiveAttributetoLDAP.php @@ -34,19 +34,9 @@ class MapActiveAttributetoLDAP extends Command { $this->commandOutput = $output; $this->userManager->callForSeenUsers(function (IUser $user) { if ($this->isUserValid($user)) { - if ($user->isEnabled()) { - $userActiveAttributes = [ - 'active' => 'TRUE', - 'mailActive' => 'TRUE', - ]; - } else { - $userActiveAttributes = [ - 'active' => 'FALSE', - 'mailActive' => 'FALSE', - ]; - } $username = $user->getUID(); - $this->userService->updateAttributesInLDAP($username, $userActiveAttributes); + $isEnabled = $user->isEnabled() ? true : false; + $this->userService->mapActiveAttributesInLDAP($username, $isEnabled); } }); return 0; diff --git a/lib/Listeners/UserChangedListener.php b/lib/Listeners/UserChangedListener.php index 0d9ff9cc..98360c90 100644 --- a/lib/Listeners/UserChangedListener.php +++ b/lib/Listeners/UserChangedListener.php @@ -65,27 +65,10 @@ class UserChangedListener implements IEventListener { $this->userService->updateAttributesInLDAP($username, $recoveryEmailAttribute); } - /** @var mixed $oldValue */ - $oldValue = $event->getOldValue(); - /** @var mixed $value */ - $value = $event->getValue(); + /** @var mixed $newValue */ + $newValue = $event->getValue(); if ($feature === self::ENABLED_FEATURE) { - if($value === true && $oldValue === false) { - $this->logger->info('Enabling an user', ['event' => $event]); - $userActiveAttributes = [ - 'active' => 'TRUE', - 'mailActive' => 'TRUE', - ]; - $this->userService->updateAttributesInLDAP($username, $userActiveAttributes); - } - if($value === false && $oldValue === true) { - $this->logger->info('Disabling an user', ['event' => $event]); - $userActiveAttributes = [ - 'active' => 'FALSE', - 'mailActive' => 'FALSE', - ]; - $this->userService->updateAttributesInLDAP($username, $userActiveAttributes); - } + $this->userService->mapActiveAttributesInLDAP($username, $newValue); } } diff --git a/lib/Service/UserService.php b/lib/Service/UserService.php index 1454e457..c443b955 100644 --- a/lib/Service/UserService.php +++ b/lib/Service/UserService.php @@ -520,6 +520,13 @@ class UserService { throw new AddUsernameToCommonStoreException("Error adding username '$username' to common data store."); } } + public function mapActiveAttributesInLDAP(string $username, bool $isEnabled): void { + $userActiveAttributes = [ + 'active' => $isEnabled ? 'TRUE' : 'FALSE', + 'mailActive' => $isEnabled ? 'TRUE' : 'FALSE', + ]; + $this->updateAttributesInLDAP($username, $userActiveAttributes); + } public function updateAttributesInLDAP(string $username, array $attributes) { if ($this->LDAPConnectionService->isLDAPEnabled()) { try { -- GitLab From 4783b7e1e9f95adead5c00894e3e423e73e68549 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 17 May 2024 18:30:05 -0700 Subject: [PATCH 11/18] changes in try catch --- lib/Service/UserService.php | 50 ++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/lib/Service/UserService.php b/lib/Service/UserService.php index c443b955..63951704 100644 --- a/lib/Service/UserService.php +++ b/lib/Service/UserService.php @@ -521,34 +521,38 @@ class UserService { } } public function mapActiveAttributesInLDAP(string $username, bool $isEnabled): void { - $userActiveAttributes = [ + $userActiveAttributes = $this->getActiveAttributes($isEnabled); + try { + $this->updateAttributesInLDAP($username, $userActiveAttributes); + } catch (Exception $e) { + $this->logger->error('Failed to update LDAP attributes for user: ' . $username, ['exception' => $e]); + } + } + private function getActiveAttributes(bool $isEnabled): array { + return [ 'active' => $isEnabled ? 'TRUE' : 'FALSE', 'mailActive' => $isEnabled ? 'TRUE' : 'FALSE', ]; - $this->updateAttributesInLDAP($username, $userActiveAttributes); - } - public function updateAttributesInLDAP(string $username, array $attributes) { - if ($this->LDAPConnectionService->isLDAPEnabled()) { - try { - $conn = $this->LDAPConnectionService->getLDAPConnection(); - $userDn = $this->LDAPConnectionService->username2dn($username); - - if ($userDn === false) { - $this->logger->error('Could not find DN for username: ' . $username); - return; - } - if (!ldap_modify($conn, $userDn, $attributes)) { - $this->logger->error('Could not modify user '.$username.' entry at LDAP server!'); - return; - } - $this->LDAPConnectionService->closeLDAPConnection($conn); - } catch (Exception $e) { - // Handle the exception or log it as needed - $this->logger->error('LDAP operation failed', ['exception' => $e]); - throw $e; // Re-throw the exception if you want it to propagate - } + } + public function updateAttributesInLDAP(string $username, array $attributes): void { + if (!$this->LDAPConnectionService->isLDAPEnabled()) { + return; } + + $conn = $this->LDAPConnectionService->getLDAPConnection(); + $userDn = $this->LDAPConnectionService->username2dn($username); + + if ($userDn === false) { + throw new Exception('Could not find DN for username: ' . $username); + } + + if (!ldap_modify($conn, $userDn, $attributes)) { + throw new Exception('Could not modify user ' . $username . ' entry at LDAP server!'); + } + + $this->LDAPConnectionService->closeLDAPConnection($conn); } + private function getDefaultQuota() { return $this->config->getSystemValueInt('default_quota_in_megabytes', 1024); } -- GitLab From 42a7da1db94e1c93f9b7015d093f80a361f0eef3 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 17 May 2024 18:34:52 -0700 Subject: [PATCH 12/18] added message --- lib/Command/MapActiveAttributetoLDAP.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Command/MapActiveAttributetoLDAP.php b/lib/Command/MapActiveAttributetoLDAP.php index 7e628725..de8a3797 100644 --- a/lib/Command/MapActiveAttributetoLDAP.php +++ b/lib/Command/MapActiveAttributetoLDAP.php @@ -39,6 +39,7 @@ class MapActiveAttributetoLDAP extends Command { $this->userService->mapActiveAttributesInLDAP($username, $isEnabled); } }); + $this->commandOutput->writeln('Active attributes mapped successfully.'); return 0; } catch (\Exception $e) { $this->commandOutput->writeln($e->getMessage()); -- GitLab From fb18af42ba6bab5ac71751507c269dacd8b30475 Mon Sep 17 00:00:00 2001 From: Akhil Date: Fri, 17 May 2024 14:08:50 +0000 Subject: [PATCH 13/18] Apply 2 suggestion(s) to 1 file(s) Co-authored-by: Akhil --- lib/Service/UserService.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Service/UserService.php b/lib/Service/UserService.php index 63951704..3b3b910d 100644 --- a/lib/Service/UserService.php +++ b/lib/Service/UserService.php @@ -525,7 +525,7 @@ class UserService { try { $this->updateAttributesInLDAP($username, $userActiveAttributes); } catch (Exception $e) { - $this->logger->error('Failed to update LDAP attributes for user: ' . $username, ['exception' => $e]); + $this->logger->logException('Failed to update LDAP attributes for user: ' . $username, ['exception' => $e]); } } private function getActiveAttributes(bool $isEnabled): array { @@ -547,7 +547,7 @@ class UserService { } if (!ldap_modify($conn, $userDn, $attributes)) { - throw new Exception('Could not modify user ' . $username . ' entry at LDAP server!'); + throw new Exception('Could not modify user ' . $username . ' entry at LDAP server. Attributes: ' . print_r($attributes, true)); } $this->LDAPConnectionService->closeLDAPConnection($conn); -- GitLab From 9f88c462fd9da2339550dc0d037c70e1fb0d7daa Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 17 May 2024 20:10:08 -0700 Subject: [PATCH 14/18] added try-catch at function level --- lib/Command/MapActiveAttributetoLDAP.php | 12 ++++++++++-- lib/Listeners/UserChangedListener.php | 6 +++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/Command/MapActiveAttributetoLDAP.php b/lib/Command/MapActiveAttributetoLDAP.php index de8a3797..56c26c34 100644 --- a/lib/Command/MapActiveAttributetoLDAP.php +++ b/lib/Command/MapActiveAttributetoLDAP.php @@ -4,8 +4,10 @@ declare(strict_types=1); namespace OCA\EcloudAccounts\Command; +use Exception; use OCA\EcloudAccounts\AppInfo\Application; use OCA\EcloudAccounts\Service\UserService; +use OCP\ILogger; use OCP\IUser; use OCP\IUserManager; use Symfony\Component\Console\Command\Command; @@ -16,10 +18,12 @@ class MapActiveAttributetoLDAP extends Command { private OutputInterface $commandOutput; private IUserManager $userManager; private $userService; + private $logger; - public function __construct(IUserManager $userManager, UserService $userService) { + public function __construct(IUserManager $userManager, ILogger $logger, UserService $userService) { $this->userManager = $userManager; $this->userService = $userService; + $this->logger = $logger; parent::__construct(); } @@ -36,7 +40,11 @@ class MapActiveAttributetoLDAP extends Command { if ($this->isUserValid($user)) { $username = $user->getUID(); $isEnabled = $user->isEnabled() ? true : false; - $this->userService->mapActiveAttributesInLDAP($username, $isEnabled); + try { + $this->userService->mapActiveAttributesInLDAP($username, $isEnabled); + } catch (Exception $e) { + $this->logger->error('Failed to update LDAP attributes for user: ' . $username, ['exception' => $e]); + } } }); $this->commandOutput->writeln('Active attributes mapped successfully.'); diff --git a/lib/Listeners/UserChangedListener.php b/lib/Listeners/UserChangedListener.php index 98360c90..a83238a5 100644 --- a/lib/Listeners/UserChangedListener.php +++ b/lib/Listeners/UserChangedListener.php @@ -68,7 +68,11 @@ class UserChangedListener implements IEventListener { /** @var mixed $newValue */ $newValue = $event->getValue(); if ($feature === self::ENABLED_FEATURE) { - $this->userService->mapActiveAttributesInLDAP($username, $newValue); + try { + $this->userService->mapActiveAttributesInLDAP($username, $newValue); + } catch (Exception $e) { + $this->logger->error('Failed to update LDAP attributes for user: ' . $username, ['exception' => $e]); + } } } -- GitLab From 85a557043396f90c675c417bf7510abd5cf3d737 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 17 May 2024 20:13:52 -0700 Subject: [PATCH 15/18] changes as per suggestions --- lib/Command/MapActiveAttributetoLDAP.php | 31 ++++++++++-------------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/lib/Command/MapActiveAttributetoLDAP.php b/lib/Command/MapActiveAttributetoLDAP.php index 56c26c34..e75e6cd5 100644 --- a/lib/Command/MapActiveAttributetoLDAP.php +++ b/lib/Command/MapActiveAttributetoLDAP.php @@ -34,25 +34,20 @@ class MapActiveAttributetoLDAP extends Command { } protected function execute(InputInterface $input, OutputInterface $output): int { - try { - $this->commandOutput = $output; - $this->userManager->callForSeenUsers(function (IUser $user) { - if ($this->isUserValid($user)) { - $username = $user->getUID(); - $isEnabled = $user->isEnabled() ? true : false; - try { - $this->userService->mapActiveAttributesInLDAP($username, $isEnabled); - } catch (Exception $e) { - $this->logger->error('Failed to update LDAP attributes for user: ' . $username, ['exception' => $e]); - } + $this->commandOutput = $output; + $this->userManager->callForSeenUsers(function (IUser $user) { + if ($this->isUserValid($user)) { + $username = $user->getUID(); + $isEnabled = $user->isEnabled() ? true : false; + try { + $this->userService->mapActiveAttributesInLDAP($username, $isEnabled); + } catch (Exception $e) { + $this->logger->error('Failed to update LDAP attributes for user: ' . $username, ['exception' => $e]); } - }); - $this->commandOutput->writeln('Active attributes mapped successfully.'); - return 0; - } catch (\Exception $e) { - $this->commandOutput->writeln($e->getMessage()); - return 1; - } + } + }); + $this->commandOutput->writeln('Active attributes mapped successfully.'); + return 0; } /** * validate user -- GitLab From 9215e21a1c7288af2a393346a2cd9f985de47efc Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 17 May 2024 20:17:10 -0700 Subject: [PATCH 16/18] changes as per suggestions --- lib/Service/UserService.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/Service/UserService.php b/lib/Service/UserService.php index 3b3b910d..e4d4753c 100644 --- a/lib/Service/UserService.php +++ b/lib/Service/UserService.php @@ -522,11 +522,7 @@ class UserService { } public function mapActiveAttributesInLDAP(string $username, bool $isEnabled): void { $userActiveAttributes = $this->getActiveAttributes($isEnabled); - try { - $this->updateAttributesInLDAP($username, $userActiveAttributes); - } catch (Exception $e) { - $this->logger->logException('Failed to update LDAP attributes for user: ' . $username, ['exception' => $e]); - } + $this->updateAttributesInLDAP($username, $userActiveAttributes); } private function getActiveAttributes(bool $isEnabled): array { return [ -- GitLab From 8d36f5e7aa6b12735460d199b499d66639e00b26 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Mon, 20 May 2024 13:39:28 +0000 Subject: [PATCH 17/18] applying suggestion --- lib/Command/MapActiveAttributetoLDAP.php | 2 +- lib/Listeners/UserChangedListener.php | 2 +- lib/Service/UserService.php | 3 +++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/Command/MapActiveAttributetoLDAP.php b/lib/Command/MapActiveAttributetoLDAP.php index e75e6cd5..6d918d92 100644 --- a/lib/Command/MapActiveAttributetoLDAP.php +++ b/lib/Command/MapActiveAttributetoLDAP.php @@ -42,7 +42,7 @@ class MapActiveAttributetoLDAP extends Command { try { $this->userService->mapActiveAttributesInLDAP($username, $isEnabled); } catch (Exception $e) { - $this->logger->error('Failed to update LDAP attributes for user: ' . $username, ['exception' => $e]); + $this->logger->logException('Failed to update LDAP attributes for user: ' . $username, ['exception' => $e]); } } }); diff --git a/lib/Listeners/UserChangedListener.php b/lib/Listeners/UserChangedListener.php index a83238a5..96e2ab78 100644 --- a/lib/Listeners/UserChangedListener.php +++ b/lib/Listeners/UserChangedListener.php @@ -71,7 +71,7 @@ class UserChangedListener implements IEventListener { try { $this->userService->mapActiveAttributesInLDAP($username, $newValue); } catch (Exception $e) { - $this->logger->error('Failed to update LDAP attributes for user: ' . $username, ['exception' => $e]); + $this->logger->logException('Failed to update LDAP attributes for user: ' . $username, ['exception' => $e]); } } } diff --git a/lib/Service/UserService.php b/lib/Service/UserService.php index e4d4753c..0627035c 100644 --- a/lib/Service/UserService.php +++ b/lib/Service/UserService.php @@ -520,16 +520,19 @@ class UserService { throw new AddUsernameToCommonStoreException("Error adding username '$username' to common data store."); } } + public function mapActiveAttributesInLDAP(string $username, bool $isEnabled): void { $userActiveAttributes = $this->getActiveAttributes($isEnabled); $this->updateAttributesInLDAP($username, $userActiveAttributes); } + private function getActiveAttributes(bool $isEnabled): array { return [ 'active' => $isEnabled ? 'TRUE' : 'FALSE', 'mailActive' => $isEnabled ? 'TRUE' : 'FALSE', ]; } + public function updateAttributesInLDAP(string $username, array $attributes): void { if (!$this->LDAPConnectionService->isLDAPEnabled()) { return; -- GitLab From 811d3686e60f5ddaf69bef87c29667fb217c603b Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Mon, 20 May 2024 19:11:03 -0700 Subject: [PATCH 18/18] grouped var --- lib/Listeners/UserChangedListener.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/Listeners/UserChangedListener.php b/lib/Listeners/UserChangedListener.php index 96e2ab78..7204f3e0 100644 --- a/lib/Listeners/UserChangedListener.php +++ b/lib/Listeners/UserChangedListener.php @@ -47,6 +47,7 @@ class UserChangedListener implements IEventListener { $feature = $event->getFeature(); $user = $event->getUser(); $username = $user->getUID(); + $newValue = $event->getValue(); if ($feature === self::QUOTA_FEATURE) { $updatedQuota = $event->getValue(); @@ -65,8 +66,6 @@ class UserChangedListener implements IEventListener { $this->userService->updateAttributesInLDAP($username, $recoveryEmailAttribute); } - /** @var mixed $newValue */ - $newValue = $event->getValue(); if ($feature === self::ENABLED_FEATURE) { try { $this->userService->mapActiveAttributesInLDAP($username, $newValue); -- GitLab