2014-08-21 11:59:13 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
2016-07-21 10:49:16 -04:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
|
*
|
2016-05-26 13:56:05 -04:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2020-04-29 05:57:22 -04:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-07-21 10:49:16 -04:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2014-08-21 11:59:13 -04:00
|
|
|
*
|
2015-03-26 06:44:34 -04:00
|
|
|
* @license AGPL-3.0
|
2014-08-21 11:59:13 -04:00
|
|
|
*
|
2015-03-26 06:44:34 -04:00
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
|
* as published by the Free Software Foundation.
|
2014-08-21 11:59:13 -04:00
|
|
|
*
|
2015-03-26 06:44:34 -04:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2014-08-21 11:59:13 -04:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 06:44:34 -04:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU Affero General Public License for more details.
|
2014-08-21 11:59:13 -04:00
|
|
|
*
|
2015-03-26 06:44:34 -04:00
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 13:57:53 -05:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2014-08-21 11:59:13 -04:00
|
|
|
*
|
|
|
|
|
*/
|
2016-05-12 05:25:50 -04:00
|
|
|
namespace OCA\User_LDAP\User;
|
2014-08-21 11:59:13 -04:00
|
|
|
|
2014-12-20 10:09:04 -05:00
|
|
|
use OCA\User_LDAP\Mapping\UserMapping;
|
2022-10-05 11:58:56 -04:00
|
|
|
use OCP\IConfig;
|
2020-10-26 09:44:15 -04:00
|
|
|
use OCP\Share\IManager;
|
2014-08-21 11:59:13 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class DeletedUsersIndex
|
|
|
|
|
* @package OCA\User_LDAP
|
|
|
|
|
*/
|
|
|
|
|
class DeletedUsersIndex {
|
2022-10-05 11:58:56 -04:00
|
|
|
protected IConfig $config;
|
|
|
|
|
protected UserMapping $mapping;
|
|
|
|
|
protected ?array $deletedUsers = null;
|
|
|
|
|
private IManager $shareManager;
|
2014-08-21 11:59:13 -04:00
|
|
|
|
2022-10-05 11:58:56 -04:00
|
|
|
public function __construct(
|
|
|
|
|
IConfig $config,
|
|
|
|
|
UserMapping $mapping,
|
|
|
|
|
IManager $shareManager
|
|
|
|
|
) {
|
2014-12-20 10:33:37 -05:00
|
|
|
$this->config = $config;
|
2014-12-20 10:09:04 -05:00
|
|
|
$this->mapping = $mapping;
|
2020-10-26 09:44:15 -04:00
|
|
|
$this->shareManager = $shareManager;
|
2014-08-21 11:59:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* reads LDAP users marked as deleted from the database
|
2022-10-05 11:58:56 -04:00
|
|
|
* @return OfflineUser[]
|
2014-08-21 11:59:13 -04:00
|
|
|
*/
|
2022-10-05 11:58:56 -04:00
|
|
|
private function fetchDeletedUsers(): array {
|
|
|
|
|
$deletedUsers = $this->config->getUsersForUserValue('user_ldap', 'isDeleted', '1');
|
2014-08-21 11:59:13 -04:00
|
|
|
|
2018-12-17 08:55:00 -05:00
|
|
|
$userObjects = [];
|
2020-04-10 08:19:56 -04:00
|
|
|
foreach ($deletedUsers as $user) {
|
2024-05-02 11:28:29 -04:00
|
|
|
$userObject = new OfflineUser($user, $this->config, $this->mapping, $this->shareManager);
|
2023-03-01 11:22:21 -05:00
|
|
|
if ($userObject->getLastLogin() > $userObject->getDetectedOn()) {
|
|
|
|
|
$userObject->unmark();
|
|
|
|
|
} else {
|
|
|
|
|
$userObjects[] = $userObject;
|
|
|
|
|
}
|
2014-08-21 11:59:13 -04:00
|
|
|
}
|
2014-12-20 11:08:26 -05:00
|
|
|
$this->deletedUsers = $userObjects;
|
2014-08-21 11:59:13 -04:00
|
|
|
|
2014-12-20 10:33:37 -05:00
|
|
|
return $this->deletedUsers;
|
2014-08-21 11:59:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* returns all LDAP users that are marked as deleted
|
2022-10-05 11:58:56 -04:00
|
|
|
* @return OfflineUser[]
|
2014-08-21 11:59:13 -04:00
|
|
|
*/
|
2022-10-05 11:58:56 -04:00
|
|
|
public function getUsers(): array {
|
2020-04-10 08:19:56 -04:00
|
|
|
if (is_array($this->deletedUsers)) {
|
2014-12-20 10:33:37 -05:00
|
|
|
return $this->deletedUsers;
|
2014-08-21 11:59:13 -04:00
|
|
|
}
|
2014-12-20 10:33:37 -05:00
|
|
|
return $this->fetchDeletedUsers();
|
2014-08-21 11:59:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* whether at least one user was detected as deleted
|
|
|
|
|
*/
|
2022-10-05 11:58:56 -04:00
|
|
|
public function hasUsers(): bool {
|
2020-04-10 08:19:56 -04:00
|
|
|
if (!is_array($this->deletedUsers)) {
|
2014-12-20 10:33:37 -05:00
|
|
|
$this->fetchDeletedUsers();
|
2014-08-21 11:59:13 -04:00
|
|
|
}
|
2018-12-17 08:55:00 -05:00
|
|
|
return is_array($this->deletedUsers) && (count($this->deletedUsers) > 0);
|
2014-08-21 11:59:13 -04:00
|
|
|
}
|
2014-12-20 11:08:26 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* marks a user as deleted
|
2018-12-17 08:55:00 -05:00
|
|
|
*
|
|
|
|
|
* @throws \OCP\PreConditionNotMetException
|
2014-12-20 11:08:26 -05:00
|
|
|
*/
|
2022-10-05 11:58:56 -04:00
|
|
|
public function markUser(string $ocName): void {
|
|
|
|
|
if ($this->isUserMarked($ocName)) {
|
2018-12-17 11:37:45 -05:00
|
|
|
// the user is already marked, do not write to DB again
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-12-20 11:08:26 -05:00
|
|
|
$this->config->setUserValue($ocName, 'user_ldap', 'isDeleted', '1');
|
2018-12-17 11:37:45 -05:00
|
|
|
$this->config->setUserValue($ocName, 'user_ldap', 'foundDeleted', (string)time());
|
2018-12-17 08:55:00 -05:00
|
|
|
$this->deletedUsers = null;
|
2014-12-20 11:08:26 -05:00
|
|
|
}
|
2022-10-05 11:58:56 -04:00
|
|
|
|
|
|
|
|
public function isUserMarked(string $ocName): bool {
|
|
|
|
|
return ($this->config->getUserValue($ocName, 'user_ldap', 'isDeleted', '0') === '1');
|
|
|
|
|
}
|
2014-08-21 11:59:13 -04:00
|
|
|
}
|