Completed
Push — stable8.2 ( b4bbd4...3350d6 )
by
unknown
59:02
created

Manager::getAttributes()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 5.0187

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
ccs 20
cts 22
cp 0.9091
rs 8.439
cc 5
eloc 18
nc 12
nop 1
crap 5.0187
1
<?php
2
/**
3
 * @author Arthur Schiwon <[email protected]>
4
 * @author Jörn Friedrich Dreyer <[email protected]>
5
 * @author Morris Jobke <[email protected]>
6
 *
7
 * @copyright Copyright (c) 2015, ownCloud, Inc.
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OCA\user_ldap\lib\user;
25
26
use OCA\user_ldap\lib\user\IUserTools;
27
use OCA\user_ldap\lib\user\User;
28
use OCA\user_ldap\lib\LogWrapper;
29
use OCA\user_ldap\lib\FilesystemHelper;
30
use OCA\user_ldap\lib\user\OfflineUser;
31
32
/**
33
 * Manager
34
 *
35
 * upon request, returns an LDAP user object either by creating or from run-time
36
 * cache
37
 */
38
class Manager {
39
	/** @var IUserTools */
40
	protected $access;
41
42
	/** @var \OCP\IConfig */
43
	protected $ocConfig;
44
45
	/** @var \OCP\IDBConnection */
46
	protected $db;
47
48
	/** @var FilesystemHelper */
49
	protected $ocFilesystem;
50
51
	/** @var LogWrapper */
52
	protected $ocLog;
53
54
	/** @var \OCP\Image */
55
	protected $image;
56
57
	/** @param \OCP\IAvatarManager */
58
	protected $avatarManager;
59
60
	/**
61
	 * array['byDN']	\OCA\user_ldap\lib\User[]
62
	 * 	['byUid']	\OCA\user_ldap\lib\User[]
63
	 * @var array $users
64
	 */
65
	protected $users = array(
66
		'byDN'  => array(),
67
		'byUid' => array(),
68
	);
69
70
	/**
71
	 * @param \OCP\IConfig $ocConfig
72
	 * @param \OCA\user_ldap\lib\FilesystemHelper $ocFilesystem object that
73
	 * gives access to necessary functions from the OC filesystem
74
	 * @param  \OCA\user_ldap\lib\LogWrapper $ocLog
75
	 * @param \OCP\IAvatarManager $avatarManager
76
	 * @param \OCP\Image $image an empty image instance
77
	 * @param \OCP\IDBConnection $db
78
	 * @throws Exception when the methods mentioned above do not exist
79
	 */
80 78
	public function __construct(\OCP\IConfig $ocConfig,
81
		FilesystemHelper $ocFilesystem, LogWrapper $ocLog,
82
		\OCP\IAvatarManager $avatarManager, \OCP\Image $image, \OCP\IDBConnection $db) {
83
84 78
		$this->ocConfig      = $ocConfig;
85 78
		$this->ocFilesystem  = $ocFilesystem;
86 78
		$this->ocLog         = $ocLog;
87 78
		$this->avatarManager = $avatarManager;
88 78
		$this->image         = $image;
89 78
		$this->db            = $db;
90 78
	}
91
92
	/**
93
	 * @brief binds manager to an instance of IUserTools (implemented by
94
	 * Access). It needs to be assigned first before the manager can be used.
95
	 * @param IUserTools
96
	 */
97 42
	public function setLdapAccess(IUserTools $access) {
98 42
		$this->access = $access;
99 42
	}
100
101
	/**
102
	 * @brief creates an instance of User and caches (just runtime) it in the
103
	 * property array
104
	 * @param string the DN of the user
105
	 * @param string the internal (owncloud) username
106
	 * @return \OCA\user_ldap\lib\User
107
	 */
108 17
	private function createAndCache($dn, $uid) {
109 17
		$this->checkAccess();
110 17
		$user = new User($uid, $dn, $this->access, $this->ocConfig,
111 17
			$this->ocFilesystem, clone $this->image, $this->ocLog,
112 17
			$this->avatarManager);
113 17
		$this->users['byDN'][$dn]   = $user;
114 17
		$this->users['byUid'][$uid] = $user;
115 17
		return $user;
116
	}
117
118
	/**
119
	 * @brief checks whether the Access instance has been set
120
	 * @throws Exception if Access has not been set
121
	 * @return null
122
	 */
123 23
	private function checkAccess() {
124 23
		if(is_null($this->access)) {
125
			throw new \Exception('LDAP Access instance must be set first');
126
		}
127 23
	}
128
129
	/**
130
	 * returns a list of attributes that will be processed further, e.g. quota,
131
	 * email, displayname, or others.
132
	 * @param bool $minimal - optional, set to true to skip attributes with big
133
	 * payload
134
	 * @return string[]
135
	 */
136 19
	public function getAttributes($minimal = false) {
137 19
		$attributes = array('dn', 'uid', 'samaccountname', 'memberof');
138
		$possible = array(
139 19
			$this->access->getConnection()->ldapQuotaAttribute,
140 19
			$this->access->getConnection()->ldapEmailAttribute,
141 19
			$this->access->getConnection()->ldapUserDisplayName,
142 19
			$this->access->getConnection()->ldapUserDisplayName2,
143 19
		);
144 19
		foreach($possible as $attr) {
145 19
			if(!is_null($attr)) {
146 1
				$attributes[] = $attr;
147 1
			}
148 19
		}
149
150 19
		$homeRule = $this->access->getConnection()->homeFolderNamingRule;
151 19
		if(strpos($homeRule, 'attr:') === 0) {
152
			$attributes[] = substr($homeRule, strlen('attr:'));
153
		}
154
155 19
		if(!$minimal) {
156
			// attributes that are not really important but may come with big
157
			// payload.
158 8
			$attributes = array_merge($attributes, array(
159 8
				'jpegphoto',
160
				'thumbnailphoto'
161 8
			));
162 8
		}
163
164 19
		return $attributes;
165
	}
166
167
	/**
168
	 * Checks whether the specified user is marked as deleted
169
	 * @param string $id the ownCloud user name
170
	 * @return bool
171
	 */
172 16
	public function isDeletedUser($id) {
173 16
		$isDeleted = $this->ocConfig->getUserValue(
174 16
			$id, 'user_ldap', 'isDeleted', 0);
175 16
		return intval($isDeleted) === 1;
176
	}
177
178
	/**
179
	 * creates and returns an instance of OfflineUser for the specified user
180
	 * @param string $id
181
	 * @return \OCA\user_ldap\lib\user\OfflineUser
182
	 */
183
	public function getDeletedUser($id) {
184
		return new OfflineUser(
185
			$id,
186
			$this->ocConfig,
187
			$this->db,
188
			$this->access->getUserMapper());
189
	}
190
191
	/**
192
	 * @brief returns a User object by it's ownCloud username
193
	 * @param string the DN or username of the user
194
	 * @return \OCA\user_ldap\lib\user\User|\OCA\user_ldap\lib\user\OfflineUser|null
195
	 */
196 16
	protected function createInstancyByUserName($id) {
197
		//most likely a uid. Check whether it is a deleted user
198 16
		if($this->isDeletedUser($id)) {
199 1
			return $this->getDeletedUser($id);
200
		}
201 15
		$dn = $this->access->username2dn($id);
202 15
		if($dn !== false) {
203 10
			return $this->createAndCache($dn, $id);
204
		}
205 5
		return null;
206
	}
207
208
	/**
209
	 * @brief returns a User object by it's DN or ownCloud username
210
	 * @param string the DN or username of the user
211
	 * @return \OCA\user_ldap\lib\user\User|\OCA\user_ldap\lib\user\OfflineUser|null
212
	 * @throws \Exception when connection could not be established
213
	 */
214 23
	public function get($id) {
215 23
		$this->checkAccess();
216 23
		if(isset($this->users['byDN'][$id])) {
217 1
			return $this->users['byDN'][$id];
218 23
		} else if(isset($this->users['byUid'][$id])) {
219 5
			return $this->users['byUid'][$id];
220
		}
221
222 23
		if($this->access->stringResemblesDN($id) ) {
223 9
			$uid = $this->access->dn2username($id);
224 9
			if($uid !== false) {
225 7
				return $this->createAndCache($id, $uid);
226
			}
227 2
		}
228
229 16
		return $this->createInstancyByUserName($id);
230
	}
231
232
}
233