Completed
Push — master ( abdf8c...733110 )
by Blizzz
10:14
created

LDAPProvider::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 13
rs 9.4285
1
<?php
2
/**
3
 *
4
 * @copyright Copyright (c) 2016, Roger Szabo ([email protected])
5
 *
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
namespace OCA\User_LDAP;
24
25
use OCP\IUserBackend;
26
use OCP\LDAP\ILDAPProvider;
27
use OCP\LDAP\IDeletionFlagSupport;
28
use OCP\IServerContainer;
29
use OCA\User_LDAP\User\DeletedUsersIndex;
30
use OCA\User_LDAP\Mapping\UserMapping;
31
32
/**
33
 * LDAP provider for pulic access to the LDAP backend.
34
 */
35
class LDAPProvider implements ILDAPProvider, IDeletionFlagSupport {
36
37
	private $backend;
38
	private $logger;
39
	private $helper;
40
	private $deletedUsersIndex;
41
	
42
	/**
43
	 * Create new LDAPProvider
44
	 * @param \OCP\IServerContainer $serverContainer
45
	 * @throws \Exception if user_ldap app was not enabled
46
	 */
47
	public function __construct(IServerContainer $serverContainer, Helper $helper, DeletedUsersIndex $deletedUsersIndex) {
48
		$this->logger = $serverContainer->getLogger();
49
		$this->helper = $helper;
50
		$this->deletedUsersIndex = $deletedUsersIndex;
51
		foreach ($serverContainer->getUserManager()->getBackends() as $backend){
52
			$this->logger->debug('instance '.get_class($backend).' backend.', ['app' => 'user_ldap']);
53
			if ($backend instanceof IUserLDAP) {
54
				$this->backend = $backend;
55
				return;
56
			}
57
        }
58
		throw new \Exception('To use the LDAPProvider, user_ldap app must be enabled');
59
	}
60
	
61
	/**
62
	 * Translate an user id to LDAP DN
63
	 * @param string $uid user id
64
	 * @return string with the LDAP DN
65
	 * @throws \Exception if translation was unsuccessful
66
	 */
67
	public function getUserDN($uid) {
68
		if(!$this->backend->userExists($uid)){
69
			throw new \Exception('User id not found in LDAP');
70
		}
71
		$result = $this->backend->getLDAPAccess($uid)->username2dn($uid);
72
		if(!$result){
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
73
			throw new \Exception('Translation to LDAP DN unsuccessful');
74
		}
75
		return $result;
76
	}
77
	
78
	/**
79
	 * Translate a LDAP DN to an internal user name. If there is no mapping between 
80
	 * the DN and the user name, a new one will be created.
81
	 * @param string $dn LDAP DN
82
	 * @return string with the internal user name
83
	 * @throws \Exception if translation was unsuccessful
84
	 */
85
	public function getUserName($dn) {
86
		$result = $this->backend->dn2UserName($dn);
87
		if(!$result){
0 ignored issues
show
Bug Best Practice introduced by
The expression $result of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
88
			throw new \Exception('Translation to internal user name unsuccessful');
89
		}
90
		return $result;
91
	}
92
	
93
	/**
94
	 * Convert a stored DN so it can be used as base parameter for LDAP queries.
95
	 * @param string $dn the DN in question
96
	 * @return string
97
	 */
98
	public function DNasBaseParameter($dn) {
99
		return $this->helper->DNasBaseParameter($dn);
100
	}
101
	
102
	/**
103
	 * Sanitize a DN received from the LDAP server.
104
	 * @param array $dn the DN in question
105
	 * @return array the sanitized DN
106
	 */
107
	public function sanitizeDN($dn) {
108
		return $this->helper->sanitizeDN($dn);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->helper->sanitizeDN($dn); of type array|string adds the type string to the return on line 108 which is incompatible with the return type declared by the interface OCP\LDAP\ILDAPProvider::sanitizeDN of type array.
Loading history...
109
	}
110
	
111
	/**
112
	 * Return a new LDAP connection resource for the specified user. 
113
	 * The connection must be closed manually.
114
	 * @param string $uid user id
115
	 * @return resource of the LDAP connection
116
	 * @throws \Exception if user id was not found in LDAP
117
	 */
118
	public function getLDAPConnection($uid) {
119
		if(!$this->backend->userExists($uid)){
120
			throw new \Exception('User id not found in LDAP');
121
		}
122
		return $this->backend->getNewLDAPConnection($uid);
123
	}
124
	
125
	/**
126
	 * Get the LDAP base for users.
127
	 * @param string $uid user id
128
	 * @return string the base for users
129
	 * @throws \Exception if user id was not found in LDAP
130
	 */
131
	public function getLDAPBaseUsers($uid) {
132
		if(!$this->backend->userExists($uid)){
133
			throw new \Exception('User id not found in LDAP');
134
		}	
135
		return $this->backend->getLDAPAccess($uid)->getConnection()->getConfiguration()['ldap_base_users'];
136
	}
137
	
138
	/**
139
	 * Get the LDAP base for groups.
140
	 * @param string $uid user id
141
	 * @return string the base for groups
142
	 * @throws \Exception if user id was not found in LDAP
143
	 */
144
	public function getLDAPBaseGroups($uid) {
145
		if(!$this->backend->userExists($uid)){
146
			throw new \Exception('User id not found in LDAP');
147
		}
148
		return $this->backend->getLDAPAccess($uid)->getConnection()->getConfiguration()['ldap_base_groups'];
149
	}
150
	
151
	/**
152
	 * Clear the cache if a cache is used, otherwise do nothing.
153
	 * @param string $uid user id
154
	 * @throws \Exception if user id was not found in LDAP
155
	 */
156
	public function clearCache($uid) {
157
		if(!$this->backend->userExists($uid)){
158
			throw new \Exception('User id not found in LDAP');
159
		}
160
		$this->backend->getLDAPAccess($uid)->getConnection()->clearCache();
161
	}
162
	
163
	/**
164
	 * Check whether a LDAP DN exists
165
	 * @param string $dn LDAP DN
166
	 * @return bool whether the DN exists
167
	 */
168
	public function dnExists($dn) {
169
		$result = $this->backend->dn2UserName($dn);
170
		return !$result ? false : true;
171
	}
172
	
173
	/**
174
	 * Flag record for deletion.
175
	 * @param string $uid user id
176
	 */
177
	public function flagRecord($uid) {
178
		$this->deletedUsersIndex->markUser($uid);
179
	}
180
	
181
	/**
182
	 * Unflag record for deletion.
183
	 * @param string $uid user id
184
	 */
185
	public function unflagRecord($uid) {
186
		//do nothing
187
	}
188
}
189