Skip to content

Commit

Permalink
Use the new IProvideEnabledStateBackend interface in user_ldap
Browse files Browse the repository at this point in the history
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
  • Loading branch information
come-nc committed Mar 30, 2023
1 parent 00f3eba commit 41caf05
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 40 deletions.
51 changes: 22 additions & 29 deletions apps/user_ldap/lib/User/DeletedUsersIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,43 +24,35 @@
namespace OCA\User_LDAP\User;

use OCA\User_LDAP\Mapping\UserMapping;
use OCP\IConfig;
use OCP\Share\IManager;

/**
* Class DeletedUsersIndex
* @package OCA\User_LDAP
*/
class DeletedUsersIndex {
/**
* @var \OCP\IConfig $config
*/
protected $config;

/**
* @var \OCA\User_LDAP\Mapping\UserMapping $mapping
*/
protected $mapping;
protected IConfig $config;
protected UserMapping $mapping;
protected ?array $deletedUsers = null;
private IManager $shareManager;

/**
* @var array $deletedUsers
*/
protected $deletedUsers;
/** @var IManager */
private $shareManager;

public function __construct(\OCP\IConfig $config, UserMapping $mapping, IManager $shareManager) {
public function __construct(
IConfig $config,
UserMapping $mapping,
IManager $shareManager
) {
$this->config = $config;
$this->mapping = $mapping;
$this->shareManager = $shareManager;
}

/**
* reads LDAP users marked as deleted from the database
* @return \OCA\User_LDAP\User\OfflineUser[]
* @return OfflineUser[]
*/
private function fetchDeletedUsers() {
$deletedUsers = $this->config->getUsersForUserValue(
'user_ldap', 'isDeleted', '1');
private function fetchDeletedUsers(): array {
$deletedUsers = $this->config->getUsersForUserValue('user_ldap', 'isDeleted', '1');

$userObjects = [];
foreach ($deletedUsers as $user) {
Expand All @@ -73,9 +65,9 @@ private function fetchDeletedUsers() {

/**
* returns all LDAP users that are marked as deleted
* @return \OCA\User_LDAP\User\OfflineUser[]
* @return OfflineUser[]
*/
public function getUsers() {
public function getUsers(): array {
if (is_array($this->deletedUsers)) {
return $this->deletedUsers;
}
Expand All @@ -84,9 +76,8 @@ public function getUsers() {

/**
* whether at least one user was detected as deleted
* @return bool
*/
public function hasUsers() {
public function hasUsers(): bool {
if (!is_array($this->deletedUsers)) {
$this->fetchDeletedUsers();
}
Expand All @@ -96,17 +87,19 @@ public function hasUsers() {
/**
* marks a user as deleted
*
* @param string $ocName
* @throws \OCP\PreConditionNotMetException
*/
public function markUser($ocName) {
$curValue = $this->config->getUserValue($ocName, 'user_ldap', 'isDeleted', '0');
if ($curValue === '1') {
public function markUser(string $ocName): void {
if ($this->isUserMarked($ocName)) {
// the user is already marked, do not write to DB again
return;
}
$this->config->setUserValue($ocName, 'user_ldap', 'isDeleted', '1');
$this->config->setUserValue($ocName, 'user_ldap', 'foundDeleted', (string)time());
$this->deletedUsers = null;
}

public function isUserMarked(string $ocName): bool {
return ($this->config->getUserValue($ocName, 'user_ldap', 'isDeleted', '0') === '1');
}
}
27 changes: 22 additions & 5 deletions apps/user_ldap/lib/User_LDAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use OC\User\Backend;
use OC\User\NoUserException;
use OCA\User_LDAP\Exceptions\NotOnLDAP;
use OCA\User_LDAP\User\DeletedUsersIndex;
use OCA\User_LDAP\User\OfflineUser;
use OCA\User_LDAP\User\User;
use OCP\IConfig;
Expand All @@ -50,10 +51,11 @@
use OCP\Notification\IManager as INotificationManager;
use OCP\User\Backend\ICountMappedUsersBackend;
use OCP\User\Backend\ICountUsersBackend;
use OCP\User\Backend\IProvideEnabledStateBackend;
use OCP\UserInterface;
use Psr\Log\LoggerInterface;

class User_LDAP extends BackendUtility implements IUserBackend, UserInterface, IUserLDAP, ICountUsersBackend, ICountMappedUsersBackend {
class User_LDAP extends BackendUtility implements IUserBackend, UserInterface, IUserLDAP, ICountUsersBackend, ICountMappedUsersBackend, IProvideEnabledStateBackend {
/** @var \OCP\IConfig */
protected $ocConfig;

Expand All @@ -66,6 +68,8 @@ class User_LDAP extends BackendUtility implements IUserBackend, UserInterface, I
/** @var LoggerInterface */
protected $logger;

protected DeletedUsersIndex $deletedUsersIndex;

/**
* @param Access $access
* @param \OCP\IConfig $ocConfig
Expand All @@ -78,6 +82,7 @@ public function __construct(Access $access, IConfig $ocConfig, INotificationMana
$this->notificationManager = $notificationManager;
$this->userPluginManager = $userPluginManager;
$this->logger = \OC::$server->get(LoggerInterface::class);
$this->deletedUsersIndex = \OC::$server->get(DeletedUsersIndex::class);
}

/**
Expand Down Expand Up @@ -392,21 +397,21 @@ public function deleteUser($uid) {
}
}

$marked = (int)$this->ocConfig->getUserValue($uid, 'user_ldap', 'isDeleted', 0);
if ($marked === 0) {
$marked = $this->deletedUsersIndex->isUserMarked($uid);
if (!$marked) {
try {
$user = $this->access->userManager->get($uid);
if (($user instanceof User) && !$this->userExistsOnLDAP($uid, true)) {
$user->markUser();
$marked = 1;
$marked = true;
}
} catch (\Exception $e) {
$this->logger->debug(
$e->getMessage(),
['app' => 'user_ldap', 'exception' => $e]
);
}
if ($marked === 0) {
if (!$marked) {
$this->logger->notice(
'User '.$uid . ' is not marked as deleted, not cleaning up.',
['app' => 'user_ldap']
Expand Down Expand Up @@ -669,4 +674,16 @@ public function createUser($username, $password) {
}
return false;
}

public function isUserEnabled(string $uid, callable $queryDatabaseValue): bool {
if ($this->deletedUsersIndex->isUserMarked($uid) && ($this->ocConfig->getAppValue('user_ldap', 'markRemnantsAsDisabled', '0') === '1')) {
return true;
} else {
return $queryDatabaseValue();
}
}

public function setUserEnabled(string $uid, bool $enabled, callable $queryDatabaseValue, callable $setDatabaseValue): void {
$setDatabaseValue($enabled);
}
}
20 changes: 14 additions & 6 deletions apps/user_ldap/lib/User_Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@
use OCP\IUserBackend;
use OCP\IUserSession;
use OCP\Notification\IManager as INotificationManager;
use OCP\UserInterface;
use OCP\User\Backend\ICountMappedUsersBackend;
use OCP\User\Backend\ICountUsersBackend;
use OCP\UserInterface;
use OCP\User\Backend\IProvideEnabledStateBackend;

class User_Proxy extends Proxy implements IUserBackend, UserInterface, IUserLDAP, ICountUsersBackend, ICountMappedUsersBackend {
/** @var array<string,User_LDAP> */
private $backends = [];
/** @var ?User_LDAP */
private $refBackend = null;
class User_Proxy extends Proxy implements IUserBackend, UserInterface, IUserLDAP, ICountUsersBackend, ICountMappedUsersBackend, IProvideEnabledStateBackend {
/** @var User_LDAP[] */
private array $backends = [];
private ?User_LDAP $refBackend = null;

private bool $isSetUp = false;
private Helper $helper;
Expand Down Expand Up @@ -438,4 +438,12 @@ public function getNewLDAPConnection($uid) {
public function createUser($username, $password) {
return $this->handleRequest($username, 'createUser', [$username, $password]);
}

public function isUserEnabled(string $uid, callable $queryDatabaseValue): bool {
return $this->handleRequest($uid, 'isUserEnabled', [$uid, $queryDatabaseValue]);
}

public function setUserEnabled(string $uid, bool $enabled, callable $queryDatabaseValue, callable $setDatabaseValue): void {
$this->handleRequest($uid, 'setUserEnabled', [$uid, $enabled, $queryDatabaseValue, $setDatabaseValue]);
}
}

0 comments on commit 41caf05

Please sign in to comment.