Blame view
sources/apps/user_ldap/user_ldap.php
8.42 KB
|
03e52840d
|
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 |
<?php /** * ownCloud * * @author Dominik Schmidt * @author Artuhr Schiwon * @copyright 2011 Dominik Schmidt dev@dominik-schmidt.de * @copyright 2012 Arthur Schiwon blizzz@owncloud.com * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE * License as published by the Free Software Foundation; either * version 3 of the License, or any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU AFFERO GENERAL PUBLIC LICENSE for more details. * * You should have received a copy of the GNU Affero General Public * License along with this library. If not, see <http://www.gnu.org/licenses/>. * */ namespace OCA\user_ldap; |
|
31b7f2792
|
27 28 29 |
use OCA\user_ldap\lib\BackendUtility;
class USER_LDAP extends BackendUtility implements \OCP\UserInterface {
|
|
03e52840d
|
30 |
/** |
|
6d9380f96
|
31 32 |
* checks whether the user is allowed to change his avatar in ownCloud * @param string $uid the ownCloud user name |
|
31b7f2792
|
33 34 35 |
* @return boolean either the user can or cannot
*/
public function canChangeAvatar($uid) {
|
|
6d9380f96
|
36 37 |
$user = $this->access->userManager->get($uid);
if(is_null($user)) {
|
|
31b7f2792
|
38 39 |
return false; } |
|
6d9380f96
|
40 |
if($user->getAvatarImage() === false) {
|
|
31b7f2792
|
41 42 |
return true; } |
|
a293d369c
|
43 44 45 46 47 |
return false; } /** |
|
6d9380f96
|
48 49 50 51 |
* Check if the password is correct * @param string $uid The username * @param string $password The password * @return boolean |
|
03e52840d
|
52 53 54 55 |
*
* Check if the password is correct without logging in the user
*/
public function checkPassword($uid, $password) {
|
|
a293d369c
|
56 |
$uid = $this->access->escapeFilterPart($uid); |
|
03e52840d
|
57 |
//find out dn of the user name |
|
31b7f2792
|
58 59 60 |
$filter = \OCP\Util::mb_str_replace( '%uid', $uid, $this->access->connection->ldapLoginFilter, 'UTF-8'); $ldap_users = $this->access->fetchListOfUsers($filter, 'dn'); |
|
03e52840d
|
61 62 63 64 |
if(count($ldap_users) < 1) {
return false;
}
$dn = $ldap_users[0];
|
|
6d9380f96
|
65 66 |
$user = $this->access->userManager->get($dn);
if($user->getUsername() !== false) {
|
|
03e52840d
|
67 |
//are the credentials OK? |
|
31b7f2792
|
68 |
if(!$this->access->areCredentialsValid($dn, $password)) {
|
|
03e52840d
|
69 70 |
return false; } |
|
6d9380f96
|
71 |
$user->markLogin(); |
|
31b7f2792
|
72 |
|
|
6d9380f96
|
73 |
return $user->getUsername(); |
|
03e52840d
|
74 75 76 77 78 79 |
} return false; } /** |
|
6d9380f96
|
80 81 |
* Get a list of all users * @return string[] with all uids |
|
03e52840d
|
82 83 84 85 |
*
* Get a list of all users.
*/
public function getUsers($search = '', $limit = 10, $offset = 0) {
|
|
a293d369c
|
86 |
$search = $this->access->escapeFilterPart($search); |
|
03e52840d
|
87 88 89 |
$cachekey = 'getUsers-'.$search.'-'.$limit.'-'.$offset; //check if users are cached, if so return |
|
31b7f2792
|
90 |
$ldap_users = $this->access->connection->getFromCache($cachekey); |
|
03e52840d
|
91 92 93 94 95 96 97 98 99 |
if(!is_null($ldap_users)) {
return $ldap_users;
}
// if we'd pass -1 to LDAP search, we'd end up in a Protocol
// error. With a limit of 0, we get 0 results. So we pass null.
if($limit <= 0) {
$limit = null;
}
|
|
31b7f2792
|
100 101 102 |
$filter = $this->access->combineFilterWithAnd(array( $this->access->connection->ldapUserFilter, $this->access->getFilterPartForUserSearch($search) |
|
03e52840d
|
103 104 105 106 107 108 |
));
\OCP\Util::writeLog('user_ldap',
'getUsers: Options: search '.$search.' limit '.$limit.' offset '.$offset.' Filter: '.$filter,
\OCP\Util::DEBUG);
//do the search and translate results to owncloud names
|
|
31b7f2792
|
109 110 111 |
$ldap_users = $this->access->fetchListOfUsers( $filter, array($this->access->connection->ldapUserDisplayName, 'dn'), |
|
03e52840d
|
112 |
$limit, $offset); |
|
31b7f2792
|
113 |
$ldap_users = $this->access->ownCloudUserNames($ldap_users); |
|
03e52840d
|
114 |
\OCP\Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', \OCP\Util::DEBUG);
|
|
31b7f2792
|
115 |
$this->access->connection->writeToCache($cachekey, $ldap_users); |
|
03e52840d
|
116 117 118 119 |
return $ldap_users; } /** |
|
6d9380f96
|
120 |
* check if a user exists |
|
03e52840d
|
121 122 123 124 |
* @param string $uid the username
* @return boolean
*/
public function userExists($uid) {
|
|
31b7f2792
|
125 126 |
if($this->access->connection->isCached('userExists'.$uid)) {
return $this->access->connection->getFromCache('userExists'.$uid);
|
|
03e52840d
|
127 |
} |
|
03e52840d
|
128 |
//getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking. |
|
6d9380f96
|
129 130 |
$user = $this->access->userManager->get($uid);
if(is_null($user)) {
|
|
31b7f2792
|
131 132 133 |
\OCP\Util::writeLog('user_ldap', 'No DN found for '.$uid.' on '.
$this->access->connection->ldapHost, \OCP\Util::DEBUG);
$this->access->connection->writeToCache('userExists'.$uid, false);
|
|
03e52840d
|
134 135 |
return false; } |
|
6d9380f96
|
136 |
$dn = $user->getDN(); |
|
03e52840d
|
137 |
//check if user really still exists by reading its entry |
|
31b7f2792
|
138 |
if(!is_array($this->access->readAttribute($dn, ''))) {
|
|
a293d369c
|
139 140 |
\OCP\Util::writeLog('user_ldap', 'LDAP says no user '.$dn.' on '.
$this->access->connection->ldapHost, \OCP\Util::DEBUG);
|
|
31b7f2792
|
141 |
$this->access->connection->writeToCache('userExists'.$uid, false);
|
|
03e52840d
|
142 143 |
return false; } |
|
31b7f2792
|
144 |
$this->access->connection->writeToCache('userExists'.$uid, true);
|
|
6d9380f96
|
145 |
$user->update(); |
|
03e52840d
|
146 147 148 149 |
return true; } /** |
|
6d9380f96
|
150 151 152 |
* delete a user * @param string $uid The username of the user to delete * @return bool |
|
03e52840d
|
153 154 155 156 157 158 159 160 |
*
* Deletes a user
*/
public function deleteUser($uid) {
return false;
}
/**
|
|
6d9380f96
|
161 |
* get the user's home directory |
|
03e52840d
|
162 163 164 165 166 167 168 169 170 171 |
* @param string $uid the username
* @return boolean
*/
public function getHome($uid) {
// user Exists check required as it is not done in user proxy!
if(!$this->userExists($uid)) {
return false;
}
$cacheKey = 'getHome'.$uid;
|
|
31b7f2792
|
172 173 |
if($this->access->connection->isCached($cacheKey)) {
return $this->access->connection->getFromCache($cacheKey);
|
|
03e52840d
|
174 |
} |
|
31b7f2792
|
175 176 177 178 |
if(strpos($this->access->connection->homeFolderNamingRule, 'attr:') === 0) {
$attr = substr($this->access->connection->homeFolderNamingRule, strlen('attr:'));
$homedir = $this->access->readAttribute(
$this->access->username2dn($uid), $attr);
|
|
03e52840d
|
179 180 181 182 183 |
if($homedir && isset($homedir[0])) {
$path = $homedir[0];
//if attribute's value is an absolute path take this, otherwise append it to data dir
//check for / at the beginning or pattern c:\ resp. c:/
if(
|
|
31b7f2792
|
184 |
'/' === $path[0] |
|
03e52840d
|
185 |
|| (3 < strlen($path) && ctype_alpha($path[0]) |
|
31b7f2792
|
186 |
&& $path[1] === ':' && ('\\' === $path[2] || '/' === $path[2]))
|
|
03e52840d
|
187 188 189 190 191 192 |
) {
$homedir = $path;
} else {
$homedir = \OCP\Config::getSystemValue('datadirectory',
\OC::$SERVERROOT.'/data' ) . '/' . $homedir[0];
}
|
|
31b7f2792
|
193 |
$this->access->connection->writeToCache($cacheKey, $homedir); |
|
03e52840d
|
194 195 196 197 198 |
return $homedir; } } //false will apply default behaviour as defined and done by OC_User |
|
31b7f2792
|
199 |
$this->access->connection->writeToCache($cacheKey, false); |
|
03e52840d
|
200 201 202 203 |
return false; } /** |
|
6d9380f96
|
204 205 206 |
* get display name of the user * @param string $uid user ID of the user * @return string display name |
|
03e52840d
|
207 208 209 210 211 212 213 |
*/
public function getDisplayName($uid) {
if(!$this->userExists($uid)) {
return false;
}
$cacheKey = 'getDisplayName'.$uid;
|
|
31b7f2792
|
214 |
if(!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) {
|
|
03e52840d
|
215 216 |
return $displayName; } |
|
31b7f2792
|
217 218 219 |
$displayName = $this->access->readAttribute( $this->access->username2dn($uid), $this->access->connection->ldapUserDisplayName); |
|
03e52840d
|
220 221 |
if($displayName && (count($displayName) > 0)) {
|
|
31b7f2792
|
222 |
$this->access->connection->writeToCache($cacheKey, $displayName[0]); |
|
03e52840d
|
223 224 225 226 227 228 229 |
return $displayName[0]; } return null; } /** |
|
6d9380f96
|
230 231 |
* Get a list of all display names * @return array with all displayNames (value) and the correspondig uids (key) |
|
03e52840d
|
232 233 234 235 236 |
*
* Get a list of all display names and user ids.
*/
public function getDisplayNames($search = '', $limit = null, $offset = null) {
$cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset;
|
|
31b7f2792
|
237 |
if(!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) {
|
|
03e52840d
|
238 239 240 241 242 243 244 245 |
return $displayNames;
}
$displayNames = array();
$users = $this->getUsers($search, $limit, $offset);
foreach ($users as $user) {
$displayNames[$user] = $this->getDisplayName($user);
}
|
|
31b7f2792
|
246 |
$this->access->connection->writeToCache($cacheKey, $displayNames); |
|
03e52840d
|
247 248 |
return $displayNames; } |
|
6d9380f96
|
249 250 251 252 |
/** * Check if backend implements actions * @param int $actions bitwise-or'ed actions * @return boolean |
|
03e52840d
|
253 254 255 256 257 258 259 |
*
* Returns the supported actions as int to be
* compared with OC_USER_BACKEND_CREATE_USER etc.
*/
public function implementsActions($actions) {
return (bool)((OC_USER_BACKEND_CHECK_PASSWORD
| OC_USER_BACKEND_GET_HOME
|
|
31b7f2792
|
260 |
| OC_USER_BACKEND_GET_DISPLAYNAME |
|
a293d369c
|
261 262 |
| OC_USER_BACKEND_PROVIDE_AVATAR | OC_USER_BACKEND_COUNT_USERS) |
|
03e52840d
|
263 264 265 266 267 268 269 270 271 |
& $actions);
}
/**
* @return bool
*/
public function hasUserListings() {
return true;
}
|
|
a293d369c
|
272 273 274 275 |
/** * counts the users in LDAP * |
|
6d9380f96
|
276 |
* @return int|bool |
|
a293d369c
|
277 278 279 280 281 282 283 |
*/
public function countUsers() {
$filter = \OCP\Util::mb_str_replace(
'%uid', '*', $this->access->connection->ldapLoginFilter, 'UTF-8');
$entries = $this->access->countUsers($filter);
return $entries;
}
|
|
03e52840d
|
284 |
} |