-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathOfflineUser.php
More file actions
200 lines (178 loc) · 4.92 KB
/
Copy pathOfflineUser.php
File metadata and controls
200 lines (178 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/**
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\User_LDAP\User;
use OCA\User_LDAP\Mapping\AbstractMapping;
use OCP\Config\IUserConfig;
use OCP\Share\IManager;
use OCP\Share\IShare;
class OfflineUser {
protected ?string $dn = null;
/** @var ?string $uid the UID as provided by LDAP */
protected ?string $uid = null;
protected ?string $displayName = null;
protected ?string $homePath = null;
/** @var ?int $lastLogin the timestamp of the last login */
protected ?int $lastLogin = null;
/** @var ?int $foundDeleted the timestamp when the user was detected as unavailable */
protected ?int $foundDeleted = null;
protected ?string $extStorageHome = null;
protected ?string $email = null;
protected ?bool $hasActiveShares = null;
public function __construct(
protected string $ocName,
protected IUserConfig $userConfig,
protected AbstractMapping $mapping,
private IManager $shareManager,
) {
}
/**
* Remove the Delete-flag from the user.
*/
public function unmark(): void {
$this->userConfig->deleteUserConfig($this->ocName, 'user_ldap', 'isDeleted');
$this->userConfig->deleteUserConfig($this->ocName, 'user_ldap', 'foundDeleted');
}
/**
* Exports the user details in an assoc array.
*/
public function export(): array {
$data = [];
$data['ocName'] = $this->getOCName();
$data['dn'] = $this->getDN();
$data['uid'] = $this->getUID();
$data['displayName'] = $this->getDisplayName();
$data['homePath'] = $this->getHomePath();
$data['lastLogin'] = $this->getLastLogin();
$data['email'] = $this->getEmail();
$data['hasActiveShares'] = $this->getHasActiveShares();
return $data;
}
/**
* Getter for Nextcloud internal name.
*/
public function getOCName(): string {
return $this->ocName;
}
/**
* Getter for LDAP uid.
*/
public function getUID(): string {
if ($this->uid === null) {
$this->fetchDetails();
}
return $this->uid ?? '';
}
/**
* Getter for LDAP DN.
*/
public function getDN(): string {
if ($this->dn === null) {
$dn = $this->mapping->getDNByName($this->ocName);
$this->dn = ($dn !== false) ? $dn : '';
}
return $this->dn;
}
/**
* Getter for display name.
*/
public function getDisplayName(): string {
if ($this->displayName === null) {
$this->fetchDetails();
}
return $this->displayName ?? '';
}
/**
* Getter for email.
*/
public function getEmail(): string {
if ($this->email === null) {
$this->fetchDetails();
}
return $this->email ?? '';
}
/**
* Getter for home directory path.
*/
public function getHomePath(): string {
if ($this->homePath === null) {
$this->fetchDetails();
}
return $this->homePath ?? '';
}
/**
* Getter for the last login timestamp.
*/
public function getLastLogin(): int {
if ($this->lastLogin === null) {
$this->fetchDetails();
}
return $this->lastLogin ?? -1;
}
/**
* Getter for the detection timestamp.
*/
public function getDetectedOn(): int {
if ($this->foundDeleted === null) {
$this->fetchDetails();
}
return $this->foundDeleted ?? -1;
}
public function getExtStorageHome(): string {
if ($this->extStorageHome === null) {
$this->fetchDetails();
}
return $this->extStorageHome ?? '';
}
/**
* Getter for having active shares.
*/
public function getHasActiveShares(): bool {
if ($this->hasActiveShares === null) {
$this->determineShares();
}
return $this->hasActiveShares ?? false;
}
/**
* Reads the user details.
*/
protected function fetchDetails(): void {
$this->displayName = $this->userConfig->getValueString($this->ocName, 'user_ldap', 'displayName');
$this->uid = $this->userConfig->getValueString($this->ocName, 'user_ldap', 'uid');
$this->homePath = $this->userConfig->getValueString($this->ocName, 'user_ldap', 'homePath');
$this->foundDeleted = $this->userConfig->getValueInt($this->ocName, 'user_ldap', 'foundDeleted');
$this->extStorageHome = $this->userConfig->getValueString($this->ocName, 'user_ldap', 'extStorageHome');
$this->email = $this->userConfig->getValueString($this->ocName, 'user_ldap', 'email');
$this->lastLogin = $this->userConfig->getValueInt($this->ocName, 'login', 'lastLogin');
}
/**
* Finds out whether the user has active shares. The result is stored in
* $this->hasActiveShares.
*/
protected function determineShares(): void {
$shareInterface = new \ReflectionClass(IShare::class);
$shareConstants = $shareInterface->getConstants();
foreach ($shareConstants as $constantName => $constantValue) {
if (!str_starts_with($constantName, 'TYPE_')
|| $constantValue === IShare::TYPE_USERGROUP
) {
continue;
}
$shares = $this->shareManager->getSharesBy(
$this->ocName,
$constantValue,
null,
false,
1
);
if (!empty($shares)) {
$this->hasActiveShares = true;
return;
}
}
$this->hasActiveShares = false;
}
}