Completed
Pull Request — master (#5689)
by Blizzz
14:57
created
apps/user_ldap/lib/User/OfflineUser.php 1 patch
Indentation   +193 added lines, -193 removed lines patch added patch discarded remove patch
@@ -29,205 +29,205 @@
 block discarded – undo
29 29
 use OCP\IDBConnection;
30 30
 
31 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 $email
58
-	 */
59
-	protected $email;
60
-	/**
61
-	 * @var bool $hasActiveShares
62
-	 */
63
-	protected $hasActiveShares;
64
-	/**
65
-	 * @var IConfig $config
66
-	 */
67
-	protected $config;
68
-	/**
69
-	 * @var IDBConnection $db
70
-	 */
71
-	protected $db;
72
-	/**
73
-	 * @var \OCA\User_LDAP\Mapping\UserMapping
74
-	 */
75
-	protected $mapping;
76
-
77
-	/**
78
-	 * @param string $ocName
79
-	 * @param IConfig $config
80
-	 * @param IDBConnection $db
81
-	 * @param \OCA\User_LDAP\Mapping\UserMapping $mapping
82
-	 */
83
-	public function __construct($ocName, IConfig $config, IDBConnection $db, UserMapping $mapping) {
84
-		$this->ocName = $ocName;
85
-		$this->config = $config;
86
-		$this->db = $db;
87
-		$this->mapping = $mapping;
88
-		$this->fetchDetails();
89
-	}
90
-
91
-	/**
92
-	 * remove the Delete-flag from the user.
93
-	 */
94
-	public function unmark() {
95
-		$this->config->setUserValue($this->ocName, 'user_ldap', 'isDeleted', '0');
96
-	}
97
-
98
-	/**
99
-	 * exports the user details in an assoc array
100
-	 * @return array
101
-	 */
102
-	public function export() {
103
-		$data = array();
104
-		$data['ocName'] = $this->getOCName();
105
-		$data['dn'] = $this->getDN();
106
-		$data['uid'] = $this->getUID();
107
-		$data['displayName'] = $this->getDisplayName();
108
-		$data['homePath'] = $this->getHomePath();
109
-		$data['lastLogin'] = $this->getLastLogin();
110
-		$data['email'] = $this->getEmail();
111
-		$data['hasActiveShares'] = $this->getHasActiveShares();
112
-
113
-		return $data;
114
-	}
115
-
116
-	/**
117
-	 * getter for Nextcloud internal name
118
-	 * @return string
119
-	 */
120
-	public function getOCName() {
121
-		return $this->ocName;
122
-	}
123
-
124
-	/**
125
-	 * getter for LDAP uid
126
-	 * @return string
127
-	 */
128
-	public function getUID() {
129
-		return $this->uid;
130
-	}
131
-
132
-	/**
133
-	 * getter for LDAP DN
134
-	 * @return string
135
-	 */
136
-	public function getDN() {
137
-		return $this->dn;
138
-	}
139
-
140
-	/**
141
-	 * getter for display name
142
-	 * @return string
143
-	 */
144
-	public function getDisplayName() {
145
-		return $this->displayName;
146
-	}
147
-
148
-	/**
149
-	 * getter for email
150
-	 * @return string
151
-	 */
152
-	public function getEmail() {
153
-		return $this->email;
154
-	}
155
-
156
-	/**
157
-	 * getter for home directory path
158
-	 * @return string
159
-	 */
160
-	public function getHomePath() {
161
-		return $this->homePath;
162
-	}
163
-
164
-	/**
165
-	 * getter for the last login timestamp
166
-	 * @return int
167
-	 */
168
-	public function getLastLogin() {
169
-		return intval($this->lastLogin);
170
-	}
171
-
172
-	/**
173
-	 * getter for having active shares
174
-	 * @return bool
175
-	 */
176
-	public function getHasActiveShares() {
177
-		return $this->hasActiveShares;
178
-	}
179
-
180
-	/**
181
-	 * reads the user details
182
-	 */
183
-	protected function fetchDetails() {
184
-		$properties = array (
185
-			'displayName' => 'user_ldap',
186
-			'uid'         => 'user_ldap',
187
-			'homePath'    => 'user_ldap',
188
-			'email'       => 'settings',
189
-			'lastLogin'   => 'login'
190
-		);
191
-		foreach($properties as $property => $app) {
192
-			$this->$property = $this->config->getUserValue($this->ocName, $app, $property, '');
193
-		}
194
-
195
-		$dn = $this->mapping->getDNByName($this->ocName);
196
-		$this->dn = ($dn !== false) ? $dn : '';
197
-
198
-		$this->determineShares();
199
-	}
200
-
201
-
202
-	/**
203
-	 * finds out whether the user has active shares. The result is stored in
204
-	 * $this->hasActiveShares
205
-	 */
206
-	protected function determineShares() {
207
-		$query = $this->db->prepare('
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 $email
58
+     */
59
+    protected $email;
60
+    /**
61
+     * @var bool $hasActiveShares
62
+     */
63
+    protected $hasActiveShares;
64
+    /**
65
+     * @var IConfig $config
66
+     */
67
+    protected $config;
68
+    /**
69
+     * @var IDBConnection $db
70
+     */
71
+    protected $db;
72
+    /**
73
+     * @var \OCA\User_LDAP\Mapping\UserMapping
74
+     */
75
+    protected $mapping;
76
+
77
+    /**
78
+     * @param string $ocName
79
+     * @param IConfig $config
80
+     * @param IDBConnection $db
81
+     * @param \OCA\User_LDAP\Mapping\UserMapping $mapping
82
+     */
83
+    public function __construct($ocName, IConfig $config, IDBConnection $db, UserMapping $mapping) {
84
+        $this->ocName = $ocName;
85
+        $this->config = $config;
86
+        $this->db = $db;
87
+        $this->mapping = $mapping;
88
+        $this->fetchDetails();
89
+    }
90
+
91
+    /**
92
+     * remove the Delete-flag from the user.
93
+     */
94
+    public function unmark() {
95
+        $this->config->setUserValue($this->ocName, 'user_ldap', 'isDeleted', '0');
96
+    }
97
+
98
+    /**
99
+     * exports the user details in an assoc array
100
+     * @return array
101
+     */
102
+    public function export() {
103
+        $data = array();
104
+        $data['ocName'] = $this->getOCName();
105
+        $data['dn'] = $this->getDN();
106
+        $data['uid'] = $this->getUID();
107
+        $data['displayName'] = $this->getDisplayName();
108
+        $data['homePath'] = $this->getHomePath();
109
+        $data['lastLogin'] = $this->getLastLogin();
110
+        $data['email'] = $this->getEmail();
111
+        $data['hasActiveShares'] = $this->getHasActiveShares();
112
+
113
+        return $data;
114
+    }
115
+
116
+    /**
117
+     * getter for Nextcloud internal name
118
+     * @return string
119
+     */
120
+    public function getOCName() {
121
+        return $this->ocName;
122
+    }
123
+
124
+    /**
125
+     * getter for LDAP uid
126
+     * @return string
127
+     */
128
+    public function getUID() {
129
+        return $this->uid;
130
+    }
131
+
132
+    /**
133
+     * getter for LDAP DN
134
+     * @return string
135
+     */
136
+    public function getDN() {
137
+        return $this->dn;
138
+    }
139
+
140
+    /**
141
+     * getter for display name
142
+     * @return string
143
+     */
144
+    public function getDisplayName() {
145
+        return $this->displayName;
146
+    }
147
+
148
+    /**
149
+     * getter for email
150
+     * @return string
151
+     */
152
+    public function getEmail() {
153
+        return $this->email;
154
+    }
155
+
156
+    /**
157
+     * getter for home directory path
158
+     * @return string
159
+     */
160
+    public function getHomePath() {
161
+        return $this->homePath;
162
+    }
163
+
164
+    /**
165
+     * getter for the last login timestamp
166
+     * @return int
167
+     */
168
+    public function getLastLogin() {
169
+        return intval($this->lastLogin);
170
+    }
171
+
172
+    /**
173
+     * getter for having active shares
174
+     * @return bool
175
+     */
176
+    public function getHasActiveShares() {
177
+        return $this->hasActiveShares;
178
+    }
179
+
180
+    /**
181
+     * reads the user details
182
+     */
183
+    protected function fetchDetails() {
184
+        $properties = array (
185
+            'displayName' => 'user_ldap',
186
+            'uid'         => 'user_ldap',
187
+            'homePath'    => 'user_ldap',
188
+            'email'       => 'settings',
189
+            'lastLogin'   => 'login'
190
+        );
191
+        foreach($properties as $property => $app) {
192
+            $this->$property = $this->config->getUserValue($this->ocName, $app, $property, '');
193
+        }
194
+
195
+        $dn = $this->mapping->getDNByName($this->ocName);
196
+        $this->dn = ($dn !== false) ? $dn : '';
197
+
198
+        $this->determineShares();
199
+    }
200
+
201
+
202
+    /**
203
+     * finds out whether the user has active shares. The result is stored in
204
+     * $this->hasActiveShares
205
+     */
206
+    protected function determineShares() {
207
+        $query = $this->db->prepare('
208 208
 			SELECT COUNT(`uid_owner`)
209 209
 			FROM `*PREFIX*share`
210 210
 			WHERE `uid_owner` = ?
211 211
 		', 1);
212
-		$query->execute(array($this->ocName));
213
-		$sResult = $query->fetchColumn(0);
214
-		if(intval($sResult) === 1) {
215
-			$this->hasActiveShares = true;
216
-			return;
217
-		}
218
-
219
-		$query = $this->db->prepare('
212
+        $query->execute(array($this->ocName));
213
+        $sResult = $query->fetchColumn(0);
214
+        if(intval($sResult) === 1) {
215
+            $this->hasActiveShares = true;
216
+            return;
217
+        }
218
+
219
+        $query = $this->db->prepare('
220 220
 			SELECT COUNT(`owner`)
221 221
 			FROM `*PREFIX*share_external`
222 222
 			WHERE `owner` = ?
223 223
 		', 1);
224
-		$query->execute(array($this->ocName));
225
-		$sResult = $query->fetchColumn(0);
226
-		if(intval($sResult) === 1) {
227
-			$this->hasActiveShares = true;
228
-			return;
229
-		}
230
-
231
-		$this->hasActiveShares = false;
232
-	}
224
+        $query->execute(array($this->ocName));
225
+        $sResult = $query->fetchColumn(0);
226
+        if(intval($sResult) === 1) {
227
+            $this->hasActiveShares = true;
228
+            return;
229
+        }
230
+
231
+        $this->hasActiveShares = false;
232
+    }
233 233
 }
Please login to merge, or discard this patch.
apps/user_ldap/lib/User_LDAP.php 2 patches
Indentation   +488 added lines, -488 removed lines patch added patch discarded remove patch
@@ -45,495 +45,495 @@
 block discarded – undo
45 45
 use OCP\Util;
46 46
 
47 47
 class User_LDAP extends BackendUtility implements \OCP\IUserBackend, \OCP\UserInterface, IUserLDAP {
48
-	/** @var \OCP\IConfig */
49
-	protected $ocConfig;
50
-
51
-	/** @var INotificationManager */
52
-	protected $notificationManager;
53
-
54
-	/**
55
-	 * @param Access $access
56
-	 * @param \OCP\IConfig $ocConfig
57
-	 * @param \OCP\Notification\IManager $notificationManager
58
-	 */
59
-	public function __construct(Access $access, IConfig $ocConfig, INotificationManager $notificationManager) {
60
-		parent::__construct($access);
61
-		$this->ocConfig = $ocConfig;
62
-		$this->notificationManager = $notificationManager;
63
-	}
64
-
65
-	/**
66
-	 * checks whether the user is allowed to change his avatar in Nextcloud
67
-	 * @param string $uid the Nextcloud user name
68
-	 * @return boolean either the user can or cannot
69
-	 */
70
-	public function canChangeAvatar($uid) {
71
-		$user = $this->access->userManager->get($uid);
72
-		if(!$user instanceof User) {
73
-			return false;
74
-		}
75
-		if($user->getAvatarImage() === false) {
76
-			return true;
77
-		}
78
-
79
-		return false;
80
-	}
81
-
82
-	/**
83
-	 * returns the username for the given login name, if available
84
-	 *
85
-	 * @param string $loginName
86
-	 * @return string|false
87
-	 */
88
-	public function loginName2UserName($loginName) {
89
-		$cacheKey = 'loginName2UserName-'.$loginName;
90
-		$username = $this->access->connection->getFromCache($cacheKey);
91
-		if(!is_null($username)) {
92
-			return $username;
93
-		}
94
-
95
-		try {
96
-			$ldapRecord = $this->getLDAPUserByLoginName($loginName);
97
-			$user = $this->access->userManager->get($ldapRecord['dn'][0]);
98
-			if($user instanceof OfflineUser) {
99
-				// this path is not really possible, however get() is documented
100
-				// to return User or OfflineUser so we are very defensive here.
101
-				$this->access->connection->writeToCache($cacheKey, false);
102
-				return false;
103
-			}
104
-			$username = $user->getUsername();
105
-			$this->access->connection->writeToCache($cacheKey, $username);
106
-			return $username;
107
-		} catch (NotOnLDAP $e) {
108
-			$this->access->connection->writeToCache($cacheKey, false);
109
-			return false;
110
-		}
111
-	}
48
+    /** @var \OCP\IConfig */
49
+    protected $ocConfig;
50
+
51
+    /** @var INotificationManager */
52
+    protected $notificationManager;
53
+
54
+    /**
55
+     * @param Access $access
56
+     * @param \OCP\IConfig $ocConfig
57
+     * @param \OCP\Notification\IManager $notificationManager
58
+     */
59
+    public function __construct(Access $access, IConfig $ocConfig, INotificationManager $notificationManager) {
60
+        parent::__construct($access);
61
+        $this->ocConfig = $ocConfig;
62
+        $this->notificationManager = $notificationManager;
63
+    }
64
+
65
+    /**
66
+     * checks whether the user is allowed to change his avatar in Nextcloud
67
+     * @param string $uid the Nextcloud user name
68
+     * @return boolean either the user can or cannot
69
+     */
70
+    public function canChangeAvatar($uid) {
71
+        $user = $this->access->userManager->get($uid);
72
+        if(!$user instanceof User) {
73
+            return false;
74
+        }
75
+        if($user->getAvatarImage() === false) {
76
+            return true;
77
+        }
78
+
79
+        return false;
80
+    }
81
+
82
+    /**
83
+     * returns the username for the given login name, if available
84
+     *
85
+     * @param string $loginName
86
+     * @return string|false
87
+     */
88
+    public function loginName2UserName($loginName) {
89
+        $cacheKey = 'loginName2UserName-'.$loginName;
90
+        $username = $this->access->connection->getFromCache($cacheKey);
91
+        if(!is_null($username)) {
92
+            return $username;
93
+        }
94
+
95
+        try {
96
+            $ldapRecord = $this->getLDAPUserByLoginName($loginName);
97
+            $user = $this->access->userManager->get($ldapRecord['dn'][0]);
98
+            if($user instanceof OfflineUser) {
99
+                // this path is not really possible, however get() is documented
100
+                // to return User or OfflineUser so we are very defensive here.
101
+                $this->access->connection->writeToCache($cacheKey, false);
102
+                return false;
103
+            }
104
+            $username = $user->getUsername();
105
+            $this->access->connection->writeToCache($cacheKey, $username);
106
+            return $username;
107
+        } catch (NotOnLDAP $e) {
108
+            $this->access->connection->writeToCache($cacheKey, false);
109
+            return false;
110
+        }
111
+    }
112 112
 	
113
-	/**
114
-	 * returns the username for the given LDAP DN, if available
115
-	 *
116
-	 * @param string $dn
117
-	 * @return string|false with the username
118
-	 */
119
-	public function dn2UserName($dn) {
120
-		return $this->access->dn2username($dn);
121
-	}
122
-
123
-	/**
124
-	 * returns an LDAP record based on a given login name
125
-	 *
126
-	 * @param string $loginName
127
-	 * @return array
128
-	 * @throws NotOnLDAP
129
-	 */
130
-	public function getLDAPUserByLoginName($loginName) {
131
-		//find out dn of the user name
132
-		$attrs = $this->access->userManager->getAttributes();
133
-		$users = $this->access->fetchUsersByLoginName($loginName, $attrs);
134
-		if(count($users) < 1) {
135
-			throw new NotOnLDAP('No user available for the given login name on ' .
136
-				$this->access->connection->ldapHost . ':' . $this->access->connection->ldapPort);
137
-		}
138
-		return $users[0];
139
-	}
140
-
141
-	/**
142
-	 * Check if the password is correct without logging in the user
143
-	 *
144
-	 * @param string $uid The username
145
-	 * @param string $password The password
146
-	 * @return false|string
147
-	 */
148
-	public function checkPassword($uid, $password) {
149
-		try {
150
-			$ldapRecord = $this->getLDAPUserByLoginName($uid);
151
-		} catch(NotOnLDAP $e) {
152
-			if($this->ocConfig->getSystemValue('loglevel', Util::WARN) === Util::DEBUG) {
153
-				\OC::$server->getLogger()->logException($e, ['app' => 'user_ldap']);
154
-			}
155
-			return false;
156
-		}
157
-		$dn = $ldapRecord['dn'][0];
158
-		$user = $this->access->userManager->get($dn);
159
-
160
-		if(!$user instanceof User) {
161
-			Util::writeLog('user_ldap',
162
-				'LDAP Login: Could not get user object for DN ' . $dn .
163
-				'. Maybe the LDAP entry has no set display name attribute?',
164
-				Util::WARN);
165
-			return false;
166
-		}
167
-		if($user->getUsername() !== false) {
168
-			//are the credentials OK?
169
-			if(!$this->access->areCredentialsValid($dn, $password)) {
170
-				return false;
171
-			}
172
-
173
-			$this->access->cacheUserExists($user->getUsername());
174
-			$user->processAttributes($ldapRecord);
175
-			$user->markLogin();
176
-
177
-			return $user->getUsername();
178
-		}
179
-
180
-		return false;
181
-	}
182
-
183
-	/**
184
-	 * Set password
185
-	 * @param string $uid The username
186
-	 * @param string $password The new password
187
-	 * @return bool
188
-	 */
189
-	public function setPassword($uid, $password) {
190
-		$user = $this->access->userManager->get($uid);
191
-
192
-		if(!$user instanceof User) {
193
-			throw new \Exception('LDAP setPassword: Could not get user object for uid ' . $uid .
194
-				'. Maybe the LDAP entry has no set display name attribute?');
195
-		}
196
-		if($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) {
197
-			$ldapDefaultPPolicyDN = $this->access->connection->ldapDefaultPPolicyDN;
198
-			$turnOnPasswordChange = $this->access->connection->turnOnPasswordChange;
199
-			if (!empty($ldapDefaultPPolicyDN) && (intval($turnOnPasswordChange) === 1)) {
200
-				//remove last password expiry warning if any
201
-				$notification = $this->notificationManager->createNotification();
202
-				$notification->setApp('user_ldap')
203
-					->setUser($uid)
204
-					->setObject('pwd_exp_warn', $uid)
205
-				;
206
-				$this->notificationManager->markProcessed($notification);
207
-			}
208
-			return true;
209
-		}
210
-
211
-		return false;
212
-	}
213
-
214
-	/**
215
-	 * Get a list of all users
216
-	 *
217
-	 * @param string $search
218
-	 * @param integer $limit
219
-	 * @param integer $offset
220
-	 * @return string[] an array of all uids
221
-	 */
222
-	public function getUsers($search = '', $limit = 10, $offset = 0) {
223
-		$search = $this->access->escapeFilterPart($search, true);
224
-		$cachekey = 'getUsers-'.$search.'-'.$limit.'-'.$offset;
225
-
226
-		//check if users are cached, if so return
227
-		$ldap_users = $this->access->connection->getFromCache($cachekey);
228
-		if(!is_null($ldap_users)) {
229
-			return $ldap_users;
230
-		}
231
-
232
-		// if we'd pass -1 to LDAP search, we'd end up in a Protocol
233
-		// error. With a limit of 0, we get 0 results. So we pass null.
234
-		if($limit <= 0) {
235
-			$limit = null;
236
-		}
237
-		$filter = $this->access->combineFilterWithAnd(array(
238
-			$this->access->connection->ldapUserFilter,
239
-			$this->access->connection->ldapUserDisplayName . '=*',
240
-			$this->access->getFilterPartForUserSearch($search)
241
-		));
242
-
243
-		Util::writeLog('user_ldap',
244
-			'getUsers: Options: search '.$search.' limit '.$limit.' offset '.$offset.' Filter: '.$filter,
245
-			Util::DEBUG);
246
-		//do the search and translate results to owncloud names
247
-		$ldap_users = $this->access->fetchListOfUsers(
248
-			$filter,
249
-			$this->access->userManager->getAttributes(true),
250
-			$limit, $offset);
251
-		$ldap_users = $this->access->nextcloudUserNames($ldap_users);
252
-		Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', Util::DEBUG);
253
-
254
-		$this->access->connection->writeToCache($cachekey, $ldap_users);
255
-		return $ldap_users;
256
-	}
257
-
258
-	/**
259
-	 * checks whether a user is still available on LDAP
260
-	 *
261
-	 * @param string|\OCA\User_LDAP\User\User $user either the Nextcloud user
262
-	 * name or an instance of that user
263
-	 * @return bool
264
-	 * @throws \Exception
265
-	 * @throws \OC\ServerNotAvailableException
266
-	 */
267
-	public function userExistsOnLDAP($user) {
268
-		if(is_string($user)) {
269
-			$user = $this->access->userManager->get($user);
270
-		}
271
-		if(is_null($user)) {
272
-			return false;
273
-		}
274
-
275
-		$dn = $user->getDN();
276
-		//check if user really still exists by reading its entry
277
-		if(!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) {
278
-			$lcr = $this->access->connection->getConnectionResource();
279
-			if(is_null($lcr)) {
280
-				throw new \Exception('No LDAP Connection to server ' . $this->access->connection->ldapHost);
281
-			}
282
-
283
-			try {
284
-				$uuid = $this->access->getUserMapper()->getUUIDByDN($dn);
285
-				if(!$uuid) {
286
-					return false;
287
-				}
288
-				$newDn = $this->access->getUserDnByUuid($uuid);
289
-				//check if renamed user is still valid by reapplying the ldap filter
290
-				if(!is_array($this->access->readAttribute($newDn, '', $this->access->connection->ldapUserFilter))) {
291
-					return false;
292
-				}
293
-				$this->access->getUserMapper()->setDNbyUUID($newDn, $uuid);
294
-				return true;
295
-			} catch (\Exception $e) {
296
-				return false;
297
-			}
298
-		}
299
-
300
-		if($user instanceof OfflineUser) {
301
-			$user->unmark();
302
-		}
303
-
304
-		return true;
305
-	}
306
-
307
-	/**
308
-	 * check if a user exists
309
-	 * @param string $uid the username
310
-	 * @return boolean
311
-	 * @throws \Exception when connection could not be established
312
-	 */
313
-	public function userExists($uid) {
314
-		$userExists = $this->access->connection->getFromCache('userExists'.$uid);
315
-		if(!is_null($userExists)) {
316
-			return (bool)$userExists;
317
-		}
318
-		//getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking.
319
-		$user = $this->access->userManager->get($uid);
320
-
321
-		if(is_null($user)) {
322
-			Util::writeLog('user_ldap', 'No DN found for '.$uid.' on '.
323
-				$this->access->connection->ldapHost, Util::DEBUG);
324
-			$this->access->connection->writeToCache('userExists'.$uid, false);
325
-			return false;
326
-		} else if($user instanceof OfflineUser) {
327
-			//express check for users marked as deleted. Returning true is
328
-			//necessary for cleanup
329
-			return true;
330
-		}
331
-
332
-		$result = $this->userExistsOnLDAP($user);
333
-		$this->access->connection->writeToCache('userExists'.$uid, $result);
334
-		if($result === true) {
335
-			$user->update();
336
-		}
337
-		return $result;
338
-	}
339
-
340
-	/**
341
-	* returns whether a user was deleted in LDAP
342
-	*
343
-	* @param string $uid The username of the user to delete
344
-	* @return bool
345
-	*/
346
-	public function deleteUser($uid) {
347
-		$marked = $this->ocConfig->getUserValue($uid, 'user_ldap', 'isDeleted', 0);
348
-		if(intval($marked) === 0) {
349
-			\OC::$server->getLogger()->notice(
350
-				'User '.$uid . ' is not marked as deleted, not cleaning up.',
351
-				array('app' => 'user_ldap'));
352
-			return false;
353
-		}
354
-		\OC::$server->getLogger()->info('Cleaning up after user ' . $uid,
355
-			array('app' => 'user_ldap'));
356
-
357
-		//Get Home Directory out of user preferences so we can return it later,
358
-		//necessary for removing directories as done by OC_User.
359
-		$this->access->getUserMapper()->unmap($uid);
360
-		return true;
361
-	}
362
-
363
-	/**
364
-	 * get the user's home directory
365
-	 *
366
-	 * @param string $uid the username
367
-	 * @return bool|string
368
-	 * @throws NoUserException
369
-	 * @throws \Exception
370
-	 */
371
-	public function getHome($uid) {
372
-		// user Exists check required as it is not done in user proxy!
373
-		if(!$this->userExists($uid)) {
374
-			return false;
375
-		}
376
-
377
-		$cacheKey = 'getHome'.$uid;
378
-		$path = $this->access->connection->getFromCache($cacheKey);
379
-		if(!is_null($path)) {
380
-			return $path;
381
-		}
382
-
383
-		// early return path if it is a deleted user
384
-		$user = $this->access->userManager->get($uid);
385
-		if($user instanceof OfflineUser) {
386
-			return $user->getHomePath();
387
-		} else if ($user === null) {
388
-			throw new NoUserException($uid . ' is not a valid user anymore');
389
-		}
390
-
391
-		$path = $user->getHomePath();
392
-		$this->access->cacheUserHome($uid, $path);
393
-
394
-		return $path;
395
-	}
396
-
397
-	/**
398
-	 * get display name of the user
399
-	 * @param string $uid user ID of the user
400
-	 * @return string|false display name
401
-	 */
402
-	public function getDisplayName($uid) {
403
-		if(!$this->userExists($uid)) {
404
-			return false;
405
-		}
406
-
407
-		$cacheKey = 'getDisplayName'.$uid;
408
-		if(!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) {
409
-			return $displayName;
410
-		}
411
-
412
-		//Check whether the display name is configured to have a 2nd feature
413
-		$additionalAttribute = $this->access->connection->ldapUserDisplayName2;
414
-		$displayName2 = '';
415
-		if ($additionalAttribute !== '') {
416
-			$displayName2 = $this->access->readAttribute(
417
-				$this->access->username2dn($uid),
418
-				$additionalAttribute);
419
-		}
420
-
421
-		$displayName = $this->access->readAttribute(
422
-			$this->access->username2dn($uid),
423
-			$this->access->connection->ldapUserDisplayName);
424
-
425
-		if($displayName && (count($displayName) > 0)) {
426
-			$displayName = $displayName[0];
427
-
428
-			if (is_array($displayName2)){
429
-				$displayName2 = count($displayName2) > 0 ? $displayName2[0] : '';
430
-			}
431
-
432
-			$user = $this->access->userManager->get($uid);
433
-			if ($user instanceof User) {
434
-				$displayName = $user->composeAndStoreDisplayName($displayName, $displayName2);
435
-				$this->access->connection->writeToCache($cacheKey, $displayName);
436
-			}
437
-			if ($user instanceof OfflineUser) {
438
-				/** @var OfflineUser $user*/
439
-				$displayName = $user->getDisplayName();
440
-			}
441
-			return $displayName;
442
-		}
443
-
444
-		return null;
445
-	}
446
-
447
-	/**
448
-	 * Get a list of all display names
449
-	 *
450
-	 * @param string $search
451
-	 * @param string|null $limit
452
-	 * @param string|null $offset
453
-	 * @return array an array of all displayNames (value) and the corresponding uids (key)
454
-	 */
455
-	public function getDisplayNames($search = '', $limit = null, $offset = null) {
456
-		$cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset;
457
-		if(!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) {
458
-			return $displayNames;
459
-		}
460
-
461
-		$displayNames = array();
462
-		$users = $this->getUsers($search, $limit, $offset);
463
-		foreach ($users as $user) {
464
-			$displayNames[$user] = $this->getDisplayName($user);
465
-		}
466
-		$this->access->connection->writeToCache($cacheKey, $displayNames);
467
-		return $displayNames;
468
-	}
469
-
470
-	/**
471
-	* Check if backend implements actions
472
-	* @param int $actions bitwise-or'ed actions
473
-	* @return boolean
474
-	*
475
-	* Returns the supported actions as int to be
476
-	* compared with OC_USER_BACKEND_CREATE_USER etc.
477
-	*/
478
-	public function implementsActions($actions) {
479
-		return (bool)((Backend::CHECK_PASSWORD
480
-			| Backend::GET_HOME
481
-			| Backend::GET_DISPLAYNAME
482
-			| Backend::PROVIDE_AVATAR
483
-			| Backend::COUNT_USERS
484
-			| ((intval($this->access->connection->turnOnPasswordChange) === 1)?(Backend::SET_PASSWORD):0))
485
-			& $actions);
486
-	}
487
-
488
-	/**
489
-	 * @return bool
490
-	 */
491
-	public function hasUserListings() {
492
-		return true;
493
-	}
494
-
495
-	/**
496
-	 * counts the users in LDAP
497
-	 *
498
-	 * @return int|bool
499
-	 */
500
-	public function countUsers() {
501
-		$filter = $this->access->getFilterForUserCount();
502
-		$cacheKey = 'countUsers-'.$filter;
503
-		if(!is_null($entries = $this->access->connection->getFromCache($cacheKey))) {
504
-			return $entries;
505
-		}
506
-		$entries = $this->access->countUsers($filter);
507
-		$this->access->connection->writeToCache($cacheKey, $entries);
508
-		return $entries;
509
-	}
510
-
511
-	/**
512
-	 * Backend name to be shown in user management
513
-	 * @return string the name of the backend to be shown
514
-	 */
515
-	public function getBackendName(){
516
-		return 'LDAP';
517
-	}
113
+    /**
114
+     * returns the username for the given LDAP DN, if available
115
+     *
116
+     * @param string $dn
117
+     * @return string|false with the username
118
+     */
119
+    public function dn2UserName($dn) {
120
+        return $this->access->dn2username($dn);
121
+    }
122
+
123
+    /**
124
+     * returns an LDAP record based on a given login name
125
+     *
126
+     * @param string $loginName
127
+     * @return array
128
+     * @throws NotOnLDAP
129
+     */
130
+    public function getLDAPUserByLoginName($loginName) {
131
+        //find out dn of the user name
132
+        $attrs = $this->access->userManager->getAttributes();
133
+        $users = $this->access->fetchUsersByLoginName($loginName, $attrs);
134
+        if(count($users) < 1) {
135
+            throw new NotOnLDAP('No user available for the given login name on ' .
136
+                $this->access->connection->ldapHost . ':' . $this->access->connection->ldapPort);
137
+        }
138
+        return $users[0];
139
+    }
140
+
141
+    /**
142
+     * Check if the password is correct without logging in the user
143
+     *
144
+     * @param string $uid The username
145
+     * @param string $password The password
146
+     * @return false|string
147
+     */
148
+    public function checkPassword($uid, $password) {
149
+        try {
150
+            $ldapRecord = $this->getLDAPUserByLoginName($uid);
151
+        } catch(NotOnLDAP $e) {
152
+            if($this->ocConfig->getSystemValue('loglevel', Util::WARN) === Util::DEBUG) {
153
+                \OC::$server->getLogger()->logException($e, ['app' => 'user_ldap']);
154
+            }
155
+            return false;
156
+        }
157
+        $dn = $ldapRecord['dn'][0];
158
+        $user = $this->access->userManager->get($dn);
159
+
160
+        if(!$user instanceof User) {
161
+            Util::writeLog('user_ldap',
162
+                'LDAP Login: Could not get user object for DN ' . $dn .
163
+                '. Maybe the LDAP entry has no set display name attribute?',
164
+                Util::WARN);
165
+            return false;
166
+        }
167
+        if($user->getUsername() !== false) {
168
+            //are the credentials OK?
169
+            if(!$this->access->areCredentialsValid($dn, $password)) {
170
+                return false;
171
+            }
172
+
173
+            $this->access->cacheUserExists($user->getUsername());
174
+            $user->processAttributes($ldapRecord);
175
+            $user->markLogin();
176
+
177
+            return $user->getUsername();
178
+        }
179
+
180
+        return false;
181
+    }
182
+
183
+    /**
184
+     * Set password
185
+     * @param string $uid The username
186
+     * @param string $password The new password
187
+     * @return bool
188
+     */
189
+    public function setPassword($uid, $password) {
190
+        $user = $this->access->userManager->get($uid);
191
+
192
+        if(!$user instanceof User) {
193
+            throw new \Exception('LDAP setPassword: Could not get user object for uid ' . $uid .
194
+                '. Maybe the LDAP entry has no set display name attribute?');
195
+        }
196
+        if($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) {
197
+            $ldapDefaultPPolicyDN = $this->access->connection->ldapDefaultPPolicyDN;
198
+            $turnOnPasswordChange = $this->access->connection->turnOnPasswordChange;
199
+            if (!empty($ldapDefaultPPolicyDN) && (intval($turnOnPasswordChange) === 1)) {
200
+                //remove last password expiry warning if any
201
+                $notification = $this->notificationManager->createNotification();
202
+                $notification->setApp('user_ldap')
203
+                    ->setUser($uid)
204
+                    ->setObject('pwd_exp_warn', $uid)
205
+                ;
206
+                $this->notificationManager->markProcessed($notification);
207
+            }
208
+            return true;
209
+        }
210
+
211
+        return false;
212
+    }
213
+
214
+    /**
215
+     * Get a list of all users
216
+     *
217
+     * @param string $search
218
+     * @param integer $limit
219
+     * @param integer $offset
220
+     * @return string[] an array of all uids
221
+     */
222
+    public function getUsers($search = '', $limit = 10, $offset = 0) {
223
+        $search = $this->access->escapeFilterPart($search, true);
224
+        $cachekey = 'getUsers-'.$search.'-'.$limit.'-'.$offset;
225
+
226
+        //check if users are cached, if so return
227
+        $ldap_users = $this->access->connection->getFromCache($cachekey);
228
+        if(!is_null($ldap_users)) {
229
+            return $ldap_users;
230
+        }
231
+
232
+        // if we'd pass -1 to LDAP search, we'd end up in a Protocol
233
+        // error. With a limit of 0, we get 0 results. So we pass null.
234
+        if($limit <= 0) {
235
+            $limit = null;
236
+        }
237
+        $filter = $this->access->combineFilterWithAnd(array(
238
+            $this->access->connection->ldapUserFilter,
239
+            $this->access->connection->ldapUserDisplayName . '=*',
240
+            $this->access->getFilterPartForUserSearch($search)
241
+        ));
242
+
243
+        Util::writeLog('user_ldap',
244
+            'getUsers: Options: search '.$search.' limit '.$limit.' offset '.$offset.' Filter: '.$filter,
245
+            Util::DEBUG);
246
+        //do the search and translate results to owncloud names
247
+        $ldap_users = $this->access->fetchListOfUsers(
248
+            $filter,
249
+            $this->access->userManager->getAttributes(true),
250
+            $limit, $offset);
251
+        $ldap_users = $this->access->nextcloudUserNames($ldap_users);
252
+        Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', Util::DEBUG);
253
+
254
+        $this->access->connection->writeToCache($cachekey, $ldap_users);
255
+        return $ldap_users;
256
+    }
257
+
258
+    /**
259
+     * checks whether a user is still available on LDAP
260
+     *
261
+     * @param string|\OCA\User_LDAP\User\User $user either the Nextcloud user
262
+     * name or an instance of that user
263
+     * @return bool
264
+     * @throws \Exception
265
+     * @throws \OC\ServerNotAvailableException
266
+     */
267
+    public function userExistsOnLDAP($user) {
268
+        if(is_string($user)) {
269
+            $user = $this->access->userManager->get($user);
270
+        }
271
+        if(is_null($user)) {
272
+            return false;
273
+        }
274
+
275
+        $dn = $user->getDN();
276
+        //check if user really still exists by reading its entry
277
+        if(!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) {
278
+            $lcr = $this->access->connection->getConnectionResource();
279
+            if(is_null($lcr)) {
280
+                throw new \Exception('No LDAP Connection to server ' . $this->access->connection->ldapHost);
281
+            }
282
+
283
+            try {
284
+                $uuid = $this->access->getUserMapper()->getUUIDByDN($dn);
285
+                if(!$uuid) {
286
+                    return false;
287
+                }
288
+                $newDn = $this->access->getUserDnByUuid($uuid);
289
+                //check if renamed user is still valid by reapplying the ldap filter
290
+                if(!is_array($this->access->readAttribute($newDn, '', $this->access->connection->ldapUserFilter))) {
291
+                    return false;
292
+                }
293
+                $this->access->getUserMapper()->setDNbyUUID($newDn, $uuid);
294
+                return true;
295
+            } catch (\Exception $e) {
296
+                return false;
297
+            }
298
+        }
299
+
300
+        if($user instanceof OfflineUser) {
301
+            $user->unmark();
302
+        }
303
+
304
+        return true;
305
+    }
306
+
307
+    /**
308
+     * check if a user exists
309
+     * @param string $uid the username
310
+     * @return boolean
311
+     * @throws \Exception when connection could not be established
312
+     */
313
+    public function userExists($uid) {
314
+        $userExists = $this->access->connection->getFromCache('userExists'.$uid);
315
+        if(!is_null($userExists)) {
316
+            return (bool)$userExists;
317
+        }
318
+        //getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking.
319
+        $user = $this->access->userManager->get($uid);
320
+
321
+        if(is_null($user)) {
322
+            Util::writeLog('user_ldap', 'No DN found for '.$uid.' on '.
323
+                $this->access->connection->ldapHost, Util::DEBUG);
324
+            $this->access->connection->writeToCache('userExists'.$uid, false);
325
+            return false;
326
+        } else if($user instanceof OfflineUser) {
327
+            //express check for users marked as deleted. Returning true is
328
+            //necessary for cleanup
329
+            return true;
330
+        }
331
+
332
+        $result = $this->userExistsOnLDAP($user);
333
+        $this->access->connection->writeToCache('userExists'.$uid, $result);
334
+        if($result === true) {
335
+            $user->update();
336
+        }
337
+        return $result;
338
+    }
339
+
340
+    /**
341
+     * returns whether a user was deleted in LDAP
342
+     *
343
+     * @param string $uid The username of the user to delete
344
+     * @return bool
345
+     */
346
+    public function deleteUser($uid) {
347
+        $marked = $this->ocConfig->getUserValue($uid, 'user_ldap', 'isDeleted', 0);
348
+        if(intval($marked) === 0) {
349
+            \OC::$server->getLogger()->notice(
350
+                'User '.$uid . ' is not marked as deleted, not cleaning up.',
351
+                array('app' => 'user_ldap'));
352
+            return false;
353
+        }
354
+        \OC::$server->getLogger()->info('Cleaning up after user ' . $uid,
355
+            array('app' => 'user_ldap'));
356
+
357
+        //Get Home Directory out of user preferences so we can return it later,
358
+        //necessary for removing directories as done by OC_User.
359
+        $this->access->getUserMapper()->unmap($uid);
360
+        return true;
361
+    }
362
+
363
+    /**
364
+     * get the user's home directory
365
+     *
366
+     * @param string $uid the username
367
+     * @return bool|string
368
+     * @throws NoUserException
369
+     * @throws \Exception
370
+     */
371
+    public function getHome($uid) {
372
+        // user Exists check required as it is not done in user proxy!
373
+        if(!$this->userExists($uid)) {
374
+            return false;
375
+        }
376
+
377
+        $cacheKey = 'getHome'.$uid;
378
+        $path = $this->access->connection->getFromCache($cacheKey);
379
+        if(!is_null($path)) {
380
+            return $path;
381
+        }
382
+
383
+        // early return path if it is a deleted user
384
+        $user = $this->access->userManager->get($uid);
385
+        if($user instanceof OfflineUser) {
386
+            return $user->getHomePath();
387
+        } else if ($user === null) {
388
+            throw new NoUserException($uid . ' is not a valid user anymore');
389
+        }
390
+
391
+        $path = $user->getHomePath();
392
+        $this->access->cacheUserHome($uid, $path);
393
+
394
+        return $path;
395
+    }
396
+
397
+    /**
398
+     * get display name of the user
399
+     * @param string $uid user ID of the user
400
+     * @return string|false display name
401
+     */
402
+    public function getDisplayName($uid) {
403
+        if(!$this->userExists($uid)) {
404
+            return false;
405
+        }
406
+
407
+        $cacheKey = 'getDisplayName'.$uid;
408
+        if(!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) {
409
+            return $displayName;
410
+        }
411
+
412
+        //Check whether the display name is configured to have a 2nd feature
413
+        $additionalAttribute = $this->access->connection->ldapUserDisplayName2;
414
+        $displayName2 = '';
415
+        if ($additionalAttribute !== '') {
416
+            $displayName2 = $this->access->readAttribute(
417
+                $this->access->username2dn($uid),
418
+                $additionalAttribute);
419
+        }
420
+
421
+        $displayName = $this->access->readAttribute(
422
+            $this->access->username2dn($uid),
423
+            $this->access->connection->ldapUserDisplayName);
424
+
425
+        if($displayName && (count($displayName) > 0)) {
426
+            $displayName = $displayName[0];
427
+
428
+            if (is_array($displayName2)){
429
+                $displayName2 = count($displayName2) > 0 ? $displayName2[0] : '';
430
+            }
431
+
432
+            $user = $this->access->userManager->get($uid);
433
+            if ($user instanceof User) {
434
+                $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2);
435
+                $this->access->connection->writeToCache($cacheKey, $displayName);
436
+            }
437
+            if ($user instanceof OfflineUser) {
438
+                /** @var OfflineUser $user*/
439
+                $displayName = $user->getDisplayName();
440
+            }
441
+            return $displayName;
442
+        }
443
+
444
+        return null;
445
+    }
446
+
447
+    /**
448
+     * Get a list of all display names
449
+     *
450
+     * @param string $search
451
+     * @param string|null $limit
452
+     * @param string|null $offset
453
+     * @return array an array of all displayNames (value) and the corresponding uids (key)
454
+     */
455
+    public function getDisplayNames($search = '', $limit = null, $offset = null) {
456
+        $cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset;
457
+        if(!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) {
458
+            return $displayNames;
459
+        }
460
+
461
+        $displayNames = array();
462
+        $users = $this->getUsers($search, $limit, $offset);
463
+        foreach ($users as $user) {
464
+            $displayNames[$user] = $this->getDisplayName($user);
465
+        }
466
+        $this->access->connection->writeToCache($cacheKey, $displayNames);
467
+        return $displayNames;
468
+    }
469
+
470
+    /**
471
+     * Check if backend implements actions
472
+     * @param int $actions bitwise-or'ed actions
473
+     * @return boolean
474
+     *
475
+     * Returns the supported actions as int to be
476
+     * compared with OC_USER_BACKEND_CREATE_USER etc.
477
+     */
478
+    public function implementsActions($actions) {
479
+        return (bool)((Backend::CHECK_PASSWORD
480
+            | Backend::GET_HOME
481
+            | Backend::GET_DISPLAYNAME
482
+            | Backend::PROVIDE_AVATAR
483
+            | Backend::COUNT_USERS
484
+            | ((intval($this->access->connection->turnOnPasswordChange) === 1)?(Backend::SET_PASSWORD):0))
485
+            & $actions);
486
+    }
487
+
488
+    /**
489
+     * @return bool
490
+     */
491
+    public function hasUserListings() {
492
+        return true;
493
+    }
494
+
495
+    /**
496
+     * counts the users in LDAP
497
+     *
498
+     * @return int|bool
499
+     */
500
+    public function countUsers() {
501
+        $filter = $this->access->getFilterForUserCount();
502
+        $cacheKey = 'countUsers-'.$filter;
503
+        if(!is_null($entries = $this->access->connection->getFromCache($cacheKey))) {
504
+            return $entries;
505
+        }
506
+        $entries = $this->access->countUsers($filter);
507
+        $this->access->connection->writeToCache($cacheKey, $entries);
508
+        return $entries;
509
+    }
510
+
511
+    /**
512
+     * Backend name to be shown in user management
513
+     * @return string the name of the backend to be shown
514
+     */
515
+    public function getBackendName(){
516
+        return 'LDAP';
517
+    }
518 518
 	
519
-	/**
520
-	 * Return access for LDAP interaction.
521
-	 * @param string $uid
522
-	 * @return Access instance of Access for LDAP interaction
523
-	 */
524
-	public function getLDAPAccess($uid) {
525
-		return $this->access;
526
-	}
519
+    /**
520
+     * Return access for LDAP interaction.
521
+     * @param string $uid
522
+     * @return Access instance of Access for LDAP interaction
523
+     */
524
+    public function getLDAPAccess($uid) {
525
+        return $this->access;
526
+    }
527 527
 	
528
-	/**
529
-	 * Return LDAP connection resource from a cloned connection.
530
-	 * The cloned connection needs to be closed manually.
531
-	 * of the current access.
532
-	 * @param string $uid
533
-	 * @return resource of the LDAP connection
534
-	 */
535
-	public function getNewLDAPConnection($uid) {
536
-		$connection = clone $this->access->getConnection();
537
-		return $connection->getConnectionResource();
538
-	}
528
+    /**
529
+     * Return LDAP connection resource from a cloned connection.
530
+     * The cloned connection needs to be closed manually.
531
+     * of the current access.
532
+     * @param string $uid
533
+     * @return resource of the LDAP connection
534
+     */
535
+    public function getNewLDAPConnection($uid) {
536
+        $connection = clone $this->access->getConnection();
537
+        return $connection->getConnectionResource();
538
+    }
539 539
 }
Please login to merge, or discard this patch.
Spacing   +49 added lines, -49 removed lines patch added patch discarded remove patch
@@ -69,10 +69,10 @@  discard block
 block discarded – undo
69 69
 	 */
70 70
 	public function canChangeAvatar($uid) {
71 71
 		$user = $this->access->userManager->get($uid);
72
-		if(!$user instanceof User) {
72
+		if (!$user instanceof User) {
73 73
 			return false;
74 74
 		}
75
-		if($user->getAvatarImage() === false) {
75
+		if ($user->getAvatarImage() === false) {
76 76
 			return true;
77 77
 		}
78 78
 
@@ -88,14 +88,14 @@  discard block
 block discarded – undo
88 88
 	public function loginName2UserName($loginName) {
89 89
 		$cacheKey = 'loginName2UserName-'.$loginName;
90 90
 		$username = $this->access->connection->getFromCache($cacheKey);
91
-		if(!is_null($username)) {
91
+		if (!is_null($username)) {
92 92
 			return $username;
93 93
 		}
94 94
 
95 95
 		try {
96 96
 			$ldapRecord = $this->getLDAPUserByLoginName($loginName);
97 97
 			$user = $this->access->userManager->get($ldapRecord['dn'][0]);
98
-			if($user instanceof OfflineUser) {
98
+			if ($user instanceof OfflineUser) {
99 99
 				// this path is not really possible, however get() is documented
100 100
 				// to return User or OfflineUser so we are very defensive here.
101 101
 				$this->access->connection->writeToCache($cacheKey, false);
@@ -131,9 +131,9 @@  discard block
 block discarded – undo
131 131
 		//find out dn of the user name
132 132
 		$attrs = $this->access->userManager->getAttributes();
133 133
 		$users = $this->access->fetchUsersByLoginName($loginName, $attrs);
134
-		if(count($users) < 1) {
135
-			throw new NotOnLDAP('No user available for the given login name on ' .
136
-				$this->access->connection->ldapHost . ':' . $this->access->connection->ldapPort);
134
+		if (count($users) < 1) {
135
+			throw new NotOnLDAP('No user available for the given login name on '.
136
+				$this->access->connection->ldapHost.':'.$this->access->connection->ldapPort);
137 137
 		}
138 138
 		return $users[0];
139 139
 	}
@@ -148,8 +148,8 @@  discard block
 block discarded – undo
148 148
 	public function checkPassword($uid, $password) {
149 149
 		try {
150 150
 			$ldapRecord = $this->getLDAPUserByLoginName($uid);
151
-		} catch(NotOnLDAP $e) {
152
-			if($this->ocConfig->getSystemValue('loglevel', Util::WARN) === Util::DEBUG) {
151
+		} catch (NotOnLDAP $e) {
152
+			if ($this->ocConfig->getSystemValue('loglevel', Util::WARN) === Util::DEBUG) {
153 153
 				\OC::$server->getLogger()->logException($e, ['app' => 'user_ldap']);
154 154
 			}
155 155
 			return false;
@@ -157,16 +157,16 @@  discard block
 block discarded – undo
157 157
 		$dn = $ldapRecord['dn'][0];
158 158
 		$user = $this->access->userManager->get($dn);
159 159
 
160
-		if(!$user instanceof User) {
160
+		if (!$user instanceof User) {
161 161
 			Util::writeLog('user_ldap',
162
-				'LDAP Login: Could not get user object for DN ' . $dn .
162
+				'LDAP Login: Could not get user object for DN '.$dn.
163 163
 				'. Maybe the LDAP entry has no set display name attribute?',
164 164
 				Util::WARN);
165 165
 			return false;
166 166
 		}
167
-		if($user->getUsername() !== false) {
167
+		if ($user->getUsername() !== false) {
168 168
 			//are the credentials OK?
169
-			if(!$this->access->areCredentialsValid($dn, $password)) {
169
+			if (!$this->access->areCredentialsValid($dn, $password)) {
170 170
 				return false;
171 171
 			}
172 172
 
@@ -189,11 +189,11 @@  discard block
 block discarded – undo
189 189
 	public function setPassword($uid, $password) {
190 190
 		$user = $this->access->userManager->get($uid);
191 191
 
192
-		if(!$user instanceof User) {
193
-			throw new \Exception('LDAP setPassword: Could not get user object for uid ' . $uid .
192
+		if (!$user instanceof User) {
193
+			throw new \Exception('LDAP setPassword: Could not get user object for uid '.$uid.
194 194
 				'. Maybe the LDAP entry has no set display name attribute?');
195 195
 		}
196
-		if($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) {
196
+		if ($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) {
197 197
 			$ldapDefaultPPolicyDN = $this->access->connection->ldapDefaultPPolicyDN;
198 198
 			$turnOnPasswordChange = $this->access->connection->turnOnPasswordChange;
199 199
 			if (!empty($ldapDefaultPPolicyDN) && (intval($turnOnPasswordChange) === 1)) {
@@ -225,18 +225,18 @@  discard block
 block discarded – undo
225 225
 
226 226
 		//check if users are cached, if so return
227 227
 		$ldap_users = $this->access->connection->getFromCache($cachekey);
228
-		if(!is_null($ldap_users)) {
228
+		if (!is_null($ldap_users)) {
229 229
 			return $ldap_users;
230 230
 		}
231 231
 
232 232
 		// if we'd pass -1 to LDAP search, we'd end up in a Protocol
233 233
 		// error. With a limit of 0, we get 0 results. So we pass null.
234
-		if($limit <= 0) {
234
+		if ($limit <= 0) {
235 235
 			$limit = null;
236 236
 		}
237 237
 		$filter = $this->access->combineFilterWithAnd(array(
238 238
 			$this->access->connection->ldapUserFilter,
239
-			$this->access->connection->ldapUserDisplayName . '=*',
239
+			$this->access->connection->ldapUserDisplayName.'=*',
240 240
 			$this->access->getFilterPartForUserSearch($search)
241 241
 		));
242 242
 
@@ -249,7 +249,7 @@  discard block
 block discarded – undo
249 249
 			$this->access->userManager->getAttributes(true),
250 250
 			$limit, $offset);
251 251
 		$ldap_users = $this->access->nextcloudUserNames($ldap_users);
252
-		Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', Util::DEBUG);
252
+		Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users).' Users found', Util::DEBUG);
253 253
 
254 254
 		$this->access->connection->writeToCache($cachekey, $ldap_users);
255 255
 		return $ldap_users;
@@ -265,29 +265,29 @@  discard block
 block discarded – undo
265 265
 	 * @throws \OC\ServerNotAvailableException
266 266
 	 */
267 267
 	public function userExistsOnLDAP($user) {
268
-		if(is_string($user)) {
268
+		if (is_string($user)) {
269 269
 			$user = $this->access->userManager->get($user);
270 270
 		}
271
-		if(is_null($user)) {
271
+		if (is_null($user)) {
272 272
 			return false;
273 273
 		}
274 274
 
275 275
 		$dn = $user->getDN();
276 276
 		//check if user really still exists by reading its entry
277
-		if(!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) {
277
+		if (!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) {
278 278
 			$lcr = $this->access->connection->getConnectionResource();
279
-			if(is_null($lcr)) {
280
-				throw new \Exception('No LDAP Connection to server ' . $this->access->connection->ldapHost);
279
+			if (is_null($lcr)) {
280
+				throw new \Exception('No LDAP Connection to server '.$this->access->connection->ldapHost);
281 281
 			}
282 282
 
283 283
 			try {
284 284
 				$uuid = $this->access->getUserMapper()->getUUIDByDN($dn);
285
-				if(!$uuid) {
285
+				if (!$uuid) {
286 286
 					return false;
287 287
 				}
288 288
 				$newDn = $this->access->getUserDnByUuid($uuid);
289 289
 				//check if renamed user is still valid by reapplying the ldap filter
290
-				if(!is_array($this->access->readAttribute($newDn, '', $this->access->connection->ldapUserFilter))) {
290
+				if (!is_array($this->access->readAttribute($newDn, '', $this->access->connection->ldapUserFilter))) {
291 291
 					return false;
292 292
 				}
293 293
 				$this->access->getUserMapper()->setDNbyUUID($newDn, $uuid);
@@ -297,7 +297,7 @@  discard block
 block discarded – undo
297 297
 			}
298 298
 		}
299 299
 
300
-		if($user instanceof OfflineUser) {
300
+		if ($user instanceof OfflineUser) {
301 301
 			$user->unmark();
302 302
 		}
303 303
 
@@ -312,18 +312,18 @@  discard block
 block discarded – undo
312 312
 	 */
313 313
 	public function userExists($uid) {
314 314
 		$userExists = $this->access->connection->getFromCache('userExists'.$uid);
315
-		if(!is_null($userExists)) {
316
-			return (bool)$userExists;
315
+		if (!is_null($userExists)) {
316
+			return (bool) $userExists;
317 317
 		}
318 318
 		//getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking.
319 319
 		$user = $this->access->userManager->get($uid);
320 320
 
321
-		if(is_null($user)) {
321
+		if (is_null($user)) {
322 322
 			Util::writeLog('user_ldap', 'No DN found for '.$uid.' on '.
323 323
 				$this->access->connection->ldapHost, Util::DEBUG);
324 324
 			$this->access->connection->writeToCache('userExists'.$uid, false);
325 325
 			return false;
326
-		} else if($user instanceof OfflineUser) {
326
+		} else if ($user instanceof OfflineUser) {
327 327
 			//express check for users marked as deleted. Returning true is
328 328
 			//necessary for cleanup
329 329
 			return true;
@@ -331,7 +331,7 @@  discard block
 block discarded – undo
331 331
 
332 332
 		$result = $this->userExistsOnLDAP($user);
333 333
 		$this->access->connection->writeToCache('userExists'.$uid, $result);
334
-		if($result === true) {
334
+		if ($result === true) {
335 335
 			$user->update();
336 336
 		}
337 337
 		return $result;
@@ -345,13 +345,13 @@  discard block
 block discarded – undo
345 345
 	*/
346 346
 	public function deleteUser($uid) {
347 347
 		$marked = $this->ocConfig->getUserValue($uid, 'user_ldap', 'isDeleted', 0);
348
-		if(intval($marked) === 0) {
348
+		if (intval($marked) === 0) {
349 349
 			\OC::$server->getLogger()->notice(
350
-				'User '.$uid . ' is not marked as deleted, not cleaning up.',
350
+				'User '.$uid.' is not marked as deleted, not cleaning up.',
351 351
 				array('app' => 'user_ldap'));
352 352
 			return false;
353 353
 		}
354
-		\OC::$server->getLogger()->info('Cleaning up after user ' . $uid,
354
+		\OC::$server->getLogger()->info('Cleaning up after user '.$uid,
355 355
 			array('app' => 'user_ldap'));
356 356
 
357 357
 		//Get Home Directory out of user preferences so we can return it later,
@@ -370,22 +370,22 @@  discard block
 block discarded – undo
370 370
 	 */
371 371
 	public function getHome($uid) {
372 372
 		// user Exists check required as it is not done in user proxy!
373
-		if(!$this->userExists($uid)) {
373
+		if (!$this->userExists($uid)) {
374 374
 			return false;
375 375
 		}
376 376
 
377 377
 		$cacheKey = 'getHome'.$uid;
378 378
 		$path = $this->access->connection->getFromCache($cacheKey);
379
-		if(!is_null($path)) {
379
+		if (!is_null($path)) {
380 380
 			return $path;
381 381
 		}
382 382
 
383 383
 		// early return path if it is a deleted user
384 384
 		$user = $this->access->userManager->get($uid);
385
-		if($user instanceof OfflineUser) {
385
+		if ($user instanceof OfflineUser) {
386 386
 			return $user->getHomePath();
387 387
 		} else if ($user === null) {
388
-			throw new NoUserException($uid . ' is not a valid user anymore');
388
+			throw new NoUserException($uid.' is not a valid user anymore');
389 389
 		}
390 390
 
391 391
 		$path = $user->getHomePath();
@@ -400,12 +400,12 @@  discard block
 block discarded – undo
400 400
 	 * @return string|false display name
401 401
 	 */
402 402
 	public function getDisplayName($uid) {
403
-		if(!$this->userExists($uid)) {
403
+		if (!$this->userExists($uid)) {
404 404
 			return false;
405 405
 		}
406 406
 
407 407
 		$cacheKey = 'getDisplayName'.$uid;
408
-		if(!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) {
408
+		if (!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) {
409 409
 			return $displayName;
410 410
 		}
411 411
 
@@ -422,10 +422,10 @@  discard block
 block discarded – undo
422 422
 			$this->access->username2dn($uid),
423 423
 			$this->access->connection->ldapUserDisplayName);
424 424
 
425
-		if($displayName && (count($displayName) > 0)) {
425
+		if ($displayName && (count($displayName) > 0)) {
426 426
 			$displayName = $displayName[0];
427 427
 
428
-			if (is_array($displayName2)){
428
+			if (is_array($displayName2)) {
429 429
 				$displayName2 = count($displayName2) > 0 ? $displayName2[0] : '';
430 430
 			}
431 431
 
@@ -454,7 +454,7 @@  discard block
 block discarded – undo
454 454
 	 */
455 455
 	public function getDisplayNames($search = '', $limit = null, $offset = null) {
456 456
 		$cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset;
457
-		if(!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) {
457
+		if (!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) {
458 458
 			return $displayNames;
459 459
 		}
460 460
 
@@ -476,12 +476,12 @@  discard block
 block discarded – undo
476 476
 	* compared with OC_USER_BACKEND_CREATE_USER etc.
477 477
 	*/
478 478
 	public function implementsActions($actions) {
479
-		return (bool)((Backend::CHECK_PASSWORD
479
+		return (bool) ((Backend::CHECK_PASSWORD
480 480
 			| Backend::GET_HOME
481 481
 			| Backend::GET_DISPLAYNAME
482 482
 			| Backend::PROVIDE_AVATAR
483 483
 			| Backend::COUNT_USERS
484
-			| ((intval($this->access->connection->turnOnPasswordChange) === 1)?(Backend::SET_PASSWORD):0))
484
+			| ((intval($this->access->connection->turnOnPasswordChange) === 1) ? (Backend::SET_PASSWORD) : 0))
485 485
 			& $actions);
486 486
 	}
487 487
 
@@ -500,7 +500,7 @@  discard block
 block discarded – undo
500 500
 	public function countUsers() {
501 501
 		$filter = $this->access->getFilterForUserCount();
502 502
 		$cacheKey = 'countUsers-'.$filter;
503
-		if(!is_null($entries = $this->access->connection->getFromCache($cacheKey))) {
503
+		if (!is_null($entries = $this->access->connection->getFromCache($cacheKey))) {
504 504
 			return $entries;
505 505
 		}
506 506
 		$entries = $this->access->countUsers($filter);
@@ -512,7 +512,7 @@  discard block
 block discarded – undo
512 512
 	 * Backend name to be shown in user management
513 513
 	 * @return string the name of the backend to be shown
514 514
 	 */
515
-	public function getBackendName(){
515
+	public function getBackendName() {
516 516
 		return 'LDAP';
517 517
 	}
518 518
 	
Please login to merge, or discard this patch.