Passed
Push — master ( 6741b3...0be754 )
by Blizzz
23:34 queued 12s
created

OfflineUser   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 241
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 85
c 1
b 1
f 0
dl 0
loc 241
rs 10
wmc 30

15 Methods

Rating   Name   Duplication   Size   Complexity  
A getExtStorageHome() 0 5 2
A getEmail() 0 5 2
A getHasActiveShares() 0 5 2
A getDN() 0 6 3
A export() 0 12 1
A fetchDetails() 0 12 2
A unmark() 0 3 1
A getDetectedOn() 0 5 2
A determineShares() 0 24 5
A __construct() 0 10 1
A getOCName() 0 2 1
A getHomePath() 0 5 2
A getUID() 0 5 2
A getLastLogin() 0 5 2
A getDisplayName() 0 5 2
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Roeland Jago Douma <[email protected]>
10
 *
11
 * @license AGPL-3.0
12
 *
13
 * This code is free software: you can redistribute it and/or modify
14
 * it under the terms of the GNU Affero General Public License, version 3,
15
 * as published by the Free Software Foundation.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License, version 3,
23
 * along with this program. If not, see <http://www.gnu.org/licenses/>
24
 *
25
 */
26
namespace OCA\User_LDAP\User;
27
28
use OCA\User_LDAP\Mapping\UserMapping;
29
use OCP\IConfig;
30
use OCP\IDBConnection;
31
use OCP\Share\IManager;
32
use OCP\Share\IShare;
33
34
class OfflineUser {
35
	/**
36
	 * @var string $ocName
37
	 */
38
	protected $ocName;
39
	/**
40
	 * @var string $dn
41
	 */
42
	protected $dn;
43
	/**
44
	 * @var string $uid the UID as provided by LDAP
45
	 */
46
	protected $uid;
47
	/**
48
	 * @var string $displayName
49
	 */
50
	protected $displayName;
51
	/**
52
	 * @var string $homePath
53
	 */
54
	protected $homePath;
55
	/**
56
	 * @var string $lastLogin the timestamp of the last login
57
	 */
58
	protected $lastLogin;
59
	/**
60
	 * @var string $foundDeleted the timestamp when the user was detected as unavailable
61
	 */
62
	protected $foundDeleted;
63
	protected ?string $extStorageHome = null;
64
	/**
65
	 * @var string $email
66
	 */
67
	protected $email;
68
	/**
69
	 * @var bool $hasActiveShares
70
	 */
71
	protected $hasActiveShares;
72
	/**
73
	 * @var IConfig $config
74
	 */
75
	protected $config;
76
	/**
77
	 * @var IDBConnection $db
78
	 */
79
	protected $db;
80
	/**
81
	 * @var \OCA\User_LDAP\Mapping\UserMapping
82
	 */
83
	protected $mapping;
84
	/** @var IManager */
85
	private $shareManager;
86
87
	public function __construct(
88
		$ocName,
89
		IConfig $config,
90
		UserMapping $mapping,
91
		IManager $shareManager
92
	) {
93
		$this->ocName = $ocName;
94
		$this->config = $config;
95
		$this->mapping = $mapping;
96
		$this->shareManager = $shareManager;
97
	}
98
99
	/**
100
	 * remove the Delete-flag from the user.
101
	 */
102
	public function unmark() {
103
		$this->config->deleteUserValue($this->ocName, 'user_ldap', 'isDeleted');
104
		$this->config->deleteUserValue($this->ocName, 'user_ldap', 'foundDeleted');
105
	}
106
107
	/**
108
	 * exports the user details in an assoc array
109
	 * @return array
110
	 */
111
	public function export() {
112
		$data = [];
113
		$data['ocName'] = $this->getOCName();
114
		$data['dn'] = $this->getDN();
115
		$data['uid'] = $this->getUID();
116
		$data['displayName'] = $this->getDisplayName();
117
		$data['homePath'] = $this->getHomePath();
118
		$data['lastLogin'] = $this->getLastLogin();
119
		$data['email'] = $this->getEmail();
120
		$data['hasActiveShares'] = $this->getHasActiveShares();
121
122
		return $data;
123
	}
124
125
	/**
126
	 * getter for Nextcloud internal name
127
	 * @return string
128
	 */
129
	public function getOCName() {
130
		return $this->ocName;
131
	}
132
133
	/**
134
	 * getter for LDAP uid
135
	 * @return string
136
	 */
137
	public function getUID() {
138
		if ($this->uid === null) {
139
			$this->fetchDetails();
140
		}
141
		return $this->uid;
142
	}
143
144
	/**
145
	 * getter for LDAP DN
146
	 * @return string
147
	 */
148
	public function getDN() {
149
		if ($this->dn === null) {
150
			$dn = $this->mapping->getDNByName($this->ocName);
151
			$this->dn = ($dn !== false) ? $dn : '';
0 ignored issues
show
introduced by
The condition $dn !== false is always true.
Loading history...
152
		}
153
		return $this->dn;
154
	}
155
156
	/**
157
	 * getter for display name
158
	 * @return string
159
	 */
160
	public function getDisplayName() {
161
		if ($this->displayName === null) {
162
			$this->fetchDetails();
163
		}
164
		return $this->displayName;
165
	}
166
167
	/**
168
	 * getter for email
169
	 * @return string
170
	 */
171
	public function getEmail() {
172
		if ($this->email === null) {
173
			$this->fetchDetails();
174
		}
175
		return $this->email;
176
	}
177
178
	/**
179
	 * getter for home directory path
180
	 * @return string
181
	 */
182
	public function getHomePath() {
183
		if ($this->homePath === null) {
184
			$this->fetchDetails();
185
		}
186
		return $this->homePath;
187
	}
188
189
	/**
190
	 * getter for the last login timestamp
191
	 * @return int
192
	 */
193
	public function getLastLogin() {
194
		if ($this->lastLogin === null) {
195
			$this->fetchDetails();
196
		}
197
		return (int)$this->lastLogin;
198
	}
199
200
	/**
201
	 * getter for the detection timestamp
202
	 * @return int
203
	 */
204
	public function getDetectedOn() {
205
		if ($this->foundDeleted === null) {
206
			$this->fetchDetails();
207
		}
208
		return (int)$this->foundDeleted;
209
	}
210
211
	public function getExtStorageHome(): string {
212
		if ($this->extStorageHome === null) {
213
			$this->fetchDetails();
214
		}
215
		return (string)$this->extStorageHome;
216
	}
217
218
	/**
219
	 * getter for having active shares
220
	 * @return bool
221
	 */
222
	public function getHasActiveShares() {
223
		if ($this->hasActiveShares === null) {
224
			$this->determineShares();
225
		}
226
		return $this->hasActiveShares;
227
	}
228
229
	/**
230
	 * reads the user details
231
	 */
232
	protected function fetchDetails() {
233
		$properties = [
234
			'displayName' => 'user_ldap',
235
			'uid' => 'user_ldap',
236
			'homePath' => 'user_ldap',
237
			'foundDeleted' => 'user_ldap',
238
			'extStorageHome' => 'user_ldap',
239
			'email' => 'settings',
240
			'lastLogin' => 'login',
241
		];
242
		foreach ($properties as $property => $app) {
243
			$this->$property = $this->config->getUserValue($this->ocName, $app, $property, '');
244
		}
245
	}
246
247
	/**
248
	 * finds out whether the user has active shares. The result is stored in
249
	 * $this->hasActiveShares
250
	 */
251
	protected function determineShares() {
252
		$shareInterface = new \ReflectionClass(IShare::class);
253
		$shareConstants = $shareInterface->getConstants();
254
255
		foreach ($shareConstants as $constantName => $constantValue) {
256
			if (strpos($constantName, 'TYPE_') !== 0
257
				|| $constantValue === IShare::TYPE_USERGROUP
258
			) {
259
				continue;
260
			}
261
			$shares = $this->shareManager->getSharesBy(
262
				$this->ocName,
263
				$constantValue,
264
				null,
265
				false,
266
				1
267
			);
268
			if (!empty($shares)) {
269
				$this->hasActiveShares = true;
270
				return;
271
			}
272
		}
273
274
		$this->hasActiveShares = false;
275
	}
276
}
277