Skip to content
Prev Previous commit
Next Next commit
feat(user_ldap): Introduce user id assigned typed events for LDAP usage
Based on work from #32019

Signed-off-by: Thomas Citharel <[email protected]>
  • Loading branch information
CarlSchwan authored and come-nc committed Sep 10, 2024
commit 986a3d45f8fab60c8e7dddc7015bd60c6ee356d4
15 changes: 12 additions & 3 deletions apps/user_ldap/ajax/clearMappings.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/

use OCA\User_LDAP\Mapping\GroupMapping;
use OCA\User_LDAP\Mapping\UserMapping;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Server;
use OCP\User\Events\BeforeUserIdUnassignedEvent;
use OCP\User\Events\UserIdUnassignedEvent;

// Check user and app status
\OC_JSON::checkAdminUser();
Expand All @@ -17,12 +22,16 @@
$mapping = null;
try {
if ($subject === 'user') {
$mapping = \OCP\Server::get(UserMapping::class);
$mapping = Server::get(UserMapping::class);
/** @var IEventDispatcher $dispatcher */
$dispatcher = Server::get(IEventDispatcher::class);
$result = $mapping->clearCb(
function ($uid) {
function (string $uid) use ($dispatcher): void {
$dispatcher->dispatchTyped(new BeforeUserIdUnassignedEvent($uid));
\OC::$server->getUserManager()->emit('\OC\User', 'preUnassignedUserId', [$uid]);
},
function ($uid) {
function (string $uid) use ($dispatcher): void {
$dispatcher->dispatchTyped(new UserIdUnassignedEvent($uid));
\OC::$server->getUserManager()->emit('\OC\User', 'postUnassignedUserId', [$uid]);
}
);
Expand Down
19 changes: 10 additions & 9 deletions apps/user_ldap/lib/Access.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
use OCA\User_LDAP\Mapping\AbstractMapping;
use OCA\User_LDAP\User\Manager;
use OCA\User_LDAP\User\OfflineUser;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\HintException;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IUserManager;
use OCP\User\Events\UserIdAssignedEvent;
use Psr\Log\LoggerInterface;
use function strlen;
use function substr;
Expand All @@ -42,6 +44,7 @@ class Access extends LDAPUtility {

/** @var ?AbstractMapping */
protected $groupMapper;

private string $lastCookie = '';

public function __construct(
Expand All @@ -53,15 +56,10 @@ public function __construct(
private IUserManager $ncUserManager,
private LoggerInterface $logger,
private IAppConfig $appConfig,
private IEventDispatcher $dispatcher,
) {
parent::__construct($ldap);
$this->connection = $connection;
$this->userManager = $userManager;
$this->userManager->setLdapAccess($this);
$this->helper = $helper;
$this->config = $config;
$this->ncUserManager = $ncUserManager;
$this->logger = $logger;
}

/**
Expand Down Expand Up @@ -630,10 +628,13 @@ public function mapAndAnnounceIfApplicable(
bool $isUser
): bool {
if ($mapper->map($fdn, $name, $uuid)) {
if ($this->ncUserManager instanceof PublicEmitter && $isUser) {
if ($isUser) {
$this->cacheUserExists($name);
$this->ncUserManager->emit('\OC\User', 'assignedUserId', [$name]);
} elseif (!$isUser) {
$this->dispatcher->dispatchTyped(new UserIdAssignedEvent($name));
if ($this->ncUserManager instanceof PublicEmitter) {
$this->ncUserManager->emit('\OC\User', 'assignedUserId', [$name]);

Check notice

Code scanning / Psalm

DeprecatedMethod

The method OC\Hooks\PublicEmitter::emit has been marked as deprecated
}
} else {
$this->cacheGroupExists($name);
}
return true;
Expand Down
8 changes: 3 additions & 5 deletions apps/user_ldap/lib/AccessFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace OCA\User_LDAP;

use OCA\User_LDAP\User\Manager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IUserManager;
Expand All @@ -21,12 +22,8 @@ public function __construct(
private IAppConfig $appConfig,
private IUserManager $ncUserManager,
private LoggerInterface $logger,
private IEventDispatcher $dispatcher,
) {
$this->ldap = $ldap;
$this->helper = $helper;
$this->config = $config;
$this->ncUserManager = $ncUserManager;
$this->logger = $logger;
}

public function get(Connection $connection): Access {
Expand All @@ -40,6 +37,7 @@ public function get(Connection $connection): Access {
$this->ncUserManager,
$this->logger,
$this->appConfig,
$this->dispatcher,
);
}
}
Loading