Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7e0dc3c0 authored by Ronak Patel's avatar Ronak Patel
Browse files

Added changes to call updateAttribute

parent 9dd9bb79
Loading
Loading
Loading
Loading
Loading
+5 −2
Original line number Diff line number Diff line
@@ -6,6 +6,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;
@@ -24,12 +25,14 @@ class UserChangedListener implements IEventListener {
	private $mailboxMapper;

	private $userService;
	private $LDAPConnectionService;

	public function __construct(Util $util, ILogger $logger, MailboxMapper $mailboxMapper, UserService $userService) {
	public function __construct(Util $util, ILogger $logger, MailboxMapper $mailboxMapper, UserService $userService, LDAPConnectionService $LDAPConnectionService) {
		$this->util = $util;
		$this->mailboxMapper = $mailboxMapper;
		$this->logger = $logger;
		$this->userService = $userService;
		$this->LDAPConnectionService = $LDAPConnectionService;
	}

	public function handle(Event $event): void {
@@ -68,7 +71,7 @@ class UserChangedListener implements IEventListener {
				$quotaAttribute = [
					'quota' => $quotaInBytes
				];
				$this->userService->updateAttributesInLDAP($username, $quotaAttribute);
				$this->LDAPConnectionService->updateAttributesInLDAP($username, $quotaAttribute);
			}
		} catch (Exception $e) {
			$this->logger->error("Error setting quota for user $username " . $e->getMessage());
+18 −0
Original line number Diff line number Diff line
@@ -104,4 +104,22 @@ class LDAPConnectionService {
	public function getLdapQuota() {
		return $this->config->getSystemValue('default_quota', '1024');
	}
	public function updateAttributesInLDAP(string $username, array $attributes): void {
		if (!$this->isLDAPEnabled()) {
			return;
		}

		$conn = $this->getLDAPConnection();
		$userDn = $this->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. Attributes: ' . print_r($attributes, true));
		}

		$this->closeLDAPConnection($conn);
	}
}
+1 −21
Original line number Diff line number Diff line
@@ -453,7 +453,7 @@ class UserService {

	public function mapActiveAttributesInLDAP(string $username, bool $isEnabled): void {
		$userActiveAttributes = $this->getActiveAttributes($isEnabled);
		$this->updateAttributesInLDAP($username, $userActiveAttributes);
		$this->LDAPConnectionService->updateAttributesInLDAP($username, $userActiveAttributes);
	}

	private function getActiveAttributes(bool $isEnabled): array {
@@ -462,26 +462,6 @@ class UserService {
			'mailActive' => $isEnabled ? 'TRUE' : 'FALSE',
		];
	}

	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. Attributes: ' . print_r($attributes, true));
		}
	
		$this->LDAPConnectionService->closeLDAPConnection($conn);
	}
	
	private function getDefaultQuota() {
		return $this->config->getSystemValueInt('default_quota_in_megabytes', 1024);
	}