Passed
Push — master ( 076f6b...c1ddd2 )
by Morris
15:36 queued 04:46
created

OfflineUser::getDetectedOn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Morris Jobke <[email protected]>
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
 */
24
25
namespace OCA\User_LDAP\User;
26
27
use OCA\User_LDAP\Mapping\UserMapping;
28
use OCP\IConfig;
29
use OCP\IDBConnection;
30
31
class OfflineUser {
32
	/**
33
	 * @var string $ocName
34
	 */
35
	protected $ocName;
36
	/**
37
	 * @var string $dn
38
	 */
39
	protected $dn;
40
	/**
41
	 * @var string $uid the UID as provided by LDAP
42
	 */
43
	protected $uid;
44
	/**
45
	 * @var string $displayName
46
	 */
47
	protected $displayName;
48
	/**
49
	 * @var string $homePath
50
	 */
51
	protected $homePath;
52
	/**
53
	 * @var string $lastLogin the timestamp of the last login
54
	 */
55
	protected $lastLogin;
56
	/**
57
	 * @var string $foundDeleted the timestamp when the user was detected as unavailable
58
	 */
59
	protected $foundDeleted;
60
	/**
61
	 * @var string $email
62
	 */
63
	protected $email;
64
	/**
65
	 * @var bool $hasActiveShares
66
	 */
67
	protected $hasActiveShares;
68
	/**
69
	 * @var IConfig $config
70
	 */
71
	protected $config;
72
	/**
73
	 * @var IDBConnection $db
74
	 */
75
	protected $db;
76
	/**
77
	 * @var \OCA\User_LDAP\Mapping\UserMapping
78
	 */
79
	protected $mapping;
80
81
	/**
82
	 * @param string $ocName
83
	 * @param IConfig $config
84
	 * @param IDBConnection $db
85
	 * @param \OCA\User_LDAP\Mapping\UserMapping $mapping
86
	 */
87
	public function __construct($ocName, IConfig $config, IDBConnection $db, UserMapping $mapping) {
88
		$this->ocName = $ocName;
89
		$this->config = $config;
90
		$this->db = $db;
91
		$this->mapping = $mapping;
92
		$this->fetchDetails();
93
	}
94
95
	/**
96
	 * remove the Delete-flag from the user.
97
	 */
98
	public function unmark() {
99
		$this->config->deleteUserValue($this->ocName, 'user_ldap', 'isDeleted');
100
		$this->config->deleteUserValue($this->ocName, 'user_ldap', 'foundDeleted');
101
	}
102
103
	/**
104
	 * exports the user details in an assoc array
105
	 * @return array
106
	 */
107
	public function export() {
108
		$data = array();
109
		$data['ocName'] = $this->getOCName();
110
		$data['dn'] = $this->getDN();
111
		$data['uid'] = $this->getUID();
112
		$data['displayName'] = $this->getDisplayName();
113
		$data['homePath'] = $this->getHomePath();
114
		$data['lastLogin'] = $this->getLastLogin();
115
		$data['email'] = $this->getEmail();
116
		$data['hasActiveShares'] = $this->getHasActiveShares();
117
118
		return $data;
119
	}
120
121
	/**
122
	 * getter for Nextcloud internal name
123
	 * @return string
124
	 */
125
	public function getOCName() {
126
		return $this->ocName;
127
	}
128
129
	/**
130
	 * getter for LDAP uid
131
	 * @return string
132
	 */
133
	public function getUID() {
134
		return $this->uid;
135
	}
136
137
	/**
138
	 * getter for LDAP DN
139
	 * @return string
140
	 */
141
	public function getDN() {
142
		return $this->dn;
143
	}
144
145
	/**
146
	 * getter for display name
147
	 * @return string
148
	 */
149
	public function getDisplayName() {
150
		return $this->displayName;
151
	}
152
153
	/**
154
	 * getter for email
155
	 * @return string
156
	 */
157
	public function getEmail() {
158
		return $this->email;
159
	}
160
161
	/**
162
	 * getter for home directory path
163
	 * @return string
164
	 */
165
	public function getHomePath() {
166
		return $this->homePath;
167
	}
168
169
	/**
170
	 * getter for the last login timestamp
171
	 * @return int
172
	 */
173
	public function getLastLogin() {
174
		return (int)$this->lastLogin;
175
	}
176
177
	/**
178
	 * getter for the detection timestamp
179
	 * @return int
180
	 */
181
	public function getDetectedOn() {
182
		return (int)$this->foundDeleted;
183
	}
184
185
	/**
186
	 * getter for having active shares
187
	 * @return bool
188
	 */
189
	public function getHasActiveShares() {
190
		return $this->hasActiveShares;
191
	}
192
193
	/**
194
	 * reads the user details
195
	 */
196
	protected function fetchDetails() {
197
		$properties = [
198
			'displayName'  => 'user_ldap',
199
			'uid'          => 'user_ldap',
200
			'homePath'     => 'user_ldap',
201
			'foundDeleted' => 'user_ldap',
202
			'email'        => 'settings',
203
			'lastLogin'    => 'login',
204
		];
205
		foreach($properties as $property => $app) {
206
			$this->$property = $this->config->getUserValue($this->ocName, $app, $property, '');
207
		}
208
209
		$dn = $this->mapping->getDNByName($this->ocName);
210
		$this->dn = ($dn !== false) ? $dn : '';
0 ignored issues
show
introduced by
The condition $dn !== false is always true.
Loading history...
211
212
		$this->determineShares();
213
	}
214
215
216
	/**
217
	 * finds out whether the user has active shares. The result is stored in
218
	 * $this->hasActiveShares
219
	 */
220
	protected function determineShares() {
221
		$query = $this->db->prepare('
222
			SELECT COUNT(`uid_owner`)
223
			FROM `*PREFIX*share`
224
			WHERE `uid_owner` = ?
225
		', 1);
226
		$query->execute(array($this->ocName));
227
		$sResult = $query->fetchColumn(0);
228
		if((int)$sResult === 1) {
229
			$this->hasActiveShares = true;
230
			return;
231
		}
232
233
		$query = $this->db->prepare('
234
			SELECT COUNT(`owner`)
235
			FROM `*PREFIX*share_external`
236
			WHERE `owner` = ?
237
		', 1);
238
		$query->execute(array($this->ocName));
239
		$sResult = $query->fetchColumn(0);
240
		if((int)$sResult === 1) {
241
			$this->hasActiveShares = true;
242
			return;
243
		}
244
245
		$this->hasActiveShares = false;
246
	}
247
}
248