Skip to content

Commit

Permalink
Introduce user id assigned typed events for LDAP usage
Browse files Browse the repository at this point in the history
Based on work from #32019

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
  • Loading branch information
CarlSchwan authored and tcitworld committed May 2, 2023
1 parent 73291ba commit a047c14
Show file tree
Hide file tree
Showing 11 changed files with 341 additions and 223 deletions.
14 changes: 11 additions & 3 deletions apps/user_ldap/ajax/clearMappings.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
*/
use OCA\User_LDAP\Mapping\UserMapping;
use OCA\User_LDAP\Mapping\GroupMapping;
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 @@ -35,12 +39,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
47 changes: 20 additions & 27 deletions apps/user_ldap/lib/Access.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
use OCA\User_LDAP\Mapping\AbstractMapping;
use OCA\User_LDAP\User\Manager;
use OCA\User_LDAP\User\OfflineUser;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\User\Events\UserIdAssignedEvent;
use OCP\HintException;
use OCP\IConfig;
use OCP\IUserManager;
Expand All @@ -69,32 +71,18 @@
class Access extends LDAPUtility {
public const UUID_ATTRIBUTES = ['entryuuid', 'nsuniqueid', 'objectguid', 'guid', 'ipauniqueid'];

/** @var \OCA\User_LDAP\Connection */
public $connection;
/** @var Manager */
public $userManager;
/**
* never ever check this var directly, always use getPagedSearchResultState
* @var ?bool
*/
protected $pagedSearchedSuccessful;

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

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

/**
* @var \OCA\User_LDAP\Helper
*/
private $helper;
/** @var IConfig */
private $config;
/** @var IUserManager */
private $ncUserManager;
/** @var LoggerInterface */
private $logger;
public Connection $connection;
public Manager $userManager;
/** never ever check this var directly, always use getPagedSearchResultState */
protected ?bool $pagedSearchedSuccessful;
protected ?AbstractMapping $userMapper;
protected ?AbstractMapping $groupMapper;

private Helper $helper;
private IConfig $config;
private IUserManager $ncUserManager;
private LoggerInterface $logger;
private IEventDispatcher $dispatcher;
private string $lastCookie = '';

public function __construct(
Expand All @@ -104,7 +92,8 @@ public function __construct(
Helper $helper,
IConfig $config,
IUserManager $ncUserManager,
LoggerInterface $logger
LoggerInterface $logger,
IEventDispatcher $dispatcher
) {
parent::__construct($ldap);
$this->connection = $connection;
Expand All @@ -114,6 +103,7 @@ public function __construct(
$this->config = $config;
$this->ncUserManager = $ncUserManager;
$this->logger = $logger;
$this->dispatcher = $dispatcher;
}

/**
Expand Down Expand Up @@ -616,6 +606,9 @@ public function mapAndAnnounceIfApplicable(
bool $isUser
): bool {
if ($mapper->map($fdn, $name, $uuid)) {
if ($isUser) {
$this->dispatcher->dispatchTyped(new UserIdAssignedEvent($name));
}
if ($this->ncUserManager instanceof PublicEmitter && $isUser) {
$this->cacheUserExists($name);
$this->ncUserManager->emit('\OC\User', 'assignedUserId', [$name]);
Expand Down
9 changes: 7 additions & 2 deletions apps/user_ldap/lib/AccessFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace OCA\User_LDAP;

use OCA\User_LDAP\User\Manager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IUserManager;
use Psr\Log\LoggerInterface;
Expand All @@ -35,20 +36,23 @@ class AccessFactory {
private IConfig $config;
private IUserManager $ncUserManager;
private LoggerInterface $logger;
private IEventDispatcher $dispatcher;

public function __construct(
ILDAPWrapper $ldap,
Manager $userManager,
Helper $helper,
IConfig $config,
IUserManager $ncUserManager,
LoggerInterface $logger) {
LoggerInterface $logger,
IEventDispatcher $dispatcher) {
$this->ldap = $ldap;
$this->userManager = $userManager;
$this->helper = $helper;
$this->config = $config;
$this->ncUserManager = $ncUserManager;
$this->logger = $logger;
$this->dispatcher = $dispatcher;
}

public function get(Connection $connection): Access {
Expand All @@ -59,7 +63,8 @@ public function get(Connection $connection): Access {
$this->helper,
$this->config,
$this->ncUserManager,
$this->logger
$this->logger,
$this->dispatcher
);
}
}
Loading

0 comments on commit a047c14

Please sign in to comment.