1<?php
2/**
3 * @copyright Copyright (c) 2016, ownCloud, Inc.
4 *
5 * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
6 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
7 * @author Joas Schilling <coding@schilljs.com>
8 *
9 * @license AGPL-3.0
10 *
11 * This code is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Affero General Public License, version 3,
13 * as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
19 *
20 * You should have received a copy of the GNU Affero General Public License, version 3,
21 * along with this program. If not, see <http://www.gnu.org/licenses/>
22 *
23 */
24namespace OCA\User_LDAP\User;
25
26use OCA\User_LDAP\Mapping\UserMapping;
27use OCP\Share\IManager;
28
29/**
30 * Class DeletedUsersIndex
31 * @package OCA\User_LDAP
32 */
33class DeletedUsersIndex {
34	/**
35	 * @var \OCP\IConfig $config
36	 */
37	protected $config;
38
39	/**
40	 * @var \OCA\User_LDAP\Mapping\UserMapping $mapping
41	 */
42	protected $mapping;
43
44	/**
45	 * @var array $deletedUsers
46	 */
47	protected $deletedUsers;
48	/** @var IManager */
49	private $shareManager;
50
51	public function __construct(\OCP\IConfig $config, UserMapping $mapping, IManager $shareManager) {
52		$this->config = $config;
53		$this->mapping = $mapping;
54		$this->shareManager = $shareManager;
55	}
56
57	/**
58	 * reads LDAP users marked as deleted from the database
59	 * @return \OCA\User_LDAP\User\OfflineUser[]
60	 */
61	private function fetchDeletedUsers() {
62		$deletedUsers = $this->config->getUsersForUserValue(
63			'user_ldap', 'isDeleted', '1');
64
65		$userObjects = [];
66		foreach ($deletedUsers as $user) {
67			$userObjects[] = new OfflineUser($user, $this->config, $this->mapping, $this->shareManager);
68		}
69		$this->deletedUsers = $userObjects;
70
71		return $this->deletedUsers;
72	}
73
74	/**
75	 * returns all LDAP users that are marked as deleted
76	 * @return \OCA\User_LDAP\User\OfflineUser[]
77	 */
78	public function getUsers() {
79		if (is_array($this->deletedUsers)) {
80			return $this->deletedUsers;
81		}
82		return $this->fetchDeletedUsers();
83	}
84
85	/**
86	 * whether at least one user was detected as deleted
87	 * @return bool
88	 */
89	public function hasUsers() {
90		if (!is_array($this->deletedUsers)) {
91			$this->fetchDeletedUsers();
92		}
93		return is_array($this->deletedUsers) && (count($this->deletedUsers) > 0);
94	}
95
96	/**
97	 * marks a user as deleted
98	 *
99	 * @param string $ocName
100	 * @throws \OCP\PreConditionNotMetException
101	 */
102	public function markUser($ocName) {
103		$curValue = $this->config->getUserValue($ocName, 'user_ldap', 'isDeleted', '0');
104		if ($curValue === '1') {
105			// the user is already marked, do not write to DB again
106			return;
107		}
108		$this->config->setUserValue($ocName, 'user_ldap', 'isDeleted', '1');
109		$this->config->setUserValue($ocName, 'user_ldap', 'foundDeleted', (string)time());
110		$this->deletedUsers = null;
111	}
112}
113