@@ -45,291 +45,291 @@ |
||
| 45 | 45 | */ |
| 46 | 46 | class Database extends \OC\Group\Backend { |
| 47 | 47 | |
| 48 | - /** @var string[] */ |
|
| 49 | - private $groupCache = []; |
|
| 50 | - |
|
| 51 | - /** @var \OCP\IDBConnection */ |
|
| 52 | - private $dbConn; |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * \OC\Group\Database constructor. |
|
| 56 | - * |
|
| 57 | - * @param \OCP\IDBConnection|null $dbConn |
|
| 58 | - */ |
|
| 59 | - public function __construct(\OCP\IDBConnection $dbConn = null) { |
|
| 60 | - $this->dbConn = $dbConn; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * FIXME: This function should not be required! |
|
| 65 | - */ |
|
| 66 | - private function fixDI() { |
|
| 67 | - if ($this->dbConn === null) { |
|
| 68 | - $this->dbConn = \OC::$server->getDatabaseConnection(); |
|
| 69 | - } |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * Try to create a new group |
|
| 74 | - * @param string $gid The name of the group to create |
|
| 75 | - * @return bool |
|
| 76 | - * |
|
| 77 | - * Tries to create a new group. If the group name already exists, false will |
|
| 78 | - * be returned. |
|
| 79 | - */ |
|
| 80 | - public function createGroup( $gid ) { |
|
| 81 | - $this->fixDI(); |
|
| 82 | - |
|
| 83 | - // Add group |
|
| 84 | - $result = $this->dbConn->insertIfNotExist('*PREFIX*groups', [ |
|
| 85 | - 'gid' => $gid, |
|
| 86 | - ]); |
|
| 87 | - |
|
| 88 | - // Add to cache |
|
| 89 | - $this->groupCache[$gid] = $gid; |
|
| 90 | - |
|
| 91 | - return $result === 1; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * delete a group |
|
| 96 | - * @param string $gid gid of the group to delete |
|
| 97 | - * @return bool |
|
| 98 | - * |
|
| 99 | - * Deletes a group and removes it from the group_user-table |
|
| 100 | - */ |
|
| 101 | - public function deleteGroup( $gid ) { |
|
| 102 | - $this->fixDI(); |
|
| 103 | - |
|
| 104 | - // Delete the group |
|
| 105 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 106 | - $qb->delete('groups') |
|
| 107 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 108 | - ->execute(); |
|
| 109 | - |
|
| 110 | - // Delete the group-user relation |
|
| 111 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 112 | - $qb->delete('group_user') |
|
| 113 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 114 | - ->execute(); |
|
| 115 | - |
|
| 116 | - // Delete the group-groupadmin relation |
|
| 117 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 118 | - $qb->delete('group_admin') |
|
| 119 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 120 | - ->execute(); |
|
| 121 | - |
|
| 122 | - // Delete from cache |
|
| 123 | - unset($this->groupCache[$gid]); |
|
| 124 | - |
|
| 125 | - return true; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * is user in group? |
|
| 130 | - * @param string $uid uid of the user |
|
| 131 | - * @param string $gid gid of the group |
|
| 132 | - * @return bool |
|
| 133 | - * |
|
| 134 | - * Checks whether the user is member of a group or not. |
|
| 135 | - */ |
|
| 136 | - public function inGroup( $uid, $gid ) { |
|
| 137 | - $this->fixDI(); |
|
| 138 | - |
|
| 139 | - // check |
|
| 140 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 141 | - $cursor = $qb->select('uid') |
|
| 142 | - ->from('group_user') |
|
| 143 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 144 | - ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 145 | - ->execute(); |
|
| 146 | - |
|
| 147 | - $result = $cursor->fetch(); |
|
| 148 | - $cursor->closeCursor(); |
|
| 149 | - |
|
| 150 | - return $result ? true : false; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Add a user to a group |
|
| 155 | - * @param string $uid Name of the user to add to group |
|
| 156 | - * @param string $gid Name of the group in which add the user |
|
| 157 | - * @return bool |
|
| 158 | - * |
|
| 159 | - * Adds a user to a group. |
|
| 160 | - */ |
|
| 161 | - public function addToGroup( $uid, $gid ) { |
|
| 162 | - $this->fixDI(); |
|
| 163 | - |
|
| 164 | - // No duplicate entries! |
|
| 165 | - if( !$this->inGroup( $uid, $gid )) { |
|
| 166 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 167 | - $qb->insert('group_user') |
|
| 168 | - ->setValue('uid', $qb->createNamedParameter($uid)) |
|
| 169 | - ->setValue('gid', $qb->createNamedParameter($gid)) |
|
| 170 | - ->execute(); |
|
| 171 | - return true; |
|
| 172 | - }else{ |
|
| 173 | - return false; |
|
| 174 | - } |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - /** |
|
| 178 | - * Removes a user from a group |
|
| 179 | - * @param string $uid Name of the user to remove from group |
|
| 180 | - * @param string $gid Name of the group from which remove the user |
|
| 181 | - * @return bool |
|
| 182 | - * |
|
| 183 | - * removes the user from a group. |
|
| 184 | - */ |
|
| 185 | - public function removeFromGroup( $uid, $gid ) { |
|
| 186 | - $this->fixDI(); |
|
| 187 | - |
|
| 188 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 189 | - $qb->delete('group_user') |
|
| 190 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 191 | - ->andWhere($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 192 | - ->execute(); |
|
| 193 | - |
|
| 194 | - return true; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Get all groups a user belongs to |
|
| 199 | - * @param string $uid Name of the user |
|
| 200 | - * @return array an array of group names |
|
| 201 | - * |
|
| 202 | - * This function fetches all groups a user belongs to. It does not check |
|
| 203 | - * if the user exists at all. |
|
| 204 | - */ |
|
| 205 | - public function getUserGroups( $uid ) { |
|
| 206 | - //guests has empty or null $uid |
|
| 207 | - if ($uid === null || $uid === '') { |
|
| 208 | - return []; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - $this->fixDI(); |
|
| 212 | - |
|
| 213 | - // No magic! |
|
| 214 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 215 | - $cursor = $qb->select('gid') |
|
| 216 | - ->from('group_user') |
|
| 217 | - ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 218 | - ->execute(); |
|
| 219 | - |
|
| 220 | - $groups = []; |
|
| 221 | - while( $row = $cursor->fetch()) { |
|
| 222 | - $groups[] = $row["gid"]; |
|
| 223 | - $this->groupCache[$row['gid']] = $row['gid']; |
|
| 224 | - } |
|
| 225 | - $cursor->closeCursor(); |
|
| 226 | - |
|
| 227 | - return $groups; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * get a list of all groups |
|
| 232 | - * @param string $search |
|
| 233 | - * @param int $limit |
|
| 234 | - * @param int $offset |
|
| 235 | - * @return array an array of group names |
|
| 236 | - * |
|
| 237 | - * Returns a list with all groups |
|
| 238 | - */ |
|
| 239 | - public function getGroups($search = '', $limit = null, $offset = null) { |
|
| 240 | - $parameters = []; |
|
| 241 | - $searchLike = ''; |
|
| 242 | - if ($search !== '') { |
|
| 243 | - $parameters[] = '%' . $search . '%'; |
|
| 244 | - $searchLike = ' WHERE LOWER(`gid`) LIKE LOWER(?)'; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - $stmt = \OC_DB::prepare('SELECT `gid` FROM `*PREFIX*groups`' . $searchLike . ' ORDER BY `gid` ASC', $limit, $offset); |
|
| 248 | - $result = $stmt->execute($parameters); |
|
| 249 | - $groups = array(); |
|
| 250 | - while ($row = $result->fetchRow()) { |
|
| 251 | - $groups[] = $row['gid']; |
|
| 252 | - } |
|
| 253 | - return $groups; |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * check if a group exists |
|
| 258 | - * @param string $gid |
|
| 259 | - * @return bool |
|
| 260 | - */ |
|
| 261 | - public function groupExists($gid) { |
|
| 262 | - $this->fixDI(); |
|
| 263 | - |
|
| 264 | - // Check cache first |
|
| 265 | - if (isset($this->groupCache[$gid])) { |
|
| 266 | - return true; |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - $qb = $this->dbConn->getQueryBuilder(); |
|
| 270 | - $cursor = $qb->select('gid') |
|
| 271 | - ->from('groups') |
|
| 272 | - ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 273 | - ->execute(); |
|
| 274 | - $result = $cursor->fetch(); |
|
| 275 | - $cursor->closeCursor(); |
|
| 276 | - |
|
| 277 | - if ($result !== false) { |
|
| 278 | - $this->groupCache[$gid] = $gid; |
|
| 279 | - return true; |
|
| 280 | - } |
|
| 281 | - return false; |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * get a list of all users in a group |
|
| 286 | - * @param string $gid |
|
| 287 | - * @param string $search |
|
| 288 | - * @param int $limit |
|
| 289 | - * @param int $offset |
|
| 290 | - * @return array an array of user ids |
|
| 291 | - */ |
|
| 292 | - public function usersInGroup($gid, $search = '', $limit = null, $offset = null) { |
|
| 293 | - $parameters = [$gid]; |
|
| 294 | - $searchLike = ''; |
|
| 295 | - if ($search !== '') { |
|
| 296 | - $parameters[] = '%' . $this->dbConn->escapeLikeParameter($search) . '%'; |
|
| 297 | - $searchLike = ' AND `uid` LIKE ?'; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - $stmt = \OC_DB::prepare('SELECT `uid` FROM `*PREFIX*group_user` WHERE `gid` = ?' . $searchLike . ' ORDER BY `uid` ASC', |
|
| 301 | - $limit, |
|
| 302 | - $offset); |
|
| 303 | - $result = $stmt->execute($parameters); |
|
| 304 | - $users = array(); |
|
| 305 | - while ($row = $result->fetchRow()) { |
|
| 306 | - $users[] = $row['uid']; |
|
| 307 | - } |
|
| 308 | - return $users; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * get the number of all users matching the search string in a group |
|
| 313 | - * @param string $gid |
|
| 314 | - * @param string $search |
|
| 315 | - * @return int|false |
|
| 316 | - * @throws \OC\DatabaseException |
|
| 317 | - */ |
|
| 318 | - public function countUsersInGroup($gid, $search = '') { |
|
| 319 | - $parameters = [$gid]; |
|
| 320 | - $searchLike = ''; |
|
| 321 | - if ($search !== '') { |
|
| 322 | - $parameters[] = '%' . $this->dbConn->escapeLikeParameter($search) . '%'; |
|
| 323 | - $searchLike = ' AND `uid` LIKE ?'; |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - $stmt = \OC_DB::prepare('SELECT COUNT(`uid`) AS `count` FROM `*PREFIX*group_user` WHERE `gid` = ?' . $searchLike); |
|
| 327 | - $result = $stmt->execute($parameters); |
|
| 328 | - $count = $result->fetchOne(); |
|
| 329 | - if($count !== false) { |
|
| 330 | - $count = (int)$count; |
|
| 331 | - } |
|
| 332 | - return $count; |
|
| 333 | - } |
|
| 48 | + /** @var string[] */ |
|
| 49 | + private $groupCache = []; |
|
| 50 | + |
|
| 51 | + /** @var \OCP\IDBConnection */ |
|
| 52 | + private $dbConn; |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * \OC\Group\Database constructor. |
|
| 56 | + * |
|
| 57 | + * @param \OCP\IDBConnection|null $dbConn |
|
| 58 | + */ |
|
| 59 | + public function __construct(\OCP\IDBConnection $dbConn = null) { |
|
| 60 | + $this->dbConn = $dbConn; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * FIXME: This function should not be required! |
|
| 65 | + */ |
|
| 66 | + private function fixDI() { |
|
| 67 | + if ($this->dbConn === null) { |
|
| 68 | + $this->dbConn = \OC::$server->getDatabaseConnection(); |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * Try to create a new group |
|
| 74 | + * @param string $gid The name of the group to create |
|
| 75 | + * @return bool |
|
| 76 | + * |
|
| 77 | + * Tries to create a new group. If the group name already exists, false will |
|
| 78 | + * be returned. |
|
| 79 | + */ |
|
| 80 | + public function createGroup( $gid ) { |
|
| 81 | + $this->fixDI(); |
|
| 82 | + |
|
| 83 | + // Add group |
|
| 84 | + $result = $this->dbConn->insertIfNotExist('*PREFIX*groups', [ |
|
| 85 | + 'gid' => $gid, |
|
| 86 | + ]); |
|
| 87 | + |
|
| 88 | + // Add to cache |
|
| 89 | + $this->groupCache[$gid] = $gid; |
|
| 90 | + |
|
| 91 | + return $result === 1; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * delete a group |
|
| 96 | + * @param string $gid gid of the group to delete |
|
| 97 | + * @return bool |
|
| 98 | + * |
|
| 99 | + * Deletes a group and removes it from the group_user-table |
|
| 100 | + */ |
|
| 101 | + public function deleteGroup( $gid ) { |
|
| 102 | + $this->fixDI(); |
|
| 103 | + |
|
| 104 | + // Delete the group |
|
| 105 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 106 | + $qb->delete('groups') |
|
| 107 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 108 | + ->execute(); |
|
| 109 | + |
|
| 110 | + // Delete the group-user relation |
|
| 111 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 112 | + $qb->delete('group_user') |
|
| 113 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 114 | + ->execute(); |
|
| 115 | + |
|
| 116 | + // Delete the group-groupadmin relation |
|
| 117 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 118 | + $qb->delete('group_admin') |
|
| 119 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 120 | + ->execute(); |
|
| 121 | + |
|
| 122 | + // Delete from cache |
|
| 123 | + unset($this->groupCache[$gid]); |
|
| 124 | + |
|
| 125 | + return true; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * is user in group? |
|
| 130 | + * @param string $uid uid of the user |
|
| 131 | + * @param string $gid gid of the group |
|
| 132 | + * @return bool |
|
| 133 | + * |
|
| 134 | + * Checks whether the user is member of a group or not. |
|
| 135 | + */ |
|
| 136 | + public function inGroup( $uid, $gid ) { |
|
| 137 | + $this->fixDI(); |
|
| 138 | + |
|
| 139 | + // check |
|
| 140 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 141 | + $cursor = $qb->select('uid') |
|
| 142 | + ->from('group_user') |
|
| 143 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 144 | + ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 145 | + ->execute(); |
|
| 146 | + |
|
| 147 | + $result = $cursor->fetch(); |
|
| 148 | + $cursor->closeCursor(); |
|
| 149 | + |
|
| 150 | + return $result ? true : false; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Add a user to a group |
|
| 155 | + * @param string $uid Name of the user to add to group |
|
| 156 | + * @param string $gid Name of the group in which add the user |
|
| 157 | + * @return bool |
|
| 158 | + * |
|
| 159 | + * Adds a user to a group. |
|
| 160 | + */ |
|
| 161 | + public function addToGroup( $uid, $gid ) { |
|
| 162 | + $this->fixDI(); |
|
| 163 | + |
|
| 164 | + // No duplicate entries! |
|
| 165 | + if( !$this->inGroup( $uid, $gid )) { |
|
| 166 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 167 | + $qb->insert('group_user') |
|
| 168 | + ->setValue('uid', $qb->createNamedParameter($uid)) |
|
| 169 | + ->setValue('gid', $qb->createNamedParameter($gid)) |
|
| 170 | + ->execute(); |
|
| 171 | + return true; |
|
| 172 | + }else{ |
|
| 173 | + return false; |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + /** |
|
| 178 | + * Removes a user from a group |
|
| 179 | + * @param string $uid Name of the user to remove from group |
|
| 180 | + * @param string $gid Name of the group from which remove the user |
|
| 181 | + * @return bool |
|
| 182 | + * |
|
| 183 | + * removes the user from a group. |
|
| 184 | + */ |
|
| 185 | + public function removeFromGroup( $uid, $gid ) { |
|
| 186 | + $this->fixDI(); |
|
| 187 | + |
|
| 188 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 189 | + $qb->delete('group_user') |
|
| 190 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 191 | + ->andWhere($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 192 | + ->execute(); |
|
| 193 | + |
|
| 194 | + return true; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Get all groups a user belongs to |
|
| 199 | + * @param string $uid Name of the user |
|
| 200 | + * @return array an array of group names |
|
| 201 | + * |
|
| 202 | + * This function fetches all groups a user belongs to. It does not check |
|
| 203 | + * if the user exists at all. |
|
| 204 | + */ |
|
| 205 | + public function getUserGroups( $uid ) { |
|
| 206 | + //guests has empty or null $uid |
|
| 207 | + if ($uid === null || $uid === '') { |
|
| 208 | + return []; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + $this->fixDI(); |
|
| 212 | + |
|
| 213 | + // No magic! |
|
| 214 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 215 | + $cursor = $qb->select('gid') |
|
| 216 | + ->from('group_user') |
|
| 217 | + ->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid))) |
|
| 218 | + ->execute(); |
|
| 219 | + |
|
| 220 | + $groups = []; |
|
| 221 | + while( $row = $cursor->fetch()) { |
|
| 222 | + $groups[] = $row["gid"]; |
|
| 223 | + $this->groupCache[$row['gid']] = $row['gid']; |
|
| 224 | + } |
|
| 225 | + $cursor->closeCursor(); |
|
| 226 | + |
|
| 227 | + return $groups; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * get a list of all groups |
|
| 232 | + * @param string $search |
|
| 233 | + * @param int $limit |
|
| 234 | + * @param int $offset |
|
| 235 | + * @return array an array of group names |
|
| 236 | + * |
|
| 237 | + * Returns a list with all groups |
|
| 238 | + */ |
|
| 239 | + public function getGroups($search = '', $limit = null, $offset = null) { |
|
| 240 | + $parameters = []; |
|
| 241 | + $searchLike = ''; |
|
| 242 | + if ($search !== '') { |
|
| 243 | + $parameters[] = '%' . $search . '%'; |
|
| 244 | + $searchLike = ' WHERE LOWER(`gid`) LIKE LOWER(?)'; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + $stmt = \OC_DB::prepare('SELECT `gid` FROM `*PREFIX*groups`' . $searchLike . ' ORDER BY `gid` ASC', $limit, $offset); |
|
| 248 | + $result = $stmt->execute($parameters); |
|
| 249 | + $groups = array(); |
|
| 250 | + while ($row = $result->fetchRow()) { |
|
| 251 | + $groups[] = $row['gid']; |
|
| 252 | + } |
|
| 253 | + return $groups; |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * check if a group exists |
|
| 258 | + * @param string $gid |
|
| 259 | + * @return bool |
|
| 260 | + */ |
|
| 261 | + public function groupExists($gid) { |
|
| 262 | + $this->fixDI(); |
|
| 263 | + |
|
| 264 | + // Check cache first |
|
| 265 | + if (isset($this->groupCache[$gid])) { |
|
| 266 | + return true; |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + $qb = $this->dbConn->getQueryBuilder(); |
|
| 270 | + $cursor = $qb->select('gid') |
|
| 271 | + ->from('groups') |
|
| 272 | + ->where($qb->expr()->eq('gid', $qb->createNamedParameter($gid))) |
|
| 273 | + ->execute(); |
|
| 274 | + $result = $cursor->fetch(); |
|
| 275 | + $cursor->closeCursor(); |
|
| 276 | + |
|
| 277 | + if ($result !== false) { |
|
| 278 | + $this->groupCache[$gid] = $gid; |
|
| 279 | + return true; |
|
| 280 | + } |
|
| 281 | + return false; |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * get a list of all users in a group |
|
| 286 | + * @param string $gid |
|
| 287 | + * @param string $search |
|
| 288 | + * @param int $limit |
|
| 289 | + * @param int $offset |
|
| 290 | + * @return array an array of user ids |
|
| 291 | + */ |
|
| 292 | + public function usersInGroup($gid, $search = '', $limit = null, $offset = null) { |
|
| 293 | + $parameters = [$gid]; |
|
| 294 | + $searchLike = ''; |
|
| 295 | + if ($search !== '') { |
|
| 296 | + $parameters[] = '%' . $this->dbConn->escapeLikeParameter($search) . '%'; |
|
| 297 | + $searchLike = ' AND `uid` LIKE ?'; |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + $stmt = \OC_DB::prepare('SELECT `uid` FROM `*PREFIX*group_user` WHERE `gid` = ?' . $searchLike . ' ORDER BY `uid` ASC', |
|
| 301 | + $limit, |
|
| 302 | + $offset); |
|
| 303 | + $result = $stmt->execute($parameters); |
|
| 304 | + $users = array(); |
|
| 305 | + while ($row = $result->fetchRow()) { |
|
| 306 | + $users[] = $row['uid']; |
|
| 307 | + } |
|
| 308 | + return $users; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * get the number of all users matching the search string in a group |
|
| 313 | + * @param string $gid |
|
| 314 | + * @param string $search |
|
| 315 | + * @return int|false |
|
| 316 | + * @throws \OC\DatabaseException |
|
| 317 | + */ |
|
| 318 | + public function countUsersInGroup($gid, $search = '') { |
|
| 319 | + $parameters = [$gid]; |
|
| 320 | + $searchLike = ''; |
|
| 321 | + if ($search !== '') { |
|
| 322 | + $parameters[] = '%' . $this->dbConn->escapeLikeParameter($search) . '%'; |
|
| 323 | + $searchLike = ' AND `uid` LIKE ?'; |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + $stmt = \OC_DB::prepare('SELECT COUNT(`uid`) AS `count` FROM `*PREFIX*group_user` WHERE `gid` = ?' . $searchLike); |
|
| 327 | + $result = $stmt->execute($parameters); |
|
| 328 | + $count = $result->fetchOne(); |
|
| 329 | + if($count !== false) { |
|
| 330 | + $count = (int)$count; |
|
| 331 | + } |
|
| 332 | + return $count; |
|
| 333 | + } |
|
| 334 | 334 | |
| 335 | 335 | } |
@@ -77,7 +77,7 @@ discard block |
||
| 77 | 77 | * Tries to create a new group. If the group name already exists, false will |
| 78 | 78 | * be returned. |
| 79 | 79 | */ |
| 80 | - public function createGroup( $gid ) { |
|
| 80 | + public function createGroup($gid) { |
|
| 81 | 81 | $this->fixDI(); |
| 82 | 82 | |
| 83 | 83 | // Add group |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | * |
| 99 | 99 | * Deletes a group and removes it from the group_user-table |
| 100 | 100 | */ |
| 101 | - public function deleteGroup( $gid ) { |
|
| 101 | + public function deleteGroup($gid) { |
|
| 102 | 102 | $this->fixDI(); |
| 103 | 103 | |
| 104 | 104 | // Delete the group |
@@ -133,7 +133,7 @@ discard block |
||
| 133 | 133 | * |
| 134 | 134 | * Checks whether the user is member of a group or not. |
| 135 | 135 | */ |
| 136 | - public function inGroup( $uid, $gid ) { |
|
| 136 | + public function inGroup($uid, $gid) { |
|
| 137 | 137 | $this->fixDI(); |
| 138 | 138 | |
| 139 | 139 | // check |
@@ -158,18 +158,18 @@ discard block |
||
| 158 | 158 | * |
| 159 | 159 | * Adds a user to a group. |
| 160 | 160 | */ |
| 161 | - public function addToGroup( $uid, $gid ) { |
|
| 161 | + public function addToGroup($uid, $gid) { |
|
| 162 | 162 | $this->fixDI(); |
| 163 | 163 | |
| 164 | 164 | // No duplicate entries! |
| 165 | - if( !$this->inGroup( $uid, $gid )) { |
|
| 165 | + if (!$this->inGroup($uid, $gid)) { |
|
| 166 | 166 | $qb = $this->dbConn->getQueryBuilder(); |
| 167 | 167 | $qb->insert('group_user') |
| 168 | 168 | ->setValue('uid', $qb->createNamedParameter($uid)) |
| 169 | 169 | ->setValue('gid', $qb->createNamedParameter($gid)) |
| 170 | 170 | ->execute(); |
| 171 | 171 | return true; |
| 172 | - }else{ |
|
| 172 | + } else { |
|
| 173 | 173 | return false; |
| 174 | 174 | } |
| 175 | 175 | } |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | * |
| 183 | 183 | * removes the user from a group. |
| 184 | 184 | */ |
| 185 | - public function removeFromGroup( $uid, $gid ) { |
|
| 185 | + public function removeFromGroup($uid, $gid) { |
|
| 186 | 186 | $this->fixDI(); |
| 187 | 187 | |
| 188 | 188 | $qb = $this->dbConn->getQueryBuilder(); |
@@ -202,7 +202,7 @@ discard block |
||
| 202 | 202 | * This function fetches all groups a user belongs to. It does not check |
| 203 | 203 | * if the user exists at all. |
| 204 | 204 | */ |
| 205 | - public function getUserGroups( $uid ) { |
|
| 205 | + public function getUserGroups($uid) { |
|
| 206 | 206 | //guests has empty or null $uid |
| 207 | 207 | if ($uid === null || $uid === '') { |
| 208 | 208 | return []; |
@@ -218,7 +218,7 @@ discard block |
||
| 218 | 218 | ->execute(); |
| 219 | 219 | |
| 220 | 220 | $groups = []; |
| 221 | - while( $row = $cursor->fetch()) { |
|
| 221 | + while ($row = $cursor->fetch()) { |
|
| 222 | 222 | $groups[] = $row["gid"]; |
| 223 | 223 | $this->groupCache[$row['gid']] = $row['gid']; |
| 224 | 224 | } |
@@ -240,11 +240,11 @@ discard block |
||
| 240 | 240 | $parameters = []; |
| 241 | 241 | $searchLike = ''; |
| 242 | 242 | if ($search !== '') { |
| 243 | - $parameters[] = '%' . $search . '%'; |
|
| 243 | + $parameters[] = '%'.$search.'%'; |
|
| 244 | 244 | $searchLike = ' WHERE LOWER(`gid`) LIKE LOWER(?)'; |
| 245 | 245 | } |
| 246 | 246 | |
| 247 | - $stmt = \OC_DB::prepare('SELECT `gid` FROM `*PREFIX*groups`' . $searchLike . ' ORDER BY `gid` ASC', $limit, $offset); |
|
| 247 | + $stmt = \OC_DB::prepare('SELECT `gid` FROM `*PREFIX*groups`'.$searchLike.' ORDER BY `gid` ASC', $limit, $offset); |
|
| 248 | 248 | $result = $stmt->execute($parameters); |
| 249 | 249 | $groups = array(); |
| 250 | 250 | while ($row = $result->fetchRow()) { |
@@ -293,11 +293,11 @@ discard block |
||
| 293 | 293 | $parameters = [$gid]; |
| 294 | 294 | $searchLike = ''; |
| 295 | 295 | if ($search !== '') { |
| 296 | - $parameters[] = '%' . $this->dbConn->escapeLikeParameter($search) . '%'; |
|
| 296 | + $parameters[] = '%'.$this->dbConn->escapeLikeParameter($search).'%'; |
|
| 297 | 297 | $searchLike = ' AND `uid` LIKE ?'; |
| 298 | 298 | } |
| 299 | 299 | |
| 300 | - $stmt = \OC_DB::prepare('SELECT `uid` FROM `*PREFIX*group_user` WHERE `gid` = ?' . $searchLike . ' ORDER BY `uid` ASC', |
|
| 300 | + $stmt = \OC_DB::prepare('SELECT `uid` FROM `*PREFIX*group_user` WHERE `gid` = ?'.$searchLike.' ORDER BY `uid` ASC', |
|
| 301 | 301 | $limit, |
| 302 | 302 | $offset); |
| 303 | 303 | $result = $stmt->execute($parameters); |
@@ -319,15 +319,15 @@ discard block |
||
| 319 | 319 | $parameters = [$gid]; |
| 320 | 320 | $searchLike = ''; |
| 321 | 321 | if ($search !== '') { |
| 322 | - $parameters[] = '%' . $this->dbConn->escapeLikeParameter($search) . '%'; |
|
| 322 | + $parameters[] = '%'.$this->dbConn->escapeLikeParameter($search).'%'; |
|
| 323 | 323 | $searchLike = ' AND `uid` LIKE ?'; |
| 324 | 324 | } |
| 325 | 325 | |
| 326 | - $stmt = \OC_DB::prepare('SELECT COUNT(`uid`) AS `count` FROM `*PREFIX*group_user` WHERE `gid` = ?' . $searchLike); |
|
| 326 | + $stmt = \OC_DB::prepare('SELECT COUNT(`uid`) AS `count` FROM `*PREFIX*group_user` WHERE `gid` = ?'.$searchLike); |
|
| 327 | 327 | $result = $stmt->execute($parameters); |
| 328 | 328 | $count = $result->fetchOne(); |
| 329 | - if($count !== false) { |
|
| 330 | - $count = (int)$count; |
|
| 329 | + if ($count !== false) { |
|
| 330 | + $count = (int) $count; |
|
| 331 | 331 | } |
| 332 | 332 | return $count; |
| 333 | 333 | } |
@@ -59,1636 +59,1636 @@ discard block |
||
| 59 | 59 | * @package OCA\User_LDAP |
| 60 | 60 | */ |
| 61 | 61 | class Access extends LDAPUtility implements IUserTools { |
| 62 | - const UUID_ATTRIBUTES = ['entryuuid', 'nsuniqueid', 'objectguid', 'guid', 'ipauniqueid']; |
|
| 63 | - |
|
| 64 | - /** @var \OCA\User_LDAP\Connection */ |
|
| 65 | - public $connection; |
|
| 66 | - /** @var Manager */ |
|
| 67 | - public $userManager; |
|
| 68 | - //never ever check this var directly, always use getPagedSearchResultState |
|
| 69 | - protected $pagedSearchedSuccessful; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @var string[] $cookies an array of returned Paged Result cookies |
|
| 73 | - */ |
|
| 74 | - protected $cookies = array(); |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @var string $lastCookie the last cookie returned from a Paged Results |
|
| 78 | - * operation, defaults to an empty string |
|
| 79 | - */ |
|
| 80 | - protected $lastCookie = ''; |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * @var AbstractMapping $userMapper |
|
| 84 | - */ |
|
| 85 | - protected $userMapper; |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @var AbstractMapping $userMapper |
|
| 89 | - */ |
|
| 90 | - protected $groupMapper; |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @var \OCA\User_LDAP\Helper |
|
| 94 | - */ |
|
| 95 | - private $helper; |
|
| 96 | - /** @var IConfig */ |
|
| 97 | - private $config; |
|
| 98 | - |
|
| 99 | - public function __construct( |
|
| 100 | - Connection $connection, |
|
| 101 | - ILDAPWrapper $ldap, |
|
| 102 | - Manager $userManager, |
|
| 103 | - Helper $helper, |
|
| 104 | - IConfig $config |
|
| 105 | - ) { |
|
| 106 | - parent::__construct($ldap); |
|
| 107 | - $this->connection = $connection; |
|
| 108 | - $this->userManager = $userManager; |
|
| 109 | - $this->userManager->setLdapAccess($this); |
|
| 110 | - $this->helper = $helper; |
|
| 111 | - $this->config = $config; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * sets the User Mapper |
|
| 116 | - * @param AbstractMapping $mapper |
|
| 117 | - */ |
|
| 118 | - public function setUserMapper(AbstractMapping $mapper) { |
|
| 119 | - $this->userMapper = $mapper; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * returns the User Mapper |
|
| 124 | - * @throws \Exception |
|
| 125 | - * @return AbstractMapping |
|
| 126 | - */ |
|
| 127 | - public function getUserMapper() { |
|
| 128 | - if(is_null($this->userMapper)) { |
|
| 129 | - throw new \Exception('UserMapper was not assigned to this Access instance.'); |
|
| 130 | - } |
|
| 131 | - return $this->userMapper; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * sets the Group Mapper |
|
| 136 | - * @param AbstractMapping $mapper |
|
| 137 | - */ |
|
| 138 | - public function setGroupMapper(AbstractMapping $mapper) { |
|
| 139 | - $this->groupMapper = $mapper; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * returns the Group Mapper |
|
| 144 | - * @throws \Exception |
|
| 145 | - * @return AbstractMapping |
|
| 146 | - */ |
|
| 147 | - public function getGroupMapper() { |
|
| 148 | - if(is_null($this->groupMapper)) { |
|
| 149 | - throw new \Exception('GroupMapper was not assigned to this Access instance.'); |
|
| 150 | - } |
|
| 151 | - return $this->groupMapper; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * @return bool |
|
| 156 | - */ |
|
| 157 | - private function checkConnection() { |
|
| 158 | - return ($this->connection instanceof Connection); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * returns the Connection instance |
|
| 163 | - * @return \OCA\User_LDAP\Connection |
|
| 164 | - */ |
|
| 165 | - public function getConnection() { |
|
| 166 | - return $this->connection; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * reads a given attribute for an LDAP record identified by a DN |
|
| 171 | - * |
|
| 172 | - * @param string $dn the record in question |
|
| 173 | - * @param string $attr the attribute that shall be retrieved |
|
| 174 | - * if empty, just check the record's existence |
|
| 175 | - * @param string $filter |
|
| 176 | - * @return array|false an array of values on success or an empty |
|
| 177 | - * array if $attr is empty, false otherwise |
|
| 178 | - * @throws ServerNotAvailableException |
|
| 179 | - */ |
|
| 180 | - public function readAttribute($dn, $attr, $filter = 'objectClass=*') { |
|
| 181 | - if(!$this->checkConnection()) { |
|
| 182 | - \OCP\Util::writeLog('user_ldap', |
|
| 183 | - 'No LDAP Connector assigned, access impossible for readAttribute.', |
|
| 184 | - \OCP\Util::WARN); |
|
| 185 | - return false; |
|
| 186 | - } |
|
| 187 | - $cr = $this->connection->getConnectionResource(); |
|
| 188 | - if(!$this->ldap->isResource($cr)) { |
|
| 189 | - //LDAP not available |
|
| 190 | - \OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', \OCP\Util::DEBUG); |
|
| 191 | - return false; |
|
| 192 | - } |
|
| 193 | - //Cancel possibly running Paged Results operation, otherwise we run in |
|
| 194 | - //LDAP protocol errors |
|
| 195 | - $this->abandonPagedSearch(); |
|
| 196 | - // openLDAP requires that we init a new Paged Search. Not needed by AD, |
|
| 197 | - // but does not hurt either. |
|
| 198 | - $pagingSize = (int)$this->connection->ldapPagingSize; |
|
| 199 | - // 0 won't result in replies, small numbers may leave out groups |
|
| 200 | - // (cf. #12306), 500 is default for paging and should work everywhere. |
|
| 201 | - $maxResults = $pagingSize > 20 ? $pagingSize : 500; |
|
| 202 | - $attr = mb_strtolower($attr, 'UTF-8'); |
|
| 203 | - // the actual read attribute later may contain parameters on a ranged |
|
| 204 | - // request, e.g. member;range=99-199. Depends on server reply. |
|
| 205 | - $attrToRead = $attr; |
|
| 206 | - |
|
| 207 | - $values = []; |
|
| 208 | - $isRangeRequest = false; |
|
| 209 | - do { |
|
| 210 | - $result = $this->executeRead($cr, $dn, $attrToRead, $filter, $maxResults); |
|
| 211 | - if(is_bool($result)) { |
|
| 212 | - // when an exists request was run and it was successful, an empty |
|
| 213 | - // array must be returned |
|
| 214 | - return $result ? [] : false; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - if (!$isRangeRequest) { |
|
| 218 | - $values = $this->extractAttributeValuesFromResult($result, $attr); |
|
| 219 | - if (!empty($values)) { |
|
| 220 | - return $values; |
|
| 221 | - } |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - $isRangeRequest = false; |
|
| 225 | - $result = $this->extractRangeData($result, $attr); |
|
| 226 | - if (!empty($result)) { |
|
| 227 | - $normalizedResult = $this->extractAttributeValuesFromResult( |
|
| 228 | - [ $attr => $result['values'] ], |
|
| 229 | - $attr |
|
| 230 | - ); |
|
| 231 | - $values = array_merge($values, $normalizedResult); |
|
| 232 | - |
|
| 233 | - if($result['rangeHigh'] === '*') { |
|
| 234 | - // when server replies with * as high range value, there are |
|
| 235 | - // no more results left |
|
| 236 | - return $values; |
|
| 237 | - } else { |
|
| 238 | - $low = $result['rangeHigh'] + 1; |
|
| 239 | - $attrToRead = $result['attributeName'] . ';range=' . $low . '-*'; |
|
| 240 | - $isRangeRequest = true; |
|
| 241 | - } |
|
| 242 | - } |
|
| 243 | - } while($isRangeRequest); |
|
| 244 | - |
|
| 245 | - \OCP\Util::writeLog('user_ldap', 'Requested attribute '.$attr.' not found for '.$dn, \OCP\Util::DEBUG); |
|
| 246 | - return false; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * Runs an read operation against LDAP |
|
| 251 | - * |
|
| 252 | - * @param resource $cr the LDAP connection |
|
| 253 | - * @param string $dn |
|
| 254 | - * @param string $attribute |
|
| 255 | - * @param string $filter |
|
| 256 | - * @param int $maxResults |
|
| 257 | - * @return array|bool false if there was any error, true if an exists check |
|
| 258 | - * was performed and the requested DN found, array with the |
|
| 259 | - * returned data on a successful usual operation |
|
| 260 | - * @throws ServerNotAvailableException |
|
| 261 | - */ |
|
| 262 | - public function executeRead($cr, $dn, $attribute, $filter, $maxResults) { |
|
| 263 | - $this->initPagedSearch($filter, array($dn), array($attribute), $maxResults, 0); |
|
| 264 | - $dn = $this->helper->DNasBaseParameter($dn); |
|
| 265 | - $rr = @$this->invokeLDAPMethod('read', $cr, $dn, $filter, array($attribute)); |
|
| 266 | - if (!$this->ldap->isResource($rr)) { |
|
| 267 | - if ($attribute !== '') { |
|
| 268 | - //do not throw this message on userExists check, irritates |
|
| 269 | - \OCP\Util::writeLog('user_ldap', 'readAttribute failed for DN ' . $dn, \OCP\Util::DEBUG); |
|
| 270 | - } |
|
| 271 | - //in case an error occurs , e.g. object does not exist |
|
| 272 | - return false; |
|
| 273 | - } |
|
| 274 | - if ($attribute === '' && ($filter === 'objectclass=*' || $this->invokeLDAPMethod('countEntries', $cr, $rr) === 1)) { |
|
| 275 | - \OCP\Util::writeLog('user_ldap', 'readAttribute: ' . $dn . ' found', \OCP\Util::DEBUG); |
|
| 276 | - return true; |
|
| 277 | - } |
|
| 278 | - $er = $this->invokeLDAPMethod('firstEntry', $cr, $rr); |
|
| 279 | - if (!$this->ldap->isResource($er)) { |
|
| 280 | - //did not match the filter, return false |
|
| 281 | - return false; |
|
| 282 | - } |
|
| 283 | - //LDAP attributes are not case sensitive |
|
| 284 | - $result = \OCP\Util::mb_array_change_key_case( |
|
| 285 | - $this->invokeLDAPMethod('getAttributes', $cr, $er), MB_CASE_LOWER, 'UTF-8'); |
|
| 286 | - |
|
| 287 | - return $result; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Normalizes a result grom getAttributes(), i.e. handles DNs and binary |
|
| 292 | - * data if present. |
|
| 293 | - * |
|
| 294 | - * @param array $result from ILDAPWrapper::getAttributes() |
|
| 295 | - * @param string $attribute the attribute name that was read |
|
| 296 | - * @return string[] |
|
| 297 | - */ |
|
| 298 | - public function extractAttributeValuesFromResult($result, $attribute) { |
|
| 299 | - $values = []; |
|
| 300 | - if(isset($result[$attribute]) && $result[$attribute]['count'] > 0) { |
|
| 301 | - $lowercaseAttribute = strtolower($attribute); |
|
| 302 | - for($i=0;$i<$result[$attribute]['count'];$i++) { |
|
| 303 | - if($this->resemblesDN($attribute)) { |
|
| 304 | - $values[] = $this->helper->sanitizeDN($result[$attribute][$i]); |
|
| 305 | - } elseif($lowercaseAttribute === 'objectguid' || $lowercaseAttribute === 'guid') { |
|
| 306 | - $values[] = $this->convertObjectGUID2Str($result[$attribute][$i]); |
|
| 307 | - } else { |
|
| 308 | - $values[] = $result[$attribute][$i]; |
|
| 309 | - } |
|
| 310 | - } |
|
| 311 | - } |
|
| 312 | - return $values; |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - /** |
|
| 316 | - * Attempts to find ranged data in a getAttribute results and extracts the |
|
| 317 | - * returned values as well as information on the range and full attribute |
|
| 318 | - * name for further processing. |
|
| 319 | - * |
|
| 320 | - * @param array $result from ILDAPWrapper::getAttributes() |
|
| 321 | - * @param string $attribute the attribute name that was read. Without ";range=…" |
|
| 322 | - * @return array If a range was detected with keys 'values', 'attributeName', |
|
| 323 | - * 'attributeFull' and 'rangeHigh', otherwise empty. |
|
| 324 | - */ |
|
| 325 | - public function extractRangeData($result, $attribute) { |
|
| 326 | - $keys = array_keys($result); |
|
| 327 | - foreach($keys as $key) { |
|
| 328 | - if($key !== $attribute && strpos($key, $attribute) === 0) { |
|
| 329 | - $queryData = explode(';', $key); |
|
| 330 | - if(strpos($queryData[1], 'range=') === 0) { |
|
| 331 | - $high = substr($queryData[1], 1 + strpos($queryData[1], '-')); |
|
| 332 | - $data = [ |
|
| 333 | - 'values' => $result[$key], |
|
| 334 | - 'attributeName' => $queryData[0], |
|
| 335 | - 'attributeFull' => $key, |
|
| 336 | - 'rangeHigh' => $high, |
|
| 337 | - ]; |
|
| 338 | - return $data; |
|
| 339 | - } |
|
| 340 | - } |
|
| 341 | - } |
|
| 342 | - return []; |
|
| 343 | - } |
|
| 62 | + const UUID_ATTRIBUTES = ['entryuuid', 'nsuniqueid', 'objectguid', 'guid', 'ipauniqueid']; |
|
| 63 | + |
|
| 64 | + /** @var \OCA\User_LDAP\Connection */ |
|
| 65 | + public $connection; |
|
| 66 | + /** @var Manager */ |
|
| 67 | + public $userManager; |
|
| 68 | + //never ever check this var directly, always use getPagedSearchResultState |
|
| 69 | + protected $pagedSearchedSuccessful; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @var string[] $cookies an array of returned Paged Result cookies |
|
| 73 | + */ |
|
| 74 | + protected $cookies = array(); |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @var string $lastCookie the last cookie returned from a Paged Results |
|
| 78 | + * operation, defaults to an empty string |
|
| 79 | + */ |
|
| 80 | + protected $lastCookie = ''; |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * @var AbstractMapping $userMapper |
|
| 84 | + */ |
|
| 85 | + protected $userMapper; |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @var AbstractMapping $userMapper |
|
| 89 | + */ |
|
| 90 | + protected $groupMapper; |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * @var \OCA\User_LDAP\Helper |
|
| 94 | + */ |
|
| 95 | + private $helper; |
|
| 96 | + /** @var IConfig */ |
|
| 97 | + private $config; |
|
| 98 | + |
|
| 99 | + public function __construct( |
|
| 100 | + Connection $connection, |
|
| 101 | + ILDAPWrapper $ldap, |
|
| 102 | + Manager $userManager, |
|
| 103 | + Helper $helper, |
|
| 104 | + IConfig $config |
|
| 105 | + ) { |
|
| 106 | + parent::__construct($ldap); |
|
| 107 | + $this->connection = $connection; |
|
| 108 | + $this->userManager = $userManager; |
|
| 109 | + $this->userManager->setLdapAccess($this); |
|
| 110 | + $this->helper = $helper; |
|
| 111 | + $this->config = $config; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * sets the User Mapper |
|
| 116 | + * @param AbstractMapping $mapper |
|
| 117 | + */ |
|
| 118 | + public function setUserMapper(AbstractMapping $mapper) { |
|
| 119 | + $this->userMapper = $mapper; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * returns the User Mapper |
|
| 124 | + * @throws \Exception |
|
| 125 | + * @return AbstractMapping |
|
| 126 | + */ |
|
| 127 | + public function getUserMapper() { |
|
| 128 | + if(is_null($this->userMapper)) { |
|
| 129 | + throw new \Exception('UserMapper was not assigned to this Access instance.'); |
|
| 130 | + } |
|
| 131 | + return $this->userMapper; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * sets the Group Mapper |
|
| 136 | + * @param AbstractMapping $mapper |
|
| 137 | + */ |
|
| 138 | + public function setGroupMapper(AbstractMapping $mapper) { |
|
| 139 | + $this->groupMapper = $mapper; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * returns the Group Mapper |
|
| 144 | + * @throws \Exception |
|
| 145 | + * @return AbstractMapping |
|
| 146 | + */ |
|
| 147 | + public function getGroupMapper() { |
|
| 148 | + if(is_null($this->groupMapper)) { |
|
| 149 | + throw new \Exception('GroupMapper was not assigned to this Access instance.'); |
|
| 150 | + } |
|
| 151 | + return $this->groupMapper; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * @return bool |
|
| 156 | + */ |
|
| 157 | + private function checkConnection() { |
|
| 158 | + return ($this->connection instanceof Connection); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * returns the Connection instance |
|
| 163 | + * @return \OCA\User_LDAP\Connection |
|
| 164 | + */ |
|
| 165 | + public function getConnection() { |
|
| 166 | + return $this->connection; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * reads a given attribute for an LDAP record identified by a DN |
|
| 171 | + * |
|
| 172 | + * @param string $dn the record in question |
|
| 173 | + * @param string $attr the attribute that shall be retrieved |
|
| 174 | + * if empty, just check the record's existence |
|
| 175 | + * @param string $filter |
|
| 176 | + * @return array|false an array of values on success or an empty |
|
| 177 | + * array if $attr is empty, false otherwise |
|
| 178 | + * @throws ServerNotAvailableException |
|
| 179 | + */ |
|
| 180 | + public function readAttribute($dn, $attr, $filter = 'objectClass=*') { |
|
| 181 | + if(!$this->checkConnection()) { |
|
| 182 | + \OCP\Util::writeLog('user_ldap', |
|
| 183 | + 'No LDAP Connector assigned, access impossible for readAttribute.', |
|
| 184 | + \OCP\Util::WARN); |
|
| 185 | + return false; |
|
| 186 | + } |
|
| 187 | + $cr = $this->connection->getConnectionResource(); |
|
| 188 | + if(!$this->ldap->isResource($cr)) { |
|
| 189 | + //LDAP not available |
|
| 190 | + \OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', \OCP\Util::DEBUG); |
|
| 191 | + return false; |
|
| 192 | + } |
|
| 193 | + //Cancel possibly running Paged Results operation, otherwise we run in |
|
| 194 | + //LDAP protocol errors |
|
| 195 | + $this->abandonPagedSearch(); |
|
| 196 | + // openLDAP requires that we init a new Paged Search. Not needed by AD, |
|
| 197 | + // but does not hurt either. |
|
| 198 | + $pagingSize = (int)$this->connection->ldapPagingSize; |
|
| 199 | + // 0 won't result in replies, small numbers may leave out groups |
|
| 200 | + // (cf. #12306), 500 is default for paging and should work everywhere. |
|
| 201 | + $maxResults = $pagingSize > 20 ? $pagingSize : 500; |
|
| 202 | + $attr = mb_strtolower($attr, 'UTF-8'); |
|
| 203 | + // the actual read attribute later may contain parameters on a ranged |
|
| 204 | + // request, e.g. member;range=99-199. Depends on server reply. |
|
| 205 | + $attrToRead = $attr; |
|
| 206 | + |
|
| 207 | + $values = []; |
|
| 208 | + $isRangeRequest = false; |
|
| 209 | + do { |
|
| 210 | + $result = $this->executeRead($cr, $dn, $attrToRead, $filter, $maxResults); |
|
| 211 | + if(is_bool($result)) { |
|
| 212 | + // when an exists request was run and it was successful, an empty |
|
| 213 | + // array must be returned |
|
| 214 | + return $result ? [] : false; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + if (!$isRangeRequest) { |
|
| 218 | + $values = $this->extractAttributeValuesFromResult($result, $attr); |
|
| 219 | + if (!empty($values)) { |
|
| 220 | + return $values; |
|
| 221 | + } |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + $isRangeRequest = false; |
|
| 225 | + $result = $this->extractRangeData($result, $attr); |
|
| 226 | + if (!empty($result)) { |
|
| 227 | + $normalizedResult = $this->extractAttributeValuesFromResult( |
|
| 228 | + [ $attr => $result['values'] ], |
|
| 229 | + $attr |
|
| 230 | + ); |
|
| 231 | + $values = array_merge($values, $normalizedResult); |
|
| 232 | + |
|
| 233 | + if($result['rangeHigh'] === '*') { |
|
| 234 | + // when server replies with * as high range value, there are |
|
| 235 | + // no more results left |
|
| 236 | + return $values; |
|
| 237 | + } else { |
|
| 238 | + $low = $result['rangeHigh'] + 1; |
|
| 239 | + $attrToRead = $result['attributeName'] . ';range=' . $low . '-*'; |
|
| 240 | + $isRangeRequest = true; |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | + } while($isRangeRequest); |
|
| 244 | + |
|
| 245 | + \OCP\Util::writeLog('user_ldap', 'Requested attribute '.$attr.' not found for '.$dn, \OCP\Util::DEBUG); |
|
| 246 | + return false; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * Runs an read operation against LDAP |
|
| 251 | + * |
|
| 252 | + * @param resource $cr the LDAP connection |
|
| 253 | + * @param string $dn |
|
| 254 | + * @param string $attribute |
|
| 255 | + * @param string $filter |
|
| 256 | + * @param int $maxResults |
|
| 257 | + * @return array|bool false if there was any error, true if an exists check |
|
| 258 | + * was performed and the requested DN found, array with the |
|
| 259 | + * returned data on a successful usual operation |
|
| 260 | + * @throws ServerNotAvailableException |
|
| 261 | + */ |
|
| 262 | + public function executeRead($cr, $dn, $attribute, $filter, $maxResults) { |
|
| 263 | + $this->initPagedSearch($filter, array($dn), array($attribute), $maxResults, 0); |
|
| 264 | + $dn = $this->helper->DNasBaseParameter($dn); |
|
| 265 | + $rr = @$this->invokeLDAPMethod('read', $cr, $dn, $filter, array($attribute)); |
|
| 266 | + if (!$this->ldap->isResource($rr)) { |
|
| 267 | + if ($attribute !== '') { |
|
| 268 | + //do not throw this message on userExists check, irritates |
|
| 269 | + \OCP\Util::writeLog('user_ldap', 'readAttribute failed for DN ' . $dn, \OCP\Util::DEBUG); |
|
| 270 | + } |
|
| 271 | + //in case an error occurs , e.g. object does not exist |
|
| 272 | + return false; |
|
| 273 | + } |
|
| 274 | + if ($attribute === '' && ($filter === 'objectclass=*' || $this->invokeLDAPMethod('countEntries', $cr, $rr) === 1)) { |
|
| 275 | + \OCP\Util::writeLog('user_ldap', 'readAttribute: ' . $dn . ' found', \OCP\Util::DEBUG); |
|
| 276 | + return true; |
|
| 277 | + } |
|
| 278 | + $er = $this->invokeLDAPMethod('firstEntry', $cr, $rr); |
|
| 279 | + if (!$this->ldap->isResource($er)) { |
|
| 280 | + //did not match the filter, return false |
|
| 281 | + return false; |
|
| 282 | + } |
|
| 283 | + //LDAP attributes are not case sensitive |
|
| 284 | + $result = \OCP\Util::mb_array_change_key_case( |
|
| 285 | + $this->invokeLDAPMethod('getAttributes', $cr, $er), MB_CASE_LOWER, 'UTF-8'); |
|
| 286 | + |
|
| 287 | + return $result; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Normalizes a result grom getAttributes(), i.e. handles DNs and binary |
|
| 292 | + * data if present. |
|
| 293 | + * |
|
| 294 | + * @param array $result from ILDAPWrapper::getAttributes() |
|
| 295 | + * @param string $attribute the attribute name that was read |
|
| 296 | + * @return string[] |
|
| 297 | + */ |
|
| 298 | + public function extractAttributeValuesFromResult($result, $attribute) { |
|
| 299 | + $values = []; |
|
| 300 | + if(isset($result[$attribute]) && $result[$attribute]['count'] > 0) { |
|
| 301 | + $lowercaseAttribute = strtolower($attribute); |
|
| 302 | + for($i=0;$i<$result[$attribute]['count'];$i++) { |
|
| 303 | + if($this->resemblesDN($attribute)) { |
|
| 304 | + $values[] = $this->helper->sanitizeDN($result[$attribute][$i]); |
|
| 305 | + } elseif($lowercaseAttribute === 'objectguid' || $lowercaseAttribute === 'guid') { |
|
| 306 | + $values[] = $this->convertObjectGUID2Str($result[$attribute][$i]); |
|
| 307 | + } else { |
|
| 308 | + $values[] = $result[$attribute][$i]; |
|
| 309 | + } |
|
| 310 | + } |
|
| 311 | + } |
|
| 312 | + return $values; |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + /** |
|
| 316 | + * Attempts to find ranged data in a getAttribute results and extracts the |
|
| 317 | + * returned values as well as information on the range and full attribute |
|
| 318 | + * name for further processing. |
|
| 319 | + * |
|
| 320 | + * @param array $result from ILDAPWrapper::getAttributes() |
|
| 321 | + * @param string $attribute the attribute name that was read. Without ";range=…" |
|
| 322 | + * @return array If a range was detected with keys 'values', 'attributeName', |
|
| 323 | + * 'attributeFull' and 'rangeHigh', otherwise empty. |
|
| 324 | + */ |
|
| 325 | + public function extractRangeData($result, $attribute) { |
|
| 326 | + $keys = array_keys($result); |
|
| 327 | + foreach($keys as $key) { |
|
| 328 | + if($key !== $attribute && strpos($key, $attribute) === 0) { |
|
| 329 | + $queryData = explode(';', $key); |
|
| 330 | + if(strpos($queryData[1], 'range=') === 0) { |
|
| 331 | + $high = substr($queryData[1], 1 + strpos($queryData[1], '-')); |
|
| 332 | + $data = [ |
|
| 333 | + 'values' => $result[$key], |
|
| 334 | + 'attributeName' => $queryData[0], |
|
| 335 | + 'attributeFull' => $key, |
|
| 336 | + 'rangeHigh' => $high, |
|
| 337 | + ]; |
|
| 338 | + return $data; |
|
| 339 | + } |
|
| 340 | + } |
|
| 341 | + } |
|
| 342 | + return []; |
|
| 343 | + } |
|
| 344 | 344 | |
| 345 | - /** |
|
| 346 | - * Set password for an LDAP user identified by a DN |
|
| 347 | - * |
|
| 348 | - * @param string $userDN the user in question |
|
| 349 | - * @param string $password the new password |
|
| 350 | - * @return bool |
|
| 351 | - * @throws HintException |
|
| 352 | - * @throws \Exception |
|
| 353 | - */ |
|
| 354 | - public function setPassword($userDN, $password) { |
|
| 355 | - if((int)$this->connection->turnOnPasswordChange !== 1) { |
|
| 356 | - throw new \Exception('LDAP password changes are disabled.'); |
|
| 357 | - } |
|
| 358 | - $cr = $this->connection->getConnectionResource(); |
|
| 359 | - if(!$this->ldap->isResource($cr)) { |
|
| 360 | - //LDAP not available |
|
| 361 | - \OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', \OCP\Util::DEBUG); |
|
| 362 | - return false; |
|
| 363 | - } |
|
| 364 | - try { |
|
| 365 | - return @$this->invokeLDAPMethod('modReplace', $cr, $userDN, $password); |
|
| 366 | - } catch(ConstraintViolationException $e) { |
|
| 367 | - throw new HintException('Password change rejected.', \OC::$server->getL10N('user_ldap')->t('Password change rejected. Hint: ').$e->getMessage(), $e->getCode()); |
|
| 368 | - } |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * checks whether the given attributes value is probably a DN |
|
| 373 | - * @param string $attr the attribute in question |
|
| 374 | - * @return boolean if so true, otherwise false |
|
| 375 | - */ |
|
| 376 | - private function resemblesDN($attr) { |
|
| 377 | - $resemblingAttributes = array( |
|
| 378 | - 'dn', |
|
| 379 | - 'uniquemember', |
|
| 380 | - 'member', |
|
| 381 | - // memberOf is an "operational" attribute, without a definition in any RFC |
|
| 382 | - 'memberof' |
|
| 383 | - ); |
|
| 384 | - return in_array($attr, $resemblingAttributes); |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - /** |
|
| 388 | - * checks whether the given string is probably a DN |
|
| 389 | - * @param string $string |
|
| 390 | - * @return boolean |
|
| 391 | - */ |
|
| 392 | - public function stringResemblesDN($string) { |
|
| 393 | - $r = $this->ldap->explodeDN($string, 0); |
|
| 394 | - // if exploding a DN succeeds and does not end up in |
|
| 395 | - // an empty array except for $r[count] being 0. |
|
| 396 | - return (is_array($r) && count($r) > 1); |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - /** |
|
| 400 | - * returns a DN-string that is cleaned from not domain parts, e.g. |
|
| 401 | - * cn=foo,cn=bar,dc=foobar,dc=server,dc=org |
|
| 402 | - * becomes dc=foobar,dc=server,dc=org |
|
| 403 | - * @param string $dn |
|
| 404 | - * @return string |
|
| 405 | - */ |
|
| 406 | - public function getDomainDNFromDN($dn) { |
|
| 407 | - $allParts = $this->ldap->explodeDN($dn, 0); |
|
| 408 | - if($allParts === false) { |
|
| 409 | - //not a valid DN |
|
| 410 | - return ''; |
|
| 411 | - } |
|
| 412 | - $domainParts = array(); |
|
| 413 | - $dcFound = false; |
|
| 414 | - foreach($allParts as $part) { |
|
| 415 | - if(!$dcFound && strpos($part, 'dc=') === 0) { |
|
| 416 | - $dcFound = true; |
|
| 417 | - } |
|
| 418 | - if($dcFound) { |
|
| 419 | - $domainParts[] = $part; |
|
| 420 | - } |
|
| 421 | - } |
|
| 422 | - $domainDN = implode(',', $domainParts); |
|
| 423 | - return $domainDN; |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - /** |
|
| 427 | - * returns the LDAP DN for the given internal Nextcloud name of the group |
|
| 428 | - * @param string $name the Nextcloud name in question |
|
| 429 | - * @return string|false LDAP DN on success, otherwise false |
|
| 430 | - */ |
|
| 431 | - public function groupname2dn($name) { |
|
| 432 | - return $this->groupMapper->getDNByName($name); |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - /** |
|
| 436 | - * returns the LDAP DN for the given internal Nextcloud name of the user |
|
| 437 | - * @param string $name the Nextcloud name in question |
|
| 438 | - * @return string|false with the LDAP DN on success, otherwise false |
|
| 439 | - */ |
|
| 440 | - public function username2dn($name) { |
|
| 441 | - $fdn = $this->userMapper->getDNByName($name); |
|
| 442 | - |
|
| 443 | - //Check whether the DN belongs to the Base, to avoid issues on multi- |
|
| 444 | - //server setups |
|
| 445 | - if(is_string($fdn) && $this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 446 | - return $fdn; |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - return false; |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * returns the internal Nextcloud name for the given LDAP DN of the group, false on DN outside of search DN or failure |
|
| 454 | - * @param string $fdn the dn of the group object |
|
| 455 | - * @param string $ldapName optional, the display name of the object |
|
| 456 | - * @return string|false with the name to use in Nextcloud, false on DN outside of search DN |
|
| 457 | - */ |
|
| 458 | - public function dn2groupname($fdn, $ldapName = null) { |
|
| 459 | - //To avoid bypassing the base DN settings under certain circumstances |
|
| 460 | - //with the group support, check whether the provided DN matches one of |
|
| 461 | - //the given Bases |
|
| 462 | - if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseGroups)) { |
|
| 463 | - return false; |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - return $this->dn2ocname($fdn, $ldapName, false); |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - /** |
|
| 470 | - * accepts an array of group DNs and tests whether they match the user |
|
| 471 | - * filter by doing read operations against the group entries. Returns an |
|
| 472 | - * array of DNs that match the filter. |
|
| 473 | - * |
|
| 474 | - * @param string[] $groupDNs |
|
| 475 | - * @return string[] |
|
| 476 | - */ |
|
| 477 | - public function groupsMatchFilter($groupDNs) { |
|
| 478 | - $validGroupDNs = []; |
|
| 479 | - foreach($groupDNs as $dn) { |
|
| 480 | - $cacheKey = 'groupsMatchFilter-'.$dn; |
|
| 481 | - $groupMatchFilter = $this->connection->getFromCache($cacheKey); |
|
| 482 | - if(!is_null($groupMatchFilter)) { |
|
| 483 | - if($groupMatchFilter) { |
|
| 484 | - $validGroupDNs[] = $dn; |
|
| 485 | - } |
|
| 486 | - continue; |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - // Check the base DN first. If this is not met already, we don't |
|
| 490 | - // need to ask the server at all. |
|
| 491 | - if(!$this->isDNPartOfBase($dn, $this->connection->ldapBaseGroups)) { |
|
| 492 | - $this->connection->writeToCache($cacheKey, false); |
|
| 493 | - continue; |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - $result = $this->readAttribute($dn, 'cn', $this->connection->ldapGroupFilter); |
|
| 497 | - if(is_array($result)) { |
|
| 498 | - $this->connection->writeToCache($cacheKey, true); |
|
| 499 | - $validGroupDNs[] = $dn; |
|
| 500 | - } else { |
|
| 501 | - $this->connection->writeToCache($cacheKey, false); |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - } |
|
| 505 | - return $validGroupDNs; |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - /** |
|
| 509 | - * returns the internal Nextcloud name for the given LDAP DN of the user, false on DN outside of search DN or failure |
|
| 510 | - * @param string $dn the dn of the user object |
|
| 511 | - * @param string $ldapName optional, the display name of the object |
|
| 512 | - * @return string|false with with the name to use in Nextcloud |
|
| 513 | - */ |
|
| 514 | - public function dn2username($fdn, $ldapName = null) { |
|
| 515 | - //To avoid bypassing the base DN settings under certain circumstances |
|
| 516 | - //with the group support, check whether the provided DN matches one of |
|
| 517 | - //the given Bases |
|
| 518 | - if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 519 | - return false; |
|
| 520 | - } |
|
| 521 | - |
|
| 522 | - return $this->dn2ocname($fdn, $ldapName, true); |
|
| 523 | - } |
|
| 524 | - |
|
| 525 | - /** |
|
| 526 | - * returns an internal Nextcloud name for the given LDAP DN, false on DN outside of search DN |
|
| 527 | - * |
|
| 528 | - * @param string $fdn the dn of the user object |
|
| 529 | - * @param string|null $ldapName optional, the display name of the object |
|
| 530 | - * @param bool $isUser optional, whether it is a user object (otherwise group assumed) |
|
| 531 | - * @param bool|null $newlyMapped |
|
| 532 | - * @param array|null $record |
|
| 533 | - * @return false|string with with the name to use in Nextcloud |
|
| 534 | - * @throws \Exception |
|
| 535 | - */ |
|
| 536 | - public function dn2ocname($fdn, $ldapName = null, $isUser = true, &$newlyMapped = null, array $record = null) { |
|
| 537 | - $newlyMapped = false; |
|
| 538 | - if($isUser) { |
|
| 539 | - $mapper = $this->getUserMapper(); |
|
| 540 | - $nameAttribute = $this->connection->ldapUserDisplayName; |
|
| 541 | - } else { |
|
| 542 | - $mapper = $this->getGroupMapper(); |
|
| 543 | - $nameAttribute = $this->connection->ldapGroupDisplayName; |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - //let's try to retrieve the Nextcloud name from the mappings table |
|
| 547 | - $ncName = $mapper->getNameByDN($fdn); |
|
| 548 | - if(is_string($ncName)) { |
|
| 549 | - return $ncName; |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - //second try: get the UUID and check if it is known. Then, update the DN and return the name. |
|
| 553 | - $uuid = $this->getUUID($fdn, $isUser, $record); |
|
| 554 | - if(is_string($uuid)) { |
|
| 555 | - $ncName = $mapper->getNameByUUID($uuid); |
|
| 556 | - if(is_string($ncName)) { |
|
| 557 | - $mapper->setDNbyUUID($fdn, $uuid); |
|
| 558 | - return $ncName; |
|
| 559 | - } |
|
| 560 | - } else { |
|
| 561 | - //If the UUID can't be detected something is foul. |
|
| 562 | - \OCP\Util::writeLog('user_ldap', 'Cannot determine UUID for '.$fdn.'. Skipping.', \OCP\Util::INFO); |
|
| 563 | - return false; |
|
| 564 | - } |
|
| 565 | - |
|
| 566 | - if(is_null($ldapName)) { |
|
| 567 | - $ldapName = $this->readAttribute($fdn, $nameAttribute); |
|
| 568 | - if(!isset($ldapName[0]) && empty($ldapName[0])) { |
|
| 569 | - \OCP\Util::writeLog('user_ldap', 'No or empty name for '.$fdn.'.', \OCP\Util::INFO); |
|
| 570 | - return false; |
|
| 571 | - } |
|
| 572 | - $ldapName = $ldapName[0]; |
|
| 573 | - } |
|
| 574 | - |
|
| 575 | - if($isUser) { |
|
| 576 | - $usernameAttribute = (string)$this->connection->ldapExpertUsernameAttr; |
|
| 577 | - if ($usernameAttribute !== '') { |
|
| 578 | - $username = $this->readAttribute($fdn, $usernameAttribute); |
|
| 579 | - $username = $username[0]; |
|
| 580 | - } else { |
|
| 581 | - $username = $uuid; |
|
| 582 | - } |
|
| 583 | - $intName = $this->sanitizeUsername($username); |
|
| 584 | - } else { |
|
| 585 | - $intName = $ldapName; |
|
| 586 | - } |
|
| 587 | - |
|
| 588 | - //a new user/group! Add it only if it doesn't conflict with other backend's users or existing groups |
|
| 589 | - //disabling Cache is required to avoid that the new user is cached as not-existing in fooExists check |
|
| 590 | - //NOTE: mind, disabling cache affects only this instance! Using it |
|
| 591 | - // outside of core user management will still cache the user as non-existing. |
|
| 592 | - $originalTTL = $this->connection->ldapCacheTTL; |
|
| 593 | - $this->connection->setConfiguration(array('ldapCacheTTL' => 0)); |
|
| 594 | - if(($isUser && $intName !== '' && !\OC::$server->getUserManager()->userExists($intName)) |
|
| 595 | - || (!$isUser && !\OC::$server->getGroupManager()->groupExists($intName))) { |
|
| 596 | - if($mapper->map($fdn, $intName, $uuid)) { |
|
| 597 | - $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); |
|
| 598 | - $newlyMapped = true; |
|
| 599 | - return $intName; |
|
| 600 | - } |
|
| 601 | - } |
|
| 602 | - $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); |
|
| 603 | - |
|
| 604 | - $altName = $this->createAltInternalOwnCloudName($intName, $isUser); |
|
| 605 | - if(is_string($altName) && $mapper->map($fdn, $altName, $uuid)) { |
|
| 606 | - $newlyMapped = true; |
|
| 607 | - return $altName; |
|
| 608 | - } |
|
| 609 | - |
|
| 610 | - //if everything else did not help.. |
|
| 611 | - \OCP\Util::writeLog('user_ldap', 'Could not create unique name for '.$fdn.'.', \OCP\Util::INFO); |
|
| 612 | - return false; |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - /** |
|
| 616 | - * gives back the user names as they are used ownClod internally |
|
| 617 | - * @param array $ldapUsers as returned by fetchList() |
|
| 618 | - * @return array an array with the user names to use in Nextcloud |
|
| 619 | - * |
|
| 620 | - * gives back the user names as they are used ownClod internally |
|
| 621 | - */ |
|
| 622 | - public function nextcloudUserNames($ldapUsers) { |
|
| 623 | - return $this->ldap2NextcloudNames($ldapUsers, true); |
|
| 624 | - } |
|
| 625 | - |
|
| 626 | - /** |
|
| 627 | - * gives back the group names as they are used ownClod internally |
|
| 628 | - * @param array $ldapGroups as returned by fetchList() |
|
| 629 | - * @return array an array with the group names to use in Nextcloud |
|
| 630 | - * |
|
| 631 | - * gives back the group names as they are used ownClod internally |
|
| 632 | - */ |
|
| 633 | - public function nextcloudGroupNames($ldapGroups) { |
|
| 634 | - return $this->ldap2NextcloudNames($ldapGroups, false); |
|
| 635 | - } |
|
| 636 | - |
|
| 637 | - /** |
|
| 638 | - * @param array $ldapObjects as returned by fetchList() |
|
| 639 | - * @param bool $isUsers |
|
| 640 | - * @return array |
|
| 641 | - */ |
|
| 642 | - private function ldap2NextcloudNames($ldapObjects, $isUsers) { |
|
| 643 | - if($isUsers) { |
|
| 644 | - $nameAttribute = $this->connection->ldapUserDisplayName; |
|
| 645 | - $sndAttribute = $this->connection->ldapUserDisplayName2; |
|
| 646 | - } else { |
|
| 647 | - $nameAttribute = $this->connection->ldapGroupDisplayName; |
|
| 648 | - } |
|
| 649 | - $nextcloudNames = array(); |
|
| 650 | - |
|
| 651 | - foreach($ldapObjects as $ldapObject) { |
|
| 652 | - $nameByLDAP = null; |
|
| 653 | - if( isset($ldapObject[$nameAttribute]) |
|
| 654 | - && is_array($ldapObject[$nameAttribute]) |
|
| 655 | - && isset($ldapObject[$nameAttribute][0]) |
|
| 656 | - ) { |
|
| 657 | - // might be set, but not necessarily. if so, we use it. |
|
| 658 | - $nameByLDAP = $ldapObject[$nameAttribute][0]; |
|
| 659 | - } |
|
| 660 | - |
|
| 661 | - $ncName = $this->dn2ocname($ldapObject['dn'][0], $nameByLDAP, $isUsers); |
|
| 662 | - if($ncName) { |
|
| 663 | - $nextcloudNames[] = $ncName; |
|
| 664 | - if($isUsers) { |
|
| 665 | - //cache the user names so it does not need to be retrieved |
|
| 666 | - //again later (e.g. sharing dialogue). |
|
| 667 | - if(is_null($nameByLDAP)) { |
|
| 668 | - continue; |
|
| 669 | - } |
|
| 670 | - $sndName = isset($ldapObject[$sndAttribute][0]) |
|
| 671 | - ? $ldapObject[$sndAttribute][0] : ''; |
|
| 672 | - $this->cacheUserDisplayName($ncName, $nameByLDAP, $sndName); |
|
| 673 | - } |
|
| 674 | - } |
|
| 675 | - } |
|
| 676 | - return $nextcloudNames; |
|
| 677 | - } |
|
| 678 | - |
|
| 679 | - /** |
|
| 680 | - * caches the user display name |
|
| 681 | - * @param string $ocName the internal Nextcloud username |
|
| 682 | - * @param string|false $home the home directory path |
|
| 683 | - */ |
|
| 684 | - public function cacheUserHome($ocName, $home) { |
|
| 685 | - $cacheKey = 'getHome'.$ocName; |
|
| 686 | - $this->connection->writeToCache($cacheKey, $home); |
|
| 687 | - } |
|
| 688 | - |
|
| 689 | - /** |
|
| 690 | - * caches a user as existing |
|
| 691 | - * @param string $ocName the internal Nextcloud username |
|
| 692 | - */ |
|
| 693 | - public function cacheUserExists($ocName) { |
|
| 694 | - $this->connection->writeToCache('userExists'.$ocName, true); |
|
| 695 | - } |
|
| 696 | - |
|
| 697 | - /** |
|
| 698 | - * caches the user display name |
|
| 699 | - * @param string $ocName the internal Nextcloud username |
|
| 700 | - * @param string $displayName the display name |
|
| 701 | - * @param string $displayName2 the second display name |
|
| 702 | - */ |
|
| 703 | - public function cacheUserDisplayName($ocName, $displayName, $displayName2 = '') { |
|
| 704 | - $user = $this->userManager->get($ocName); |
|
| 705 | - if($user === null) { |
|
| 706 | - return; |
|
| 707 | - } |
|
| 708 | - $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2); |
|
| 709 | - $cacheKeyTrunk = 'getDisplayName'; |
|
| 710 | - $this->connection->writeToCache($cacheKeyTrunk.$ocName, $displayName); |
|
| 711 | - } |
|
| 712 | - |
|
| 713 | - /** |
|
| 714 | - * creates a unique name for internal Nextcloud use for users. Don't call it directly. |
|
| 715 | - * @param string $name the display name of the object |
|
| 716 | - * @return string|false with with the name to use in Nextcloud or false if unsuccessful |
|
| 717 | - * |
|
| 718 | - * Instead of using this method directly, call |
|
| 719 | - * createAltInternalOwnCloudName($name, true) |
|
| 720 | - */ |
|
| 721 | - private function _createAltInternalOwnCloudNameForUsers($name) { |
|
| 722 | - $attempts = 0; |
|
| 723 | - //while loop is just a precaution. If a name is not generated within |
|
| 724 | - //20 attempts, something else is very wrong. Avoids infinite loop. |
|
| 725 | - while($attempts < 20){ |
|
| 726 | - $altName = $name . '_' . rand(1000,9999); |
|
| 727 | - if(!\OC::$server->getUserManager()->userExists($altName)) { |
|
| 728 | - return $altName; |
|
| 729 | - } |
|
| 730 | - $attempts++; |
|
| 731 | - } |
|
| 732 | - return false; |
|
| 733 | - } |
|
| 734 | - |
|
| 735 | - /** |
|
| 736 | - * creates a unique name for internal Nextcloud use for groups. Don't call it directly. |
|
| 737 | - * @param string $name the display name of the object |
|
| 738 | - * @return string|false with with the name to use in Nextcloud or false if unsuccessful. |
|
| 739 | - * |
|
| 740 | - * Instead of using this method directly, call |
|
| 741 | - * createAltInternalOwnCloudName($name, false) |
|
| 742 | - * |
|
| 743 | - * Group names are also used as display names, so we do a sequential |
|
| 744 | - * numbering, e.g. Developers_42 when there are 41 other groups called |
|
| 745 | - * "Developers" |
|
| 746 | - */ |
|
| 747 | - private function _createAltInternalOwnCloudNameForGroups($name) { |
|
| 748 | - $usedNames = $this->groupMapper->getNamesBySearch($name, "", '_%'); |
|
| 749 | - if(!($usedNames) || count($usedNames) === 0) { |
|
| 750 | - $lastNo = 1; //will become name_2 |
|
| 751 | - } else { |
|
| 752 | - natsort($usedNames); |
|
| 753 | - $lastName = array_pop($usedNames); |
|
| 754 | - $lastNo = (int)substr($lastName, strrpos($lastName, '_') + 1); |
|
| 755 | - } |
|
| 756 | - $altName = $name.'_'. (string)($lastNo+1); |
|
| 757 | - unset($usedNames); |
|
| 758 | - |
|
| 759 | - $attempts = 1; |
|
| 760 | - while($attempts < 21){ |
|
| 761 | - // Check to be really sure it is unique |
|
| 762 | - // while loop is just a precaution. If a name is not generated within |
|
| 763 | - // 20 attempts, something else is very wrong. Avoids infinite loop. |
|
| 764 | - if(!\OC::$server->getGroupManager()->groupExists($altName)) { |
|
| 765 | - return $altName; |
|
| 766 | - } |
|
| 767 | - $altName = $name . '_' . ($lastNo + $attempts); |
|
| 768 | - $attempts++; |
|
| 769 | - } |
|
| 770 | - return false; |
|
| 771 | - } |
|
| 772 | - |
|
| 773 | - /** |
|
| 774 | - * creates a unique name for internal Nextcloud use. |
|
| 775 | - * @param string $name the display name of the object |
|
| 776 | - * @param boolean $isUser whether name should be created for a user (true) or a group (false) |
|
| 777 | - * @return string|false with with the name to use in Nextcloud or false if unsuccessful |
|
| 778 | - */ |
|
| 779 | - private function createAltInternalOwnCloudName($name, $isUser) { |
|
| 780 | - $originalTTL = $this->connection->ldapCacheTTL; |
|
| 781 | - $this->connection->setConfiguration(array('ldapCacheTTL' => 0)); |
|
| 782 | - if($isUser) { |
|
| 783 | - $altName = $this->_createAltInternalOwnCloudNameForUsers($name); |
|
| 784 | - } else { |
|
| 785 | - $altName = $this->_createAltInternalOwnCloudNameForGroups($name); |
|
| 786 | - } |
|
| 787 | - $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); |
|
| 788 | - |
|
| 789 | - return $altName; |
|
| 790 | - } |
|
| 791 | - |
|
| 792 | - /** |
|
| 793 | - * fetches a list of users according to a provided loginName and utilizing |
|
| 794 | - * the login filter. |
|
| 795 | - * |
|
| 796 | - * @param string $loginName |
|
| 797 | - * @param array $attributes optional, list of attributes to read |
|
| 798 | - * @return array |
|
| 799 | - */ |
|
| 800 | - public function fetchUsersByLoginName($loginName, $attributes = array('dn')) { |
|
| 801 | - $loginName = $this->escapeFilterPart($loginName); |
|
| 802 | - $filter = str_replace('%uid', $loginName, $this->connection->ldapLoginFilter); |
|
| 803 | - $users = $this->fetchListOfUsers($filter, $attributes); |
|
| 804 | - return $users; |
|
| 805 | - } |
|
| 806 | - |
|
| 807 | - /** |
|
| 808 | - * counts the number of users according to a provided loginName and |
|
| 809 | - * utilizing the login filter. |
|
| 810 | - * |
|
| 811 | - * @param string $loginName |
|
| 812 | - * @return int |
|
| 813 | - */ |
|
| 814 | - public function countUsersByLoginName($loginName) { |
|
| 815 | - $loginName = $this->escapeFilterPart($loginName); |
|
| 816 | - $filter = str_replace('%uid', $loginName, $this->connection->ldapLoginFilter); |
|
| 817 | - $users = $this->countUsers($filter); |
|
| 818 | - return $users; |
|
| 819 | - } |
|
| 820 | - |
|
| 821 | - /** |
|
| 822 | - * @param string $filter |
|
| 823 | - * @param string|string[] $attr |
|
| 824 | - * @param int $limit |
|
| 825 | - * @param int $offset |
|
| 826 | - * @param bool $forceApplyAttributes |
|
| 827 | - * @return array |
|
| 828 | - */ |
|
| 829 | - public function fetchListOfUsers($filter, $attr, $limit = null, $offset = null, $forceApplyAttributes = false) { |
|
| 830 | - $ldapRecords = $this->searchUsers($filter, $attr, $limit, $offset); |
|
| 831 | - $recordsToUpdate = $ldapRecords; |
|
| 832 | - if(!$forceApplyAttributes) { |
|
| 833 | - $isBackgroundJobModeAjax = $this->config |
|
| 834 | - ->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'ajax'; |
|
| 835 | - $recordsToUpdate = array_filter($ldapRecords, function($record) use ($isBackgroundJobModeAjax) { |
|
| 836 | - $newlyMapped = false; |
|
| 837 | - $uid = $this->dn2ocname($record['dn'][0], null, true, $newlyMapped, $record); |
|
| 838 | - if(is_string($uid)) { |
|
| 839 | - $this->cacheUserExists($uid); |
|
| 840 | - } |
|
| 841 | - return ($uid !== false) && ($newlyMapped || $isBackgroundJobModeAjax); |
|
| 842 | - }); |
|
| 843 | - } |
|
| 844 | - $this->batchApplyUserAttributes($recordsToUpdate); |
|
| 845 | - return $this->fetchList($ldapRecords, (count($attr) > 1)); |
|
| 846 | - } |
|
| 847 | - |
|
| 848 | - /** |
|
| 849 | - * provided with an array of LDAP user records the method will fetch the |
|
| 850 | - * user object and requests it to process the freshly fetched attributes and |
|
| 851 | - * and their values |
|
| 852 | - * @param array $ldapRecords |
|
| 853 | - */ |
|
| 854 | - public function batchApplyUserAttributes(array $ldapRecords){ |
|
| 855 | - $displayNameAttribute = strtolower($this->connection->ldapUserDisplayName); |
|
| 856 | - foreach($ldapRecords as $userRecord) { |
|
| 857 | - if(!isset($userRecord[$displayNameAttribute])) { |
|
| 858 | - // displayName is obligatory |
|
| 859 | - continue; |
|
| 860 | - } |
|
| 861 | - $ocName = $this->dn2ocname($userRecord['dn'][0], null, true); |
|
| 862 | - if($ocName === false) { |
|
| 863 | - continue; |
|
| 864 | - } |
|
| 865 | - $user = $this->userManager->get($ocName); |
|
| 866 | - if($user instanceof OfflineUser) { |
|
| 867 | - $user->unmark(); |
|
| 868 | - $user = $this->userManager->get($ocName); |
|
| 869 | - } |
|
| 870 | - if ($user !== null) { |
|
| 871 | - $user->processAttributes($userRecord); |
|
| 872 | - } else { |
|
| 873 | - \OC::$server->getLogger()->debug( |
|
| 874 | - "The ldap user manager returned null for $ocName", |
|
| 875 | - ['app'=>'user_ldap'] |
|
| 876 | - ); |
|
| 877 | - } |
|
| 878 | - } |
|
| 879 | - } |
|
| 880 | - |
|
| 881 | - /** |
|
| 882 | - * @param string $filter |
|
| 883 | - * @param string|string[] $attr |
|
| 884 | - * @param int $limit |
|
| 885 | - * @param int $offset |
|
| 886 | - * @return array |
|
| 887 | - */ |
|
| 888 | - public function fetchListOfGroups($filter, $attr, $limit = null, $offset = null) { |
|
| 889 | - return $this->fetchList($this->searchGroups($filter, $attr, $limit, $offset), (count($attr) > 1)); |
|
| 890 | - } |
|
| 891 | - |
|
| 892 | - /** |
|
| 893 | - * @param array $list |
|
| 894 | - * @param bool $manyAttributes |
|
| 895 | - * @return array |
|
| 896 | - */ |
|
| 897 | - private function fetchList($list, $manyAttributes) { |
|
| 898 | - if(is_array($list)) { |
|
| 899 | - if($manyAttributes) { |
|
| 900 | - return $list; |
|
| 901 | - } else { |
|
| 902 | - $list = array_reduce($list, function($carry, $item) { |
|
| 903 | - $attribute = array_keys($item)[0]; |
|
| 904 | - $carry[] = $item[$attribute][0]; |
|
| 905 | - return $carry; |
|
| 906 | - }, array()); |
|
| 907 | - return array_unique($list, SORT_LOCALE_STRING); |
|
| 908 | - } |
|
| 909 | - } |
|
| 910 | - |
|
| 911 | - //error cause actually, maybe throw an exception in future. |
|
| 912 | - return array(); |
|
| 913 | - } |
|
| 914 | - |
|
| 915 | - /** |
|
| 916 | - * executes an LDAP search, optimized for Users |
|
| 917 | - * @param string $filter the LDAP filter for the search |
|
| 918 | - * @param string|string[] $attr optional, when a certain attribute shall be filtered out |
|
| 919 | - * @param integer $limit |
|
| 920 | - * @param integer $offset |
|
| 921 | - * @return array with the search result |
|
| 922 | - * |
|
| 923 | - * Executes an LDAP search |
|
| 924 | - */ |
|
| 925 | - public function searchUsers($filter, $attr = null, $limit = null, $offset = null) { |
|
| 926 | - return $this->search($filter, $this->connection->ldapBaseUsers, $attr, $limit, $offset); |
|
| 927 | - } |
|
| 928 | - |
|
| 929 | - /** |
|
| 930 | - * @param string $filter |
|
| 931 | - * @param string|string[] $attr |
|
| 932 | - * @param int $limit |
|
| 933 | - * @param int $offset |
|
| 934 | - * @return false|int |
|
| 935 | - */ |
|
| 936 | - public function countUsers($filter, $attr = array('dn'), $limit = null, $offset = null) { |
|
| 937 | - return $this->count($filter, $this->connection->ldapBaseUsers, $attr, $limit, $offset); |
|
| 938 | - } |
|
| 939 | - |
|
| 940 | - /** |
|
| 941 | - * executes an LDAP search, optimized for Groups |
|
| 942 | - * @param string $filter the LDAP filter for the search |
|
| 943 | - * @param string|string[] $attr optional, when a certain attribute shall be filtered out |
|
| 944 | - * @param integer $limit |
|
| 945 | - * @param integer $offset |
|
| 946 | - * @return array with the search result |
|
| 947 | - * |
|
| 948 | - * Executes an LDAP search |
|
| 949 | - */ |
|
| 950 | - public function searchGroups($filter, $attr = null, $limit = null, $offset = null) { |
|
| 951 | - return $this->search($filter, $this->connection->ldapBaseGroups, $attr, $limit, $offset); |
|
| 952 | - } |
|
| 953 | - |
|
| 954 | - /** |
|
| 955 | - * returns the number of available groups |
|
| 956 | - * @param string $filter the LDAP search filter |
|
| 957 | - * @param string[] $attr optional |
|
| 958 | - * @param int|null $limit |
|
| 959 | - * @param int|null $offset |
|
| 960 | - * @return int|bool |
|
| 961 | - */ |
|
| 962 | - public function countGroups($filter, $attr = array('dn'), $limit = null, $offset = null) { |
|
| 963 | - return $this->count($filter, $this->connection->ldapBaseGroups, $attr, $limit, $offset); |
|
| 964 | - } |
|
| 965 | - |
|
| 966 | - /** |
|
| 967 | - * returns the number of available objects on the base DN |
|
| 968 | - * |
|
| 969 | - * @param int|null $limit |
|
| 970 | - * @param int|null $offset |
|
| 971 | - * @return int|bool |
|
| 972 | - */ |
|
| 973 | - public function countObjects($limit = null, $offset = null) { |
|
| 974 | - return $this->count('objectclass=*', $this->connection->ldapBase, array('dn'), $limit, $offset); |
|
| 975 | - } |
|
| 976 | - |
|
| 977 | - /** |
|
| 978 | - * Returns the LDAP handler |
|
| 979 | - * @throws \OC\ServerNotAvailableException |
|
| 980 | - */ |
|
| 981 | - |
|
| 982 | - /** |
|
| 983 | - * @return mixed |
|
| 984 | - * @throws \OC\ServerNotAvailableException |
|
| 985 | - */ |
|
| 986 | - private function invokeLDAPMethod() { |
|
| 987 | - $arguments = func_get_args(); |
|
| 988 | - $command = array_shift($arguments); |
|
| 989 | - $cr = array_shift($arguments); |
|
| 990 | - if (!method_exists($this->ldap, $command)) { |
|
| 991 | - return null; |
|
| 992 | - } |
|
| 993 | - array_unshift($arguments, $cr); |
|
| 994 | - // php no longer supports call-time pass-by-reference |
|
| 995 | - // thus cannot support controlPagedResultResponse as the third argument |
|
| 996 | - // is a reference |
|
| 997 | - $doMethod = function () use ($command, &$arguments) { |
|
| 998 | - if ($command == 'controlPagedResultResponse') { |
|
| 999 | - throw new \InvalidArgumentException('Invoker does not support controlPagedResultResponse, call LDAP Wrapper directly instead.'); |
|
| 1000 | - } else { |
|
| 1001 | - return call_user_func_array(array($this->ldap, $command), $arguments); |
|
| 1002 | - } |
|
| 1003 | - }; |
|
| 1004 | - try { |
|
| 1005 | - $ret = $doMethod(); |
|
| 1006 | - } catch (ServerNotAvailableException $e) { |
|
| 1007 | - /* Server connection lost, attempt to reestablish it |
|
| 345 | + /** |
|
| 346 | + * Set password for an LDAP user identified by a DN |
|
| 347 | + * |
|
| 348 | + * @param string $userDN the user in question |
|
| 349 | + * @param string $password the new password |
|
| 350 | + * @return bool |
|
| 351 | + * @throws HintException |
|
| 352 | + * @throws \Exception |
|
| 353 | + */ |
|
| 354 | + public function setPassword($userDN, $password) { |
|
| 355 | + if((int)$this->connection->turnOnPasswordChange !== 1) { |
|
| 356 | + throw new \Exception('LDAP password changes are disabled.'); |
|
| 357 | + } |
|
| 358 | + $cr = $this->connection->getConnectionResource(); |
|
| 359 | + if(!$this->ldap->isResource($cr)) { |
|
| 360 | + //LDAP not available |
|
| 361 | + \OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', \OCP\Util::DEBUG); |
|
| 362 | + return false; |
|
| 363 | + } |
|
| 364 | + try { |
|
| 365 | + return @$this->invokeLDAPMethod('modReplace', $cr, $userDN, $password); |
|
| 366 | + } catch(ConstraintViolationException $e) { |
|
| 367 | + throw new HintException('Password change rejected.', \OC::$server->getL10N('user_ldap')->t('Password change rejected. Hint: ').$e->getMessage(), $e->getCode()); |
|
| 368 | + } |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * checks whether the given attributes value is probably a DN |
|
| 373 | + * @param string $attr the attribute in question |
|
| 374 | + * @return boolean if so true, otherwise false |
|
| 375 | + */ |
|
| 376 | + private function resemblesDN($attr) { |
|
| 377 | + $resemblingAttributes = array( |
|
| 378 | + 'dn', |
|
| 379 | + 'uniquemember', |
|
| 380 | + 'member', |
|
| 381 | + // memberOf is an "operational" attribute, without a definition in any RFC |
|
| 382 | + 'memberof' |
|
| 383 | + ); |
|
| 384 | + return in_array($attr, $resemblingAttributes); |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + /** |
|
| 388 | + * checks whether the given string is probably a DN |
|
| 389 | + * @param string $string |
|
| 390 | + * @return boolean |
|
| 391 | + */ |
|
| 392 | + public function stringResemblesDN($string) { |
|
| 393 | + $r = $this->ldap->explodeDN($string, 0); |
|
| 394 | + // if exploding a DN succeeds and does not end up in |
|
| 395 | + // an empty array except for $r[count] being 0. |
|
| 396 | + return (is_array($r) && count($r) > 1); |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + /** |
|
| 400 | + * returns a DN-string that is cleaned from not domain parts, e.g. |
|
| 401 | + * cn=foo,cn=bar,dc=foobar,dc=server,dc=org |
|
| 402 | + * becomes dc=foobar,dc=server,dc=org |
|
| 403 | + * @param string $dn |
|
| 404 | + * @return string |
|
| 405 | + */ |
|
| 406 | + public function getDomainDNFromDN($dn) { |
|
| 407 | + $allParts = $this->ldap->explodeDN($dn, 0); |
|
| 408 | + if($allParts === false) { |
|
| 409 | + //not a valid DN |
|
| 410 | + return ''; |
|
| 411 | + } |
|
| 412 | + $domainParts = array(); |
|
| 413 | + $dcFound = false; |
|
| 414 | + foreach($allParts as $part) { |
|
| 415 | + if(!$dcFound && strpos($part, 'dc=') === 0) { |
|
| 416 | + $dcFound = true; |
|
| 417 | + } |
|
| 418 | + if($dcFound) { |
|
| 419 | + $domainParts[] = $part; |
|
| 420 | + } |
|
| 421 | + } |
|
| 422 | + $domainDN = implode(',', $domainParts); |
|
| 423 | + return $domainDN; |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + /** |
|
| 427 | + * returns the LDAP DN for the given internal Nextcloud name of the group |
|
| 428 | + * @param string $name the Nextcloud name in question |
|
| 429 | + * @return string|false LDAP DN on success, otherwise false |
|
| 430 | + */ |
|
| 431 | + public function groupname2dn($name) { |
|
| 432 | + return $this->groupMapper->getDNByName($name); |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + /** |
|
| 436 | + * returns the LDAP DN for the given internal Nextcloud name of the user |
|
| 437 | + * @param string $name the Nextcloud name in question |
|
| 438 | + * @return string|false with the LDAP DN on success, otherwise false |
|
| 439 | + */ |
|
| 440 | + public function username2dn($name) { |
|
| 441 | + $fdn = $this->userMapper->getDNByName($name); |
|
| 442 | + |
|
| 443 | + //Check whether the DN belongs to the Base, to avoid issues on multi- |
|
| 444 | + //server setups |
|
| 445 | + if(is_string($fdn) && $this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 446 | + return $fdn; |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + return false; |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * returns the internal Nextcloud name for the given LDAP DN of the group, false on DN outside of search DN or failure |
|
| 454 | + * @param string $fdn the dn of the group object |
|
| 455 | + * @param string $ldapName optional, the display name of the object |
|
| 456 | + * @return string|false with the name to use in Nextcloud, false on DN outside of search DN |
|
| 457 | + */ |
|
| 458 | + public function dn2groupname($fdn, $ldapName = null) { |
|
| 459 | + //To avoid bypassing the base DN settings under certain circumstances |
|
| 460 | + //with the group support, check whether the provided DN matches one of |
|
| 461 | + //the given Bases |
|
| 462 | + if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseGroups)) { |
|
| 463 | + return false; |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + return $this->dn2ocname($fdn, $ldapName, false); |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + /** |
|
| 470 | + * accepts an array of group DNs and tests whether they match the user |
|
| 471 | + * filter by doing read operations against the group entries. Returns an |
|
| 472 | + * array of DNs that match the filter. |
|
| 473 | + * |
|
| 474 | + * @param string[] $groupDNs |
|
| 475 | + * @return string[] |
|
| 476 | + */ |
|
| 477 | + public function groupsMatchFilter($groupDNs) { |
|
| 478 | + $validGroupDNs = []; |
|
| 479 | + foreach($groupDNs as $dn) { |
|
| 480 | + $cacheKey = 'groupsMatchFilter-'.$dn; |
|
| 481 | + $groupMatchFilter = $this->connection->getFromCache($cacheKey); |
|
| 482 | + if(!is_null($groupMatchFilter)) { |
|
| 483 | + if($groupMatchFilter) { |
|
| 484 | + $validGroupDNs[] = $dn; |
|
| 485 | + } |
|
| 486 | + continue; |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + // Check the base DN first. If this is not met already, we don't |
|
| 490 | + // need to ask the server at all. |
|
| 491 | + if(!$this->isDNPartOfBase($dn, $this->connection->ldapBaseGroups)) { |
|
| 492 | + $this->connection->writeToCache($cacheKey, false); |
|
| 493 | + continue; |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + $result = $this->readAttribute($dn, 'cn', $this->connection->ldapGroupFilter); |
|
| 497 | + if(is_array($result)) { |
|
| 498 | + $this->connection->writeToCache($cacheKey, true); |
|
| 499 | + $validGroupDNs[] = $dn; |
|
| 500 | + } else { |
|
| 501 | + $this->connection->writeToCache($cacheKey, false); |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + } |
|
| 505 | + return $validGroupDNs; |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + /** |
|
| 509 | + * returns the internal Nextcloud name for the given LDAP DN of the user, false on DN outside of search DN or failure |
|
| 510 | + * @param string $dn the dn of the user object |
|
| 511 | + * @param string $ldapName optional, the display name of the object |
|
| 512 | + * @return string|false with with the name to use in Nextcloud |
|
| 513 | + */ |
|
| 514 | + public function dn2username($fdn, $ldapName = null) { |
|
| 515 | + //To avoid bypassing the base DN settings under certain circumstances |
|
| 516 | + //with the group support, check whether the provided DN matches one of |
|
| 517 | + //the given Bases |
|
| 518 | + if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 519 | + return false; |
|
| 520 | + } |
|
| 521 | + |
|
| 522 | + return $this->dn2ocname($fdn, $ldapName, true); |
|
| 523 | + } |
|
| 524 | + |
|
| 525 | + /** |
|
| 526 | + * returns an internal Nextcloud name for the given LDAP DN, false on DN outside of search DN |
|
| 527 | + * |
|
| 528 | + * @param string $fdn the dn of the user object |
|
| 529 | + * @param string|null $ldapName optional, the display name of the object |
|
| 530 | + * @param bool $isUser optional, whether it is a user object (otherwise group assumed) |
|
| 531 | + * @param bool|null $newlyMapped |
|
| 532 | + * @param array|null $record |
|
| 533 | + * @return false|string with with the name to use in Nextcloud |
|
| 534 | + * @throws \Exception |
|
| 535 | + */ |
|
| 536 | + public function dn2ocname($fdn, $ldapName = null, $isUser = true, &$newlyMapped = null, array $record = null) { |
|
| 537 | + $newlyMapped = false; |
|
| 538 | + if($isUser) { |
|
| 539 | + $mapper = $this->getUserMapper(); |
|
| 540 | + $nameAttribute = $this->connection->ldapUserDisplayName; |
|
| 541 | + } else { |
|
| 542 | + $mapper = $this->getGroupMapper(); |
|
| 543 | + $nameAttribute = $this->connection->ldapGroupDisplayName; |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + //let's try to retrieve the Nextcloud name from the mappings table |
|
| 547 | + $ncName = $mapper->getNameByDN($fdn); |
|
| 548 | + if(is_string($ncName)) { |
|
| 549 | + return $ncName; |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + //second try: get the UUID and check if it is known. Then, update the DN and return the name. |
|
| 553 | + $uuid = $this->getUUID($fdn, $isUser, $record); |
|
| 554 | + if(is_string($uuid)) { |
|
| 555 | + $ncName = $mapper->getNameByUUID($uuid); |
|
| 556 | + if(is_string($ncName)) { |
|
| 557 | + $mapper->setDNbyUUID($fdn, $uuid); |
|
| 558 | + return $ncName; |
|
| 559 | + } |
|
| 560 | + } else { |
|
| 561 | + //If the UUID can't be detected something is foul. |
|
| 562 | + \OCP\Util::writeLog('user_ldap', 'Cannot determine UUID for '.$fdn.'. Skipping.', \OCP\Util::INFO); |
|
| 563 | + return false; |
|
| 564 | + } |
|
| 565 | + |
|
| 566 | + if(is_null($ldapName)) { |
|
| 567 | + $ldapName = $this->readAttribute($fdn, $nameAttribute); |
|
| 568 | + if(!isset($ldapName[0]) && empty($ldapName[0])) { |
|
| 569 | + \OCP\Util::writeLog('user_ldap', 'No or empty name for '.$fdn.'.', \OCP\Util::INFO); |
|
| 570 | + return false; |
|
| 571 | + } |
|
| 572 | + $ldapName = $ldapName[0]; |
|
| 573 | + } |
|
| 574 | + |
|
| 575 | + if($isUser) { |
|
| 576 | + $usernameAttribute = (string)$this->connection->ldapExpertUsernameAttr; |
|
| 577 | + if ($usernameAttribute !== '') { |
|
| 578 | + $username = $this->readAttribute($fdn, $usernameAttribute); |
|
| 579 | + $username = $username[0]; |
|
| 580 | + } else { |
|
| 581 | + $username = $uuid; |
|
| 582 | + } |
|
| 583 | + $intName = $this->sanitizeUsername($username); |
|
| 584 | + } else { |
|
| 585 | + $intName = $ldapName; |
|
| 586 | + } |
|
| 587 | + |
|
| 588 | + //a new user/group! Add it only if it doesn't conflict with other backend's users or existing groups |
|
| 589 | + //disabling Cache is required to avoid that the new user is cached as not-existing in fooExists check |
|
| 590 | + //NOTE: mind, disabling cache affects only this instance! Using it |
|
| 591 | + // outside of core user management will still cache the user as non-existing. |
|
| 592 | + $originalTTL = $this->connection->ldapCacheTTL; |
|
| 593 | + $this->connection->setConfiguration(array('ldapCacheTTL' => 0)); |
|
| 594 | + if(($isUser && $intName !== '' && !\OC::$server->getUserManager()->userExists($intName)) |
|
| 595 | + || (!$isUser && !\OC::$server->getGroupManager()->groupExists($intName))) { |
|
| 596 | + if($mapper->map($fdn, $intName, $uuid)) { |
|
| 597 | + $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); |
|
| 598 | + $newlyMapped = true; |
|
| 599 | + return $intName; |
|
| 600 | + } |
|
| 601 | + } |
|
| 602 | + $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); |
|
| 603 | + |
|
| 604 | + $altName = $this->createAltInternalOwnCloudName($intName, $isUser); |
|
| 605 | + if(is_string($altName) && $mapper->map($fdn, $altName, $uuid)) { |
|
| 606 | + $newlyMapped = true; |
|
| 607 | + return $altName; |
|
| 608 | + } |
|
| 609 | + |
|
| 610 | + //if everything else did not help.. |
|
| 611 | + \OCP\Util::writeLog('user_ldap', 'Could not create unique name for '.$fdn.'.', \OCP\Util::INFO); |
|
| 612 | + return false; |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + /** |
|
| 616 | + * gives back the user names as they are used ownClod internally |
|
| 617 | + * @param array $ldapUsers as returned by fetchList() |
|
| 618 | + * @return array an array with the user names to use in Nextcloud |
|
| 619 | + * |
|
| 620 | + * gives back the user names as they are used ownClod internally |
|
| 621 | + */ |
|
| 622 | + public function nextcloudUserNames($ldapUsers) { |
|
| 623 | + return $this->ldap2NextcloudNames($ldapUsers, true); |
|
| 624 | + } |
|
| 625 | + |
|
| 626 | + /** |
|
| 627 | + * gives back the group names as they are used ownClod internally |
|
| 628 | + * @param array $ldapGroups as returned by fetchList() |
|
| 629 | + * @return array an array with the group names to use in Nextcloud |
|
| 630 | + * |
|
| 631 | + * gives back the group names as they are used ownClod internally |
|
| 632 | + */ |
|
| 633 | + public function nextcloudGroupNames($ldapGroups) { |
|
| 634 | + return $this->ldap2NextcloudNames($ldapGroups, false); |
|
| 635 | + } |
|
| 636 | + |
|
| 637 | + /** |
|
| 638 | + * @param array $ldapObjects as returned by fetchList() |
|
| 639 | + * @param bool $isUsers |
|
| 640 | + * @return array |
|
| 641 | + */ |
|
| 642 | + private function ldap2NextcloudNames($ldapObjects, $isUsers) { |
|
| 643 | + if($isUsers) { |
|
| 644 | + $nameAttribute = $this->connection->ldapUserDisplayName; |
|
| 645 | + $sndAttribute = $this->connection->ldapUserDisplayName2; |
|
| 646 | + } else { |
|
| 647 | + $nameAttribute = $this->connection->ldapGroupDisplayName; |
|
| 648 | + } |
|
| 649 | + $nextcloudNames = array(); |
|
| 650 | + |
|
| 651 | + foreach($ldapObjects as $ldapObject) { |
|
| 652 | + $nameByLDAP = null; |
|
| 653 | + if( isset($ldapObject[$nameAttribute]) |
|
| 654 | + && is_array($ldapObject[$nameAttribute]) |
|
| 655 | + && isset($ldapObject[$nameAttribute][0]) |
|
| 656 | + ) { |
|
| 657 | + // might be set, but not necessarily. if so, we use it. |
|
| 658 | + $nameByLDAP = $ldapObject[$nameAttribute][0]; |
|
| 659 | + } |
|
| 660 | + |
|
| 661 | + $ncName = $this->dn2ocname($ldapObject['dn'][0], $nameByLDAP, $isUsers); |
|
| 662 | + if($ncName) { |
|
| 663 | + $nextcloudNames[] = $ncName; |
|
| 664 | + if($isUsers) { |
|
| 665 | + //cache the user names so it does not need to be retrieved |
|
| 666 | + //again later (e.g. sharing dialogue). |
|
| 667 | + if(is_null($nameByLDAP)) { |
|
| 668 | + continue; |
|
| 669 | + } |
|
| 670 | + $sndName = isset($ldapObject[$sndAttribute][0]) |
|
| 671 | + ? $ldapObject[$sndAttribute][0] : ''; |
|
| 672 | + $this->cacheUserDisplayName($ncName, $nameByLDAP, $sndName); |
|
| 673 | + } |
|
| 674 | + } |
|
| 675 | + } |
|
| 676 | + return $nextcloudNames; |
|
| 677 | + } |
|
| 678 | + |
|
| 679 | + /** |
|
| 680 | + * caches the user display name |
|
| 681 | + * @param string $ocName the internal Nextcloud username |
|
| 682 | + * @param string|false $home the home directory path |
|
| 683 | + */ |
|
| 684 | + public function cacheUserHome($ocName, $home) { |
|
| 685 | + $cacheKey = 'getHome'.$ocName; |
|
| 686 | + $this->connection->writeToCache($cacheKey, $home); |
|
| 687 | + } |
|
| 688 | + |
|
| 689 | + /** |
|
| 690 | + * caches a user as existing |
|
| 691 | + * @param string $ocName the internal Nextcloud username |
|
| 692 | + */ |
|
| 693 | + public function cacheUserExists($ocName) { |
|
| 694 | + $this->connection->writeToCache('userExists'.$ocName, true); |
|
| 695 | + } |
|
| 696 | + |
|
| 697 | + /** |
|
| 698 | + * caches the user display name |
|
| 699 | + * @param string $ocName the internal Nextcloud username |
|
| 700 | + * @param string $displayName the display name |
|
| 701 | + * @param string $displayName2 the second display name |
|
| 702 | + */ |
|
| 703 | + public function cacheUserDisplayName($ocName, $displayName, $displayName2 = '') { |
|
| 704 | + $user = $this->userManager->get($ocName); |
|
| 705 | + if($user === null) { |
|
| 706 | + return; |
|
| 707 | + } |
|
| 708 | + $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2); |
|
| 709 | + $cacheKeyTrunk = 'getDisplayName'; |
|
| 710 | + $this->connection->writeToCache($cacheKeyTrunk.$ocName, $displayName); |
|
| 711 | + } |
|
| 712 | + |
|
| 713 | + /** |
|
| 714 | + * creates a unique name for internal Nextcloud use for users. Don't call it directly. |
|
| 715 | + * @param string $name the display name of the object |
|
| 716 | + * @return string|false with with the name to use in Nextcloud or false if unsuccessful |
|
| 717 | + * |
|
| 718 | + * Instead of using this method directly, call |
|
| 719 | + * createAltInternalOwnCloudName($name, true) |
|
| 720 | + */ |
|
| 721 | + private function _createAltInternalOwnCloudNameForUsers($name) { |
|
| 722 | + $attempts = 0; |
|
| 723 | + //while loop is just a precaution. If a name is not generated within |
|
| 724 | + //20 attempts, something else is very wrong. Avoids infinite loop. |
|
| 725 | + while($attempts < 20){ |
|
| 726 | + $altName = $name . '_' . rand(1000,9999); |
|
| 727 | + if(!\OC::$server->getUserManager()->userExists($altName)) { |
|
| 728 | + return $altName; |
|
| 729 | + } |
|
| 730 | + $attempts++; |
|
| 731 | + } |
|
| 732 | + return false; |
|
| 733 | + } |
|
| 734 | + |
|
| 735 | + /** |
|
| 736 | + * creates a unique name for internal Nextcloud use for groups. Don't call it directly. |
|
| 737 | + * @param string $name the display name of the object |
|
| 738 | + * @return string|false with with the name to use in Nextcloud or false if unsuccessful. |
|
| 739 | + * |
|
| 740 | + * Instead of using this method directly, call |
|
| 741 | + * createAltInternalOwnCloudName($name, false) |
|
| 742 | + * |
|
| 743 | + * Group names are also used as display names, so we do a sequential |
|
| 744 | + * numbering, e.g. Developers_42 when there are 41 other groups called |
|
| 745 | + * "Developers" |
|
| 746 | + */ |
|
| 747 | + private function _createAltInternalOwnCloudNameForGroups($name) { |
|
| 748 | + $usedNames = $this->groupMapper->getNamesBySearch($name, "", '_%'); |
|
| 749 | + if(!($usedNames) || count($usedNames) === 0) { |
|
| 750 | + $lastNo = 1; //will become name_2 |
|
| 751 | + } else { |
|
| 752 | + natsort($usedNames); |
|
| 753 | + $lastName = array_pop($usedNames); |
|
| 754 | + $lastNo = (int)substr($lastName, strrpos($lastName, '_') + 1); |
|
| 755 | + } |
|
| 756 | + $altName = $name.'_'. (string)($lastNo+1); |
|
| 757 | + unset($usedNames); |
|
| 758 | + |
|
| 759 | + $attempts = 1; |
|
| 760 | + while($attempts < 21){ |
|
| 761 | + // Check to be really sure it is unique |
|
| 762 | + // while loop is just a precaution. If a name is not generated within |
|
| 763 | + // 20 attempts, something else is very wrong. Avoids infinite loop. |
|
| 764 | + if(!\OC::$server->getGroupManager()->groupExists($altName)) { |
|
| 765 | + return $altName; |
|
| 766 | + } |
|
| 767 | + $altName = $name . '_' . ($lastNo + $attempts); |
|
| 768 | + $attempts++; |
|
| 769 | + } |
|
| 770 | + return false; |
|
| 771 | + } |
|
| 772 | + |
|
| 773 | + /** |
|
| 774 | + * creates a unique name for internal Nextcloud use. |
|
| 775 | + * @param string $name the display name of the object |
|
| 776 | + * @param boolean $isUser whether name should be created for a user (true) or a group (false) |
|
| 777 | + * @return string|false with with the name to use in Nextcloud or false if unsuccessful |
|
| 778 | + */ |
|
| 779 | + private function createAltInternalOwnCloudName($name, $isUser) { |
|
| 780 | + $originalTTL = $this->connection->ldapCacheTTL; |
|
| 781 | + $this->connection->setConfiguration(array('ldapCacheTTL' => 0)); |
|
| 782 | + if($isUser) { |
|
| 783 | + $altName = $this->_createAltInternalOwnCloudNameForUsers($name); |
|
| 784 | + } else { |
|
| 785 | + $altName = $this->_createAltInternalOwnCloudNameForGroups($name); |
|
| 786 | + } |
|
| 787 | + $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); |
|
| 788 | + |
|
| 789 | + return $altName; |
|
| 790 | + } |
|
| 791 | + |
|
| 792 | + /** |
|
| 793 | + * fetches a list of users according to a provided loginName and utilizing |
|
| 794 | + * the login filter. |
|
| 795 | + * |
|
| 796 | + * @param string $loginName |
|
| 797 | + * @param array $attributes optional, list of attributes to read |
|
| 798 | + * @return array |
|
| 799 | + */ |
|
| 800 | + public function fetchUsersByLoginName($loginName, $attributes = array('dn')) { |
|
| 801 | + $loginName = $this->escapeFilterPart($loginName); |
|
| 802 | + $filter = str_replace('%uid', $loginName, $this->connection->ldapLoginFilter); |
|
| 803 | + $users = $this->fetchListOfUsers($filter, $attributes); |
|
| 804 | + return $users; |
|
| 805 | + } |
|
| 806 | + |
|
| 807 | + /** |
|
| 808 | + * counts the number of users according to a provided loginName and |
|
| 809 | + * utilizing the login filter. |
|
| 810 | + * |
|
| 811 | + * @param string $loginName |
|
| 812 | + * @return int |
|
| 813 | + */ |
|
| 814 | + public function countUsersByLoginName($loginName) { |
|
| 815 | + $loginName = $this->escapeFilterPart($loginName); |
|
| 816 | + $filter = str_replace('%uid', $loginName, $this->connection->ldapLoginFilter); |
|
| 817 | + $users = $this->countUsers($filter); |
|
| 818 | + return $users; |
|
| 819 | + } |
|
| 820 | + |
|
| 821 | + /** |
|
| 822 | + * @param string $filter |
|
| 823 | + * @param string|string[] $attr |
|
| 824 | + * @param int $limit |
|
| 825 | + * @param int $offset |
|
| 826 | + * @param bool $forceApplyAttributes |
|
| 827 | + * @return array |
|
| 828 | + */ |
|
| 829 | + public function fetchListOfUsers($filter, $attr, $limit = null, $offset = null, $forceApplyAttributes = false) { |
|
| 830 | + $ldapRecords = $this->searchUsers($filter, $attr, $limit, $offset); |
|
| 831 | + $recordsToUpdate = $ldapRecords; |
|
| 832 | + if(!$forceApplyAttributes) { |
|
| 833 | + $isBackgroundJobModeAjax = $this->config |
|
| 834 | + ->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'ajax'; |
|
| 835 | + $recordsToUpdate = array_filter($ldapRecords, function($record) use ($isBackgroundJobModeAjax) { |
|
| 836 | + $newlyMapped = false; |
|
| 837 | + $uid = $this->dn2ocname($record['dn'][0], null, true, $newlyMapped, $record); |
|
| 838 | + if(is_string($uid)) { |
|
| 839 | + $this->cacheUserExists($uid); |
|
| 840 | + } |
|
| 841 | + return ($uid !== false) && ($newlyMapped || $isBackgroundJobModeAjax); |
|
| 842 | + }); |
|
| 843 | + } |
|
| 844 | + $this->batchApplyUserAttributes($recordsToUpdate); |
|
| 845 | + return $this->fetchList($ldapRecords, (count($attr) > 1)); |
|
| 846 | + } |
|
| 847 | + |
|
| 848 | + /** |
|
| 849 | + * provided with an array of LDAP user records the method will fetch the |
|
| 850 | + * user object and requests it to process the freshly fetched attributes and |
|
| 851 | + * and their values |
|
| 852 | + * @param array $ldapRecords |
|
| 853 | + */ |
|
| 854 | + public function batchApplyUserAttributes(array $ldapRecords){ |
|
| 855 | + $displayNameAttribute = strtolower($this->connection->ldapUserDisplayName); |
|
| 856 | + foreach($ldapRecords as $userRecord) { |
|
| 857 | + if(!isset($userRecord[$displayNameAttribute])) { |
|
| 858 | + // displayName is obligatory |
|
| 859 | + continue; |
|
| 860 | + } |
|
| 861 | + $ocName = $this->dn2ocname($userRecord['dn'][0], null, true); |
|
| 862 | + if($ocName === false) { |
|
| 863 | + continue; |
|
| 864 | + } |
|
| 865 | + $user = $this->userManager->get($ocName); |
|
| 866 | + if($user instanceof OfflineUser) { |
|
| 867 | + $user->unmark(); |
|
| 868 | + $user = $this->userManager->get($ocName); |
|
| 869 | + } |
|
| 870 | + if ($user !== null) { |
|
| 871 | + $user->processAttributes($userRecord); |
|
| 872 | + } else { |
|
| 873 | + \OC::$server->getLogger()->debug( |
|
| 874 | + "The ldap user manager returned null for $ocName", |
|
| 875 | + ['app'=>'user_ldap'] |
|
| 876 | + ); |
|
| 877 | + } |
|
| 878 | + } |
|
| 879 | + } |
|
| 880 | + |
|
| 881 | + /** |
|
| 882 | + * @param string $filter |
|
| 883 | + * @param string|string[] $attr |
|
| 884 | + * @param int $limit |
|
| 885 | + * @param int $offset |
|
| 886 | + * @return array |
|
| 887 | + */ |
|
| 888 | + public function fetchListOfGroups($filter, $attr, $limit = null, $offset = null) { |
|
| 889 | + return $this->fetchList($this->searchGroups($filter, $attr, $limit, $offset), (count($attr) > 1)); |
|
| 890 | + } |
|
| 891 | + |
|
| 892 | + /** |
|
| 893 | + * @param array $list |
|
| 894 | + * @param bool $manyAttributes |
|
| 895 | + * @return array |
|
| 896 | + */ |
|
| 897 | + private function fetchList($list, $manyAttributes) { |
|
| 898 | + if(is_array($list)) { |
|
| 899 | + if($manyAttributes) { |
|
| 900 | + return $list; |
|
| 901 | + } else { |
|
| 902 | + $list = array_reduce($list, function($carry, $item) { |
|
| 903 | + $attribute = array_keys($item)[0]; |
|
| 904 | + $carry[] = $item[$attribute][0]; |
|
| 905 | + return $carry; |
|
| 906 | + }, array()); |
|
| 907 | + return array_unique($list, SORT_LOCALE_STRING); |
|
| 908 | + } |
|
| 909 | + } |
|
| 910 | + |
|
| 911 | + //error cause actually, maybe throw an exception in future. |
|
| 912 | + return array(); |
|
| 913 | + } |
|
| 914 | + |
|
| 915 | + /** |
|
| 916 | + * executes an LDAP search, optimized for Users |
|
| 917 | + * @param string $filter the LDAP filter for the search |
|
| 918 | + * @param string|string[] $attr optional, when a certain attribute shall be filtered out |
|
| 919 | + * @param integer $limit |
|
| 920 | + * @param integer $offset |
|
| 921 | + * @return array with the search result |
|
| 922 | + * |
|
| 923 | + * Executes an LDAP search |
|
| 924 | + */ |
|
| 925 | + public function searchUsers($filter, $attr = null, $limit = null, $offset = null) { |
|
| 926 | + return $this->search($filter, $this->connection->ldapBaseUsers, $attr, $limit, $offset); |
|
| 927 | + } |
|
| 928 | + |
|
| 929 | + /** |
|
| 930 | + * @param string $filter |
|
| 931 | + * @param string|string[] $attr |
|
| 932 | + * @param int $limit |
|
| 933 | + * @param int $offset |
|
| 934 | + * @return false|int |
|
| 935 | + */ |
|
| 936 | + public function countUsers($filter, $attr = array('dn'), $limit = null, $offset = null) { |
|
| 937 | + return $this->count($filter, $this->connection->ldapBaseUsers, $attr, $limit, $offset); |
|
| 938 | + } |
|
| 939 | + |
|
| 940 | + /** |
|
| 941 | + * executes an LDAP search, optimized for Groups |
|
| 942 | + * @param string $filter the LDAP filter for the search |
|
| 943 | + * @param string|string[] $attr optional, when a certain attribute shall be filtered out |
|
| 944 | + * @param integer $limit |
|
| 945 | + * @param integer $offset |
|
| 946 | + * @return array with the search result |
|
| 947 | + * |
|
| 948 | + * Executes an LDAP search |
|
| 949 | + */ |
|
| 950 | + public function searchGroups($filter, $attr = null, $limit = null, $offset = null) { |
|
| 951 | + return $this->search($filter, $this->connection->ldapBaseGroups, $attr, $limit, $offset); |
|
| 952 | + } |
|
| 953 | + |
|
| 954 | + /** |
|
| 955 | + * returns the number of available groups |
|
| 956 | + * @param string $filter the LDAP search filter |
|
| 957 | + * @param string[] $attr optional |
|
| 958 | + * @param int|null $limit |
|
| 959 | + * @param int|null $offset |
|
| 960 | + * @return int|bool |
|
| 961 | + */ |
|
| 962 | + public function countGroups($filter, $attr = array('dn'), $limit = null, $offset = null) { |
|
| 963 | + return $this->count($filter, $this->connection->ldapBaseGroups, $attr, $limit, $offset); |
|
| 964 | + } |
|
| 965 | + |
|
| 966 | + /** |
|
| 967 | + * returns the number of available objects on the base DN |
|
| 968 | + * |
|
| 969 | + * @param int|null $limit |
|
| 970 | + * @param int|null $offset |
|
| 971 | + * @return int|bool |
|
| 972 | + */ |
|
| 973 | + public function countObjects($limit = null, $offset = null) { |
|
| 974 | + return $this->count('objectclass=*', $this->connection->ldapBase, array('dn'), $limit, $offset); |
|
| 975 | + } |
|
| 976 | + |
|
| 977 | + /** |
|
| 978 | + * Returns the LDAP handler |
|
| 979 | + * @throws \OC\ServerNotAvailableException |
|
| 980 | + */ |
|
| 981 | + |
|
| 982 | + /** |
|
| 983 | + * @return mixed |
|
| 984 | + * @throws \OC\ServerNotAvailableException |
|
| 985 | + */ |
|
| 986 | + private function invokeLDAPMethod() { |
|
| 987 | + $arguments = func_get_args(); |
|
| 988 | + $command = array_shift($arguments); |
|
| 989 | + $cr = array_shift($arguments); |
|
| 990 | + if (!method_exists($this->ldap, $command)) { |
|
| 991 | + return null; |
|
| 992 | + } |
|
| 993 | + array_unshift($arguments, $cr); |
|
| 994 | + // php no longer supports call-time pass-by-reference |
|
| 995 | + // thus cannot support controlPagedResultResponse as the third argument |
|
| 996 | + // is a reference |
|
| 997 | + $doMethod = function () use ($command, &$arguments) { |
|
| 998 | + if ($command == 'controlPagedResultResponse') { |
|
| 999 | + throw new \InvalidArgumentException('Invoker does not support controlPagedResultResponse, call LDAP Wrapper directly instead.'); |
|
| 1000 | + } else { |
|
| 1001 | + return call_user_func_array(array($this->ldap, $command), $arguments); |
|
| 1002 | + } |
|
| 1003 | + }; |
|
| 1004 | + try { |
|
| 1005 | + $ret = $doMethod(); |
|
| 1006 | + } catch (ServerNotAvailableException $e) { |
|
| 1007 | + /* Server connection lost, attempt to reestablish it |
|
| 1008 | 1008 | * Maybe implement exponential backoff? |
| 1009 | 1009 | * This was enough to get solr indexer working which has large delays between LDAP fetches. |
| 1010 | 1010 | */ |
| 1011 | - \OCP\Util::writeLog('user_ldap', "Connection lost on $command, attempting to reestablish.", \OCP\Util::DEBUG); |
|
| 1012 | - $this->connection->resetConnectionResource(); |
|
| 1013 | - $cr = $this->connection->getConnectionResource(); |
|
| 1014 | - |
|
| 1015 | - if(!$this->ldap->isResource($cr)) { |
|
| 1016 | - // Seems like we didn't find any resource. |
|
| 1017 | - \OCP\Util::writeLog('user_ldap', "Could not $command, because resource is missing.", \OCP\Util::DEBUG); |
|
| 1018 | - throw $e; |
|
| 1019 | - } |
|
| 1020 | - |
|
| 1021 | - $arguments[0] = array_pad([], count($arguments[0]), $cr); |
|
| 1022 | - $ret = $doMethod(); |
|
| 1023 | - } |
|
| 1024 | - return $ret; |
|
| 1025 | - } |
|
| 1026 | - |
|
| 1027 | - /** |
|
| 1028 | - * retrieved. Results will according to the order in the array. |
|
| 1029 | - * |
|
| 1030 | - * @param $filter |
|
| 1031 | - * @param $base |
|
| 1032 | - * @param string[]|string|null $attr |
|
| 1033 | - * @param int $limit optional, maximum results to be counted |
|
| 1034 | - * @param int $offset optional, a starting point |
|
| 1035 | - * @return array|false array with the search result as first value and pagedSearchOK as |
|
| 1036 | - * second | false if not successful |
|
| 1037 | - * @throws ServerNotAvailableException |
|
| 1038 | - */ |
|
| 1039 | - private function executeSearch($filter, $base, &$attr = null, $limit = null, $offset = null) { |
|
| 1040 | - if(!is_null($attr) && !is_array($attr)) { |
|
| 1041 | - $attr = array(mb_strtolower($attr, 'UTF-8')); |
|
| 1042 | - } |
|
| 1043 | - |
|
| 1044 | - // See if we have a resource, in case not cancel with message |
|
| 1045 | - $cr = $this->connection->getConnectionResource(); |
|
| 1046 | - if(!$this->ldap->isResource($cr)) { |
|
| 1047 | - // Seems like we didn't find any resource. |
|
| 1048 | - // Return an empty array just like before. |
|
| 1049 | - \OCP\Util::writeLog('user_ldap', 'Could not search, because resource is missing.', \OCP\Util::DEBUG); |
|
| 1050 | - return false; |
|
| 1051 | - } |
|
| 1052 | - |
|
| 1053 | - //check whether paged search should be attempted |
|
| 1054 | - $pagedSearchOK = $this->initPagedSearch($filter, $base, $attr, (int)$limit, $offset); |
|
| 1055 | - |
|
| 1056 | - $linkResources = array_pad(array(), count($base), $cr); |
|
| 1057 | - $sr = $this->invokeLDAPMethod('search', $linkResources, $base, $filter, $attr); |
|
| 1058 | - // cannot use $cr anymore, might have changed in the previous call! |
|
| 1059 | - $error = $this->ldap->errno($this->connection->getConnectionResource()); |
|
| 1060 | - if(!is_array($sr) || $error !== 0) { |
|
| 1061 | - \OCP\Util::writeLog('user_ldap', 'Attempt for Paging? '.print_r($pagedSearchOK, true), \OCP\Util::ERROR); |
|
| 1062 | - return false; |
|
| 1063 | - } |
|
| 1064 | - |
|
| 1065 | - return array($sr, $pagedSearchOK); |
|
| 1066 | - } |
|
| 1067 | - |
|
| 1068 | - /** |
|
| 1069 | - * processes an LDAP paged search operation |
|
| 1070 | - * @param array $sr the array containing the LDAP search resources |
|
| 1071 | - * @param string $filter the LDAP filter for the search |
|
| 1072 | - * @param array $base an array containing the LDAP subtree(s) that shall be searched |
|
| 1073 | - * @param int $iFoundItems number of results in the single search operation |
|
| 1074 | - * @param int $limit maximum results to be counted |
|
| 1075 | - * @param int $offset a starting point |
|
| 1076 | - * @param bool $pagedSearchOK whether a paged search has been executed |
|
| 1077 | - * @param bool $skipHandling required for paged search when cookies to |
|
| 1078 | - * prior results need to be gained |
|
| 1079 | - * @return bool cookie validity, true if we have more pages, false otherwise. |
|
| 1080 | - */ |
|
| 1081 | - private function processPagedSearchStatus($sr, $filter, $base, $iFoundItems, $limit, $offset, $pagedSearchOK, $skipHandling) { |
|
| 1082 | - $cookie = null; |
|
| 1083 | - if($pagedSearchOK) { |
|
| 1084 | - $cr = $this->connection->getConnectionResource(); |
|
| 1085 | - foreach($sr as $key => $res) { |
|
| 1086 | - if($this->ldap->controlPagedResultResponse($cr, $res, $cookie)) { |
|
| 1087 | - $this->setPagedResultCookie($base[$key], $filter, $limit, $offset, $cookie); |
|
| 1088 | - } |
|
| 1089 | - } |
|
| 1090 | - |
|
| 1091 | - //browsing through prior pages to get the cookie for the new one |
|
| 1092 | - if($skipHandling) { |
|
| 1093 | - return false; |
|
| 1094 | - } |
|
| 1095 | - // if count is bigger, then the server does not support |
|
| 1096 | - // paged search. Instead, he did a normal search. We set a |
|
| 1097 | - // flag here, so the callee knows how to deal with it. |
|
| 1098 | - if($iFoundItems <= $limit) { |
|
| 1099 | - $this->pagedSearchedSuccessful = true; |
|
| 1100 | - } |
|
| 1101 | - } else { |
|
| 1102 | - if(!is_null($limit) && (int)$this->connection->ldapPagingSize !== 0) { |
|
| 1103 | - \OC::$server->getLogger()->debug( |
|
| 1104 | - 'Paged search was not available', |
|
| 1105 | - [ 'app' => 'user_ldap' ] |
|
| 1106 | - ); |
|
| 1107 | - } |
|
| 1108 | - } |
|
| 1109 | - /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1011 | + \OCP\Util::writeLog('user_ldap', "Connection lost on $command, attempting to reestablish.", \OCP\Util::DEBUG); |
|
| 1012 | + $this->connection->resetConnectionResource(); |
|
| 1013 | + $cr = $this->connection->getConnectionResource(); |
|
| 1014 | + |
|
| 1015 | + if(!$this->ldap->isResource($cr)) { |
|
| 1016 | + // Seems like we didn't find any resource. |
|
| 1017 | + \OCP\Util::writeLog('user_ldap', "Could not $command, because resource is missing.", \OCP\Util::DEBUG); |
|
| 1018 | + throw $e; |
|
| 1019 | + } |
|
| 1020 | + |
|
| 1021 | + $arguments[0] = array_pad([], count($arguments[0]), $cr); |
|
| 1022 | + $ret = $doMethod(); |
|
| 1023 | + } |
|
| 1024 | + return $ret; |
|
| 1025 | + } |
|
| 1026 | + |
|
| 1027 | + /** |
|
| 1028 | + * retrieved. Results will according to the order in the array. |
|
| 1029 | + * |
|
| 1030 | + * @param $filter |
|
| 1031 | + * @param $base |
|
| 1032 | + * @param string[]|string|null $attr |
|
| 1033 | + * @param int $limit optional, maximum results to be counted |
|
| 1034 | + * @param int $offset optional, a starting point |
|
| 1035 | + * @return array|false array with the search result as first value and pagedSearchOK as |
|
| 1036 | + * second | false if not successful |
|
| 1037 | + * @throws ServerNotAvailableException |
|
| 1038 | + */ |
|
| 1039 | + private function executeSearch($filter, $base, &$attr = null, $limit = null, $offset = null) { |
|
| 1040 | + if(!is_null($attr) && !is_array($attr)) { |
|
| 1041 | + $attr = array(mb_strtolower($attr, 'UTF-8')); |
|
| 1042 | + } |
|
| 1043 | + |
|
| 1044 | + // See if we have a resource, in case not cancel with message |
|
| 1045 | + $cr = $this->connection->getConnectionResource(); |
|
| 1046 | + if(!$this->ldap->isResource($cr)) { |
|
| 1047 | + // Seems like we didn't find any resource. |
|
| 1048 | + // Return an empty array just like before. |
|
| 1049 | + \OCP\Util::writeLog('user_ldap', 'Could not search, because resource is missing.', \OCP\Util::DEBUG); |
|
| 1050 | + return false; |
|
| 1051 | + } |
|
| 1052 | + |
|
| 1053 | + //check whether paged search should be attempted |
|
| 1054 | + $pagedSearchOK = $this->initPagedSearch($filter, $base, $attr, (int)$limit, $offset); |
|
| 1055 | + |
|
| 1056 | + $linkResources = array_pad(array(), count($base), $cr); |
|
| 1057 | + $sr = $this->invokeLDAPMethod('search', $linkResources, $base, $filter, $attr); |
|
| 1058 | + // cannot use $cr anymore, might have changed in the previous call! |
|
| 1059 | + $error = $this->ldap->errno($this->connection->getConnectionResource()); |
|
| 1060 | + if(!is_array($sr) || $error !== 0) { |
|
| 1061 | + \OCP\Util::writeLog('user_ldap', 'Attempt for Paging? '.print_r($pagedSearchOK, true), \OCP\Util::ERROR); |
|
| 1062 | + return false; |
|
| 1063 | + } |
|
| 1064 | + |
|
| 1065 | + return array($sr, $pagedSearchOK); |
|
| 1066 | + } |
|
| 1067 | + |
|
| 1068 | + /** |
|
| 1069 | + * processes an LDAP paged search operation |
|
| 1070 | + * @param array $sr the array containing the LDAP search resources |
|
| 1071 | + * @param string $filter the LDAP filter for the search |
|
| 1072 | + * @param array $base an array containing the LDAP subtree(s) that shall be searched |
|
| 1073 | + * @param int $iFoundItems number of results in the single search operation |
|
| 1074 | + * @param int $limit maximum results to be counted |
|
| 1075 | + * @param int $offset a starting point |
|
| 1076 | + * @param bool $pagedSearchOK whether a paged search has been executed |
|
| 1077 | + * @param bool $skipHandling required for paged search when cookies to |
|
| 1078 | + * prior results need to be gained |
|
| 1079 | + * @return bool cookie validity, true if we have more pages, false otherwise. |
|
| 1080 | + */ |
|
| 1081 | + private function processPagedSearchStatus($sr, $filter, $base, $iFoundItems, $limit, $offset, $pagedSearchOK, $skipHandling) { |
|
| 1082 | + $cookie = null; |
|
| 1083 | + if($pagedSearchOK) { |
|
| 1084 | + $cr = $this->connection->getConnectionResource(); |
|
| 1085 | + foreach($sr as $key => $res) { |
|
| 1086 | + if($this->ldap->controlPagedResultResponse($cr, $res, $cookie)) { |
|
| 1087 | + $this->setPagedResultCookie($base[$key], $filter, $limit, $offset, $cookie); |
|
| 1088 | + } |
|
| 1089 | + } |
|
| 1090 | + |
|
| 1091 | + //browsing through prior pages to get the cookie for the new one |
|
| 1092 | + if($skipHandling) { |
|
| 1093 | + return false; |
|
| 1094 | + } |
|
| 1095 | + // if count is bigger, then the server does not support |
|
| 1096 | + // paged search. Instead, he did a normal search. We set a |
|
| 1097 | + // flag here, so the callee knows how to deal with it. |
|
| 1098 | + if($iFoundItems <= $limit) { |
|
| 1099 | + $this->pagedSearchedSuccessful = true; |
|
| 1100 | + } |
|
| 1101 | + } else { |
|
| 1102 | + if(!is_null($limit) && (int)$this->connection->ldapPagingSize !== 0) { |
|
| 1103 | + \OC::$server->getLogger()->debug( |
|
| 1104 | + 'Paged search was not available', |
|
| 1105 | + [ 'app' => 'user_ldap' ] |
|
| 1106 | + ); |
|
| 1107 | + } |
|
| 1108 | + } |
|
| 1109 | + /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1110 | 1110 | * Return cookie status. If we don't have more pages, with RHDS |
| 1111 | 1111 | * cookie is null, with openldap cookie is an empty string and |
| 1112 | 1112 | * to 386ds '0' is a valid cookie. Even if $iFoundItems == 0 |
| 1113 | 1113 | */ |
| 1114 | - return !empty($cookie) || $cookie === '0'; |
|
| 1115 | - } |
|
| 1116 | - |
|
| 1117 | - /** |
|
| 1118 | - * executes an LDAP search, but counts the results only |
|
| 1119 | - * |
|
| 1120 | - * @param string $filter the LDAP filter for the search |
|
| 1121 | - * @param array $base an array containing the LDAP subtree(s) that shall be searched |
|
| 1122 | - * @param string|string[] $attr optional, array, one or more attributes that shall be |
|
| 1123 | - * retrieved. Results will according to the order in the array. |
|
| 1124 | - * @param int $limit optional, maximum results to be counted |
|
| 1125 | - * @param int $offset optional, a starting point |
|
| 1126 | - * @param bool $skipHandling indicates whether the pages search operation is |
|
| 1127 | - * completed |
|
| 1128 | - * @return int|false Integer or false if the search could not be initialized |
|
| 1129 | - * @throws ServerNotAvailableException |
|
| 1130 | - */ |
|
| 1131 | - private function count($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) { |
|
| 1132 | - \OCP\Util::writeLog('user_ldap', 'Count filter: '.print_r($filter, true), \OCP\Util::DEBUG); |
|
| 1133 | - |
|
| 1134 | - $limitPerPage = (int)$this->connection->ldapPagingSize; |
|
| 1135 | - if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1136 | - $limitPerPage = $limit; |
|
| 1137 | - } |
|
| 1138 | - |
|
| 1139 | - $counter = 0; |
|
| 1140 | - $count = null; |
|
| 1141 | - $this->connection->getConnectionResource(); |
|
| 1142 | - |
|
| 1143 | - do { |
|
| 1144 | - $search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset); |
|
| 1145 | - if($search === false) { |
|
| 1146 | - return $counter > 0 ? $counter : false; |
|
| 1147 | - } |
|
| 1148 | - list($sr, $pagedSearchOK) = $search; |
|
| 1149 | - |
|
| 1150 | - /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1114 | + return !empty($cookie) || $cookie === '0'; |
|
| 1115 | + } |
|
| 1116 | + |
|
| 1117 | + /** |
|
| 1118 | + * executes an LDAP search, but counts the results only |
|
| 1119 | + * |
|
| 1120 | + * @param string $filter the LDAP filter for the search |
|
| 1121 | + * @param array $base an array containing the LDAP subtree(s) that shall be searched |
|
| 1122 | + * @param string|string[] $attr optional, array, one or more attributes that shall be |
|
| 1123 | + * retrieved. Results will according to the order in the array. |
|
| 1124 | + * @param int $limit optional, maximum results to be counted |
|
| 1125 | + * @param int $offset optional, a starting point |
|
| 1126 | + * @param bool $skipHandling indicates whether the pages search operation is |
|
| 1127 | + * completed |
|
| 1128 | + * @return int|false Integer or false if the search could not be initialized |
|
| 1129 | + * @throws ServerNotAvailableException |
|
| 1130 | + */ |
|
| 1131 | + private function count($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) { |
|
| 1132 | + \OCP\Util::writeLog('user_ldap', 'Count filter: '.print_r($filter, true), \OCP\Util::DEBUG); |
|
| 1133 | + |
|
| 1134 | + $limitPerPage = (int)$this->connection->ldapPagingSize; |
|
| 1135 | + if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1136 | + $limitPerPage = $limit; |
|
| 1137 | + } |
|
| 1138 | + |
|
| 1139 | + $counter = 0; |
|
| 1140 | + $count = null; |
|
| 1141 | + $this->connection->getConnectionResource(); |
|
| 1142 | + |
|
| 1143 | + do { |
|
| 1144 | + $search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset); |
|
| 1145 | + if($search === false) { |
|
| 1146 | + return $counter > 0 ? $counter : false; |
|
| 1147 | + } |
|
| 1148 | + list($sr, $pagedSearchOK) = $search; |
|
| 1149 | + |
|
| 1150 | + /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1151 | 1151 | * countEntriesInSearchResults() method signature changed |
| 1152 | 1152 | * by removing $limit and &$hasHitLimit parameters |
| 1153 | 1153 | */ |
| 1154 | - $count = $this->countEntriesInSearchResults($sr); |
|
| 1155 | - $counter += $count; |
|
| 1154 | + $count = $this->countEntriesInSearchResults($sr); |
|
| 1155 | + $counter += $count; |
|
| 1156 | 1156 | |
| 1157 | - $hasMorePages = $this->processPagedSearchStatus($sr, $filter, $base, $count, $limitPerPage, |
|
| 1158 | - $offset, $pagedSearchOK, $skipHandling); |
|
| 1159 | - $offset += $limitPerPage; |
|
| 1160 | - /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1157 | + $hasMorePages = $this->processPagedSearchStatus($sr, $filter, $base, $count, $limitPerPage, |
|
| 1158 | + $offset, $pagedSearchOK, $skipHandling); |
|
| 1159 | + $offset += $limitPerPage; |
|
| 1160 | + /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1161 | 1161 | * Continue now depends on $hasMorePages value |
| 1162 | 1162 | */ |
| 1163 | - $continue = $pagedSearchOK && $hasMorePages; |
|
| 1164 | - } while($continue && (is_null($limit) || $limit <= 0 || $limit > $counter)); |
|
| 1165 | - |
|
| 1166 | - return $counter; |
|
| 1167 | - } |
|
| 1168 | - |
|
| 1169 | - /** |
|
| 1170 | - * @param array $searchResults |
|
| 1171 | - * @return int |
|
| 1172 | - */ |
|
| 1173 | - private function countEntriesInSearchResults($searchResults) { |
|
| 1174 | - $counter = 0; |
|
| 1175 | - |
|
| 1176 | - foreach($searchResults as $res) { |
|
| 1177 | - $count = (int)$this->invokeLDAPMethod('countEntries', $this->connection->getConnectionResource(), $res); |
|
| 1178 | - $counter += $count; |
|
| 1179 | - } |
|
| 1180 | - |
|
| 1181 | - return $counter; |
|
| 1182 | - } |
|
| 1183 | - |
|
| 1184 | - /** |
|
| 1185 | - * Executes an LDAP search |
|
| 1186 | - * |
|
| 1187 | - * @param string $filter the LDAP filter for the search |
|
| 1188 | - * @param array $base an array containing the LDAP subtree(s) that shall be searched |
|
| 1189 | - * @param string|string[] $attr optional, array, one or more attributes that shall be |
|
| 1190 | - * @param int $limit |
|
| 1191 | - * @param int $offset |
|
| 1192 | - * @param bool $skipHandling |
|
| 1193 | - * @return array with the search result |
|
| 1194 | - * @throws ServerNotAvailableException |
|
| 1195 | - */ |
|
| 1196 | - public function search($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) { |
|
| 1197 | - $limitPerPage = (int)$this->connection->ldapPagingSize; |
|
| 1198 | - if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1199 | - $limitPerPage = $limit; |
|
| 1200 | - } |
|
| 1201 | - |
|
| 1202 | - /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1163 | + $continue = $pagedSearchOK && $hasMorePages; |
|
| 1164 | + } while($continue && (is_null($limit) || $limit <= 0 || $limit > $counter)); |
|
| 1165 | + |
|
| 1166 | + return $counter; |
|
| 1167 | + } |
|
| 1168 | + |
|
| 1169 | + /** |
|
| 1170 | + * @param array $searchResults |
|
| 1171 | + * @return int |
|
| 1172 | + */ |
|
| 1173 | + private function countEntriesInSearchResults($searchResults) { |
|
| 1174 | + $counter = 0; |
|
| 1175 | + |
|
| 1176 | + foreach($searchResults as $res) { |
|
| 1177 | + $count = (int)$this->invokeLDAPMethod('countEntries', $this->connection->getConnectionResource(), $res); |
|
| 1178 | + $counter += $count; |
|
| 1179 | + } |
|
| 1180 | + |
|
| 1181 | + return $counter; |
|
| 1182 | + } |
|
| 1183 | + |
|
| 1184 | + /** |
|
| 1185 | + * Executes an LDAP search |
|
| 1186 | + * |
|
| 1187 | + * @param string $filter the LDAP filter for the search |
|
| 1188 | + * @param array $base an array containing the LDAP subtree(s) that shall be searched |
|
| 1189 | + * @param string|string[] $attr optional, array, one or more attributes that shall be |
|
| 1190 | + * @param int $limit |
|
| 1191 | + * @param int $offset |
|
| 1192 | + * @param bool $skipHandling |
|
| 1193 | + * @return array with the search result |
|
| 1194 | + * @throws ServerNotAvailableException |
|
| 1195 | + */ |
|
| 1196 | + public function search($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) { |
|
| 1197 | + $limitPerPage = (int)$this->connection->ldapPagingSize; |
|
| 1198 | + if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1199 | + $limitPerPage = $limit; |
|
| 1200 | + } |
|
| 1201 | + |
|
| 1202 | + /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1203 | 1203 | * As we can have pages with zero results and/or pages with less |
| 1204 | 1204 | * than $limit results but with a still valid server 'cookie', |
| 1205 | 1205 | * loops through until we get $continue equals true and |
| 1206 | 1206 | * $findings['count'] < $limit |
| 1207 | 1207 | */ |
| 1208 | - $findings = []; |
|
| 1209 | - $savedoffset = $offset; |
|
| 1210 | - do { |
|
| 1211 | - $search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset); |
|
| 1212 | - if($search === false) { |
|
| 1213 | - return []; |
|
| 1214 | - } |
|
| 1215 | - list($sr, $pagedSearchOK) = $search; |
|
| 1216 | - $cr = $this->connection->getConnectionResource(); |
|
| 1217 | - |
|
| 1218 | - if($skipHandling) { |
|
| 1219 | - //i.e. result do not need to be fetched, we just need the cookie |
|
| 1220 | - //thus pass 1 or any other value as $iFoundItems because it is not |
|
| 1221 | - //used |
|
| 1222 | - $this->processPagedSearchStatus($sr, $filter, $base, 1, $limitPerPage, |
|
| 1223 | - $offset, $pagedSearchOK, |
|
| 1224 | - $skipHandling); |
|
| 1225 | - return array(); |
|
| 1226 | - } |
|
| 1227 | - |
|
| 1228 | - $iFoundItems = 0; |
|
| 1229 | - foreach($sr as $res) { |
|
| 1230 | - $findings = array_merge($findings, $this->invokeLDAPMethod('getEntries', $cr, $res)); |
|
| 1231 | - $iFoundItems = max($iFoundItems, $findings['count']); |
|
| 1232 | - unset($findings['count']); |
|
| 1233 | - } |
|
| 1234 | - |
|
| 1235 | - $continue = $this->processPagedSearchStatus($sr, $filter, $base, $iFoundItems, |
|
| 1236 | - $limitPerPage, $offset, $pagedSearchOK, |
|
| 1237 | - $skipHandling); |
|
| 1238 | - $offset += $limitPerPage; |
|
| 1239 | - } while ($continue && $pagedSearchOK && ($limit === null || count($findings) < $limit)); |
|
| 1240 | - // reseting offset |
|
| 1241 | - $offset = $savedoffset; |
|
| 1242 | - |
|
| 1243 | - // if we're here, probably no connection resource is returned. |
|
| 1244 | - // to make Nextcloud behave nicely, we simply give back an empty array. |
|
| 1245 | - if(is_null($findings)) { |
|
| 1246 | - return array(); |
|
| 1247 | - } |
|
| 1248 | - |
|
| 1249 | - if(!is_null($attr)) { |
|
| 1250 | - $selection = []; |
|
| 1251 | - $i = 0; |
|
| 1252 | - foreach($findings as $item) { |
|
| 1253 | - if(!is_array($item)) { |
|
| 1254 | - continue; |
|
| 1255 | - } |
|
| 1256 | - $item = \OCP\Util::mb_array_change_key_case($item, MB_CASE_LOWER, 'UTF-8'); |
|
| 1257 | - foreach($attr as $key) { |
|
| 1258 | - if(isset($item[$key])) { |
|
| 1259 | - if(is_array($item[$key]) && isset($item[$key]['count'])) { |
|
| 1260 | - unset($item[$key]['count']); |
|
| 1261 | - } |
|
| 1262 | - if($key !== 'dn') { |
|
| 1263 | - if($this->resemblesDN($key)) { |
|
| 1264 | - $selection[$i][$key] = $this->helper->sanitizeDN($item[$key]); |
|
| 1265 | - } else if($key === 'objectguid' || $key === 'guid') { |
|
| 1266 | - $selection[$i][$key] = [$this->convertObjectGUID2Str($item[$key][0])]; |
|
| 1267 | - } else { |
|
| 1268 | - $selection[$i][$key] = $item[$key]; |
|
| 1269 | - } |
|
| 1270 | - } else { |
|
| 1271 | - $selection[$i][$key] = [$this->helper->sanitizeDN($item[$key])]; |
|
| 1272 | - } |
|
| 1273 | - } |
|
| 1274 | - |
|
| 1275 | - } |
|
| 1276 | - $i++; |
|
| 1277 | - } |
|
| 1278 | - $findings = $selection; |
|
| 1279 | - } |
|
| 1280 | - //we slice the findings, when |
|
| 1281 | - //a) paged search unsuccessful, though attempted |
|
| 1282 | - //b) no paged search, but limit set |
|
| 1283 | - if((!$this->getPagedSearchResultState() |
|
| 1284 | - && $pagedSearchOK) |
|
| 1285 | - || ( |
|
| 1286 | - !$pagedSearchOK |
|
| 1287 | - && !is_null($limit) |
|
| 1288 | - ) |
|
| 1289 | - ) { |
|
| 1290 | - $findings = array_slice($findings, (int)$offset, $limit); |
|
| 1291 | - } |
|
| 1292 | - return $findings; |
|
| 1293 | - } |
|
| 1294 | - |
|
| 1295 | - /** |
|
| 1296 | - * @param string $name |
|
| 1297 | - * @return bool|mixed|string |
|
| 1298 | - */ |
|
| 1299 | - public function sanitizeUsername($name) { |
|
| 1300 | - if($this->connection->ldapIgnoreNamingRules) { |
|
| 1301 | - return trim($name); |
|
| 1302 | - } |
|
| 1303 | - |
|
| 1304 | - // Transliteration |
|
| 1305 | - // latin characters to ASCII |
|
| 1306 | - $name = iconv('UTF-8', 'ASCII//TRANSLIT', $name); |
|
| 1307 | - |
|
| 1308 | - // Replacements |
|
| 1309 | - $name = str_replace(' ', '_', $name); |
|
| 1310 | - |
|
| 1311 | - // Every remaining disallowed characters will be removed |
|
| 1312 | - $name = preg_replace('/[^a-zA-Z0-9_.@-]/u', '', $name); |
|
| 1313 | - |
|
| 1314 | - return $name; |
|
| 1315 | - } |
|
| 1316 | - |
|
| 1317 | - /** |
|
| 1318 | - * escapes (user provided) parts for LDAP filter |
|
| 1319 | - * @param string $input, the provided value |
|
| 1320 | - * @param bool $allowAsterisk whether in * at the beginning should be preserved |
|
| 1321 | - * @return string the escaped string |
|
| 1322 | - */ |
|
| 1323 | - public function escapeFilterPart($input, $allowAsterisk = false) { |
|
| 1324 | - $asterisk = ''; |
|
| 1325 | - if($allowAsterisk && strlen($input) > 0 && $input[0] === '*') { |
|
| 1326 | - $asterisk = '*'; |
|
| 1327 | - $input = mb_substr($input, 1, null, 'UTF-8'); |
|
| 1328 | - } |
|
| 1329 | - $search = array('*', '\\', '(', ')'); |
|
| 1330 | - $replace = array('\\*', '\\\\', '\\(', '\\)'); |
|
| 1331 | - return $asterisk . str_replace($search, $replace, $input); |
|
| 1332 | - } |
|
| 1333 | - |
|
| 1334 | - /** |
|
| 1335 | - * combines the input filters with AND |
|
| 1336 | - * @param string[] $filters the filters to connect |
|
| 1337 | - * @return string the combined filter |
|
| 1338 | - */ |
|
| 1339 | - public function combineFilterWithAnd($filters) { |
|
| 1340 | - return $this->combineFilter($filters, '&'); |
|
| 1341 | - } |
|
| 1342 | - |
|
| 1343 | - /** |
|
| 1344 | - * combines the input filters with OR |
|
| 1345 | - * @param string[] $filters the filters to connect |
|
| 1346 | - * @return string the combined filter |
|
| 1347 | - * Combines Filter arguments with OR |
|
| 1348 | - */ |
|
| 1349 | - public function combineFilterWithOr($filters) { |
|
| 1350 | - return $this->combineFilter($filters, '|'); |
|
| 1351 | - } |
|
| 1352 | - |
|
| 1353 | - /** |
|
| 1354 | - * combines the input filters with given operator |
|
| 1355 | - * @param string[] $filters the filters to connect |
|
| 1356 | - * @param string $operator either & or | |
|
| 1357 | - * @return string the combined filter |
|
| 1358 | - */ |
|
| 1359 | - private function combineFilter($filters, $operator) { |
|
| 1360 | - $combinedFilter = '('.$operator; |
|
| 1361 | - foreach($filters as $filter) { |
|
| 1362 | - if ($filter !== '' && $filter[0] !== '(') { |
|
| 1363 | - $filter = '('.$filter.')'; |
|
| 1364 | - } |
|
| 1365 | - $combinedFilter.=$filter; |
|
| 1366 | - } |
|
| 1367 | - $combinedFilter.=')'; |
|
| 1368 | - return $combinedFilter; |
|
| 1369 | - } |
|
| 1370 | - |
|
| 1371 | - /** |
|
| 1372 | - * creates a filter part for to perform search for users |
|
| 1373 | - * @param string $search the search term |
|
| 1374 | - * @return string the final filter part to use in LDAP searches |
|
| 1375 | - */ |
|
| 1376 | - public function getFilterPartForUserSearch($search) { |
|
| 1377 | - return $this->getFilterPartForSearch($search, |
|
| 1378 | - $this->connection->ldapAttributesForUserSearch, |
|
| 1379 | - $this->connection->ldapUserDisplayName); |
|
| 1380 | - } |
|
| 1381 | - |
|
| 1382 | - /** |
|
| 1383 | - * creates a filter part for to perform search for groups |
|
| 1384 | - * @param string $search the search term |
|
| 1385 | - * @return string the final filter part to use in LDAP searches |
|
| 1386 | - */ |
|
| 1387 | - public function getFilterPartForGroupSearch($search) { |
|
| 1388 | - return $this->getFilterPartForSearch($search, |
|
| 1389 | - $this->connection->ldapAttributesForGroupSearch, |
|
| 1390 | - $this->connection->ldapGroupDisplayName); |
|
| 1391 | - } |
|
| 1392 | - |
|
| 1393 | - /** |
|
| 1394 | - * creates a filter part for searches by splitting up the given search |
|
| 1395 | - * string into single words |
|
| 1396 | - * @param string $search the search term |
|
| 1397 | - * @param string[] $searchAttributes needs to have at least two attributes, |
|
| 1398 | - * otherwise it does not make sense :) |
|
| 1399 | - * @return string the final filter part to use in LDAP searches |
|
| 1400 | - * @throws \Exception |
|
| 1401 | - */ |
|
| 1402 | - private function getAdvancedFilterPartForSearch($search, $searchAttributes) { |
|
| 1403 | - if(!is_array($searchAttributes) || count($searchAttributes) < 2) { |
|
| 1404 | - throw new \Exception('searchAttributes must be an array with at least two string'); |
|
| 1405 | - } |
|
| 1406 | - $searchWords = explode(' ', trim($search)); |
|
| 1407 | - $wordFilters = array(); |
|
| 1408 | - foreach($searchWords as $word) { |
|
| 1409 | - $word = $this->prepareSearchTerm($word); |
|
| 1410 | - //every word needs to appear at least once |
|
| 1411 | - $wordMatchOneAttrFilters = array(); |
|
| 1412 | - foreach($searchAttributes as $attr) { |
|
| 1413 | - $wordMatchOneAttrFilters[] = $attr . '=' . $word; |
|
| 1414 | - } |
|
| 1415 | - $wordFilters[] = $this->combineFilterWithOr($wordMatchOneAttrFilters); |
|
| 1416 | - } |
|
| 1417 | - return $this->combineFilterWithAnd($wordFilters); |
|
| 1418 | - } |
|
| 1419 | - |
|
| 1420 | - /** |
|
| 1421 | - * creates a filter part for searches |
|
| 1422 | - * @param string $search the search term |
|
| 1423 | - * @param string[]|null $searchAttributes |
|
| 1424 | - * @param string $fallbackAttribute a fallback attribute in case the user |
|
| 1425 | - * did not define search attributes. Typically the display name attribute. |
|
| 1426 | - * @return string the final filter part to use in LDAP searches |
|
| 1427 | - */ |
|
| 1428 | - private function getFilterPartForSearch($search, $searchAttributes, $fallbackAttribute) { |
|
| 1429 | - $filter = array(); |
|
| 1430 | - $haveMultiSearchAttributes = (is_array($searchAttributes) && count($searchAttributes) > 0); |
|
| 1431 | - if($haveMultiSearchAttributes && strpos(trim($search), ' ') !== false) { |
|
| 1432 | - try { |
|
| 1433 | - return $this->getAdvancedFilterPartForSearch($search, $searchAttributes); |
|
| 1434 | - } catch(\Exception $e) { |
|
| 1435 | - \OCP\Util::writeLog( |
|
| 1436 | - 'user_ldap', |
|
| 1437 | - 'Creating advanced filter for search failed, falling back to simple method.', |
|
| 1438 | - \OCP\Util::INFO |
|
| 1439 | - ); |
|
| 1440 | - } |
|
| 1441 | - } |
|
| 1442 | - |
|
| 1443 | - $search = $this->prepareSearchTerm($search); |
|
| 1444 | - if(!is_array($searchAttributes) || count($searchAttributes) === 0) { |
|
| 1445 | - if ($fallbackAttribute === '') { |
|
| 1446 | - return ''; |
|
| 1447 | - } |
|
| 1448 | - $filter[] = $fallbackAttribute . '=' . $search; |
|
| 1449 | - } else { |
|
| 1450 | - foreach($searchAttributes as $attribute) { |
|
| 1451 | - $filter[] = $attribute . '=' . $search; |
|
| 1452 | - } |
|
| 1453 | - } |
|
| 1454 | - if(count($filter) === 1) { |
|
| 1455 | - return '('.$filter[0].')'; |
|
| 1456 | - } |
|
| 1457 | - return $this->combineFilterWithOr($filter); |
|
| 1458 | - } |
|
| 1459 | - |
|
| 1460 | - /** |
|
| 1461 | - * returns the search term depending on whether we are allowed |
|
| 1462 | - * list users found by ldap with the current input appended by |
|
| 1463 | - * a * |
|
| 1464 | - * @return string |
|
| 1465 | - */ |
|
| 1466 | - private function prepareSearchTerm($term) { |
|
| 1467 | - $config = \OC::$server->getConfig(); |
|
| 1468 | - |
|
| 1469 | - $allowEnum = $config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes'); |
|
| 1470 | - |
|
| 1471 | - $result = $term; |
|
| 1472 | - if ($term === '') { |
|
| 1473 | - $result = '*'; |
|
| 1474 | - } else if ($allowEnum !== 'no') { |
|
| 1475 | - $result = $term . '*'; |
|
| 1476 | - } |
|
| 1477 | - return $result; |
|
| 1478 | - } |
|
| 1479 | - |
|
| 1480 | - /** |
|
| 1481 | - * returns the filter used for counting users |
|
| 1482 | - * @return string |
|
| 1483 | - */ |
|
| 1484 | - public function getFilterForUserCount() { |
|
| 1485 | - $filter = $this->combineFilterWithAnd(array( |
|
| 1486 | - $this->connection->ldapUserFilter, |
|
| 1487 | - $this->connection->ldapUserDisplayName . '=*' |
|
| 1488 | - )); |
|
| 1489 | - |
|
| 1490 | - return $filter; |
|
| 1491 | - } |
|
| 1492 | - |
|
| 1493 | - /** |
|
| 1494 | - * @param string $name |
|
| 1495 | - * @param string $password |
|
| 1496 | - * @return bool |
|
| 1497 | - */ |
|
| 1498 | - public function areCredentialsValid($name, $password) { |
|
| 1499 | - $name = $this->helper->DNasBaseParameter($name); |
|
| 1500 | - $testConnection = clone $this->connection; |
|
| 1501 | - $credentials = array( |
|
| 1502 | - 'ldapAgentName' => $name, |
|
| 1503 | - 'ldapAgentPassword' => $password |
|
| 1504 | - ); |
|
| 1505 | - if(!$testConnection->setConfiguration($credentials)) { |
|
| 1506 | - return false; |
|
| 1507 | - } |
|
| 1508 | - return $testConnection->bind(); |
|
| 1509 | - } |
|
| 1510 | - |
|
| 1511 | - /** |
|
| 1512 | - * reverse lookup of a DN given a known UUID |
|
| 1513 | - * |
|
| 1514 | - * @param string $uuid |
|
| 1515 | - * @return string |
|
| 1516 | - * @throws \Exception |
|
| 1517 | - */ |
|
| 1518 | - public function getUserDnByUuid($uuid) { |
|
| 1519 | - $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
|
| 1520 | - $filter = $this->connection->ldapUserFilter; |
|
| 1521 | - $base = $this->connection->ldapBaseUsers; |
|
| 1522 | - |
|
| 1523 | - if ($this->connection->ldapUuidUserAttribute === 'auto' && $uuidOverride === '') { |
|
| 1524 | - // Sacrebleu! The UUID attribute is unknown :( We need first an |
|
| 1525 | - // existing DN to be able to reliably detect it. |
|
| 1526 | - $result = $this->search($filter, $base, ['dn'], 1); |
|
| 1527 | - if(!isset($result[0]) || !isset($result[0]['dn'])) { |
|
| 1528 | - throw new \Exception('Cannot determine UUID attribute'); |
|
| 1529 | - } |
|
| 1530 | - $dn = $result[0]['dn'][0]; |
|
| 1531 | - if(!$this->detectUuidAttribute($dn, true)) { |
|
| 1532 | - throw new \Exception('Cannot determine UUID attribute'); |
|
| 1533 | - } |
|
| 1534 | - } else { |
|
| 1535 | - // The UUID attribute is either known or an override is given. |
|
| 1536 | - // By calling this method we ensure that $this->connection->$uuidAttr |
|
| 1537 | - // is definitely set |
|
| 1538 | - if(!$this->detectUuidAttribute('', true)) { |
|
| 1539 | - throw new \Exception('Cannot determine UUID attribute'); |
|
| 1540 | - } |
|
| 1541 | - } |
|
| 1542 | - |
|
| 1543 | - $uuidAttr = $this->connection->ldapUuidUserAttribute; |
|
| 1544 | - if($uuidAttr === 'guid' || $uuidAttr === 'objectguid') { |
|
| 1545 | - $uuid = $this->formatGuid2ForFilterUser($uuid); |
|
| 1546 | - } |
|
| 1547 | - |
|
| 1548 | - $filter = $uuidAttr . '=' . $uuid; |
|
| 1549 | - $result = $this->searchUsers($filter, ['dn'], 2); |
|
| 1550 | - if(is_array($result) && isset($result[0]) && isset($result[0]['dn']) && count($result) === 1) { |
|
| 1551 | - // we put the count into account to make sure that this is |
|
| 1552 | - // really unique |
|
| 1553 | - return $result[0]['dn'][0]; |
|
| 1554 | - } |
|
| 1555 | - |
|
| 1556 | - throw new \Exception('Cannot determine UUID attribute'); |
|
| 1557 | - } |
|
| 1558 | - |
|
| 1559 | - /** |
|
| 1560 | - * auto-detects the directory's UUID attribute |
|
| 1561 | - * |
|
| 1562 | - * @param string $dn a known DN used to check against |
|
| 1563 | - * @param bool $isUser |
|
| 1564 | - * @param bool $force the detection should be run, even if it is not set to auto |
|
| 1565 | - * @param array|null $ldapRecord |
|
| 1566 | - * @return bool true on success, false otherwise |
|
| 1567 | - */ |
|
| 1568 | - private function detectUuidAttribute($dn, $isUser = true, $force = false, array $ldapRecord = null) { |
|
| 1569 | - if($isUser) { |
|
| 1570 | - $uuidAttr = 'ldapUuidUserAttribute'; |
|
| 1571 | - $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
|
| 1572 | - } else { |
|
| 1573 | - $uuidAttr = 'ldapUuidGroupAttribute'; |
|
| 1574 | - $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr; |
|
| 1575 | - } |
|
| 1576 | - |
|
| 1577 | - if(($this->connection->$uuidAttr !== 'auto') && !$force) { |
|
| 1578 | - return true; |
|
| 1579 | - } |
|
| 1580 | - |
|
| 1581 | - if (is_string($uuidOverride) && trim($uuidOverride) !== '' && !$force) { |
|
| 1582 | - $this->connection->$uuidAttr = $uuidOverride; |
|
| 1583 | - return true; |
|
| 1584 | - } |
|
| 1585 | - |
|
| 1586 | - foreach(self::UUID_ATTRIBUTES as $attribute) { |
|
| 1587 | - if($ldapRecord !== null) { |
|
| 1588 | - // we have the info from LDAP already, we don't need to talk to the server again |
|
| 1589 | - if(isset($ldapRecord[$attribute])) { |
|
| 1590 | - $this->connection->$uuidAttr = $attribute; |
|
| 1591 | - return true; |
|
| 1592 | - } else { |
|
| 1593 | - continue; |
|
| 1594 | - } |
|
| 1595 | - } |
|
| 1596 | - |
|
| 1597 | - $value = $this->readAttribute($dn, $attribute); |
|
| 1598 | - if(is_array($value) && isset($value[0]) && !empty($value[0])) { |
|
| 1599 | - \OCP\Util::writeLog('user_ldap', |
|
| 1600 | - 'Setting '.$attribute.' as '.$uuidAttr, |
|
| 1601 | - \OCP\Util::DEBUG); |
|
| 1602 | - $this->connection->$uuidAttr = $attribute; |
|
| 1603 | - return true; |
|
| 1604 | - } |
|
| 1605 | - } |
|
| 1606 | - \OCP\Util::writeLog('user_ldap', |
|
| 1607 | - 'Could not autodetect the UUID attribute', |
|
| 1608 | - \OCP\Util::ERROR); |
|
| 1609 | - |
|
| 1610 | - return false; |
|
| 1611 | - } |
|
| 1612 | - |
|
| 1613 | - /** |
|
| 1614 | - * @param string $dn |
|
| 1615 | - * @param bool $isUser |
|
| 1616 | - * @param null $ldapRecord |
|
| 1617 | - * @return bool|string |
|
| 1618 | - */ |
|
| 1619 | - public function getUUID($dn, $isUser = true, $ldapRecord = null) { |
|
| 1620 | - if($isUser) { |
|
| 1621 | - $uuidAttr = 'ldapUuidUserAttribute'; |
|
| 1622 | - $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
|
| 1623 | - } else { |
|
| 1624 | - $uuidAttr = 'ldapUuidGroupAttribute'; |
|
| 1625 | - $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr; |
|
| 1626 | - } |
|
| 1627 | - |
|
| 1628 | - $uuid = false; |
|
| 1629 | - if($this->detectUuidAttribute($dn, $isUser, false, $ldapRecord)) { |
|
| 1630 | - $attr = $this->connection->$uuidAttr; |
|
| 1631 | - $uuid = isset($ldapRecord[$attr]) ? $ldapRecord[$attr] : $this->readAttribute($dn, $attr); |
|
| 1632 | - if( !is_array($uuid) |
|
| 1633 | - && $uuidOverride !== '' |
|
| 1634 | - && $this->detectUuidAttribute($dn, $isUser, true, $ldapRecord)) |
|
| 1635 | - { |
|
| 1636 | - $uuid = isset($ldapRecord[$this->connection->$uuidAttr]) |
|
| 1637 | - ? $ldapRecord[$this->connection->$uuidAttr] |
|
| 1638 | - : $this->readAttribute($dn, $this->connection->$uuidAttr); |
|
| 1639 | - } |
|
| 1640 | - if(is_array($uuid) && isset($uuid[0]) && !empty($uuid[0])) { |
|
| 1641 | - $uuid = $uuid[0]; |
|
| 1642 | - } |
|
| 1643 | - } |
|
| 1644 | - |
|
| 1645 | - return $uuid; |
|
| 1646 | - } |
|
| 1647 | - |
|
| 1648 | - /** |
|
| 1649 | - * converts a binary ObjectGUID into a string representation |
|
| 1650 | - * @param string $oguid the ObjectGUID in it's binary form as retrieved from AD |
|
| 1651 | - * @return string |
|
| 1652 | - * @link http://www.php.net/manual/en/function.ldap-get-values-len.php#73198 |
|
| 1653 | - */ |
|
| 1654 | - private function convertObjectGUID2Str($oguid) { |
|
| 1655 | - $hex_guid = bin2hex($oguid); |
|
| 1656 | - $hex_guid_to_guid_str = ''; |
|
| 1657 | - for($k = 1; $k <= 4; ++$k) { |
|
| 1658 | - $hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2); |
|
| 1659 | - } |
|
| 1660 | - $hex_guid_to_guid_str .= '-'; |
|
| 1661 | - for($k = 1; $k <= 2; ++$k) { |
|
| 1662 | - $hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2); |
|
| 1663 | - } |
|
| 1664 | - $hex_guid_to_guid_str .= '-'; |
|
| 1665 | - for($k = 1; $k <= 2; ++$k) { |
|
| 1666 | - $hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2); |
|
| 1667 | - } |
|
| 1668 | - $hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4); |
|
| 1669 | - $hex_guid_to_guid_str .= '-' . substr($hex_guid, 20); |
|
| 1670 | - |
|
| 1671 | - return strtoupper($hex_guid_to_guid_str); |
|
| 1672 | - } |
|
| 1673 | - |
|
| 1674 | - /** |
|
| 1675 | - * the first three blocks of the string-converted GUID happen to be in |
|
| 1676 | - * reverse order. In order to use it in a filter, this needs to be |
|
| 1677 | - * corrected. Furthermore the dashes need to be replaced and \\ preprended |
|
| 1678 | - * to every two hax figures. |
|
| 1679 | - * |
|
| 1680 | - * If an invalid string is passed, it will be returned without change. |
|
| 1681 | - * |
|
| 1682 | - * @param string $guid |
|
| 1683 | - * @return string |
|
| 1684 | - */ |
|
| 1685 | - public function formatGuid2ForFilterUser($guid) { |
|
| 1686 | - if(!is_string($guid)) { |
|
| 1687 | - throw new \InvalidArgumentException('String expected'); |
|
| 1688 | - } |
|
| 1689 | - $blocks = explode('-', $guid); |
|
| 1690 | - if(count($blocks) !== 5) { |
|
| 1691 | - /* |
|
| 1208 | + $findings = []; |
|
| 1209 | + $savedoffset = $offset; |
|
| 1210 | + do { |
|
| 1211 | + $search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset); |
|
| 1212 | + if($search === false) { |
|
| 1213 | + return []; |
|
| 1214 | + } |
|
| 1215 | + list($sr, $pagedSearchOK) = $search; |
|
| 1216 | + $cr = $this->connection->getConnectionResource(); |
|
| 1217 | + |
|
| 1218 | + if($skipHandling) { |
|
| 1219 | + //i.e. result do not need to be fetched, we just need the cookie |
|
| 1220 | + //thus pass 1 or any other value as $iFoundItems because it is not |
|
| 1221 | + //used |
|
| 1222 | + $this->processPagedSearchStatus($sr, $filter, $base, 1, $limitPerPage, |
|
| 1223 | + $offset, $pagedSearchOK, |
|
| 1224 | + $skipHandling); |
|
| 1225 | + return array(); |
|
| 1226 | + } |
|
| 1227 | + |
|
| 1228 | + $iFoundItems = 0; |
|
| 1229 | + foreach($sr as $res) { |
|
| 1230 | + $findings = array_merge($findings, $this->invokeLDAPMethod('getEntries', $cr, $res)); |
|
| 1231 | + $iFoundItems = max($iFoundItems, $findings['count']); |
|
| 1232 | + unset($findings['count']); |
|
| 1233 | + } |
|
| 1234 | + |
|
| 1235 | + $continue = $this->processPagedSearchStatus($sr, $filter, $base, $iFoundItems, |
|
| 1236 | + $limitPerPage, $offset, $pagedSearchOK, |
|
| 1237 | + $skipHandling); |
|
| 1238 | + $offset += $limitPerPage; |
|
| 1239 | + } while ($continue && $pagedSearchOK && ($limit === null || count($findings) < $limit)); |
|
| 1240 | + // reseting offset |
|
| 1241 | + $offset = $savedoffset; |
|
| 1242 | + |
|
| 1243 | + // if we're here, probably no connection resource is returned. |
|
| 1244 | + // to make Nextcloud behave nicely, we simply give back an empty array. |
|
| 1245 | + if(is_null($findings)) { |
|
| 1246 | + return array(); |
|
| 1247 | + } |
|
| 1248 | + |
|
| 1249 | + if(!is_null($attr)) { |
|
| 1250 | + $selection = []; |
|
| 1251 | + $i = 0; |
|
| 1252 | + foreach($findings as $item) { |
|
| 1253 | + if(!is_array($item)) { |
|
| 1254 | + continue; |
|
| 1255 | + } |
|
| 1256 | + $item = \OCP\Util::mb_array_change_key_case($item, MB_CASE_LOWER, 'UTF-8'); |
|
| 1257 | + foreach($attr as $key) { |
|
| 1258 | + if(isset($item[$key])) { |
|
| 1259 | + if(is_array($item[$key]) && isset($item[$key]['count'])) { |
|
| 1260 | + unset($item[$key]['count']); |
|
| 1261 | + } |
|
| 1262 | + if($key !== 'dn') { |
|
| 1263 | + if($this->resemblesDN($key)) { |
|
| 1264 | + $selection[$i][$key] = $this->helper->sanitizeDN($item[$key]); |
|
| 1265 | + } else if($key === 'objectguid' || $key === 'guid') { |
|
| 1266 | + $selection[$i][$key] = [$this->convertObjectGUID2Str($item[$key][0])]; |
|
| 1267 | + } else { |
|
| 1268 | + $selection[$i][$key] = $item[$key]; |
|
| 1269 | + } |
|
| 1270 | + } else { |
|
| 1271 | + $selection[$i][$key] = [$this->helper->sanitizeDN($item[$key])]; |
|
| 1272 | + } |
|
| 1273 | + } |
|
| 1274 | + |
|
| 1275 | + } |
|
| 1276 | + $i++; |
|
| 1277 | + } |
|
| 1278 | + $findings = $selection; |
|
| 1279 | + } |
|
| 1280 | + //we slice the findings, when |
|
| 1281 | + //a) paged search unsuccessful, though attempted |
|
| 1282 | + //b) no paged search, but limit set |
|
| 1283 | + if((!$this->getPagedSearchResultState() |
|
| 1284 | + && $pagedSearchOK) |
|
| 1285 | + || ( |
|
| 1286 | + !$pagedSearchOK |
|
| 1287 | + && !is_null($limit) |
|
| 1288 | + ) |
|
| 1289 | + ) { |
|
| 1290 | + $findings = array_slice($findings, (int)$offset, $limit); |
|
| 1291 | + } |
|
| 1292 | + return $findings; |
|
| 1293 | + } |
|
| 1294 | + |
|
| 1295 | + /** |
|
| 1296 | + * @param string $name |
|
| 1297 | + * @return bool|mixed|string |
|
| 1298 | + */ |
|
| 1299 | + public function sanitizeUsername($name) { |
|
| 1300 | + if($this->connection->ldapIgnoreNamingRules) { |
|
| 1301 | + return trim($name); |
|
| 1302 | + } |
|
| 1303 | + |
|
| 1304 | + // Transliteration |
|
| 1305 | + // latin characters to ASCII |
|
| 1306 | + $name = iconv('UTF-8', 'ASCII//TRANSLIT', $name); |
|
| 1307 | + |
|
| 1308 | + // Replacements |
|
| 1309 | + $name = str_replace(' ', '_', $name); |
|
| 1310 | + |
|
| 1311 | + // Every remaining disallowed characters will be removed |
|
| 1312 | + $name = preg_replace('/[^a-zA-Z0-9_.@-]/u', '', $name); |
|
| 1313 | + |
|
| 1314 | + return $name; |
|
| 1315 | + } |
|
| 1316 | + |
|
| 1317 | + /** |
|
| 1318 | + * escapes (user provided) parts for LDAP filter |
|
| 1319 | + * @param string $input, the provided value |
|
| 1320 | + * @param bool $allowAsterisk whether in * at the beginning should be preserved |
|
| 1321 | + * @return string the escaped string |
|
| 1322 | + */ |
|
| 1323 | + public function escapeFilterPart($input, $allowAsterisk = false) { |
|
| 1324 | + $asterisk = ''; |
|
| 1325 | + if($allowAsterisk && strlen($input) > 0 && $input[0] === '*') { |
|
| 1326 | + $asterisk = '*'; |
|
| 1327 | + $input = mb_substr($input, 1, null, 'UTF-8'); |
|
| 1328 | + } |
|
| 1329 | + $search = array('*', '\\', '(', ')'); |
|
| 1330 | + $replace = array('\\*', '\\\\', '\\(', '\\)'); |
|
| 1331 | + return $asterisk . str_replace($search, $replace, $input); |
|
| 1332 | + } |
|
| 1333 | + |
|
| 1334 | + /** |
|
| 1335 | + * combines the input filters with AND |
|
| 1336 | + * @param string[] $filters the filters to connect |
|
| 1337 | + * @return string the combined filter |
|
| 1338 | + */ |
|
| 1339 | + public function combineFilterWithAnd($filters) { |
|
| 1340 | + return $this->combineFilter($filters, '&'); |
|
| 1341 | + } |
|
| 1342 | + |
|
| 1343 | + /** |
|
| 1344 | + * combines the input filters with OR |
|
| 1345 | + * @param string[] $filters the filters to connect |
|
| 1346 | + * @return string the combined filter |
|
| 1347 | + * Combines Filter arguments with OR |
|
| 1348 | + */ |
|
| 1349 | + public function combineFilterWithOr($filters) { |
|
| 1350 | + return $this->combineFilter($filters, '|'); |
|
| 1351 | + } |
|
| 1352 | + |
|
| 1353 | + /** |
|
| 1354 | + * combines the input filters with given operator |
|
| 1355 | + * @param string[] $filters the filters to connect |
|
| 1356 | + * @param string $operator either & or | |
|
| 1357 | + * @return string the combined filter |
|
| 1358 | + */ |
|
| 1359 | + private function combineFilter($filters, $operator) { |
|
| 1360 | + $combinedFilter = '('.$operator; |
|
| 1361 | + foreach($filters as $filter) { |
|
| 1362 | + if ($filter !== '' && $filter[0] !== '(') { |
|
| 1363 | + $filter = '('.$filter.')'; |
|
| 1364 | + } |
|
| 1365 | + $combinedFilter.=$filter; |
|
| 1366 | + } |
|
| 1367 | + $combinedFilter.=')'; |
|
| 1368 | + return $combinedFilter; |
|
| 1369 | + } |
|
| 1370 | + |
|
| 1371 | + /** |
|
| 1372 | + * creates a filter part for to perform search for users |
|
| 1373 | + * @param string $search the search term |
|
| 1374 | + * @return string the final filter part to use in LDAP searches |
|
| 1375 | + */ |
|
| 1376 | + public function getFilterPartForUserSearch($search) { |
|
| 1377 | + return $this->getFilterPartForSearch($search, |
|
| 1378 | + $this->connection->ldapAttributesForUserSearch, |
|
| 1379 | + $this->connection->ldapUserDisplayName); |
|
| 1380 | + } |
|
| 1381 | + |
|
| 1382 | + /** |
|
| 1383 | + * creates a filter part for to perform search for groups |
|
| 1384 | + * @param string $search the search term |
|
| 1385 | + * @return string the final filter part to use in LDAP searches |
|
| 1386 | + */ |
|
| 1387 | + public function getFilterPartForGroupSearch($search) { |
|
| 1388 | + return $this->getFilterPartForSearch($search, |
|
| 1389 | + $this->connection->ldapAttributesForGroupSearch, |
|
| 1390 | + $this->connection->ldapGroupDisplayName); |
|
| 1391 | + } |
|
| 1392 | + |
|
| 1393 | + /** |
|
| 1394 | + * creates a filter part for searches by splitting up the given search |
|
| 1395 | + * string into single words |
|
| 1396 | + * @param string $search the search term |
|
| 1397 | + * @param string[] $searchAttributes needs to have at least two attributes, |
|
| 1398 | + * otherwise it does not make sense :) |
|
| 1399 | + * @return string the final filter part to use in LDAP searches |
|
| 1400 | + * @throws \Exception |
|
| 1401 | + */ |
|
| 1402 | + private function getAdvancedFilterPartForSearch($search, $searchAttributes) { |
|
| 1403 | + if(!is_array($searchAttributes) || count($searchAttributes) < 2) { |
|
| 1404 | + throw new \Exception('searchAttributes must be an array with at least two string'); |
|
| 1405 | + } |
|
| 1406 | + $searchWords = explode(' ', trim($search)); |
|
| 1407 | + $wordFilters = array(); |
|
| 1408 | + foreach($searchWords as $word) { |
|
| 1409 | + $word = $this->prepareSearchTerm($word); |
|
| 1410 | + //every word needs to appear at least once |
|
| 1411 | + $wordMatchOneAttrFilters = array(); |
|
| 1412 | + foreach($searchAttributes as $attr) { |
|
| 1413 | + $wordMatchOneAttrFilters[] = $attr . '=' . $word; |
|
| 1414 | + } |
|
| 1415 | + $wordFilters[] = $this->combineFilterWithOr($wordMatchOneAttrFilters); |
|
| 1416 | + } |
|
| 1417 | + return $this->combineFilterWithAnd($wordFilters); |
|
| 1418 | + } |
|
| 1419 | + |
|
| 1420 | + /** |
|
| 1421 | + * creates a filter part for searches |
|
| 1422 | + * @param string $search the search term |
|
| 1423 | + * @param string[]|null $searchAttributes |
|
| 1424 | + * @param string $fallbackAttribute a fallback attribute in case the user |
|
| 1425 | + * did not define search attributes. Typically the display name attribute. |
|
| 1426 | + * @return string the final filter part to use in LDAP searches |
|
| 1427 | + */ |
|
| 1428 | + private function getFilterPartForSearch($search, $searchAttributes, $fallbackAttribute) { |
|
| 1429 | + $filter = array(); |
|
| 1430 | + $haveMultiSearchAttributes = (is_array($searchAttributes) && count($searchAttributes) > 0); |
|
| 1431 | + if($haveMultiSearchAttributes && strpos(trim($search), ' ') !== false) { |
|
| 1432 | + try { |
|
| 1433 | + return $this->getAdvancedFilterPartForSearch($search, $searchAttributes); |
|
| 1434 | + } catch(\Exception $e) { |
|
| 1435 | + \OCP\Util::writeLog( |
|
| 1436 | + 'user_ldap', |
|
| 1437 | + 'Creating advanced filter for search failed, falling back to simple method.', |
|
| 1438 | + \OCP\Util::INFO |
|
| 1439 | + ); |
|
| 1440 | + } |
|
| 1441 | + } |
|
| 1442 | + |
|
| 1443 | + $search = $this->prepareSearchTerm($search); |
|
| 1444 | + if(!is_array($searchAttributes) || count($searchAttributes) === 0) { |
|
| 1445 | + if ($fallbackAttribute === '') { |
|
| 1446 | + return ''; |
|
| 1447 | + } |
|
| 1448 | + $filter[] = $fallbackAttribute . '=' . $search; |
|
| 1449 | + } else { |
|
| 1450 | + foreach($searchAttributes as $attribute) { |
|
| 1451 | + $filter[] = $attribute . '=' . $search; |
|
| 1452 | + } |
|
| 1453 | + } |
|
| 1454 | + if(count($filter) === 1) { |
|
| 1455 | + return '('.$filter[0].')'; |
|
| 1456 | + } |
|
| 1457 | + return $this->combineFilterWithOr($filter); |
|
| 1458 | + } |
|
| 1459 | + |
|
| 1460 | + /** |
|
| 1461 | + * returns the search term depending on whether we are allowed |
|
| 1462 | + * list users found by ldap with the current input appended by |
|
| 1463 | + * a * |
|
| 1464 | + * @return string |
|
| 1465 | + */ |
|
| 1466 | + private function prepareSearchTerm($term) { |
|
| 1467 | + $config = \OC::$server->getConfig(); |
|
| 1468 | + |
|
| 1469 | + $allowEnum = $config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes'); |
|
| 1470 | + |
|
| 1471 | + $result = $term; |
|
| 1472 | + if ($term === '') { |
|
| 1473 | + $result = '*'; |
|
| 1474 | + } else if ($allowEnum !== 'no') { |
|
| 1475 | + $result = $term . '*'; |
|
| 1476 | + } |
|
| 1477 | + return $result; |
|
| 1478 | + } |
|
| 1479 | + |
|
| 1480 | + /** |
|
| 1481 | + * returns the filter used for counting users |
|
| 1482 | + * @return string |
|
| 1483 | + */ |
|
| 1484 | + public function getFilterForUserCount() { |
|
| 1485 | + $filter = $this->combineFilterWithAnd(array( |
|
| 1486 | + $this->connection->ldapUserFilter, |
|
| 1487 | + $this->connection->ldapUserDisplayName . '=*' |
|
| 1488 | + )); |
|
| 1489 | + |
|
| 1490 | + return $filter; |
|
| 1491 | + } |
|
| 1492 | + |
|
| 1493 | + /** |
|
| 1494 | + * @param string $name |
|
| 1495 | + * @param string $password |
|
| 1496 | + * @return bool |
|
| 1497 | + */ |
|
| 1498 | + public function areCredentialsValid($name, $password) { |
|
| 1499 | + $name = $this->helper->DNasBaseParameter($name); |
|
| 1500 | + $testConnection = clone $this->connection; |
|
| 1501 | + $credentials = array( |
|
| 1502 | + 'ldapAgentName' => $name, |
|
| 1503 | + 'ldapAgentPassword' => $password |
|
| 1504 | + ); |
|
| 1505 | + if(!$testConnection->setConfiguration($credentials)) { |
|
| 1506 | + return false; |
|
| 1507 | + } |
|
| 1508 | + return $testConnection->bind(); |
|
| 1509 | + } |
|
| 1510 | + |
|
| 1511 | + /** |
|
| 1512 | + * reverse lookup of a DN given a known UUID |
|
| 1513 | + * |
|
| 1514 | + * @param string $uuid |
|
| 1515 | + * @return string |
|
| 1516 | + * @throws \Exception |
|
| 1517 | + */ |
|
| 1518 | + public function getUserDnByUuid($uuid) { |
|
| 1519 | + $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
|
| 1520 | + $filter = $this->connection->ldapUserFilter; |
|
| 1521 | + $base = $this->connection->ldapBaseUsers; |
|
| 1522 | + |
|
| 1523 | + if ($this->connection->ldapUuidUserAttribute === 'auto' && $uuidOverride === '') { |
|
| 1524 | + // Sacrebleu! The UUID attribute is unknown :( We need first an |
|
| 1525 | + // existing DN to be able to reliably detect it. |
|
| 1526 | + $result = $this->search($filter, $base, ['dn'], 1); |
|
| 1527 | + if(!isset($result[0]) || !isset($result[0]['dn'])) { |
|
| 1528 | + throw new \Exception('Cannot determine UUID attribute'); |
|
| 1529 | + } |
|
| 1530 | + $dn = $result[0]['dn'][0]; |
|
| 1531 | + if(!$this->detectUuidAttribute($dn, true)) { |
|
| 1532 | + throw new \Exception('Cannot determine UUID attribute'); |
|
| 1533 | + } |
|
| 1534 | + } else { |
|
| 1535 | + // The UUID attribute is either known or an override is given. |
|
| 1536 | + // By calling this method we ensure that $this->connection->$uuidAttr |
|
| 1537 | + // is definitely set |
|
| 1538 | + if(!$this->detectUuidAttribute('', true)) { |
|
| 1539 | + throw new \Exception('Cannot determine UUID attribute'); |
|
| 1540 | + } |
|
| 1541 | + } |
|
| 1542 | + |
|
| 1543 | + $uuidAttr = $this->connection->ldapUuidUserAttribute; |
|
| 1544 | + if($uuidAttr === 'guid' || $uuidAttr === 'objectguid') { |
|
| 1545 | + $uuid = $this->formatGuid2ForFilterUser($uuid); |
|
| 1546 | + } |
|
| 1547 | + |
|
| 1548 | + $filter = $uuidAttr . '=' . $uuid; |
|
| 1549 | + $result = $this->searchUsers($filter, ['dn'], 2); |
|
| 1550 | + if(is_array($result) && isset($result[0]) && isset($result[0]['dn']) && count($result) === 1) { |
|
| 1551 | + // we put the count into account to make sure that this is |
|
| 1552 | + // really unique |
|
| 1553 | + return $result[0]['dn'][0]; |
|
| 1554 | + } |
|
| 1555 | + |
|
| 1556 | + throw new \Exception('Cannot determine UUID attribute'); |
|
| 1557 | + } |
|
| 1558 | + |
|
| 1559 | + /** |
|
| 1560 | + * auto-detects the directory's UUID attribute |
|
| 1561 | + * |
|
| 1562 | + * @param string $dn a known DN used to check against |
|
| 1563 | + * @param bool $isUser |
|
| 1564 | + * @param bool $force the detection should be run, even if it is not set to auto |
|
| 1565 | + * @param array|null $ldapRecord |
|
| 1566 | + * @return bool true on success, false otherwise |
|
| 1567 | + */ |
|
| 1568 | + private function detectUuidAttribute($dn, $isUser = true, $force = false, array $ldapRecord = null) { |
|
| 1569 | + if($isUser) { |
|
| 1570 | + $uuidAttr = 'ldapUuidUserAttribute'; |
|
| 1571 | + $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
|
| 1572 | + } else { |
|
| 1573 | + $uuidAttr = 'ldapUuidGroupAttribute'; |
|
| 1574 | + $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr; |
|
| 1575 | + } |
|
| 1576 | + |
|
| 1577 | + if(($this->connection->$uuidAttr !== 'auto') && !$force) { |
|
| 1578 | + return true; |
|
| 1579 | + } |
|
| 1580 | + |
|
| 1581 | + if (is_string($uuidOverride) && trim($uuidOverride) !== '' && !$force) { |
|
| 1582 | + $this->connection->$uuidAttr = $uuidOverride; |
|
| 1583 | + return true; |
|
| 1584 | + } |
|
| 1585 | + |
|
| 1586 | + foreach(self::UUID_ATTRIBUTES as $attribute) { |
|
| 1587 | + if($ldapRecord !== null) { |
|
| 1588 | + // we have the info from LDAP already, we don't need to talk to the server again |
|
| 1589 | + if(isset($ldapRecord[$attribute])) { |
|
| 1590 | + $this->connection->$uuidAttr = $attribute; |
|
| 1591 | + return true; |
|
| 1592 | + } else { |
|
| 1593 | + continue; |
|
| 1594 | + } |
|
| 1595 | + } |
|
| 1596 | + |
|
| 1597 | + $value = $this->readAttribute($dn, $attribute); |
|
| 1598 | + if(is_array($value) && isset($value[0]) && !empty($value[0])) { |
|
| 1599 | + \OCP\Util::writeLog('user_ldap', |
|
| 1600 | + 'Setting '.$attribute.' as '.$uuidAttr, |
|
| 1601 | + \OCP\Util::DEBUG); |
|
| 1602 | + $this->connection->$uuidAttr = $attribute; |
|
| 1603 | + return true; |
|
| 1604 | + } |
|
| 1605 | + } |
|
| 1606 | + \OCP\Util::writeLog('user_ldap', |
|
| 1607 | + 'Could not autodetect the UUID attribute', |
|
| 1608 | + \OCP\Util::ERROR); |
|
| 1609 | + |
|
| 1610 | + return false; |
|
| 1611 | + } |
|
| 1612 | + |
|
| 1613 | + /** |
|
| 1614 | + * @param string $dn |
|
| 1615 | + * @param bool $isUser |
|
| 1616 | + * @param null $ldapRecord |
|
| 1617 | + * @return bool|string |
|
| 1618 | + */ |
|
| 1619 | + public function getUUID($dn, $isUser = true, $ldapRecord = null) { |
|
| 1620 | + if($isUser) { |
|
| 1621 | + $uuidAttr = 'ldapUuidUserAttribute'; |
|
| 1622 | + $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
|
| 1623 | + } else { |
|
| 1624 | + $uuidAttr = 'ldapUuidGroupAttribute'; |
|
| 1625 | + $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr; |
|
| 1626 | + } |
|
| 1627 | + |
|
| 1628 | + $uuid = false; |
|
| 1629 | + if($this->detectUuidAttribute($dn, $isUser, false, $ldapRecord)) { |
|
| 1630 | + $attr = $this->connection->$uuidAttr; |
|
| 1631 | + $uuid = isset($ldapRecord[$attr]) ? $ldapRecord[$attr] : $this->readAttribute($dn, $attr); |
|
| 1632 | + if( !is_array($uuid) |
|
| 1633 | + && $uuidOverride !== '' |
|
| 1634 | + && $this->detectUuidAttribute($dn, $isUser, true, $ldapRecord)) |
|
| 1635 | + { |
|
| 1636 | + $uuid = isset($ldapRecord[$this->connection->$uuidAttr]) |
|
| 1637 | + ? $ldapRecord[$this->connection->$uuidAttr] |
|
| 1638 | + : $this->readAttribute($dn, $this->connection->$uuidAttr); |
|
| 1639 | + } |
|
| 1640 | + if(is_array($uuid) && isset($uuid[0]) && !empty($uuid[0])) { |
|
| 1641 | + $uuid = $uuid[0]; |
|
| 1642 | + } |
|
| 1643 | + } |
|
| 1644 | + |
|
| 1645 | + return $uuid; |
|
| 1646 | + } |
|
| 1647 | + |
|
| 1648 | + /** |
|
| 1649 | + * converts a binary ObjectGUID into a string representation |
|
| 1650 | + * @param string $oguid the ObjectGUID in it's binary form as retrieved from AD |
|
| 1651 | + * @return string |
|
| 1652 | + * @link http://www.php.net/manual/en/function.ldap-get-values-len.php#73198 |
|
| 1653 | + */ |
|
| 1654 | + private function convertObjectGUID2Str($oguid) { |
|
| 1655 | + $hex_guid = bin2hex($oguid); |
|
| 1656 | + $hex_guid_to_guid_str = ''; |
|
| 1657 | + for($k = 1; $k <= 4; ++$k) { |
|
| 1658 | + $hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2); |
|
| 1659 | + } |
|
| 1660 | + $hex_guid_to_guid_str .= '-'; |
|
| 1661 | + for($k = 1; $k <= 2; ++$k) { |
|
| 1662 | + $hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2); |
|
| 1663 | + } |
|
| 1664 | + $hex_guid_to_guid_str .= '-'; |
|
| 1665 | + for($k = 1; $k <= 2; ++$k) { |
|
| 1666 | + $hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2); |
|
| 1667 | + } |
|
| 1668 | + $hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4); |
|
| 1669 | + $hex_guid_to_guid_str .= '-' . substr($hex_guid, 20); |
|
| 1670 | + |
|
| 1671 | + return strtoupper($hex_guid_to_guid_str); |
|
| 1672 | + } |
|
| 1673 | + |
|
| 1674 | + /** |
|
| 1675 | + * the first three blocks of the string-converted GUID happen to be in |
|
| 1676 | + * reverse order. In order to use it in a filter, this needs to be |
|
| 1677 | + * corrected. Furthermore the dashes need to be replaced and \\ preprended |
|
| 1678 | + * to every two hax figures. |
|
| 1679 | + * |
|
| 1680 | + * If an invalid string is passed, it will be returned without change. |
|
| 1681 | + * |
|
| 1682 | + * @param string $guid |
|
| 1683 | + * @return string |
|
| 1684 | + */ |
|
| 1685 | + public function formatGuid2ForFilterUser($guid) { |
|
| 1686 | + if(!is_string($guid)) { |
|
| 1687 | + throw new \InvalidArgumentException('String expected'); |
|
| 1688 | + } |
|
| 1689 | + $blocks = explode('-', $guid); |
|
| 1690 | + if(count($blocks) !== 5) { |
|
| 1691 | + /* |
|
| 1692 | 1692 | * Why not throw an Exception instead? This method is a utility |
| 1693 | 1693 | * called only when trying to figure out whether a "missing" known |
| 1694 | 1694 | * LDAP user was or was not renamed on the LDAP server. And this |
@@ -1699,270 +1699,270 @@ discard block |
||
| 1699 | 1699 | * an exception here would kill the experience for a valid, acting |
| 1700 | 1700 | * user. Instead we write a log message. |
| 1701 | 1701 | */ |
| 1702 | - \OC::$server->getLogger()->info( |
|
| 1703 | - 'Passed string does not resemble a valid GUID. Known UUID ' . |
|
| 1704 | - '({uuid}) probably does not match UUID configuration.', |
|
| 1705 | - [ 'app' => 'user_ldap', 'uuid' => $guid ] |
|
| 1706 | - ); |
|
| 1707 | - return $guid; |
|
| 1708 | - } |
|
| 1709 | - for($i=0; $i < 3; $i++) { |
|
| 1710 | - $pairs = str_split($blocks[$i], 2); |
|
| 1711 | - $pairs = array_reverse($pairs); |
|
| 1712 | - $blocks[$i] = implode('', $pairs); |
|
| 1713 | - } |
|
| 1714 | - for($i=0; $i < 5; $i++) { |
|
| 1715 | - $pairs = str_split($blocks[$i], 2); |
|
| 1716 | - $blocks[$i] = '\\' . implode('\\', $pairs); |
|
| 1717 | - } |
|
| 1718 | - return implode('', $blocks); |
|
| 1719 | - } |
|
| 1720 | - |
|
| 1721 | - /** |
|
| 1722 | - * gets a SID of the domain of the given dn |
|
| 1723 | - * @param string $dn |
|
| 1724 | - * @return string|bool |
|
| 1725 | - */ |
|
| 1726 | - public function getSID($dn) { |
|
| 1727 | - $domainDN = $this->getDomainDNFromDN($dn); |
|
| 1728 | - $cacheKey = 'getSID-'.$domainDN; |
|
| 1729 | - $sid = $this->connection->getFromCache($cacheKey); |
|
| 1730 | - if(!is_null($sid)) { |
|
| 1731 | - return $sid; |
|
| 1732 | - } |
|
| 1733 | - |
|
| 1734 | - $objectSid = $this->readAttribute($domainDN, 'objectsid'); |
|
| 1735 | - if(!is_array($objectSid) || empty($objectSid)) { |
|
| 1736 | - $this->connection->writeToCache($cacheKey, false); |
|
| 1737 | - return false; |
|
| 1738 | - } |
|
| 1739 | - $domainObjectSid = $this->convertSID2Str($objectSid[0]); |
|
| 1740 | - $this->connection->writeToCache($cacheKey, $domainObjectSid); |
|
| 1741 | - |
|
| 1742 | - return $domainObjectSid; |
|
| 1743 | - } |
|
| 1744 | - |
|
| 1745 | - /** |
|
| 1746 | - * converts a binary SID into a string representation |
|
| 1747 | - * @param string $sid |
|
| 1748 | - * @return string |
|
| 1749 | - */ |
|
| 1750 | - public function convertSID2Str($sid) { |
|
| 1751 | - // The format of a SID binary string is as follows: |
|
| 1752 | - // 1 byte for the revision level |
|
| 1753 | - // 1 byte for the number n of variable sub-ids |
|
| 1754 | - // 6 bytes for identifier authority value |
|
| 1755 | - // n*4 bytes for n sub-ids |
|
| 1756 | - // |
|
| 1757 | - // Example: 010400000000000515000000a681e50e4d6c6c2bca32055f |
|
| 1758 | - // Legend: RRNNAAAAAAAAAAAA11111111222222223333333344444444 |
|
| 1759 | - $revision = ord($sid[0]); |
|
| 1760 | - $numberSubID = ord($sid[1]); |
|
| 1761 | - |
|
| 1762 | - $subIdStart = 8; // 1 + 1 + 6 |
|
| 1763 | - $subIdLength = 4; |
|
| 1764 | - if (strlen($sid) !== $subIdStart + $subIdLength * $numberSubID) { |
|
| 1765 | - // Incorrect number of bytes present. |
|
| 1766 | - return ''; |
|
| 1767 | - } |
|
| 1768 | - |
|
| 1769 | - // 6 bytes = 48 bits can be represented using floats without loss of |
|
| 1770 | - // precision (see https://gist.github.com/bantu/886ac680b0aef5812f71) |
|
| 1771 | - $iav = number_format(hexdec(bin2hex(substr($sid, 2, 6))), 0, '', ''); |
|
| 1772 | - |
|
| 1773 | - $subIDs = array(); |
|
| 1774 | - for ($i = 0; $i < $numberSubID; $i++) { |
|
| 1775 | - $subID = unpack('V', substr($sid, $subIdStart + $subIdLength * $i, $subIdLength)); |
|
| 1776 | - $subIDs[] = sprintf('%u', $subID[1]); |
|
| 1777 | - } |
|
| 1778 | - |
|
| 1779 | - // Result for example above: S-1-5-21-249921958-728525901-1594176202 |
|
| 1780 | - return sprintf('S-%d-%s-%s', $revision, $iav, implode('-', $subIDs)); |
|
| 1781 | - } |
|
| 1782 | - |
|
| 1783 | - /** |
|
| 1784 | - * checks if the given DN is part of the given base DN(s) |
|
| 1785 | - * @param string $dn the DN |
|
| 1786 | - * @param string[] $bases array containing the allowed base DN or DNs |
|
| 1787 | - * @return bool |
|
| 1788 | - */ |
|
| 1789 | - public function isDNPartOfBase($dn, $bases) { |
|
| 1790 | - $belongsToBase = false; |
|
| 1791 | - $bases = $this->helper->sanitizeDN($bases); |
|
| 1792 | - |
|
| 1793 | - foreach($bases as $base) { |
|
| 1794 | - $belongsToBase = true; |
|
| 1795 | - if(mb_strripos($dn, $base, 0, 'UTF-8') !== (mb_strlen($dn, 'UTF-8')-mb_strlen($base, 'UTF-8'))) { |
|
| 1796 | - $belongsToBase = false; |
|
| 1797 | - } |
|
| 1798 | - if($belongsToBase) { |
|
| 1799 | - break; |
|
| 1800 | - } |
|
| 1801 | - } |
|
| 1802 | - return $belongsToBase; |
|
| 1803 | - } |
|
| 1804 | - |
|
| 1805 | - /** |
|
| 1806 | - * resets a running Paged Search operation |
|
| 1807 | - */ |
|
| 1808 | - private function abandonPagedSearch() { |
|
| 1809 | - if($this->connection->hasPagedResultSupport) { |
|
| 1810 | - $cr = $this->connection->getConnectionResource(); |
|
| 1811 | - $this->invokeLDAPMethod('controlPagedResult', $cr, 0, false, $this->lastCookie); |
|
| 1812 | - $this->getPagedSearchResultState(); |
|
| 1813 | - $this->lastCookie = ''; |
|
| 1814 | - $this->cookies = array(); |
|
| 1815 | - } |
|
| 1816 | - } |
|
| 1817 | - |
|
| 1818 | - /** |
|
| 1819 | - * get a cookie for the next LDAP paged search |
|
| 1820 | - * @param string $base a string with the base DN for the search |
|
| 1821 | - * @param string $filter the search filter to identify the correct search |
|
| 1822 | - * @param int $limit the limit (or 'pageSize'), to identify the correct search well |
|
| 1823 | - * @param int $offset the offset for the new search to identify the correct search really good |
|
| 1824 | - * @return string containing the key or empty if none is cached |
|
| 1825 | - */ |
|
| 1826 | - private function getPagedResultCookie($base, $filter, $limit, $offset) { |
|
| 1827 | - if($offset === 0) { |
|
| 1828 | - return ''; |
|
| 1829 | - } |
|
| 1830 | - $offset -= $limit; |
|
| 1831 | - //we work with cache here |
|
| 1832 | - $cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' . (int)$limit . '-' . (int)$offset; |
|
| 1833 | - $cookie = ''; |
|
| 1834 | - if(isset($this->cookies[$cacheKey])) { |
|
| 1835 | - $cookie = $this->cookies[$cacheKey]; |
|
| 1836 | - if(is_null($cookie)) { |
|
| 1837 | - $cookie = ''; |
|
| 1838 | - } |
|
| 1839 | - } |
|
| 1840 | - return $cookie; |
|
| 1841 | - } |
|
| 1842 | - |
|
| 1843 | - /** |
|
| 1844 | - * checks whether an LDAP paged search operation has more pages that can be |
|
| 1845 | - * retrieved, typically when offset and limit are provided. |
|
| 1846 | - * |
|
| 1847 | - * Be very careful to use it: the last cookie value, which is inspected, can |
|
| 1848 | - * be reset by other operations. Best, call it immediately after a search(), |
|
| 1849 | - * searchUsers() or searchGroups() call. count-methods are probably safe as |
|
| 1850 | - * well. Don't rely on it with any fetchList-method. |
|
| 1851 | - * @return bool |
|
| 1852 | - */ |
|
| 1853 | - public function hasMoreResults() { |
|
| 1854 | - if(!$this->connection->hasPagedResultSupport) { |
|
| 1855 | - return false; |
|
| 1856 | - } |
|
| 1857 | - |
|
| 1858 | - if(empty($this->lastCookie) && $this->lastCookie !== '0') { |
|
| 1859 | - // as in RFC 2696, when all results are returned, the cookie will |
|
| 1860 | - // be empty. |
|
| 1861 | - return false; |
|
| 1862 | - } |
|
| 1863 | - |
|
| 1864 | - return true; |
|
| 1865 | - } |
|
| 1866 | - |
|
| 1867 | - /** |
|
| 1868 | - * set a cookie for LDAP paged search run |
|
| 1869 | - * @param string $base a string with the base DN for the search |
|
| 1870 | - * @param string $filter the search filter to identify the correct search |
|
| 1871 | - * @param int $limit the limit (or 'pageSize'), to identify the correct search well |
|
| 1872 | - * @param int $offset the offset for the run search to identify the correct search really good |
|
| 1873 | - * @param string $cookie string containing the cookie returned by ldap_control_paged_result_response |
|
| 1874 | - * @return void |
|
| 1875 | - */ |
|
| 1876 | - private function setPagedResultCookie($base, $filter, $limit, $offset, $cookie) { |
|
| 1877 | - // allow '0' for 389ds |
|
| 1878 | - if(!empty($cookie) || $cookie === '0') { |
|
| 1879 | - $cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' . (int)$limit . '-' . (int)$offset; |
|
| 1880 | - $this->cookies[$cacheKey] = $cookie; |
|
| 1881 | - $this->lastCookie = $cookie; |
|
| 1882 | - } |
|
| 1883 | - } |
|
| 1884 | - |
|
| 1885 | - /** |
|
| 1886 | - * Check whether the most recent paged search was successful. It flushed the state var. Use it always after a possible paged search. |
|
| 1887 | - * @return boolean|null true on success, null or false otherwise |
|
| 1888 | - */ |
|
| 1889 | - public function getPagedSearchResultState() { |
|
| 1890 | - $result = $this->pagedSearchedSuccessful; |
|
| 1891 | - $this->pagedSearchedSuccessful = null; |
|
| 1892 | - return $result; |
|
| 1893 | - } |
|
| 1894 | - |
|
| 1895 | - /** |
|
| 1896 | - * Prepares a paged search, if possible |
|
| 1897 | - * @param string $filter the LDAP filter for the search |
|
| 1898 | - * @param string[] $bases an array containing the LDAP subtree(s) that shall be searched |
|
| 1899 | - * @param string[] $attr optional, when a certain attribute shall be filtered outside |
|
| 1900 | - * @param int $limit |
|
| 1901 | - * @param int $offset |
|
| 1902 | - * @return bool|true |
|
| 1903 | - */ |
|
| 1904 | - private function initPagedSearch($filter, $bases, $attr, $limit, $offset) { |
|
| 1905 | - $pagedSearchOK = false; |
|
| 1906 | - if($this->connection->hasPagedResultSupport && ($limit !== 0)) { |
|
| 1907 | - $offset = (int)$offset; //can be null |
|
| 1908 | - \OCP\Util::writeLog('user_ldap', |
|
| 1909 | - 'initializing paged search for Filter '.$filter.' base '.print_r($bases, true) |
|
| 1910 | - .' attr '.print_r($attr, true). ' limit ' .$limit.' offset '.$offset, |
|
| 1911 | - \OCP\Util::DEBUG); |
|
| 1912 | - //get the cookie from the search for the previous search, required by LDAP |
|
| 1913 | - foreach($bases as $base) { |
|
| 1914 | - |
|
| 1915 | - $cookie = $this->getPagedResultCookie($base, $filter, $limit, $offset); |
|
| 1916 | - if(empty($cookie) && $cookie !== "0" && ($offset > 0)) { |
|
| 1917 | - // no cookie known from a potential previous search. We need |
|
| 1918 | - // to start from 0 to come to the desired page. cookie value |
|
| 1919 | - // of '0' is valid, because 389ds |
|
| 1920 | - $reOffset = ($offset - $limit) < 0 ? 0 : $offset - $limit; |
|
| 1921 | - $this->search($filter, array($base), $attr, $limit, $reOffset, true); |
|
| 1922 | - $cookie = $this->getPagedResultCookie($base, $filter, $limit, $offset); |
|
| 1923 | - //still no cookie? obviously, the server does not like us. Let's skip paging efforts. |
|
| 1924 | - // '0' is valid, because 389ds |
|
| 1925 | - //TODO: remember this, probably does not change in the next request... |
|
| 1926 | - if(empty($cookie) && $cookie !== '0') { |
|
| 1927 | - $cookie = null; |
|
| 1928 | - } |
|
| 1929 | - } |
|
| 1930 | - if(!is_null($cookie)) { |
|
| 1931 | - //since offset = 0, this is a new search. We abandon other searches that might be ongoing. |
|
| 1932 | - $this->abandonPagedSearch(); |
|
| 1933 | - $pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult', |
|
| 1934 | - $this->connection->getConnectionResource(), $limit, |
|
| 1935 | - false, $cookie); |
|
| 1936 | - if(!$pagedSearchOK) { |
|
| 1937 | - return false; |
|
| 1938 | - } |
|
| 1939 | - \OCP\Util::writeLog('user_ldap', 'Ready for a paged search', \OCP\Util::DEBUG); |
|
| 1940 | - } else { |
|
| 1941 | - $e = new \Exception('No paged search possible, Limit '.$limit.' Offset '.$offset); |
|
| 1942 | - \OC::$server->getLogger()->logException($e, ['level' => Util::DEBUG]); |
|
| 1943 | - } |
|
| 1944 | - |
|
| 1945 | - } |
|
| 1946 | - /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1702 | + \OC::$server->getLogger()->info( |
|
| 1703 | + 'Passed string does not resemble a valid GUID. Known UUID ' . |
|
| 1704 | + '({uuid}) probably does not match UUID configuration.', |
|
| 1705 | + [ 'app' => 'user_ldap', 'uuid' => $guid ] |
|
| 1706 | + ); |
|
| 1707 | + return $guid; |
|
| 1708 | + } |
|
| 1709 | + for($i=0; $i < 3; $i++) { |
|
| 1710 | + $pairs = str_split($blocks[$i], 2); |
|
| 1711 | + $pairs = array_reverse($pairs); |
|
| 1712 | + $blocks[$i] = implode('', $pairs); |
|
| 1713 | + } |
|
| 1714 | + for($i=0; $i < 5; $i++) { |
|
| 1715 | + $pairs = str_split($blocks[$i], 2); |
|
| 1716 | + $blocks[$i] = '\\' . implode('\\', $pairs); |
|
| 1717 | + } |
|
| 1718 | + return implode('', $blocks); |
|
| 1719 | + } |
|
| 1720 | + |
|
| 1721 | + /** |
|
| 1722 | + * gets a SID of the domain of the given dn |
|
| 1723 | + * @param string $dn |
|
| 1724 | + * @return string|bool |
|
| 1725 | + */ |
|
| 1726 | + public function getSID($dn) { |
|
| 1727 | + $domainDN = $this->getDomainDNFromDN($dn); |
|
| 1728 | + $cacheKey = 'getSID-'.$domainDN; |
|
| 1729 | + $sid = $this->connection->getFromCache($cacheKey); |
|
| 1730 | + if(!is_null($sid)) { |
|
| 1731 | + return $sid; |
|
| 1732 | + } |
|
| 1733 | + |
|
| 1734 | + $objectSid = $this->readAttribute($domainDN, 'objectsid'); |
|
| 1735 | + if(!is_array($objectSid) || empty($objectSid)) { |
|
| 1736 | + $this->connection->writeToCache($cacheKey, false); |
|
| 1737 | + return false; |
|
| 1738 | + } |
|
| 1739 | + $domainObjectSid = $this->convertSID2Str($objectSid[0]); |
|
| 1740 | + $this->connection->writeToCache($cacheKey, $domainObjectSid); |
|
| 1741 | + |
|
| 1742 | + return $domainObjectSid; |
|
| 1743 | + } |
|
| 1744 | + |
|
| 1745 | + /** |
|
| 1746 | + * converts a binary SID into a string representation |
|
| 1747 | + * @param string $sid |
|
| 1748 | + * @return string |
|
| 1749 | + */ |
|
| 1750 | + public function convertSID2Str($sid) { |
|
| 1751 | + // The format of a SID binary string is as follows: |
|
| 1752 | + // 1 byte for the revision level |
|
| 1753 | + // 1 byte for the number n of variable sub-ids |
|
| 1754 | + // 6 bytes for identifier authority value |
|
| 1755 | + // n*4 bytes for n sub-ids |
|
| 1756 | + // |
|
| 1757 | + // Example: 010400000000000515000000a681e50e4d6c6c2bca32055f |
|
| 1758 | + // Legend: RRNNAAAAAAAAAAAA11111111222222223333333344444444 |
|
| 1759 | + $revision = ord($sid[0]); |
|
| 1760 | + $numberSubID = ord($sid[1]); |
|
| 1761 | + |
|
| 1762 | + $subIdStart = 8; // 1 + 1 + 6 |
|
| 1763 | + $subIdLength = 4; |
|
| 1764 | + if (strlen($sid) !== $subIdStart + $subIdLength * $numberSubID) { |
|
| 1765 | + // Incorrect number of bytes present. |
|
| 1766 | + return ''; |
|
| 1767 | + } |
|
| 1768 | + |
|
| 1769 | + // 6 bytes = 48 bits can be represented using floats without loss of |
|
| 1770 | + // precision (see https://gist.github.com/bantu/886ac680b0aef5812f71) |
|
| 1771 | + $iav = number_format(hexdec(bin2hex(substr($sid, 2, 6))), 0, '', ''); |
|
| 1772 | + |
|
| 1773 | + $subIDs = array(); |
|
| 1774 | + for ($i = 0; $i < $numberSubID; $i++) { |
|
| 1775 | + $subID = unpack('V', substr($sid, $subIdStart + $subIdLength * $i, $subIdLength)); |
|
| 1776 | + $subIDs[] = sprintf('%u', $subID[1]); |
|
| 1777 | + } |
|
| 1778 | + |
|
| 1779 | + // Result for example above: S-1-5-21-249921958-728525901-1594176202 |
|
| 1780 | + return sprintf('S-%d-%s-%s', $revision, $iav, implode('-', $subIDs)); |
|
| 1781 | + } |
|
| 1782 | + |
|
| 1783 | + /** |
|
| 1784 | + * checks if the given DN is part of the given base DN(s) |
|
| 1785 | + * @param string $dn the DN |
|
| 1786 | + * @param string[] $bases array containing the allowed base DN or DNs |
|
| 1787 | + * @return bool |
|
| 1788 | + */ |
|
| 1789 | + public function isDNPartOfBase($dn, $bases) { |
|
| 1790 | + $belongsToBase = false; |
|
| 1791 | + $bases = $this->helper->sanitizeDN($bases); |
|
| 1792 | + |
|
| 1793 | + foreach($bases as $base) { |
|
| 1794 | + $belongsToBase = true; |
|
| 1795 | + if(mb_strripos($dn, $base, 0, 'UTF-8') !== (mb_strlen($dn, 'UTF-8')-mb_strlen($base, 'UTF-8'))) { |
|
| 1796 | + $belongsToBase = false; |
|
| 1797 | + } |
|
| 1798 | + if($belongsToBase) { |
|
| 1799 | + break; |
|
| 1800 | + } |
|
| 1801 | + } |
|
| 1802 | + return $belongsToBase; |
|
| 1803 | + } |
|
| 1804 | + |
|
| 1805 | + /** |
|
| 1806 | + * resets a running Paged Search operation |
|
| 1807 | + */ |
|
| 1808 | + private function abandonPagedSearch() { |
|
| 1809 | + if($this->connection->hasPagedResultSupport) { |
|
| 1810 | + $cr = $this->connection->getConnectionResource(); |
|
| 1811 | + $this->invokeLDAPMethod('controlPagedResult', $cr, 0, false, $this->lastCookie); |
|
| 1812 | + $this->getPagedSearchResultState(); |
|
| 1813 | + $this->lastCookie = ''; |
|
| 1814 | + $this->cookies = array(); |
|
| 1815 | + } |
|
| 1816 | + } |
|
| 1817 | + |
|
| 1818 | + /** |
|
| 1819 | + * get a cookie for the next LDAP paged search |
|
| 1820 | + * @param string $base a string with the base DN for the search |
|
| 1821 | + * @param string $filter the search filter to identify the correct search |
|
| 1822 | + * @param int $limit the limit (or 'pageSize'), to identify the correct search well |
|
| 1823 | + * @param int $offset the offset for the new search to identify the correct search really good |
|
| 1824 | + * @return string containing the key or empty if none is cached |
|
| 1825 | + */ |
|
| 1826 | + private function getPagedResultCookie($base, $filter, $limit, $offset) { |
|
| 1827 | + if($offset === 0) { |
|
| 1828 | + return ''; |
|
| 1829 | + } |
|
| 1830 | + $offset -= $limit; |
|
| 1831 | + //we work with cache here |
|
| 1832 | + $cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' . (int)$limit . '-' . (int)$offset; |
|
| 1833 | + $cookie = ''; |
|
| 1834 | + if(isset($this->cookies[$cacheKey])) { |
|
| 1835 | + $cookie = $this->cookies[$cacheKey]; |
|
| 1836 | + if(is_null($cookie)) { |
|
| 1837 | + $cookie = ''; |
|
| 1838 | + } |
|
| 1839 | + } |
|
| 1840 | + return $cookie; |
|
| 1841 | + } |
|
| 1842 | + |
|
| 1843 | + /** |
|
| 1844 | + * checks whether an LDAP paged search operation has more pages that can be |
|
| 1845 | + * retrieved, typically when offset and limit are provided. |
|
| 1846 | + * |
|
| 1847 | + * Be very careful to use it: the last cookie value, which is inspected, can |
|
| 1848 | + * be reset by other operations. Best, call it immediately after a search(), |
|
| 1849 | + * searchUsers() or searchGroups() call. count-methods are probably safe as |
|
| 1850 | + * well. Don't rely on it with any fetchList-method. |
|
| 1851 | + * @return bool |
|
| 1852 | + */ |
|
| 1853 | + public function hasMoreResults() { |
|
| 1854 | + if(!$this->connection->hasPagedResultSupport) { |
|
| 1855 | + return false; |
|
| 1856 | + } |
|
| 1857 | + |
|
| 1858 | + if(empty($this->lastCookie) && $this->lastCookie !== '0') { |
|
| 1859 | + // as in RFC 2696, when all results are returned, the cookie will |
|
| 1860 | + // be empty. |
|
| 1861 | + return false; |
|
| 1862 | + } |
|
| 1863 | + |
|
| 1864 | + return true; |
|
| 1865 | + } |
|
| 1866 | + |
|
| 1867 | + /** |
|
| 1868 | + * set a cookie for LDAP paged search run |
|
| 1869 | + * @param string $base a string with the base DN for the search |
|
| 1870 | + * @param string $filter the search filter to identify the correct search |
|
| 1871 | + * @param int $limit the limit (or 'pageSize'), to identify the correct search well |
|
| 1872 | + * @param int $offset the offset for the run search to identify the correct search really good |
|
| 1873 | + * @param string $cookie string containing the cookie returned by ldap_control_paged_result_response |
|
| 1874 | + * @return void |
|
| 1875 | + */ |
|
| 1876 | + private function setPagedResultCookie($base, $filter, $limit, $offset, $cookie) { |
|
| 1877 | + // allow '0' for 389ds |
|
| 1878 | + if(!empty($cookie) || $cookie === '0') { |
|
| 1879 | + $cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' . (int)$limit . '-' . (int)$offset; |
|
| 1880 | + $this->cookies[$cacheKey] = $cookie; |
|
| 1881 | + $this->lastCookie = $cookie; |
|
| 1882 | + } |
|
| 1883 | + } |
|
| 1884 | + |
|
| 1885 | + /** |
|
| 1886 | + * Check whether the most recent paged search was successful. It flushed the state var. Use it always after a possible paged search. |
|
| 1887 | + * @return boolean|null true on success, null or false otherwise |
|
| 1888 | + */ |
|
| 1889 | + public function getPagedSearchResultState() { |
|
| 1890 | + $result = $this->pagedSearchedSuccessful; |
|
| 1891 | + $this->pagedSearchedSuccessful = null; |
|
| 1892 | + return $result; |
|
| 1893 | + } |
|
| 1894 | + |
|
| 1895 | + /** |
|
| 1896 | + * Prepares a paged search, if possible |
|
| 1897 | + * @param string $filter the LDAP filter for the search |
|
| 1898 | + * @param string[] $bases an array containing the LDAP subtree(s) that shall be searched |
|
| 1899 | + * @param string[] $attr optional, when a certain attribute shall be filtered outside |
|
| 1900 | + * @param int $limit |
|
| 1901 | + * @param int $offset |
|
| 1902 | + * @return bool|true |
|
| 1903 | + */ |
|
| 1904 | + private function initPagedSearch($filter, $bases, $attr, $limit, $offset) { |
|
| 1905 | + $pagedSearchOK = false; |
|
| 1906 | + if($this->connection->hasPagedResultSupport && ($limit !== 0)) { |
|
| 1907 | + $offset = (int)$offset; //can be null |
|
| 1908 | + \OCP\Util::writeLog('user_ldap', |
|
| 1909 | + 'initializing paged search for Filter '.$filter.' base '.print_r($bases, true) |
|
| 1910 | + .' attr '.print_r($attr, true). ' limit ' .$limit.' offset '.$offset, |
|
| 1911 | + \OCP\Util::DEBUG); |
|
| 1912 | + //get the cookie from the search for the previous search, required by LDAP |
|
| 1913 | + foreach($bases as $base) { |
|
| 1914 | + |
|
| 1915 | + $cookie = $this->getPagedResultCookie($base, $filter, $limit, $offset); |
|
| 1916 | + if(empty($cookie) && $cookie !== "0" && ($offset > 0)) { |
|
| 1917 | + // no cookie known from a potential previous search. We need |
|
| 1918 | + // to start from 0 to come to the desired page. cookie value |
|
| 1919 | + // of '0' is valid, because 389ds |
|
| 1920 | + $reOffset = ($offset - $limit) < 0 ? 0 : $offset - $limit; |
|
| 1921 | + $this->search($filter, array($base), $attr, $limit, $reOffset, true); |
|
| 1922 | + $cookie = $this->getPagedResultCookie($base, $filter, $limit, $offset); |
|
| 1923 | + //still no cookie? obviously, the server does not like us. Let's skip paging efforts. |
|
| 1924 | + // '0' is valid, because 389ds |
|
| 1925 | + //TODO: remember this, probably does not change in the next request... |
|
| 1926 | + if(empty($cookie) && $cookie !== '0') { |
|
| 1927 | + $cookie = null; |
|
| 1928 | + } |
|
| 1929 | + } |
|
| 1930 | + if(!is_null($cookie)) { |
|
| 1931 | + //since offset = 0, this is a new search. We abandon other searches that might be ongoing. |
|
| 1932 | + $this->abandonPagedSearch(); |
|
| 1933 | + $pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult', |
|
| 1934 | + $this->connection->getConnectionResource(), $limit, |
|
| 1935 | + false, $cookie); |
|
| 1936 | + if(!$pagedSearchOK) { |
|
| 1937 | + return false; |
|
| 1938 | + } |
|
| 1939 | + \OCP\Util::writeLog('user_ldap', 'Ready for a paged search', \OCP\Util::DEBUG); |
|
| 1940 | + } else { |
|
| 1941 | + $e = new \Exception('No paged search possible, Limit '.$limit.' Offset '.$offset); |
|
| 1942 | + \OC::$server->getLogger()->logException($e, ['level' => Util::DEBUG]); |
|
| 1943 | + } |
|
| 1944 | + |
|
| 1945 | + } |
|
| 1946 | + /* ++ Fixing RHDS searches with pages with zero results ++ |
|
| 1947 | 1947 | * We coudn't get paged searches working with our RHDS for login ($limit = 0), |
| 1948 | 1948 | * due to pages with zero results. |
| 1949 | 1949 | * So we added "&& !empty($this->lastCookie)" to this test to ignore pagination |
| 1950 | 1950 | * if we don't have a previous paged search. |
| 1951 | 1951 | */ |
| 1952 | - } else if($this->connection->hasPagedResultSupport && $limit === 0 && !empty($this->lastCookie)) { |
|
| 1953 | - // a search without limit was requested. However, if we do use |
|
| 1954 | - // Paged Search once, we always must do it. This requires us to |
|
| 1955 | - // initialize it with the configured page size. |
|
| 1956 | - $this->abandonPagedSearch(); |
|
| 1957 | - // in case someone set it to 0 … use 500, otherwise no results will |
|
| 1958 | - // be returned. |
|
| 1959 | - $pageSize = (int)$this->connection->ldapPagingSize > 0 ? (int)$this->connection->ldapPagingSize : 500; |
|
| 1960 | - $pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult', |
|
| 1961 | - $this->connection->getConnectionResource(), |
|
| 1962 | - $pageSize, false, ''); |
|
| 1963 | - } |
|
| 1964 | - |
|
| 1965 | - return $pagedSearchOK; |
|
| 1966 | - } |
|
| 1952 | + } else if($this->connection->hasPagedResultSupport && $limit === 0 && !empty($this->lastCookie)) { |
|
| 1953 | + // a search without limit was requested. However, if we do use |
|
| 1954 | + // Paged Search once, we always must do it. This requires us to |
|
| 1955 | + // initialize it with the configured page size. |
|
| 1956 | + $this->abandonPagedSearch(); |
|
| 1957 | + // in case someone set it to 0 … use 500, otherwise no results will |
|
| 1958 | + // be returned. |
|
| 1959 | + $pageSize = (int)$this->connection->ldapPagingSize > 0 ? (int)$this->connection->ldapPagingSize : 500; |
|
| 1960 | + $pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult', |
|
| 1961 | + $this->connection->getConnectionResource(), |
|
| 1962 | + $pageSize, false, ''); |
|
| 1963 | + } |
|
| 1964 | + |
|
| 1965 | + return $pagedSearchOK; |
|
| 1966 | + } |
|
| 1967 | 1967 | |
| 1968 | 1968 | } |
@@ -125,7 +125,7 @@ discard block |
||
| 125 | 125 | * @return AbstractMapping |
| 126 | 126 | */ |
| 127 | 127 | public function getUserMapper() { |
| 128 | - if(is_null($this->userMapper)) { |
|
| 128 | + if (is_null($this->userMapper)) { |
|
| 129 | 129 | throw new \Exception('UserMapper was not assigned to this Access instance.'); |
| 130 | 130 | } |
| 131 | 131 | return $this->userMapper; |
@@ -145,7 +145,7 @@ discard block |
||
| 145 | 145 | * @return AbstractMapping |
| 146 | 146 | */ |
| 147 | 147 | public function getGroupMapper() { |
| 148 | - if(is_null($this->groupMapper)) { |
|
| 148 | + if (is_null($this->groupMapper)) { |
|
| 149 | 149 | throw new \Exception('GroupMapper was not assigned to this Access instance.'); |
| 150 | 150 | } |
| 151 | 151 | return $this->groupMapper; |
@@ -178,14 +178,14 @@ discard block |
||
| 178 | 178 | * @throws ServerNotAvailableException |
| 179 | 179 | */ |
| 180 | 180 | public function readAttribute($dn, $attr, $filter = 'objectClass=*') { |
| 181 | - if(!$this->checkConnection()) { |
|
| 181 | + if (!$this->checkConnection()) { |
|
| 182 | 182 | \OCP\Util::writeLog('user_ldap', |
| 183 | 183 | 'No LDAP Connector assigned, access impossible for readAttribute.', |
| 184 | 184 | \OCP\Util::WARN); |
| 185 | 185 | return false; |
| 186 | 186 | } |
| 187 | 187 | $cr = $this->connection->getConnectionResource(); |
| 188 | - if(!$this->ldap->isResource($cr)) { |
|
| 188 | + if (!$this->ldap->isResource($cr)) { |
|
| 189 | 189 | //LDAP not available |
| 190 | 190 | \OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', \OCP\Util::DEBUG); |
| 191 | 191 | return false; |
@@ -195,7 +195,7 @@ discard block |
||
| 195 | 195 | $this->abandonPagedSearch(); |
| 196 | 196 | // openLDAP requires that we init a new Paged Search. Not needed by AD, |
| 197 | 197 | // but does not hurt either. |
| 198 | - $pagingSize = (int)$this->connection->ldapPagingSize; |
|
| 198 | + $pagingSize = (int) $this->connection->ldapPagingSize; |
|
| 199 | 199 | // 0 won't result in replies, small numbers may leave out groups |
| 200 | 200 | // (cf. #12306), 500 is default for paging and should work everywhere. |
| 201 | 201 | $maxResults = $pagingSize > 20 ? $pagingSize : 500; |
@@ -208,7 +208,7 @@ discard block |
||
| 208 | 208 | $isRangeRequest = false; |
| 209 | 209 | do { |
| 210 | 210 | $result = $this->executeRead($cr, $dn, $attrToRead, $filter, $maxResults); |
| 211 | - if(is_bool($result)) { |
|
| 211 | + if (is_bool($result)) { |
|
| 212 | 212 | // when an exists request was run and it was successful, an empty |
| 213 | 213 | // array must be returned |
| 214 | 214 | return $result ? [] : false; |
@@ -225,22 +225,22 @@ discard block |
||
| 225 | 225 | $result = $this->extractRangeData($result, $attr); |
| 226 | 226 | if (!empty($result)) { |
| 227 | 227 | $normalizedResult = $this->extractAttributeValuesFromResult( |
| 228 | - [ $attr => $result['values'] ], |
|
| 228 | + [$attr => $result['values']], |
|
| 229 | 229 | $attr |
| 230 | 230 | ); |
| 231 | 231 | $values = array_merge($values, $normalizedResult); |
| 232 | 232 | |
| 233 | - if($result['rangeHigh'] === '*') { |
|
| 233 | + if ($result['rangeHigh'] === '*') { |
|
| 234 | 234 | // when server replies with * as high range value, there are |
| 235 | 235 | // no more results left |
| 236 | 236 | return $values; |
| 237 | 237 | } else { |
| 238 | - $low = $result['rangeHigh'] + 1; |
|
| 239 | - $attrToRead = $result['attributeName'] . ';range=' . $low . '-*'; |
|
| 238 | + $low = $result['rangeHigh'] + 1; |
|
| 239 | + $attrToRead = $result['attributeName'].';range='.$low.'-*'; |
|
| 240 | 240 | $isRangeRequest = true; |
| 241 | 241 | } |
| 242 | 242 | } |
| 243 | - } while($isRangeRequest); |
|
| 243 | + } while ($isRangeRequest); |
|
| 244 | 244 | |
| 245 | 245 | \OCP\Util::writeLog('user_ldap', 'Requested attribute '.$attr.' not found for '.$dn, \OCP\Util::DEBUG); |
| 246 | 246 | return false; |
@@ -266,13 +266,13 @@ discard block |
||
| 266 | 266 | if (!$this->ldap->isResource($rr)) { |
| 267 | 267 | if ($attribute !== '') { |
| 268 | 268 | //do not throw this message on userExists check, irritates |
| 269 | - \OCP\Util::writeLog('user_ldap', 'readAttribute failed for DN ' . $dn, \OCP\Util::DEBUG); |
|
| 269 | + \OCP\Util::writeLog('user_ldap', 'readAttribute failed for DN '.$dn, \OCP\Util::DEBUG); |
|
| 270 | 270 | } |
| 271 | 271 | //in case an error occurs , e.g. object does not exist |
| 272 | 272 | return false; |
| 273 | 273 | } |
| 274 | 274 | if ($attribute === '' && ($filter === 'objectclass=*' || $this->invokeLDAPMethod('countEntries', $cr, $rr) === 1)) { |
| 275 | - \OCP\Util::writeLog('user_ldap', 'readAttribute: ' . $dn . ' found', \OCP\Util::DEBUG); |
|
| 275 | + \OCP\Util::writeLog('user_ldap', 'readAttribute: '.$dn.' found', \OCP\Util::DEBUG); |
|
| 276 | 276 | return true; |
| 277 | 277 | } |
| 278 | 278 | $er = $this->invokeLDAPMethod('firstEntry', $cr, $rr); |
@@ -297,12 +297,12 @@ discard block |
||
| 297 | 297 | */ |
| 298 | 298 | public function extractAttributeValuesFromResult($result, $attribute) { |
| 299 | 299 | $values = []; |
| 300 | - if(isset($result[$attribute]) && $result[$attribute]['count'] > 0) { |
|
| 300 | + if (isset($result[$attribute]) && $result[$attribute]['count'] > 0) { |
|
| 301 | 301 | $lowercaseAttribute = strtolower($attribute); |
| 302 | - for($i=0;$i<$result[$attribute]['count'];$i++) { |
|
| 303 | - if($this->resemblesDN($attribute)) { |
|
| 302 | + for ($i = 0; $i < $result[$attribute]['count']; $i++) { |
|
| 303 | + if ($this->resemblesDN($attribute)) { |
|
| 304 | 304 | $values[] = $this->helper->sanitizeDN($result[$attribute][$i]); |
| 305 | - } elseif($lowercaseAttribute === 'objectguid' || $lowercaseAttribute === 'guid') { |
|
| 305 | + } elseif ($lowercaseAttribute === 'objectguid' || $lowercaseAttribute === 'guid') { |
|
| 306 | 306 | $values[] = $this->convertObjectGUID2Str($result[$attribute][$i]); |
| 307 | 307 | } else { |
| 308 | 308 | $values[] = $result[$attribute][$i]; |
@@ -324,10 +324,10 @@ discard block |
||
| 324 | 324 | */ |
| 325 | 325 | public function extractRangeData($result, $attribute) { |
| 326 | 326 | $keys = array_keys($result); |
| 327 | - foreach($keys as $key) { |
|
| 328 | - if($key !== $attribute && strpos($key, $attribute) === 0) { |
|
| 327 | + foreach ($keys as $key) { |
|
| 328 | + if ($key !== $attribute && strpos($key, $attribute) === 0) { |
|
| 329 | 329 | $queryData = explode(';', $key); |
| 330 | - if(strpos($queryData[1], 'range=') === 0) { |
|
| 330 | + if (strpos($queryData[1], 'range=') === 0) { |
|
| 331 | 331 | $high = substr($queryData[1], 1 + strpos($queryData[1], '-')); |
| 332 | 332 | $data = [ |
| 333 | 333 | 'values' => $result[$key], |
@@ -352,18 +352,18 @@ discard block |
||
| 352 | 352 | * @throws \Exception |
| 353 | 353 | */ |
| 354 | 354 | public function setPassword($userDN, $password) { |
| 355 | - if((int)$this->connection->turnOnPasswordChange !== 1) { |
|
| 355 | + if ((int) $this->connection->turnOnPasswordChange !== 1) { |
|
| 356 | 356 | throw new \Exception('LDAP password changes are disabled.'); |
| 357 | 357 | } |
| 358 | 358 | $cr = $this->connection->getConnectionResource(); |
| 359 | - if(!$this->ldap->isResource($cr)) { |
|
| 359 | + if (!$this->ldap->isResource($cr)) { |
|
| 360 | 360 | //LDAP not available |
| 361 | 361 | \OCP\Util::writeLog('user_ldap', 'LDAP resource not available.', \OCP\Util::DEBUG); |
| 362 | 362 | return false; |
| 363 | 363 | } |
| 364 | 364 | try { |
| 365 | 365 | return @$this->invokeLDAPMethod('modReplace', $cr, $userDN, $password); |
| 366 | - } catch(ConstraintViolationException $e) { |
|
| 366 | + } catch (ConstraintViolationException $e) { |
|
| 367 | 367 | throw new HintException('Password change rejected.', \OC::$server->getL10N('user_ldap')->t('Password change rejected. Hint: ').$e->getMessage(), $e->getCode()); |
| 368 | 368 | } |
| 369 | 369 | } |
@@ -405,17 +405,17 @@ discard block |
||
| 405 | 405 | */ |
| 406 | 406 | public function getDomainDNFromDN($dn) { |
| 407 | 407 | $allParts = $this->ldap->explodeDN($dn, 0); |
| 408 | - if($allParts === false) { |
|
| 408 | + if ($allParts === false) { |
|
| 409 | 409 | //not a valid DN |
| 410 | 410 | return ''; |
| 411 | 411 | } |
| 412 | 412 | $domainParts = array(); |
| 413 | 413 | $dcFound = false; |
| 414 | - foreach($allParts as $part) { |
|
| 415 | - if(!$dcFound && strpos($part, 'dc=') === 0) { |
|
| 414 | + foreach ($allParts as $part) { |
|
| 415 | + if (!$dcFound && strpos($part, 'dc=') === 0) { |
|
| 416 | 416 | $dcFound = true; |
| 417 | 417 | } |
| 418 | - if($dcFound) { |
|
| 418 | + if ($dcFound) { |
|
| 419 | 419 | $domainParts[] = $part; |
| 420 | 420 | } |
| 421 | 421 | } |
@@ -442,7 +442,7 @@ discard block |
||
| 442 | 442 | |
| 443 | 443 | //Check whether the DN belongs to the Base, to avoid issues on multi- |
| 444 | 444 | //server setups |
| 445 | - if(is_string($fdn) && $this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 445 | + if (is_string($fdn) && $this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 446 | 446 | return $fdn; |
| 447 | 447 | } |
| 448 | 448 | |
@@ -459,7 +459,7 @@ discard block |
||
| 459 | 459 | //To avoid bypassing the base DN settings under certain circumstances |
| 460 | 460 | //with the group support, check whether the provided DN matches one of |
| 461 | 461 | //the given Bases |
| 462 | - if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseGroups)) { |
|
| 462 | + if (!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseGroups)) { |
|
| 463 | 463 | return false; |
| 464 | 464 | } |
| 465 | 465 | |
@@ -476,11 +476,11 @@ discard block |
||
| 476 | 476 | */ |
| 477 | 477 | public function groupsMatchFilter($groupDNs) { |
| 478 | 478 | $validGroupDNs = []; |
| 479 | - foreach($groupDNs as $dn) { |
|
| 479 | + foreach ($groupDNs as $dn) { |
|
| 480 | 480 | $cacheKey = 'groupsMatchFilter-'.$dn; |
| 481 | 481 | $groupMatchFilter = $this->connection->getFromCache($cacheKey); |
| 482 | - if(!is_null($groupMatchFilter)) { |
|
| 483 | - if($groupMatchFilter) { |
|
| 482 | + if (!is_null($groupMatchFilter)) { |
|
| 483 | + if ($groupMatchFilter) { |
|
| 484 | 484 | $validGroupDNs[] = $dn; |
| 485 | 485 | } |
| 486 | 486 | continue; |
@@ -488,13 +488,13 @@ discard block |
||
| 488 | 488 | |
| 489 | 489 | // Check the base DN first. If this is not met already, we don't |
| 490 | 490 | // need to ask the server at all. |
| 491 | - if(!$this->isDNPartOfBase($dn, $this->connection->ldapBaseGroups)) { |
|
| 491 | + if (!$this->isDNPartOfBase($dn, $this->connection->ldapBaseGroups)) { |
|
| 492 | 492 | $this->connection->writeToCache($cacheKey, false); |
| 493 | 493 | continue; |
| 494 | 494 | } |
| 495 | 495 | |
| 496 | 496 | $result = $this->readAttribute($dn, 'cn', $this->connection->ldapGroupFilter); |
| 497 | - if(is_array($result)) { |
|
| 497 | + if (is_array($result)) { |
|
| 498 | 498 | $this->connection->writeToCache($cacheKey, true); |
| 499 | 499 | $validGroupDNs[] = $dn; |
| 500 | 500 | } else { |
@@ -515,7 +515,7 @@ discard block |
||
| 515 | 515 | //To avoid bypassing the base DN settings under certain circumstances |
| 516 | 516 | //with the group support, check whether the provided DN matches one of |
| 517 | 517 | //the given Bases |
| 518 | - if(!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 518 | + if (!$this->isDNPartOfBase($fdn, $this->connection->ldapBaseUsers)) { |
|
| 519 | 519 | return false; |
| 520 | 520 | } |
| 521 | 521 | |
@@ -535,7 +535,7 @@ discard block |
||
| 535 | 535 | */ |
| 536 | 536 | public function dn2ocname($fdn, $ldapName = null, $isUser = true, &$newlyMapped = null, array $record = null) { |
| 537 | 537 | $newlyMapped = false; |
| 538 | - if($isUser) { |
|
| 538 | + if ($isUser) { |
|
| 539 | 539 | $mapper = $this->getUserMapper(); |
| 540 | 540 | $nameAttribute = $this->connection->ldapUserDisplayName; |
| 541 | 541 | } else { |
@@ -545,15 +545,15 @@ discard block |
||
| 545 | 545 | |
| 546 | 546 | //let's try to retrieve the Nextcloud name from the mappings table |
| 547 | 547 | $ncName = $mapper->getNameByDN($fdn); |
| 548 | - if(is_string($ncName)) { |
|
| 548 | + if (is_string($ncName)) { |
|
| 549 | 549 | return $ncName; |
| 550 | 550 | } |
| 551 | 551 | |
| 552 | 552 | //second try: get the UUID and check if it is known. Then, update the DN and return the name. |
| 553 | 553 | $uuid = $this->getUUID($fdn, $isUser, $record); |
| 554 | - if(is_string($uuid)) { |
|
| 554 | + if (is_string($uuid)) { |
|
| 555 | 555 | $ncName = $mapper->getNameByUUID($uuid); |
| 556 | - if(is_string($ncName)) { |
|
| 556 | + if (is_string($ncName)) { |
|
| 557 | 557 | $mapper->setDNbyUUID($fdn, $uuid); |
| 558 | 558 | return $ncName; |
| 559 | 559 | } |
@@ -563,17 +563,17 @@ discard block |
||
| 563 | 563 | return false; |
| 564 | 564 | } |
| 565 | 565 | |
| 566 | - if(is_null($ldapName)) { |
|
| 566 | + if (is_null($ldapName)) { |
|
| 567 | 567 | $ldapName = $this->readAttribute($fdn, $nameAttribute); |
| 568 | - if(!isset($ldapName[0]) && empty($ldapName[0])) { |
|
| 568 | + if (!isset($ldapName[0]) && empty($ldapName[0])) { |
|
| 569 | 569 | \OCP\Util::writeLog('user_ldap', 'No or empty name for '.$fdn.'.', \OCP\Util::INFO); |
| 570 | 570 | return false; |
| 571 | 571 | } |
| 572 | 572 | $ldapName = $ldapName[0]; |
| 573 | 573 | } |
| 574 | 574 | |
| 575 | - if($isUser) { |
|
| 576 | - $usernameAttribute = (string)$this->connection->ldapExpertUsernameAttr; |
|
| 575 | + if ($isUser) { |
|
| 576 | + $usernameAttribute = (string) $this->connection->ldapExpertUsernameAttr; |
|
| 577 | 577 | if ($usernameAttribute !== '') { |
| 578 | 578 | $username = $this->readAttribute($fdn, $usernameAttribute); |
| 579 | 579 | $username = $username[0]; |
@@ -591,9 +591,9 @@ discard block |
||
| 591 | 591 | // outside of core user management will still cache the user as non-existing. |
| 592 | 592 | $originalTTL = $this->connection->ldapCacheTTL; |
| 593 | 593 | $this->connection->setConfiguration(array('ldapCacheTTL' => 0)); |
| 594 | - if(($isUser && $intName !== '' && !\OC::$server->getUserManager()->userExists($intName)) |
|
| 594 | + if (($isUser && $intName !== '' && !\OC::$server->getUserManager()->userExists($intName)) |
|
| 595 | 595 | || (!$isUser && !\OC::$server->getGroupManager()->groupExists($intName))) { |
| 596 | - if($mapper->map($fdn, $intName, $uuid)) { |
|
| 596 | + if ($mapper->map($fdn, $intName, $uuid)) { |
|
| 597 | 597 | $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); |
| 598 | 598 | $newlyMapped = true; |
| 599 | 599 | return $intName; |
@@ -602,7 +602,7 @@ discard block |
||
| 602 | 602 | $this->connection->setConfiguration(array('ldapCacheTTL' => $originalTTL)); |
| 603 | 603 | |
| 604 | 604 | $altName = $this->createAltInternalOwnCloudName($intName, $isUser); |
| 605 | - if(is_string($altName) && $mapper->map($fdn, $altName, $uuid)) { |
|
| 605 | + if (is_string($altName) && $mapper->map($fdn, $altName, $uuid)) { |
|
| 606 | 606 | $newlyMapped = true; |
| 607 | 607 | return $altName; |
| 608 | 608 | } |
@@ -640,7 +640,7 @@ discard block |
||
| 640 | 640 | * @return array |
| 641 | 641 | */ |
| 642 | 642 | private function ldap2NextcloudNames($ldapObjects, $isUsers) { |
| 643 | - if($isUsers) { |
|
| 643 | + if ($isUsers) { |
|
| 644 | 644 | $nameAttribute = $this->connection->ldapUserDisplayName; |
| 645 | 645 | $sndAttribute = $this->connection->ldapUserDisplayName2; |
| 646 | 646 | } else { |
@@ -648,9 +648,9 @@ discard block |
||
| 648 | 648 | } |
| 649 | 649 | $nextcloudNames = array(); |
| 650 | 650 | |
| 651 | - foreach($ldapObjects as $ldapObject) { |
|
| 651 | + foreach ($ldapObjects as $ldapObject) { |
|
| 652 | 652 | $nameByLDAP = null; |
| 653 | - if( isset($ldapObject[$nameAttribute]) |
|
| 653 | + if (isset($ldapObject[$nameAttribute]) |
|
| 654 | 654 | && is_array($ldapObject[$nameAttribute]) |
| 655 | 655 | && isset($ldapObject[$nameAttribute][0]) |
| 656 | 656 | ) { |
@@ -659,12 +659,12 @@ discard block |
||
| 659 | 659 | } |
| 660 | 660 | |
| 661 | 661 | $ncName = $this->dn2ocname($ldapObject['dn'][0], $nameByLDAP, $isUsers); |
| 662 | - if($ncName) { |
|
| 662 | + if ($ncName) { |
|
| 663 | 663 | $nextcloudNames[] = $ncName; |
| 664 | - if($isUsers) { |
|
| 664 | + if ($isUsers) { |
|
| 665 | 665 | //cache the user names so it does not need to be retrieved |
| 666 | 666 | //again later (e.g. sharing dialogue). |
| 667 | - if(is_null($nameByLDAP)) { |
|
| 667 | + if (is_null($nameByLDAP)) { |
|
| 668 | 668 | continue; |
| 669 | 669 | } |
| 670 | 670 | $sndName = isset($ldapObject[$sndAttribute][0]) |
@@ -702,7 +702,7 @@ discard block |
||
| 702 | 702 | */ |
| 703 | 703 | public function cacheUserDisplayName($ocName, $displayName, $displayName2 = '') { |
| 704 | 704 | $user = $this->userManager->get($ocName); |
| 705 | - if($user === null) { |
|
| 705 | + if ($user === null) { |
|
| 706 | 706 | return; |
| 707 | 707 | } |
| 708 | 708 | $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2); |
@@ -722,9 +722,9 @@ discard block |
||
| 722 | 722 | $attempts = 0; |
| 723 | 723 | //while loop is just a precaution. If a name is not generated within |
| 724 | 724 | //20 attempts, something else is very wrong. Avoids infinite loop. |
| 725 | - while($attempts < 20){ |
|
| 726 | - $altName = $name . '_' . rand(1000,9999); |
|
| 727 | - if(!\OC::$server->getUserManager()->userExists($altName)) { |
|
| 725 | + while ($attempts < 20) { |
|
| 726 | + $altName = $name.'_'.rand(1000, 9999); |
|
| 727 | + if (!\OC::$server->getUserManager()->userExists($altName)) { |
|
| 728 | 728 | return $altName; |
| 729 | 729 | } |
| 730 | 730 | $attempts++; |
@@ -746,25 +746,25 @@ discard block |
||
| 746 | 746 | */ |
| 747 | 747 | private function _createAltInternalOwnCloudNameForGroups($name) { |
| 748 | 748 | $usedNames = $this->groupMapper->getNamesBySearch($name, "", '_%'); |
| 749 | - if(!($usedNames) || count($usedNames) === 0) { |
|
| 749 | + if (!($usedNames) || count($usedNames) === 0) { |
|
| 750 | 750 | $lastNo = 1; //will become name_2 |
| 751 | 751 | } else { |
| 752 | 752 | natsort($usedNames); |
| 753 | 753 | $lastName = array_pop($usedNames); |
| 754 | - $lastNo = (int)substr($lastName, strrpos($lastName, '_') + 1); |
|
| 754 | + $lastNo = (int) substr($lastName, strrpos($lastName, '_') + 1); |
|
| 755 | 755 | } |
| 756 | - $altName = $name.'_'. (string)($lastNo+1); |
|
| 756 | + $altName = $name.'_'.(string) ($lastNo + 1); |
|
| 757 | 757 | unset($usedNames); |
| 758 | 758 | |
| 759 | 759 | $attempts = 1; |
| 760 | - while($attempts < 21){ |
|
| 760 | + while ($attempts < 21) { |
|
| 761 | 761 | // Check to be really sure it is unique |
| 762 | 762 | // while loop is just a precaution. If a name is not generated within |
| 763 | 763 | // 20 attempts, something else is very wrong. Avoids infinite loop. |
| 764 | - if(!\OC::$server->getGroupManager()->groupExists($altName)) { |
|
| 764 | + if (!\OC::$server->getGroupManager()->groupExists($altName)) { |
|
| 765 | 765 | return $altName; |
| 766 | 766 | } |
| 767 | - $altName = $name . '_' . ($lastNo + $attempts); |
|
| 767 | + $altName = $name.'_'.($lastNo + $attempts); |
|
| 768 | 768 | $attempts++; |
| 769 | 769 | } |
| 770 | 770 | return false; |
@@ -779,7 +779,7 @@ discard block |
||
| 779 | 779 | private function createAltInternalOwnCloudName($name, $isUser) { |
| 780 | 780 | $originalTTL = $this->connection->ldapCacheTTL; |
| 781 | 781 | $this->connection->setConfiguration(array('ldapCacheTTL' => 0)); |
| 782 | - if($isUser) { |
|
| 782 | + if ($isUser) { |
|
| 783 | 783 | $altName = $this->_createAltInternalOwnCloudNameForUsers($name); |
| 784 | 784 | } else { |
| 785 | 785 | $altName = $this->_createAltInternalOwnCloudNameForGroups($name); |
@@ -829,13 +829,13 @@ discard block |
||
| 829 | 829 | public function fetchListOfUsers($filter, $attr, $limit = null, $offset = null, $forceApplyAttributes = false) { |
| 830 | 830 | $ldapRecords = $this->searchUsers($filter, $attr, $limit, $offset); |
| 831 | 831 | $recordsToUpdate = $ldapRecords; |
| 832 | - if(!$forceApplyAttributes) { |
|
| 832 | + if (!$forceApplyAttributes) { |
|
| 833 | 833 | $isBackgroundJobModeAjax = $this->config |
| 834 | 834 | ->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'ajax'; |
| 835 | 835 | $recordsToUpdate = array_filter($ldapRecords, function($record) use ($isBackgroundJobModeAjax) { |
| 836 | 836 | $newlyMapped = false; |
| 837 | 837 | $uid = $this->dn2ocname($record['dn'][0], null, true, $newlyMapped, $record); |
| 838 | - if(is_string($uid)) { |
|
| 838 | + if (is_string($uid)) { |
|
| 839 | 839 | $this->cacheUserExists($uid); |
| 840 | 840 | } |
| 841 | 841 | return ($uid !== false) && ($newlyMapped || $isBackgroundJobModeAjax); |
@@ -851,19 +851,19 @@ discard block |
||
| 851 | 851 | * and their values |
| 852 | 852 | * @param array $ldapRecords |
| 853 | 853 | */ |
| 854 | - public function batchApplyUserAttributes(array $ldapRecords){ |
|
| 854 | + public function batchApplyUserAttributes(array $ldapRecords) { |
|
| 855 | 855 | $displayNameAttribute = strtolower($this->connection->ldapUserDisplayName); |
| 856 | - foreach($ldapRecords as $userRecord) { |
|
| 857 | - if(!isset($userRecord[$displayNameAttribute])) { |
|
| 856 | + foreach ($ldapRecords as $userRecord) { |
|
| 857 | + if (!isset($userRecord[$displayNameAttribute])) { |
|
| 858 | 858 | // displayName is obligatory |
| 859 | 859 | continue; |
| 860 | 860 | } |
| 861 | - $ocName = $this->dn2ocname($userRecord['dn'][0], null, true); |
|
| 862 | - if($ocName === false) { |
|
| 861 | + $ocName = $this->dn2ocname($userRecord['dn'][0], null, true); |
|
| 862 | + if ($ocName === false) { |
|
| 863 | 863 | continue; |
| 864 | 864 | } |
| 865 | 865 | $user = $this->userManager->get($ocName); |
| 866 | - if($user instanceof OfflineUser) { |
|
| 866 | + if ($user instanceof OfflineUser) { |
|
| 867 | 867 | $user->unmark(); |
| 868 | 868 | $user = $this->userManager->get($ocName); |
| 869 | 869 | } |
@@ -895,8 +895,8 @@ discard block |
||
| 895 | 895 | * @return array |
| 896 | 896 | */ |
| 897 | 897 | private function fetchList($list, $manyAttributes) { |
| 898 | - if(is_array($list)) { |
|
| 899 | - if($manyAttributes) { |
|
| 898 | + if (is_array($list)) { |
|
| 899 | + if ($manyAttributes) { |
|
| 900 | 900 | return $list; |
| 901 | 901 | } else { |
| 902 | 902 | $list = array_reduce($list, function($carry, $item) { |
@@ -994,7 +994,7 @@ discard block |
||
| 994 | 994 | // php no longer supports call-time pass-by-reference |
| 995 | 995 | // thus cannot support controlPagedResultResponse as the third argument |
| 996 | 996 | // is a reference |
| 997 | - $doMethod = function () use ($command, &$arguments) { |
|
| 997 | + $doMethod = function() use ($command, &$arguments) { |
|
| 998 | 998 | if ($command == 'controlPagedResultResponse') { |
| 999 | 999 | throw new \InvalidArgumentException('Invoker does not support controlPagedResultResponse, call LDAP Wrapper directly instead.'); |
| 1000 | 1000 | } else { |
@@ -1012,7 +1012,7 @@ discard block |
||
| 1012 | 1012 | $this->connection->resetConnectionResource(); |
| 1013 | 1013 | $cr = $this->connection->getConnectionResource(); |
| 1014 | 1014 | |
| 1015 | - if(!$this->ldap->isResource($cr)) { |
|
| 1015 | + if (!$this->ldap->isResource($cr)) { |
|
| 1016 | 1016 | // Seems like we didn't find any resource. |
| 1017 | 1017 | \OCP\Util::writeLog('user_ldap', "Could not $command, because resource is missing.", \OCP\Util::DEBUG); |
| 1018 | 1018 | throw $e; |
@@ -1037,13 +1037,13 @@ discard block |
||
| 1037 | 1037 | * @throws ServerNotAvailableException |
| 1038 | 1038 | */ |
| 1039 | 1039 | private function executeSearch($filter, $base, &$attr = null, $limit = null, $offset = null) { |
| 1040 | - if(!is_null($attr) && !is_array($attr)) { |
|
| 1040 | + if (!is_null($attr) && !is_array($attr)) { |
|
| 1041 | 1041 | $attr = array(mb_strtolower($attr, 'UTF-8')); |
| 1042 | 1042 | } |
| 1043 | 1043 | |
| 1044 | 1044 | // See if we have a resource, in case not cancel with message |
| 1045 | 1045 | $cr = $this->connection->getConnectionResource(); |
| 1046 | - if(!$this->ldap->isResource($cr)) { |
|
| 1046 | + if (!$this->ldap->isResource($cr)) { |
|
| 1047 | 1047 | // Seems like we didn't find any resource. |
| 1048 | 1048 | // Return an empty array just like before. |
| 1049 | 1049 | \OCP\Util::writeLog('user_ldap', 'Could not search, because resource is missing.', \OCP\Util::DEBUG); |
@@ -1051,13 +1051,13 @@ discard block |
||
| 1051 | 1051 | } |
| 1052 | 1052 | |
| 1053 | 1053 | //check whether paged search should be attempted |
| 1054 | - $pagedSearchOK = $this->initPagedSearch($filter, $base, $attr, (int)$limit, $offset); |
|
| 1054 | + $pagedSearchOK = $this->initPagedSearch($filter, $base, $attr, (int) $limit, $offset); |
|
| 1055 | 1055 | |
| 1056 | 1056 | $linkResources = array_pad(array(), count($base), $cr); |
| 1057 | 1057 | $sr = $this->invokeLDAPMethod('search', $linkResources, $base, $filter, $attr); |
| 1058 | 1058 | // cannot use $cr anymore, might have changed in the previous call! |
| 1059 | 1059 | $error = $this->ldap->errno($this->connection->getConnectionResource()); |
| 1060 | - if(!is_array($sr) || $error !== 0) { |
|
| 1060 | + if (!is_array($sr) || $error !== 0) { |
|
| 1061 | 1061 | \OCP\Util::writeLog('user_ldap', 'Attempt for Paging? '.print_r($pagedSearchOK, true), \OCP\Util::ERROR); |
| 1062 | 1062 | return false; |
| 1063 | 1063 | } |
@@ -1080,29 +1080,29 @@ discard block |
||
| 1080 | 1080 | */ |
| 1081 | 1081 | private function processPagedSearchStatus($sr, $filter, $base, $iFoundItems, $limit, $offset, $pagedSearchOK, $skipHandling) { |
| 1082 | 1082 | $cookie = null; |
| 1083 | - if($pagedSearchOK) { |
|
| 1083 | + if ($pagedSearchOK) { |
|
| 1084 | 1084 | $cr = $this->connection->getConnectionResource(); |
| 1085 | - foreach($sr as $key => $res) { |
|
| 1086 | - if($this->ldap->controlPagedResultResponse($cr, $res, $cookie)) { |
|
| 1085 | + foreach ($sr as $key => $res) { |
|
| 1086 | + if ($this->ldap->controlPagedResultResponse($cr, $res, $cookie)) { |
|
| 1087 | 1087 | $this->setPagedResultCookie($base[$key], $filter, $limit, $offset, $cookie); |
| 1088 | 1088 | } |
| 1089 | 1089 | } |
| 1090 | 1090 | |
| 1091 | 1091 | //browsing through prior pages to get the cookie for the new one |
| 1092 | - if($skipHandling) { |
|
| 1092 | + if ($skipHandling) { |
|
| 1093 | 1093 | return false; |
| 1094 | 1094 | } |
| 1095 | 1095 | // if count is bigger, then the server does not support |
| 1096 | 1096 | // paged search. Instead, he did a normal search. We set a |
| 1097 | 1097 | // flag here, so the callee knows how to deal with it. |
| 1098 | - if($iFoundItems <= $limit) { |
|
| 1098 | + if ($iFoundItems <= $limit) { |
|
| 1099 | 1099 | $this->pagedSearchedSuccessful = true; |
| 1100 | 1100 | } |
| 1101 | 1101 | } else { |
| 1102 | - if(!is_null($limit) && (int)$this->connection->ldapPagingSize !== 0) { |
|
| 1102 | + if (!is_null($limit) && (int) $this->connection->ldapPagingSize !== 0) { |
|
| 1103 | 1103 | \OC::$server->getLogger()->debug( |
| 1104 | 1104 | 'Paged search was not available', |
| 1105 | - [ 'app' => 'user_ldap' ] |
|
| 1105 | + ['app' => 'user_ldap'] |
|
| 1106 | 1106 | ); |
| 1107 | 1107 | } |
| 1108 | 1108 | } |
@@ -1131,8 +1131,8 @@ discard block |
||
| 1131 | 1131 | private function count($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) { |
| 1132 | 1132 | \OCP\Util::writeLog('user_ldap', 'Count filter: '.print_r($filter, true), \OCP\Util::DEBUG); |
| 1133 | 1133 | |
| 1134 | - $limitPerPage = (int)$this->connection->ldapPagingSize; |
|
| 1135 | - if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1134 | + $limitPerPage = (int) $this->connection->ldapPagingSize; |
|
| 1135 | + if (!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1136 | 1136 | $limitPerPage = $limit; |
| 1137 | 1137 | } |
| 1138 | 1138 | |
@@ -1142,7 +1142,7 @@ discard block |
||
| 1142 | 1142 | |
| 1143 | 1143 | do { |
| 1144 | 1144 | $search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset); |
| 1145 | - if($search === false) { |
|
| 1145 | + if ($search === false) { |
|
| 1146 | 1146 | return $counter > 0 ? $counter : false; |
| 1147 | 1147 | } |
| 1148 | 1148 | list($sr, $pagedSearchOK) = $search; |
@@ -1161,7 +1161,7 @@ discard block |
||
| 1161 | 1161 | * Continue now depends on $hasMorePages value |
| 1162 | 1162 | */ |
| 1163 | 1163 | $continue = $pagedSearchOK && $hasMorePages; |
| 1164 | - } while($continue && (is_null($limit) || $limit <= 0 || $limit > $counter)); |
|
| 1164 | + } while ($continue && (is_null($limit) || $limit <= 0 || $limit > $counter)); |
|
| 1165 | 1165 | |
| 1166 | 1166 | return $counter; |
| 1167 | 1167 | } |
@@ -1173,8 +1173,8 @@ discard block |
||
| 1173 | 1173 | private function countEntriesInSearchResults($searchResults) { |
| 1174 | 1174 | $counter = 0; |
| 1175 | 1175 | |
| 1176 | - foreach($searchResults as $res) { |
|
| 1177 | - $count = (int)$this->invokeLDAPMethod('countEntries', $this->connection->getConnectionResource(), $res); |
|
| 1176 | + foreach ($searchResults as $res) { |
|
| 1177 | + $count = (int) $this->invokeLDAPMethod('countEntries', $this->connection->getConnectionResource(), $res); |
|
| 1178 | 1178 | $counter += $count; |
| 1179 | 1179 | } |
| 1180 | 1180 | |
@@ -1194,8 +1194,8 @@ discard block |
||
| 1194 | 1194 | * @throws ServerNotAvailableException |
| 1195 | 1195 | */ |
| 1196 | 1196 | public function search($filter, $base, $attr = null, $limit = null, $offset = null, $skipHandling = false) { |
| 1197 | - $limitPerPage = (int)$this->connection->ldapPagingSize; |
|
| 1198 | - if(!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1197 | + $limitPerPage = (int) $this->connection->ldapPagingSize; |
|
| 1198 | + if (!is_null($limit) && $limit < $limitPerPage && $limit > 0) { |
|
| 1199 | 1199 | $limitPerPage = $limit; |
| 1200 | 1200 | } |
| 1201 | 1201 | |
@@ -1209,13 +1209,13 @@ discard block |
||
| 1209 | 1209 | $savedoffset = $offset; |
| 1210 | 1210 | do { |
| 1211 | 1211 | $search = $this->executeSearch($filter, $base, $attr, $limitPerPage, $offset); |
| 1212 | - if($search === false) { |
|
| 1212 | + if ($search === false) { |
|
| 1213 | 1213 | return []; |
| 1214 | 1214 | } |
| 1215 | 1215 | list($sr, $pagedSearchOK) = $search; |
| 1216 | 1216 | $cr = $this->connection->getConnectionResource(); |
| 1217 | 1217 | |
| 1218 | - if($skipHandling) { |
|
| 1218 | + if ($skipHandling) { |
|
| 1219 | 1219 | //i.e. result do not need to be fetched, we just need the cookie |
| 1220 | 1220 | //thus pass 1 or any other value as $iFoundItems because it is not |
| 1221 | 1221 | //used |
@@ -1226,7 +1226,7 @@ discard block |
||
| 1226 | 1226 | } |
| 1227 | 1227 | |
| 1228 | 1228 | $iFoundItems = 0; |
| 1229 | - foreach($sr as $res) { |
|
| 1229 | + foreach ($sr as $res) { |
|
| 1230 | 1230 | $findings = array_merge($findings, $this->invokeLDAPMethod('getEntries', $cr, $res)); |
| 1231 | 1231 | $iFoundItems = max($iFoundItems, $findings['count']); |
| 1232 | 1232 | unset($findings['count']); |
@@ -1242,27 +1242,27 @@ discard block |
||
| 1242 | 1242 | |
| 1243 | 1243 | // if we're here, probably no connection resource is returned. |
| 1244 | 1244 | // to make Nextcloud behave nicely, we simply give back an empty array. |
| 1245 | - if(is_null($findings)) { |
|
| 1245 | + if (is_null($findings)) { |
|
| 1246 | 1246 | return array(); |
| 1247 | 1247 | } |
| 1248 | 1248 | |
| 1249 | - if(!is_null($attr)) { |
|
| 1249 | + if (!is_null($attr)) { |
|
| 1250 | 1250 | $selection = []; |
| 1251 | 1251 | $i = 0; |
| 1252 | - foreach($findings as $item) { |
|
| 1253 | - if(!is_array($item)) { |
|
| 1252 | + foreach ($findings as $item) { |
|
| 1253 | + if (!is_array($item)) { |
|
| 1254 | 1254 | continue; |
| 1255 | 1255 | } |
| 1256 | 1256 | $item = \OCP\Util::mb_array_change_key_case($item, MB_CASE_LOWER, 'UTF-8'); |
| 1257 | - foreach($attr as $key) { |
|
| 1258 | - if(isset($item[$key])) { |
|
| 1259 | - if(is_array($item[$key]) && isset($item[$key]['count'])) { |
|
| 1257 | + foreach ($attr as $key) { |
|
| 1258 | + if (isset($item[$key])) { |
|
| 1259 | + if (is_array($item[$key]) && isset($item[$key]['count'])) { |
|
| 1260 | 1260 | unset($item[$key]['count']); |
| 1261 | 1261 | } |
| 1262 | - if($key !== 'dn') { |
|
| 1263 | - if($this->resemblesDN($key)) { |
|
| 1262 | + if ($key !== 'dn') { |
|
| 1263 | + if ($this->resemblesDN($key)) { |
|
| 1264 | 1264 | $selection[$i][$key] = $this->helper->sanitizeDN($item[$key]); |
| 1265 | - } else if($key === 'objectguid' || $key === 'guid') { |
|
| 1265 | + } else if ($key === 'objectguid' || $key === 'guid') { |
|
| 1266 | 1266 | $selection[$i][$key] = [$this->convertObjectGUID2Str($item[$key][0])]; |
| 1267 | 1267 | } else { |
| 1268 | 1268 | $selection[$i][$key] = $item[$key]; |
@@ -1280,14 +1280,14 @@ discard block |
||
| 1280 | 1280 | //we slice the findings, when |
| 1281 | 1281 | //a) paged search unsuccessful, though attempted |
| 1282 | 1282 | //b) no paged search, but limit set |
| 1283 | - if((!$this->getPagedSearchResultState() |
|
| 1283 | + if ((!$this->getPagedSearchResultState() |
|
| 1284 | 1284 | && $pagedSearchOK) |
| 1285 | 1285 | || ( |
| 1286 | 1286 | !$pagedSearchOK |
| 1287 | 1287 | && !is_null($limit) |
| 1288 | 1288 | ) |
| 1289 | 1289 | ) { |
| 1290 | - $findings = array_slice($findings, (int)$offset, $limit); |
|
| 1290 | + $findings = array_slice($findings, (int) $offset, $limit); |
|
| 1291 | 1291 | } |
| 1292 | 1292 | return $findings; |
| 1293 | 1293 | } |
@@ -1297,7 +1297,7 @@ discard block |
||
| 1297 | 1297 | * @return bool|mixed|string |
| 1298 | 1298 | */ |
| 1299 | 1299 | public function sanitizeUsername($name) { |
| 1300 | - if($this->connection->ldapIgnoreNamingRules) { |
|
| 1300 | + if ($this->connection->ldapIgnoreNamingRules) { |
|
| 1301 | 1301 | return trim($name); |
| 1302 | 1302 | } |
| 1303 | 1303 | |
@@ -1322,13 +1322,13 @@ discard block |
||
| 1322 | 1322 | */ |
| 1323 | 1323 | public function escapeFilterPart($input, $allowAsterisk = false) { |
| 1324 | 1324 | $asterisk = ''; |
| 1325 | - if($allowAsterisk && strlen($input) > 0 && $input[0] === '*') { |
|
| 1325 | + if ($allowAsterisk && strlen($input) > 0 && $input[0] === '*') { |
|
| 1326 | 1326 | $asterisk = '*'; |
| 1327 | 1327 | $input = mb_substr($input, 1, null, 'UTF-8'); |
| 1328 | 1328 | } |
| 1329 | 1329 | $search = array('*', '\\', '(', ')'); |
| 1330 | 1330 | $replace = array('\\*', '\\\\', '\\(', '\\)'); |
| 1331 | - return $asterisk . str_replace($search, $replace, $input); |
|
| 1331 | + return $asterisk.str_replace($search, $replace, $input); |
|
| 1332 | 1332 | } |
| 1333 | 1333 | |
| 1334 | 1334 | /** |
@@ -1358,13 +1358,13 @@ discard block |
||
| 1358 | 1358 | */ |
| 1359 | 1359 | private function combineFilter($filters, $operator) { |
| 1360 | 1360 | $combinedFilter = '('.$operator; |
| 1361 | - foreach($filters as $filter) { |
|
| 1361 | + foreach ($filters as $filter) { |
|
| 1362 | 1362 | if ($filter !== '' && $filter[0] !== '(') { |
| 1363 | 1363 | $filter = '('.$filter.')'; |
| 1364 | 1364 | } |
| 1365 | - $combinedFilter.=$filter; |
|
| 1365 | + $combinedFilter .= $filter; |
|
| 1366 | 1366 | } |
| 1367 | - $combinedFilter.=')'; |
|
| 1367 | + $combinedFilter .= ')'; |
|
| 1368 | 1368 | return $combinedFilter; |
| 1369 | 1369 | } |
| 1370 | 1370 | |
@@ -1400,17 +1400,17 @@ discard block |
||
| 1400 | 1400 | * @throws \Exception |
| 1401 | 1401 | */ |
| 1402 | 1402 | private function getAdvancedFilterPartForSearch($search, $searchAttributes) { |
| 1403 | - if(!is_array($searchAttributes) || count($searchAttributes) < 2) { |
|
| 1403 | + if (!is_array($searchAttributes) || count($searchAttributes) < 2) { |
|
| 1404 | 1404 | throw new \Exception('searchAttributes must be an array with at least two string'); |
| 1405 | 1405 | } |
| 1406 | 1406 | $searchWords = explode(' ', trim($search)); |
| 1407 | 1407 | $wordFilters = array(); |
| 1408 | - foreach($searchWords as $word) { |
|
| 1408 | + foreach ($searchWords as $word) { |
|
| 1409 | 1409 | $word = $this->prepareSearchTerm($word); |
| 1410 | 1410 | //every word needs to appear at least once |
| 1411 | 1411 | $wordMatchOneAttrFilters = array(); |
| 1412 | - foreach($searchAttributes as $attr) { |
|
| 1413 | - $wordMatchOneAttrFilters[] = $attr . '=' . $word; |
|
| 1412 | + foreach ($searchAttributes as $attr) { |
|
| 1413 | + $wordMatchOneAttrFilters[] = $attr.'='.$word; |
|
| 1414 | 1414 | } |
| 1415 | 1415 | $wordFilters[] = $this->combineFilterWithOr($wordMatchOneAttrFilters); |
| 1416 | 1416 | } |
@@ -1428,10 +1428,10 @@ discard block |
||
| 1428 | 1428 | private function getFilterPartForSearch($search, $searchAttributes, $fallbackAttribute) { |
| 1429 | 1429 | $filter = array(); |
| 1430 | 1430 | $haveMultiSearchAttributes = (is_array($searchAttributes) && count($searchAttributes) > 0); |
| 1431 | - if($haveMultiSearchAttributes && strpos(trim($search), ' ') !== false) { |
|
| 1431 | + if ($haveMultiSearchAttributes && strpos(trim($search), ' ') !== false) { |
|
| 1432 | 1432 | try { |
| 1433 | 1433 | return $this->getAdvancedFilterPartForSearch($search, $searchAttributes); |
| 1434 | - } catch(\Exception $e) { |
|
| 1434 | + } catch (\Exception $e) { |
|
| 1435 | 1435 | \OCP\Util::writeLog( |
| 1436 | 1436 | 'user_ldap', |
| 1437 | 1437 | 'Creating advanced filter for search failed, falling back to simple method.', |
@@ -1441,17 +1441,17 @@ discard block |
||
| 1441 | 1441 | } |
| 1442 | 1442 | |
| 1443 | 1443 | $search = $this->prepareSearchTerm($search); |
| 1444 | - if(!is_array($searchAttributes) || count($searchAttributes) === 0) { |
|
| 1444 | + if (!is_array($searchAttributes) || count($searchAttributes) === 0) { |
|
| 1445 | 1445 | if ($fallbackAttribute === '') { |
| 1446 | 1446 | return ''; |
| 1447 | 1447 | } |
| 1448 | - $filter[] = $fallbackAttribute . '=' . $search; |
|
| 1448 | + $filter[] = $fallbackAttribute.'='.$search; |
|
| 1449 | 1449 | } else { |
| 1450 | - foreach($searchAttributes as $attribute) { |
|
| 1451 | - $filter[] = $attribute . '=' . $search; |
|
| 1450 | + foreach ($searchAttributes as $attribute) { |
|
| 1451 | + $filter[] = $attribute.'='.$search; |
|
| 1452 | 1452 | } |
| 1453 | 1453 | } |
| 1454 | - if(count($filter) === 1) { |
|
| 1454 | + if (count($filter) === 1) { |
|
| 1455 | 1455 | return '('.$filter[0].')'; |
| 1456 | 1456 | } |
| 1457 | 1457 | return $this->combineFilterWithOr($filter); |
@@ -1472,7 +1472,7 @@ discard block |
||
| 1472 | 1472 | if ($term === '') { |
| 1473 | 1473 | $result = '*'; |
| 1474 | 1474 | } else if ($allowEnum !== 'no') { |
| 1475 | - $result = $term . '*'; |
|
| 1475 | + $result = $term.'*'; |
|
| 1476 | 1476 | } |
| 1477 | 1477 | return $result; |
| 1478 | 1478 | } |
@@ -1484,7 +1484,7 @@ discard block |
||
| 1484 | 1484 | public function getFilterForUserCount() { |
| 1485 | 1485 | $filter = $this->combineFilterWithAnd(array( |
| 1486 | 1486 | $this->connection->ldapUserFilter, |
| 1487 | - $this->connection->ldapUserDisplayName . '=*' |
|
| 1487 | + $this->connection->ldapUserDisplayName.'=*' |
|
| 1488 | 1488 | )); |
| 1489 | 1489 | |
| 1490 | 1490 | return $filter; |
@@ -1502,7 +1502,7 @@ discard block |
||
| 1502 | 1502 | 'ldapAgentName' => $name, |
| 1503 | 1503 | 'ldapAgentPassword' => $password |
| 1504 | 1504 | ); |
| 1505 | - if(!$testConnection->setConfiguration($credentials)) { |
|
| 1505 | + if (!$testConnection->setConfiguration($credentials)) { |
|
| 1506 | 1506 | return false; |
| 1507 | 1507 | } |
| 1508 | 1508 | return $testConnection->bind(); |
@@ -1524,30 +1524,30 @@ discard block |
||
| 1524 | 1524 | // Sacrebleu! The UUID attribute is unknown :( We need first an |
| 1525 | 1525 | // existing DN to be able to reliably detect it. |
| 1526 | 1526 | $result = $this->search($filter, $base, ['dn'], 1); |
| 1527 | - if(!isset($result[0]) || !isset($result[0]['dn'])) { |
|
| 1527 | + if (!isset($result[0]) || !isset($result[0]['dn'])) { |
|
| 1528 | 1528 | throw new \Exception('Cannot determine UUID attribute'); |
| 1529 | 1529 | } |
| 1530 | 1530 | $dn = $result[0]['dn'][0]; |
| 1531 | - if(!$this->detectUuidAttribute($dn, true)) { |
|
| 1531 | + if (!$this->detectUuidAttribute($dn, true)) { |
|
| 1532 | 1532 | throw new \Exception('Cannot determine UUID attribute'); |
| 1533 | 1533 | } |
| 1534 | 1534 | } else { |
| 1535 | 1535 | // The UUID attribute is either known or an override is given. |
| 1536 | 1536 | // By calling this method we ensure that $this->connection->$uuidAttr |
| 1537 | 1537 | // is definitely set |
| 1538 | - if(!$this->detectUuidAttribute('', true)) { |
|
| 1538 | + if (!$this->detectUuidAttribute('', true)) { |
|
| 1539 | 1539 | throw new \Exception('Cannot determine UUID attribute'); |
| 1540 | 1540 | } |
| 1541 | 1541 | } |
| 1542 | 1542 | |
| 1543 | 1543 | $uuidAttr = $this->connection->ldapUuidUserAttribute; |
| 1544 | - if($uuidAttr === 'guid' || $uuidAttr === 'objectguid') { |
|
| 1544 | + if ($uuidAttr === 'guid' || $uuidAttr === 'objectguid') { |
|
| 1545 | 1545 | $uuid = $this->formatGuid2ForFilterUser($uuid); |
| 1546 | 1546 | } |
| 1547 | 1547 | |
| 1548 | - $filter = $uuidAttr . '=' . $uuid; |
|
| 1548 | + $filter = $uuidAttr.'='.$uuid; |
|
| 1549 | 1549 | $result = $this->searchUsers($filter, ['dn'], 2); |
| 1550 | - if(is_array($result) && isset($result[0]) && isset($result[0]['dn']) && count($result) === 1) { |
|
| 1550 | + if (is_array($result) && isset($result[0]) && isset($result[0]['dn']) && count($result) === 1) { |
|
| 1551 | 1551 | // we put the count into account to make sure that this is |
| 1552 | 1552 | // really unique |
| 1553 | 1553 | return $result[0]['dn'][0]; |
@@ -1566,7 +1566,7 @@ discard block |
||
| 1566 | 1566 | * @return bool true on success, false otherwise |
| 1567 | 1567 | */ |
| 1568 | 1568 | private function detectUuidAttribute($dn, $isUser = true, $force = false, array $ldapRecord = null) { |
| 1569 | - if($isUser) { |
|
| 1569 | + if ($isUser) { |
|
| 1570 | 1570 | $uuidAttr = 'ldapUuidUserAttribute'; |
| 1571 | 1571 | $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
| 1572 | 1572 | } else { |
@@ -1574,7 +1574,7 @@ discard block |
||
| 1574 | 1574 | $uuidOverride = $this->connection->ldapExpertUUIDGroupAttr; |
| 1575 | 1575 | } |
| 1576 | 1576 | |
| 1577 | - if(($this->connection->$uuidAttr !== 'auto') && !$force) { |
|
| 1577 | + if (($this->connection->$uuidAttr !== 'auto') && !$force) { |
|
| 1578 | 1578 | return true; |
| 1579 | 1579 | } |
| 1580 | 1580 | |
@@ -1583,10 +1583,10 @@ discard block |
||
| 1583 | 1583 | return true; |
| 1584 | 1584 | } |
| 1585 | 1585 | |
| 1586 | - foreach(self::UUID_ATTRIBUTES as $attribute) { |
|
| 1587 | - if($ldapRecord !== null) { |
|
| 1586 | + foreach (self::UUID_ATTRIBUTES as $attribute) { |
|
| 1587 | + if ($ldapRecord !== null) { |
|
| 1588 | 1588 | // we have the info from LDAP already, we don't need to talk to the server again |
| 1589 | - if(isset($ldapRecord[$attribute])) { |
|
| 1589 | + if (isset($ldapRecord[$attribute])) { |
|
| 1590 | 1590 | $this->connection->$uuidAttr = $attribute; |
| 1591 | 1591 | return true; |
| 1592 | 1592 | } else { |
@@ -1595,7 +1595,7 @@ discard block |
||
| 1595 | 1595 | } |
| 1596 | 1596 | |
| 1597 | 1597 | $value = $this->readAttribute($dn, $attribute); |
| 1598 | - if(is_array($value) && isset($value[0]) && !empty($value[0])) { |
|
| 1598 | + if (is_array($value) && isset($value[0]) && !empty($value[0])) { |
|
| 1599 | 1599 | \OCP\Util::writeLog('user_ldap', |
| 1600 | 1600 | 'Setting '.$attribute.' as '.$uuidAttr, |
| 1601 | 1601 | \OCP\Util::DEBUG); |
@@ -1617,7 +1617,7 @@ discard block |
||
| 1617 | 1617 | * @return bool|string |
| 1618 | 1618 | */ |
| 1619 | 1619 | public function getUUID($dn, $isUser = true, $ldapRecord = null) { |
| 1620 | - if($isUser) { |
|
| 1620 | + if ($isUser) { |
|
| 1621 | 1621 | $uuidAttr = 'ldapUuidUserAttribute'; |
| 1622 | 1622 | $uuidOverride = $this->connection->ldapExpertUUIDUserAttr; |
| 1623 | 1623 | } else { |
@@ -1626,10 +1626,10 @@ discard block |
||
| 1626 | 1626 | } |
| 1627 | 1627 | |
| 1628 | 1628 | $uuid = false; |
| 1629 | - if($this->detectUuidAttribute($dn, $isUser, false, $ldapRecord)) { |
|
| 1629 | + if ($this->detectUuidAttribute($dn, $isUser, false, $ldapRecord)) { |
|
| 1630 | 1630 | $attr = $this->connection->$uuidAttr; |
| 1631 | 1631 | $uuid = isset($ldapRecord[$attr]) ? $ldapRecord[$attr] : $this->readAttribute($dn, $attr); |
| 1632 | - if( !is_array($uuid) |
|
| 1632 | + if (!is_array($uuid) |
|
| 1633 | 1633 | && $uuidOverride !== '' |
| 1634 | 1634 | && $this->detectUuidAttribute($dn, $isUser, true, $ldapRecord)) |
| 1635 | 1635 | { |
@@ -1637,7 +1637,7 @@ discard block |
||
| 1637 | 1637 | ? $ldapRecord[$this->connection->$uuidAttr] |
| 1638 | 1638 | : $this->readAttribute($dn, $this->connection->$uuidAttr); |
| 1639 | 1639 | } |
| 1640 | - if(is_array($uuid) && isset($uuid[0]) && !empty($uuid[0])) { |
|
| 1640 | + if (is_array($uuid) && isset($uuid[0]) && !empty($uuid[0])) { |
|
| 1641 | 1641 | $uuid = $uuid[0]; |
| 1642 | 1642 | } |
| 1643 | 1643 | } |
@@ -1654,19 +1654,19 @@ discard block |
||
| 1654 | 1654 | private function convertObjectGUID2Str($oguid) { |
| 1655 | 1655 | $hex_guid = bin2hex($oguid); |
| 1656 | 1656 | $hex_guid_to_guid_str = ''; |
| 1657 | - for($k = 1; $k <= 4; ++$k) { |
|
| 1657 | + for ($k = 1; $k <= 4; ++$k) { |
|
| 1658 | 1658 | $hex_guid_to_guid_str .= substr($hex_guid, 8 - 2 * $k, 2); |
| 1659 | 1659 | } |
| 1660 | 1660 | $hex_guid_to_guid_str .= '-'; |
| 1661 | - for($k = 1; $k <= 2; ++$k) { |
|
| 1661 | + for ($k = 1; $k <= 2; ++$k) { |
|
| 1662 | 1662 | $hex_guid_to_guid_str .= substr($hex_guid, 12 - 2 * $k, 2); |
| 1663 | 1663 | } |
| 1664 | 1664 | $hex_guid_to_guid_str .= '-'; |
| 1665 | - for($k = 1; $k <= 2; ++$k) { |
|
| 1665 | + for ($k = 1; $k <= 2; ++$k) { |
|
| 1666 | 1666 | $hex_guid_to_guid_str .= substr($hex_guid, 16 - 2 * $k, 2); |
| 1667 | 1667 | } |
| 1668 | - $hex_guid_to_guid_str .= '-' . substr($hex_guid, 16, 4); |
|
| 1669 | - $hex_guid_to_guid_str .= '-' . substr($hex_guid, 20); |
|
| 1668 | + $hex_guid_to_guid_str .= '-'.substr($hex_guid, 16, 4); |
|
| 1669 | + $hex_guid_to_guid_str .= '-'.substr($hex_guid, 20); |
|
| 1670 | 1670 | |
| 1671 | 1671 | return strtoupper($hex_guid_to_guid_str); |
| 1672 | 1672 | } |
@@ -1683,11 +1683,11 @@ discard block |
||
| 1683 | 1683 | * @return string |
| 1684 | 1684 | */ |
| 1685 | 1685 | public function formatGuid2ForFilterUser($guid) { |
| 1686 | - if(!is_string($guid)) { |
|
| 1686 | + if (!is_string($guid)) { |
|
| 1687 | 1687 | throw new \InvalidArgumentException('String expected'); |
| 1688 | 1688 | } |
| 1689 | 1689 | $blocks = explode('-', $guid); |
| 1690 | - if(count($blocks) !== 5) { |
|
| 1690 | + if (count($blocks) !== 5) { |
|
| 1691 | 1691 | /* |
| 1692 | 1692 | * Why not throw an Exception instead? This method is a utility |
| 1693 | 1693 | * called only when trying to figure out whether a "missing" known |
@@ -1700,20 +1700,20 @@ discard block |
||
| 1700 | 1700 | * user. Instead we write a log message. |
| 1701 | 1701 | */ |
| 1702 | 1702 | \OC::$server->getLogger()->info( |
| 1703 | - 'Passed string does not resemble a valid GUID. Known UUID ' . |
|
| 1703 | + 'Passed string does not resemble a valid GUID. Known UUID '. |
|
| 1704 | 1704 | '({uuid}) probably does not match UUID configuration.', |
| 1705 | - [ 'app' => 'user_ldap', 'uuid' => $guid ] |
|
| 1705 | + ['app' => 'user_ldap', 'uuid' => $guid] |
|
| 1706 | 1706 | ); |
| 1707 | 1707 | return $guid; |
| 1708 | 1708 | } |
| 1709 | - for($i=0; $i < 3; $i++) { |
|
| 1709 | + for ($i = 0; $i < 3; $i++) { |
|
| 1710 | 1710 | $pairs = str_split($blocks[$i], 2); |
| 1711 | 1711 | $pairs = array_reverse($pairs); |
| 1712 | 1712 | $blocks[$i] = implode('', $pairs); |
| 1713 | 1713 | } |
| 1714 | - for($i=0; $i < 5; $i++) { |
|
| 1714 | + for ($i = 0; $i < 5; $i++) { |
|
| 1715 | 1715 | $pairs = str_split($blocks[$i], 2); |
| 1716 | - $blocks[$i] = '\\' . implode('\\', $pairs); |
|
| 1716 | + $blocks[$i] = '\\'.implode('\\', $pairs); |
|
| 1717 | 1717 | } |
| 1718 | 1718 | return implode('', $blocks); |
| 1719 | 1719 | } |
@@ -1727,12 +1727,12 @@ discard block |
||
| 1727 | 1727 | $domainDN = $this->getDomainDNFromDN($dn); |
| 1728 | 1728 | $cacheKey = 'getSID-'.$domainDN; |
| 1729 | 1729 | $sid = $this->connection->getFromCache($cacheKey); |
| 1730 | - if(!is_null($sid)) { |
|
| 1730 | + if (!is_null($sid)) { |
|
| 1731 | 1731 | return $sid; |
| 1732 | 1732 | } |
| 1733 | 1733 | |
| 1734 | 1734 | $objectSid = $this->readAttribute($domainDN, 'objectsid'); |
| 1735 | - if(!is_array($objectSid) || empty($objectSid)) { |
|
| 1735 | + if (!is_array($objectSid) || empty($objectSid)) { |
|
| 1736 | 1736 | $this->connection->writeToCache($cacheKey, false); |
| 1737 | 1737 | return false; |
| 1738 | 1738 | } |
@@ -1790,12 +1790,12 @@ discard block |
||
| 1790 | 1790 | $belongsToBase = false; |
| 1791 | 1791 | $bases = $this->helper->sanitizeDN($bases); |
| 1792 | 1792 | |
| 1793 | - foreach($bases as $base) { |
|
| 1793 | + foreach ($bases as $base) { |
|
| 1794 | 1794 | $belongsToBase = true; |
| 1795 | - if(mb_strripos($dn, $base, 0, 'UTF-8') !== (mb_strlen($dn, 'UTF-8')-mb_strlen($base, 'UTF-8'))) { |
|
| 1795 | + if (mb_strripos($dn, $base, 0, 'UTF-8') !== (mb_strlen($dn, 'UTF-8') - mb_strlen($base, 'UTF-8'))) { |
|
| 1796 | 1796 | $belongsToBase = false; |
| 1797 | 1797 | } |
| 1798 | - if($belongsToBase) { |
|
| 1798 | + if ($belongsToBase) { |
|
| 1799 | 1799 | break; |
| 1800 | 1800 | } |
| 1801 | 1801 | } |
@@ -1806,7 +1806,7 @@ discard block |
||
| 1806 | 1806 | * resets a running Paged Search operation |
| 1807 | 1807 | */ |
| 1808 | 1808 | private function abandonPagedSearch() { |
| 1809 | - if($this->connection->hasPagedResultSupport) { |
|
| 1809 | + if ($this->connection->hasPagedResultSupport) { |
|
| 1810 | 1810 | $cr = $this->connection->getConnectionResource(); |
| 1811 | 1811 | $this->invokeLDAPMethod('controlPagedResult', $cr, 0, false, $this->lastCookie); |
| 1812 | 1812 | $this->getPagedSearchResultState(); |
@@ -1824,16 +1824,16 @@ discard block |
||
| 1824 | 1824 | * @return string containing the key or empty if none is cached |
| 1825 | 1825 | */ |
| 1826 | 1826 | private function getPagedResultCookie($base, $filter, $limit, $offset) { |
| 1827 | - if($offset === 0) { |
|
| 1827 | + if ($offset === 0) { |
|
| 1828 | 1828 | return ''; |
| 1829 | 1829 | } |
| 1830 | 1830 | $offset -= $limit; |
| 1831 | 1831 | //we work with cache here |
| 1832 | - $cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' . (int)$limit . '-' . (int)$offset; |
|
| 1832 | + $cacheKey = 'lc'.crc32($base).'-'.crc32($filter).'-'.(int) $limit.'-'.(int) $offset; |
|
| 1833 | 1833 | $cookie = ''; |
| 1834 | - if(isset($this->cookies[$cacheKey])) { |
|
| 1834 | + if (isset($this->cookies[$cacheKey])) { |
|
| 1835 | 1835 | $cookie = $this->cookies[$cacheKey]; |
| 1836 | - if(is_null($cookie)) { |
|
| 1836 | + if (is_null($cookie)) { |
|
| 1837 | 1837 | $cookie = ''; |
| 1838 | 1838 | } |
| 1839 | 1839 | } |
@@ -1851,11 +1851,11 @@ discard block |
||
| 1851 | 1851 | * @return bool |
| 1852 | 1852 | */ |
| 1853 | 1853 | public function hasMoreResults() { |
| 1854 | - if(!$this->connection->hasPagedResultSupport) { |
|
| 1854 | + if (!$this->connection->hasPagedResultSupport) { |
|
| 1855 | 1855 | return false; |
| 1856 | 1856 | } |
| 1857 | 1857 | |
| 1858 | - if(empty($this->lastCookie) && $this->lastCookie !== '0') { |
|
| 1858 | + if (empty($this->lastCookie) && $this->lastCookie !== '0') { |
|
| 1859 | 1859 | // as in RFC 2696, when all results are returned, the cookie will |
| 1860 | 1860 | // be empty. |
| 1861 | 1861 | return false; |
@@ -1875,8 +1875,8 @@ discard block |
||
| 1875 | 1875 | */ |
| 1876 | 1876 | private function setPagedResultCookie($base, $filter, $limit, $offset, $cookie) { |
| 1877 | 1877 | // allow '0' for 389ds |
| 1878 | - if(!empty($cookie) || $cookie === '0') { |
|
| 1879 | - $cacheKey = 'lc' . crc32($base) . '-' . crc32($filter) . '-' . (int)$limit . '-' . (int)$offset; |
|
| 1878 | + if (!empty($cookie) || $cookie === '0') { |
|
| 1879 | + $cacheKey = 'lc'.crc32($base).'-'.crc32($filter).'-'.(int) $limit.'-'.(int) $offset; |
|
| 1880 | 1880 | $this->cookies[$cacheKey] = $cookie; |
| 1881 | 1881 | $this->lastCookie = $cookie; |
| 1882 | 1882 | } |
@@ -1903,17 +1903,17 @@ discard block |
||
| 1903 | 1903 | */ |
| 1904 | 1904 | private function initPagedSearch($filter, $bases, $attr, $limit, $offset) { |
| 1905 | 1905 | $pagedSearchOK = false; |
| 1906 | - if($this->connection->hasPagedResultSupport && ($limit !== 0)) { |
|
| 1907 | - $offset = (int)$offset; //can be null |
|
| 1906 | + if ($this->connection->hasPagedResultSupport && ($limit !== 0)) { |
|
| 1907 | + $offset = (int) $offset; //can be null |
|
| 1908 | 1908 | \OCP\Util::writeLog('user_ldap', |
| 1909 | 1909 | 'initializing paged search for Filter '.$filter.' base '.print_r($bases, true) |
| 1910 | - .' attr '.print_r($attr, true). ' limit ' .$limit.' offset '.$offset, |
|
| 1910 | + .' attr '.print_r($attr, true).' limit '.$limit.' offset '.$offset, |
|
| 1911 | 1911 | \OCP\Util::DEBUG); |
| 1912 | 1912 | //get the cookie from the search for the previous search, required by LDAP |
| 1913 | - foreach($bases as $base) { |
|
| 1913 | + foreach ($bases as $base) { |
|
| 1914 | 1914 | |
| 1915 | 1915 | $cookie = $this->getPagedResultCookie($base, $filter, $limit, $offset); |
| 1916 | - if(empty($cookie) && $cookie !== "0" && ($offset > 0)) { |
|
| 1916 | + if (empty($cookie) && $cookie !== "0" && ($offset > 0)) { |
|
| 1917 | 1917 | // no cookie known from a potential previous search. We need |
| 1918 | 1918 | // to start from 0 to come to the desired page. cookie value |
| 1919 | 1919 | // of '0' is valid, because 389ds |
@@ -1923,17 +1923,17 @@ discard block |
||
| 1923 | 1923 | //still no cookie? obviously, the server does not like us. Let's skip paging efforts. |
| 1924 | 1924 | // '0' is valid, because 389ds |
| 1925 | 1925 | //TODO: remember this, probably does not change in the next request... |
| 1926 | - if(empty($cookie) && $cookie !== '0') { |
|
| 1926 | + if (empty($cookie) && $cookie !== '0') { |
|
| 1927 | 1927 | $cookie = null; |
| 1928 | 1928 | } |
| 1929 | 1929 | } |
| 1930 | - if(!is_null($cookie)) { |
|
| 1930 | + if (!is_null($cookie)) { |
|
| 1931 | 1931 | //since offset = 0, this is a new search. We abandon other searches that might be ongoing. |
| 1932 | 1932 | $this->abandonPagedSearch(); |
| 1933 | 1933 | $pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult', |
| 1934 | 1934 | $this->connection->getConnectionResource(), $limit, |
| 1935 | 1935 | false, $cookie); |
| 1936 | - if(!$pagedSearchOK) { |
|
| 1936 | + if (!$pagedSearchOK) { |
|
| 1937 | 1937 | return false; |
| 1938 | 1938 | } |
| 1939 | 1939 | \OCP\Util::writeLog('user_ldap', 'Ready for a paged search', \OCP\Util::DEBUG); |
@@ -1949,14 +1949,14 @@ discard block |
||
| 1949 | 1949 | * So we added "&& !empty($this->lastCookie)" to this test to ignore pagination |
| 1950 | 1950 | * if we don't have a previous paged search. |
| 1951 | 1951 | */ |
| 1952 | - } else if($this->connection->hasPagedResultSupport && $limit === 0 && !empty($this->lastCookie)) { |
|
| 1952 | + } else if ($this->connection->hasPagedResultSupport && $limit === 0 && !empty($this->lastCookie)) { |
|
| 1953 | 1953 | // a search without limit was requested. However, if we do use |
| 1954 | 1954 | // Paged Search once, we always must do it. This requires us to |
| 1955 | 1955 | // initialize it with the configured page size. |
| 1956 | 1956 | $this->abandonPagedSearch(); |
| 1957 | 1957 | // in case someone set it to 0 … use 500, otherwise no results will |
| 1958 | 1958 | // be returned. |
| 1959 | - $pageSize = (int)$this->connection->ldapPagingSize > 0 ? (int)$this->connection->ldapPagingSize : 500; |
|
| 1959 | + $pageSize = (int) $this->connection->ldapPagingSize > 0 ? (int) $this->connection->ldapPagingSize : 500; |
|
| 1960 | 1960 | $pagedSearchOK = $this->invokeLDAPMethod('controlPagedResult', |
| 1961 | 1961 | $this->connection->getConnectionResource(), |
| 1962 | 1962 | $pageSize, false, ''); |
@@ -46,655 +46,655 @@ |
||
| 46 | 46 | * represents an LDAP user, gets and holds user-specific information from LDAP |
| 47 | 47 | */ |
| 48 | 48 | class User { |
| 49 | - /** |
|
| 50 | - * @var IUserTools |
|
| 51 | - */ |
|
| 52 | - protected $access; |
|
| 53 | - /** |
|
| 54 | - * @var Connection |
|
| 55 | - */ |
|
| 56 | - protected $connection; |
|
| 57 | - /** |
|
| 58 | - * @var IConfig |
|
| 59 | - */ |
|
| 60 | - protected $config; |
|
| 61 | - /** |
|
| 62 | - * @var FilesystemHelper |
|
| 63 | - */ |
|
| 64 | - protected $fs; |
|
| 65 | - /** |
|
| 66 | - * @var Image |
|
| 67 | - */ |
|
| 68 | - protected $image; |
|
| 69 | - /** |
|
| 70 | - * @var LogWrapper |
|
| 71 | - */ |
|
| 72 | - protected $log; |
|
| 73 | - /** |
|
| 74 | - * @var IAvatarManager |
|
| 75 | - */ |
|
| 76 | - protected $avatarManager; |
|
| 77 | - /** |
|
| 78 | - * @var IUserManager |
|
| 79 | - */ |
|
| 80 | - protected $userManager; |
|
| 81 | - /** |
|
| 82 | - * @var INotificationManager |
|
| 83 | - */ |
|
| 84 | - protected $notificationManager; |
|
| 85 | - /** |
|
| 86 | - * @var string |
|
| 87 | - */ |
|
| 88 | - protected $dn; |
|
| 89 | - /** |
|
| 90 | - * @var string |
|
| 91 | - */ |
|
| 92 | - protected $uid; |
|
| 93 | - /** |
|
| 94 | - * @var string[] |
|
| 95 | - */ |
|
| 96 | - protected $refreshedFeatures = array(); |
|
| 97 | - /** |
|
| 98 | - * @var string |
|
| 99 | - */ |
|
| 100 | - protected $avatarImage; |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * DB config keys for user preferences |
|
| 104 | - */ |
|
| 105 | - const USER_PREFKEY_FIRSTLOGIN = 'firstLoginAccomplished'; |
|
| 106 | - const USER_PREFKEY_LASTREFRESH = 'lastFeatureRefresh'; |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * @brief constructor, make sure the subclasses call this one! |
|
| 110 | - * @param string $username the internal username |
|
| 111 | - * @param string $dn the LDAP DN |
|
| 112 | - * @param IUserTools $access an instance that implements IUserTools for |
|
| 113 | - * LDAP interaction |
|
| 114 | - * @param IConfig $config |
|
| 115 | - * @param FilesystemHelper $fs |
|
| 116 | - * @param Image $image any empty instance |
|
| 117 | - * @param LogWrapper $log |
|
| 118 | - * @param IAvatarManager $avatarManager |
|
| 119 | - * @param IUserManager $userManager |
|
| 120 | - * @param INotificationManager $notificationManager |
|
| 121 | - */ |
|
| 122 | - public function __construct($username, $dn, IUserTools $access, |
|
| 123 | - IConfig $config, FilesystemHelper $fs, Image $image, |
|
| 124 | - LogWrapper $log, IAvatarManager $avatarManager, IUserManager $userManager, |
|
| 125 | - INotificationManager $notificationManager) { |
|
| 49 | + /** |
|
| 50 | + * @var IUserTools |
|
| 51 | + */ |
|
| 52 | + protected $access; |
|
| 53 | + /** |
|
| 54 | + * @var Connection |
|
| 55 | + */ |
|
| 56 | + protected $connection; |
|
| 57 | + /** |
|
| 58 | + * @var IConfig |
|
| 59 | + */ |
|
| 60 | + protected $config; |
|
| 61 | + /** |
|
| 62 | + * @var FilesystemHelper |
|
| 63 | + */ |
|
| 64 | + protected $fs; |
|
| 65 | + /** |
|
| 66 | + * @var Image |
|
| 67 | + */ |
|
| 68 | + protected $image; |
|
| 69 | + /** |
|
| 70 | + * @var LogWrapper |
|
| 71 | + */ |
|
| 72 | + protected $log; |
|
| 73 | + /** |
|
| 74 | + * @var IAvatarManager |
|
| 75 | + */ |
|
| 76 | + protected $avatarManager; |
|
| 77 | + /** |
|
| 78 | + * @var IUserManager |
|
| 79 | + */ |
|
| 80 | + protected $userManager; |
|
| 81 | + /** |
|
| 82 | + * @var INotificationManager |
|
| 83 | + */ |
|
| 84 | + protected $notificationManager; |
|
| 85 | + /** |
|
| 86 | + * @var string |
|
| 87 | + */ |
|
| 88 | + protected $dn; |
|
| 89 | + /** |
|
| 90 | + * @var string |
|
| 91 | + */ |
|
| 92 | + protected $uid; |
|
| 93 | + /** |
|
| 94 | + * @var string[] |
|
| 95 | + */ |
|
| 96 | + protected $refreshedFeatures = array(); |
|
| 97 | + /** |
|
| 98 | + * @var string |
|
| 99 | + */ |
|
| 100 | + protected $avatarImage; |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * DB config keys for user preferences |
|
| 104 | + */ |
|
| 105 | + const USER_PREFKEY_FIRSTLOGIN = 'firstLoginAccomplished'; |
|
| 106 | + const USER_PREFKEY_LASTREFRESH = 'lastFeatureRefresh'; |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * @brief constructor, make sure the subclasses call this one! |
|
| 110 | + * @param string $username the internal username |
|
| 111 | + * @param string $dn the LDAP DN |
|
| 112 | + * @param IUserTools $access an instance that implements IUserTools for |
|
| 113 | + * LDAP interaction |
|
| 114 | + * @param IConfig $config |
|
| 115 | + * @param FilesystemHelper $fs |
|
| 116 | + * @param Image $image any empty instance |
|
| 117 | + * @param LogWrapper $log |
|
| 118 | + * @param IAvatarManager $avatarManager |
|
| 119 | + * @param IUserManager $userManager |
|
| 120 | + * @param INotificationManager $notificationManager |
|
| 121 | + */ |
|
| 122 | + public function __construct($username, $dn, IUserTools $access, |
|
| 123 | + IConfig $config, FilesystemHelper $fs, Image $image, |
|
| 124 | + LogWrapper $log, IAvatarManager $avatarManager, IUserManager $userManager, |
|
| 125 | + INotificationManager $notificationManager) { |
|
| 126 | 126 | |
| 127 | - if ($username === null) { |
|
| 128 | - $log->log("uid for '$dn' must not be null!", Util::ERROR); |
|
| 129 | - throw new \InvalidArgumentException('uid must not be null!'); |
|
| 130 | - } else if ($username === '') { |
|
| 131 | - $log->log("uid for '$dn' must not be an empty string", Util::ERROR); |
|
| 132 | - throw new \InvalidArgumentException('uid must not be an empty string!'); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - $this->access = $access; |
|
| 136 | - $this->connection = $access->getConnection(); |
|
| 137 | - $this->config = $config; |
|
| 138 | - $this->fs = $fs; |
|
| 139 | - $this->dn = $dn; |
|
| 140 | - $this->uid = $username; |
|
| 141 | - $this->image = $image; |
|
| 142 | - $this->log = $log; |
|
| 143 | - $this->avatarManager = $avatarManager; |
|
| 144 | - $this->userManager = $userManager; |
|
| 145 | - $this->notificationManager = $notificationManager; |
|
| 146 | - |
|
| 147 | - \OCP\Util::connectHook('OC_User', 'post_login', $this, 'handlePasswordExpiry'); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * @brief updates properties like email, quota or avatar provided by LDAP |
|
| 152 | - * @return null |
|
| 153 | - */ |
|
| 154 | - public function update() { |
|
| 155 | - if(is_null($this->dn)) { |
|
| 156 | - return null; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - $hasLoggedIn = $this->config->getUserValue($this->uid, 'user_ldap', |
|
| 160 | - self::USER_PREFKEY_FIRSTLOGIN, 0); |
|
| 161 | - |
|
| 162 | - if($this->needsRefresh()) { |
|
| 163 | - $this->updateEmail(); |
|
| 164 | - $this->updateQuota(); |
|
| 165 | - if($hasLoggedIn !== 0) { |
|
| 166 | - //we do not need to try it, when the user has not been logged in |
|
| 167 | - //before, because the file system will not be ready. |
|
| 168 | - $this->updateAvatar(); |
|
| 169 | - //in order to get an avatar as soon as possible, mark the user |
|
| 170 | - //as refreshed only when updating the avatar did happen |
|
| 171 | - $this->markRefreshTime(); |
|
| 172 | - } |
|
| 173 | - } |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * processes results from LDAP for attributes as returned by getAttributesToRead() |
|
| 178 | - * @param array $ldapEntry the user entry as retrieved from LDAP |
|
| 179 | - */ |
|
| 180 | - public function processAttributes($ldapEntry) { |
|
| 181 | - $this->markRefreshTime(); |
|
| 182 | - //Quota |
|
| 183 | - $attr = strtolower($this->connection->ldapQuotaAttribute); |
|
| 184 | - if(isset($ldapEntry[$attr])) { |
|
| 185 | - $this->updateQuota($ldapEntry[$attr][0]); |
|
| 186 | - } else { |
|
| 187 | - if ($this->connection->ldapQuotaDefault !== '') { |
|
| 188 | - $this->updateQuota(); |
|
| 189 | - } |
|
| 190 | - } |
|
| 191 | - unset($attr); |
|
| 192 | - |
|
| 193 | - //displayName |
|
| 194 | - $displayName = $displayName2 = ''; |
|
| 195 | - $attr = strtolower($this->connection->ldapUserDisplayName); |
|
| 196 | - if(isset($ldapEntry[$attr])) { |
|
| 197 | - $displayName = (string)$ldapEntry[$attr][0]; |
|
| 198 | - } |
|
| 199 | - $attr = strtolower($this->connection->ldapUserDisplayName2); |
|
| 200 | - if(isset($ldapEntry[$attr])) { |
|
| 201 | - $displayName2 = (string)$ldapEntry[$attr][0]; |
|
| 202 | - } |
|
| 203 | - if ($displayName !== '') { |
|
| 204 | - $this->composeAndStoreDisplayName($displayName); |
|
| 205 | - $this->access->cacheUserDisplayName( |
|
| 206 | - $this->getUsername(), |
|
| 207 | - $displayName, |
|
| 208 | - $displayName2 |
|
| 209 | - ); |
|
| 210 | - } |
|
| 211 | - unset($attr); |
|
| 212 | - |
|
| 213 | ||
| 214 | - //email must be stored after displayname, because it would cause a user |
|
| 215 | - //change event that will trigger fetching the display name again |
|
| 216 | - $attr = strtolower($this->connection->ldapEmailAttribute); |
|
| 217 | - if(isset($ldapEntry[$attr])) { |
|
| 218 | - $this->updateEmail($ldapEntry[$attr][0]); |
|
| 219 | - } |
|
| 220 | - unset($attr); |
|
| 221 | - |
|
| 222 | - // LDAP Username, needed for s2s sharing |
|
| 223 | - if(isset($ldapEntry['uid'])) { |
|
| 224 | - $this->storeLDAPUserName($ldapEntry['uid'][0]); |
|
| 225 | - } else if(isset($ldapEntry['samaccountname'])) { |
|
| 226 | - $this->storeLDAPUserName($ldapEntry['samaccountname'][0]); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - //homePath |
|
| 230 | - if(strpos($this->connection->homeFolderNamingRule, 'attr:') === 0) { |
|
| 231 | - $attr = strtolower(substr($this->connection->homeFolderNamingRule, strlen('attr:'))); |
|
| 232 | - if(isset($ldapEntry[$attr])) { |
|
| 233 | - $this->access->cacheUserHome( |
|
| 234 | - $this->getUsername(), $this->getHomePath($ldapEntry[$attr][0])); |
|
| 235 | - } |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - //memberOf groups |
|
| 239 | - $cacheKey = 'getMemberOf'.$this->getUsername(); |
|
| 240 | - $groups = false; |
|
| 241 | - if(isset($ldapEntry['memberof'])) { |
|
| 242 | - $groups = $ldapEntry['memberof']; |
|
| 243 | - } |
|
| 244 | - $this->connection->writeToCache($cacheKey, $groups); |
|
| 245 | - |
|
| 246 | - //Avatar |
|
| 247 | - $attrs = array('jpegphoto', 'thumbnailphoto'); |
|
| 248 | - foreach ($attrs as $attr) { |
|
| 249 | - if(isset($ldapEntry[$attr])) { |
|
| 250 | - $this->avatarImage = $ldapEntry[$attr][0]; |
|
| 251 | - // the call to the method that saves the avatar in the file |
|
| 252 | - // system must be postponed after the login. It is to ensure |
|
| 253 | - // external mounts are mounted properly (e.g. with login |
|
| 254 | - // credentials from the session). |
|
| 255 | - \OCP\Util::connectHook('OC_User', 'post_login', $this, 'updateAvatarPostLogin'); |
|
| 256 | - break; |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * @brief returns the LDAP DN of the user |
|
| 263 | - * @return string |
|
| 264 | - */ |
|
| 265 | - public function getDN() { |
|
| 266 | - return $this->dn; |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - /** |
|
| 270 | - * @brief returns the Nextcloud internal username of the user |
|
| 271 | - * @return string |
|
| 272 | - */ |
|
| 273 | - public function getUsername() { |
|
| 274 | - return $this->uid; |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * returns the home directory of the user if specified by LDAP settings |
|
| 279 | - * @param string $valueFromLDAP |
|
| 280 | - * @return bool|string |
|
| 281 | - * @throws \Exception |
|
| 282 | - */ |
|
| 283 | - public function getHomePath($valueFromLDAP = null) { |
|
| 284 | - $path = (string)$valueFromLDAP; |
|
| 285 | - $attr = null; |
|
| 286 | - |
|
| 287 | - if (is_null($valueFromLDAP) |
|
| 288 | - && strpos($this->access->connection->homeFolderNamingRule, 'attr:') === 0 |
|
| 289 | - && $this->access->connection->homeFolderNamingRule !== 'attr:') |
|
| 290 | - { |
|
| 291 | - $attr = substr($this->access->connection->homeFolderNamingRule, strlen('attr:')); |
|
| 292 | - $homedir = $this->access->readAttribute( |
|
| 293 | - $this->access->username2dn($this->getUsername()), $attr); |
|
| 294 | - if ($homedir && isset($homedir[0])) { |
|
| 295 | - $path = $homedir[0]; |
|
| 296 | - } |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - if ($path !== '') { |
|
| 300 | - //if attribute's value is an absolute path take this, otherwise append it to data dir |
|
| 301 | - //check for / at the beginning or pattern c:\ resp. c:/ |
|
| 302 | - if( '/' !== $path[0] |
|
| 303 | - && !(3 < strlen($path) && ctype_alpha($path[0]) |
|
| 304 | - && $path[1] === ':' && ('\\' === $path[2] || '/' === $path[2])) |
|
| 305 | - ) { |
|
| 306 | - $path = $this->config->getSystemValue('datadirectory', |
|
| 307 | - \OC::$SERVERROOT.'/data' ) . '/' . $path; |
|
| 308 | - } |
|
| 309 | - //we need it to store it in the DB as well in case a user gets |
|
| 310 | - //deleted so we can clean up afterwards |
|
| 311 | - $this->config->setUserValue( |
|
| 312 | - $this->getUsername(), 'user_ldap', 'homePath', $path |
|
| 313 | - ); |
|
| 314 | - return $path; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - if( !is_null($attr) |
|
| 318 | - && $this->config->getAppValue('user_ldap', 'enforce_home_folder_naming_rule', true) |
|
| 319 | - ) { |
|
| 320 | - // a naming rule attribute is defined, but it doesn't exist for that LDAP user |
|
| 321 | - throw new \Exception('Home dir attribute can\'t be read from LDAP for uid: ' . $this->getUsername()); |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - //false will apply default behaviour as defined and done by OC_User |
|
| 325 | - $this->config->setUserValue($this->getUsername(), 'user_ldap', 'homePath', ''); |
|
| 326 | - return false; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - public function getMemberOfGroups() { |
|
| 330 | - $cacheKey = 'getMemberOf'.$this->getUsername(); |
|
| 331 | - $memberOfGroups = $this->connection->getFromCache($cacheKey); |
|
| 332 | - if(!is_null($memberOfGroups)) { |
|
| 333 | - return $memberOfGroups; |
|
| 334 | - } |
|
| 335 | - $groupDNs = $this->access->readAttribute($this->getDN(), 'memberOf'); |
|
| 336 | - $this->connection->writeToCache($cacheKey, $groupDNs); |
|
| 337 | - return $groupDNs; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * @brief reads the image from LDAP that shall be used as Avatar |
|
| 342 | - * @return string data (provided by LDAP) | false |
|
| 343 | - */ |
|
| 344 | - public function getAvatarImage() { |
|
| 345 | - if(!is_null($this->avatarImage)) { |
|
| 346 | - return $this->avatarImage; |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - $this->avatarImage = false; |
|
| 350 | - $attributes = array('jpegPhoto', 'thumbnailPhoto'); |
|
| 351 | - foreach($attributes as $attribute) { |
|
| 352 | - $result = $this->access->readAttribute($this->dn, $attribute); |
|
| 353 | - if($result !== false && is_array($result) && isset($result[0])) { |
|
| 354 | - $this->avatarImage = $result[0]; |
|
| 355 | - break; |
|
| 356 | - } |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - return $this->avatarImage; |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * @brief marks the user as having logged in at least once |
|
| 364 | - * @return null |
|
| 365 | - */ |
|
| 366 | - public function markLogin() { |
|
| 367 | - $this->config->setUserValue( |
|
| 368 | - $this->uid, 'user_ldap', self::USER_PREFKEY_FIRSTLOGIN, 1); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * @brief marks the time when user features like email have been updated |
|
| 373 | - * @return null |
|
| 374 | - */ |
|
| 375 | - public function markRefreshTime() { |
|
| 376 | - $this->config->setUserValue( |
|
| 377 | - $this->uid, 'user_ldap', self::USER_PREFKEY_LASTREFRESH, time()); |
|
| 378 | - } |
|
| 379 | - |
|
| 380 | - /** |
|
| 381 | - * @brief checks whether user features needs to be updated again by |
|
| 382 | - * comparing the difference of time of the last refresh to now with the |
|
| 383 | - * desired interval |
|
| 384 | - * @return bool |
|
| 385 | - */ |
|
| 386 | - private function needsRefresh() { |
|
| 387 | - $lastChecked = $this->config->getUserValue($this->uid, 'user_ldap', |
|
| 388 | - self::USER_PREFKEY_LASTREFRESH, 0); |
|
| 389 | - |
|
| 390 | - if((time() - (int)$lastChecked) < (int)$this->config->getAppValue('user_ldap', 'updateAttributesInterval', 86400)) { |
|
| 391 | - return false; |
|
| 392 | - } |
|
| 393 | - return true; |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - /** |
|
| 397 | - * Stores a key-value pair in relation to this user |
|
| 398 | - * |
|
| 399 | - * @param string $key |
|
| 400 | - * @param string $value |
|
| 401 | - */ |
|
| 402 | - private function store($key, $value) { |
|
| 403 | - $this->config->setUserValue($this->uid, 'user_ldap', $key, $value); |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - /** |
|
| 407 | - * Composes the display name and stores it in the database. The final |
|
| 408 | - * display name is returned. |
|
| 409 | - * |
|
| 410 | - * @param string $displayName |
|
| 411 | - * @param string $displayName2 |
|
| 412 | - * @returns string the effective display name |
|
| 413 | - */ |
|
| 414 | - public function composeAndStoreDisplayName($displayName, $displayName2 = '') { |
|
| 415 | - $displayName2 = (string)$displayName2; |
|
| 416 | - if($displayName2 !== '') { |
|
| 417 | - $displayName .= ' (' . $displayName2 . ')'; |
|
| 418 | - } |
|
| 419 | - $this->store('displayName', $displayName); |
|
| 420 | - return $displayName; |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * Stores the LDAP Username in the Database |
|
| 425 | - * @param string $userName |
|
| 426 | - */ |
|
| 427 | - public function storeLDAPUserName($userName) { |
|
| 428 | - $this->store('uid', $userName); |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - /** |
|
| 432 | - * @brief checks whether an update method specified by feature was run |
|
| 433 | - * already. If not, it will marked like this, because it is expected that |
|
| 434 | - * the method will be run, when false is returned. |
|
| 435 | - * @param string $feature email | quota | avatar (can be extended) |
|
| 436 | - * @return bool |
|
| 437 | - */ |
|
| 438 | - private function wasRefreshed($feature) { |
|
| 439 | - if(isset($this->refreshedFeatures[$feature])) { |
|
| 440 | - return true; |
|
| 441 | - } |
|
| 442 | - $this->refreshedFeatures[$feature] = 1; |
|
| 443 | - return false; |
|
| 444 | - } |
|
| 445 | - |
|
| 446 | - /** |
|
| 447 | - * fetches the email from LDAP and stores it as Nextcloud user value |
|
| 448 | - * @param string $valueFromLDAP if known, to save an LDAP read request |
|
| 449 | - * @return null |
|
| 450 | - */ |
|
| 451 | - public function updateEmail($valueFromLDAP = null) { |
|
| 452 | - if($this->wasRefreshed('email')) { |
|
| 453 | - return; |
|
| 454 | - } |
|
| 455 | - $email = (string)$valueFromLDAP; |
|
| 456 | - if(is_null($valueFromLDAP)) { |
|
| 457 | - $emailAttribute = $this->connection->ldapEmailAttribute; |
|
| 458 | - if ($emailAttribute !== '') { |
|
| 459 | - $aEmail = $this->access->readAttribute($this->dn, $emailAttribute); |
|
| 460 | - if(is_array($aEmail) && (count($aEmail) > 0)) { |
|
| 461 | - $email = (string)$aEmail[0]; |
|
| 462 | - } |
|
| 463 | - } |
|
| 464 | - } |
|
| 465 | - if ($email !== '') { |
|
| 466 | - $user = $this->userManager->get($this->uid); |
|
| 467 | - if (!is_null($user)) { |
|
| 468 | - $currentEmail = (string)$user->getEMailAddress(); |
|
| 469 | - if ($currentEmail !== $email) { |
|
| 470 | - $user->setEMailAddress($email); |
|
| 471 | - } |
|
| 472 | - } |
|
| 473 | - } |
|
| 474 | - } |
|
| 475 | - |
|
| 476 | - /** |
|
| 477 | - * Overall process goes as follow: |
|
| 478 | - * 1. fetch the quota from LDAP and check if it's parseable with the "verifyQuotaValue" function |
|
| 479 | - * 2. if the value can't be fetched, is empty or not parseable, use the default LDAP quota |
|
| 480 | - * 3. if the default LDAP quota can't be parsed, use the Nextcloud's default quota (use 'default') |
|
| 481 | - * 4. check if the target user exists and set the quota for the user. |
|
| 482 | - * |
|
| 483 | - * In order to improve performance and prevent an unwanted extra LDAP call, the $valueFromLDAP |
|
| 484 | - * parameter can be passed with the value of the attribute. This value will be considered as the |
|
| 485 | - * quota for the user coming from the LDAP server (step 1 of the process) It can be useful to |
|
| 486 | - * fetch all the user's attributes in one call and use the fetched values in this function. |
|
| 487 | - * The expected value for that parameter is a string describing the quota for the user. Valid |
|
| 488 | - * values are 'none' (unlimited), 'default' (the Nextcloud's default quota), '1234' (quota in |
|
| 489 | - * bytes), '1234 MB' (quota in MB - check the \OC_Helper::computerFileSize method for more info) |
|
| 490 | - * |
|
| 491 | - * fetches the quota from LDAP and stores it as Nextcloud user value |
|
| 492 | - * @param string $valueFromLDAP the quota attribute's value can be passed, |
|
| 493 | - * to save the readAttribute request |
|
| 494 | - * @return null |
|
| 495 | - */ |
|
| 496 | - public function updateQuota($valueFromLDAP = null) { |
|
| 497 | - if($this->wasRefreshed('quota')) { |
|
| 498 | - return; |
|
| 499 | - } |
|
| 500 | - |
|
| 501 | - $quota = false; |
|
| 502 | - if(is_null($valueFromLDAP)) { |
|
| 503 | - $quotaAttribute = $this->connection->ldapQuotaAttribute; |
|
| 504 | - if ($quotaAttribute !== '') { |
|
| 505 | - $aQuota = $this->access->readAttribute($this->dn, $quotaAttribute); |
|
| 506 | - if($aQuota && (count($aQuota) > 0)) { |
|
| 507 | - if ($this->verifyQuotaValue($aQuota[0])) { |
|
| 508 | - $quota = $aQuota[0]; |
|
| 509 | - } else { |
|
| 510 | - $this->log->log('not suitable LDAP quota found for user ' . $this->uid . ': [' . $aQuota[0] . ']', \OCP\Util::WARN); |
|
| 511 | - } |
|
| 512 | - } |
|
| 513 | - } |
|
| 514 | - } else { |
|
| 515 | - if ($this->verifyQuotaValue($valueFromLDAP)) { |
|
| 516 | - $quota = $valueFromLDAP; |
|
| 517 | - } else { |
|
| 518 | - $this->log->log('not suitable LDAP quota found for user ' . $this->uid . ': [' . $valueFromLDAP . ']', \OCP\Util::WARN); |
|
| 519 | - } |
|
| 520 | - } |
|
| 521 | - |
|
| 522 | - if ($quota === false) { |
|
| 523 | - // quota not found using the LDAP attribute (or not parseable). Try the default quota |
|
| 524 | - $defaultQuota = $this->connection->ldapQuotaDefault; |
|
| 525 | - if ($this->verifyQuotaValue($defaultQuota)) { |
|
| 526 | - $quota = $defaultQuota; |
|
| 527 | - } |
|
| 528 | - } |
|
| 529 | - |
|
| 530 | - $targetUser = $this->userManager->get($this->uid); |
|
| 531 | - if ($targetUser) { |
|
| 532 | - if($quota !== false) { |
|
| 533 | - $targetUser->setQuota($quota); |
|
| 534 | - } else { |
|
| 535 | - $this->log->log('not suitable default quota found for user ' . $this->uid . ': [' . $defaultQuota . ']', \OCP\Util::WARN); |
|
| 536 | - } |
|
| 537 | - } else { |
|
| 538 | - $this->log->log('trying to set a quota for user ' . $this->uid . ' but the user is missing', \OCP\Util::ERROR); |
|
| 539 | - } |
|
| 540 | - } |
|
| 541 | - |
|
| 542 | - private function verifyQuotaValue($quotaValue) { |
|
| 543 | - return $quotaValue === 'none' || $quotaValue === 'default' || \OC_Helper::computerFileSize($quotaValue) !== false; |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - /** |
|
| 547 | - * called by a post_login hook to save the avatar picture |
|
| 548 | - * |
|
| 549 | - * @param array $params |
|
| 550 | - */ |
|
| 551 | - public function updateAvatarPostLogin($params) { |
|
| 552 | - if(isset($params['uid']) && $params['uid'] === $this->getUsername()) { |
|
| 553 | - $this->updateAvatar(); |
|
| 554 | - } |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - /** |
|
| 558 | - * @brief attempts to get an image from LDAP and sets it as Nextcloud avatar |
|
| 559 | - * @return null |
|
| 560 | - */ |
|
| 561 | - public function updateAvatar() { |
|
| 562 | - if($this->wasRefreshed('avatar')) { |
|
| 563 | - return; |
|
| 564 | - } |
|
| 565 | - $avatarImage = $this->getAvatarImage(); |
|
| 566 | - if($avatarImage === false) { |
|
| 567 | - //not set, nothing left to do; |
|
| 568 | - return; |
|
| 569 | - } |
|
| 570 | - $this->image->loadFromBase64(base64_encode($avatarImage)); |
|
| 571 | - $this->setOwnCloudAvatar(); |
|
| 572 | - } |
|
| 573 | - |
|
| 574 | - /** |
|
| 575 | - * @brief sets an image as Nextcloud avatar |
|
| 576 | - * @return null |
|
| 577 | - */ |
|
| 578 | - private function setOwnCloudAvatar() { |
|
| 579 | - if(!$this->image->valid()) { |
|
| 580 | - $this->log->log('jpegPhoto data invalid for '.$this->dn, \OCP\Util::ERROR); |
|
| 581 | - return; |
|
| 582 | - } |
|
| 583 | - //make sure it is a square and not bigger than 128x128 |
|
| 584 | - $size = min(array($this->image->width(), $this->image->height(), 128)); |
|
| 585 | - if(!$this->image->centerCrop($size)) { |
|
| 586 | - $this->log->log('croping image for avatar failed for '.$this->dn, \OCP\Util::ERROR); |
|
| 587 | - return; |
|
| 588 | - } |
|
| 589 | - |
|
| 590 | - if(!$this->fs->isLoaded()) { |
|
| 591 | - $this->fs->setup($this->uid); |
|
| 592 | - } |
|
| 593 | - |
|
| 594 | - try { |
|
| 595 | - $avatar = $this->avatarManager->getAvatar($this->uid); |
|
| 596 | - $avatar->set($this->image); |
|
| 597 | - } catch (\Exception $e) { |
|
| 598 | - \OC::$server->getLogger()->logException($e, [ |
|
| 599 | - 'message' => 'Could not set avatar for ' . $this->dn, |
|
| 600 | - 'level' => \OCP\Util::INFO, |
|
| 601 | - 'app' => 'user_ldap', |
|
| 602 | - ]); |
|
| 603 | - } |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - /** |
|
| 607 | - * called by a post_login hook to handle password expiry |
|
| 608 | - * |
|
| 609 | - * @param array $params |
|
| 610 | - */ |
|
| 611 | - public function handlePasswordExpiry($params) { |
|
| 612 | - $ppolicyDN = $this->connection->ldapDefaultPPolicyDN; |
|
| 613 | - if (empty($ppolicyDN) || ((int)$this->connection->turnOnPasswordChange !== 1)) { |
|
| 614 | - return;//password expiry handling disabled |
|
| 615 | - } |
|
| 616 | - $uid = $params['uid']; |
|
| 617 | - if(isset($uid) && $uid === $this->getUsername()) { |
|
| 618 | - //retrieve relevant user attributes |
|
| 619 | - $result = $this->access->search('objectclass=*', $this->dn, ['pwdpolicysubentry', 'pwdgraceusetime', 'pwdreset', 'pwdchangedtime']); |
|
| 127 | + if ($username === null) { |
|
| 128 | + $log->log("uid for '$dn' must not be null!", Util::ERROR); |
|
| 129 | + throw new \InvalidArgumentException('uid must not be null!'); |
|
| 130 | + } else if ($username === '') { |
|
| 131 | + $log->log("uid for '$dn' must not be an empty string", Util::ERROR); |
|
| 132 | + throw new \InvalidArgumentException('uid must not be an empty string!'); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + $this->access = $access; |
|
| 136 | + $this->connection = $access->getConnection(); |
|
| 137 | + $this->config = $config; |
|
| 138 | + $this->fs = $fs; |
|
| 139 | + $this->dn = $dn; |
|
| 140 | + $this->uid = $username; |
|
| 141 | + $this->image = $image; |
|
| 142 | + $this->log = $log; |
|
| 143 | + $this->avatarManager = $avatarManager; |
|
| 144 | + $this->userManager = $userManager; |
|
| 145 | + $this->notificationManager = $notificationManager; |
|
| 146 | + |
|
| 147 | + \OCP\Util::connectHook('OC_User', 'post_login', $this, 'handlePasswordExpiry'); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * @brief updates properties like email, quota or avatar provided by LDAP |
|
| 152 | + * @return null |
|
| 153 | + */ |
|
| 154 | + public function update() { |
|
| 155 | + if(is_null($this->dn)) { |
|
| 156 | + return null; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + $hasLoggedIn = $this->config->getUserValue($this->uid, 'user_ldap', |
|
| 160 | + self::USER_PREFKEY_FIRSTLOGIN, 0); |
|
| 161 | + |
|
| 162 | + if($this->needsRefresh()) { |
|
| 163 | + $this->updateEmail(); |
|
| 164 | + $this->updateQuota(); |
|
| 165 | + if($hasLoggedIn !== 0) { |
|
| 166 | + //we do not need to try it, when the user has not been logged in |
|
| 167 | + //before, because the file system will not be ready. |
|
| 168 | + $this->updateAvatar(); |
|
| 169 | + //in order to get an avatar as soon as possible, mark the user |
|
| 170 | + //as refreshed only when updating the avatar did happen |
|
| 171 | + $this->markRefreshTime(); |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * processes results from LDAP for attributes as returned by getAttributesToRead() |
|
| 178 | + * @param array $ldapEntry the user entry as retrieved from LDAP |
|
| 179 | + */ |
|
| 180 | + public function processAttributes($ldapEntry) { |
|
| 181 | + $this->markRefreshTime(); |
|
| 182 | + //Quota |
|
| 183 | + $attr = strtolower($this->connection->ldapQuotaAttribute); |
|
| 184 | + if(isset($ldapEntry[$attr])) { |
|
| 185 | + $this->updateQuota($ldapEntry[$attr][0]); |
|
| 186 | + } else { |
|
| 187 | + if ($this->connection->ldapQuotaDefault !== '') { |
|
| 188 | + $this->updateQuota(); |
|
| 189 | + } |
|
| 190 | + } |
|
| 191 | + unset($attr); |
|
| 192 | + |
|
| 193 | + //displayName |
|
| 194 | + $displayName = $displayName2 = ''; |
|
| 195 | + $attr = strtolower($this->connection->ldapUserDisplayName); |
|
| 196 | + if(isset($ldapEntry[$attr])) { |
|
| 197 | + $displayName = (string)$ldapEntry[$attr][0]; |
|
| 198 | + } |
|
| 199 | + $attr = strtolower($this->connection->ldapUserDisplayName2); |
|
| 200 | + if(isset($ldapEntry[$attr])) { |
|
| 201 | + $displayName2 = (string)$ldapEntry[$attr][0]; |
|
| 202 | + } |
|
| 203 | + if ($displayName !== '') { |
|
| 204 | + $this->composeAndStoreDisplayName($displayName); |
|
| 205 | + $this->access->cacheUserDisplayName( |
|
| 206 | + $this->getUsername(), |
|
| 207 | + $displayName, |
|
| 208 | + $displayName2 |
|
| 209 | + ); |
|
| 210 | + } |
|
| 211 | + unset($attr); |
|
| 212 | + |
|
| 213 | ||
| 214 | + //email must be stored after displayname, because it would cause a user |
|
| 215 | + //change event that will trigger fetching the display name again |
|
| 216 | + $attr = strtolower($this->connection->ldapEmailAttribute); |
|
| 217 | + if(isset($ldapEntry[$attr])) { |
|
| 218 | + $this->updateEmail($ldapEntry[$attr][0]); |
|
| 219 | + } |
|
| 220 | + unset($attr); |
|
| 221 | + |
|
| 222 | + // LDAP Username, needed for s2s sharing |
|
| 223 | + if(isset($ldapEntry['uid'])) { |
|
| 224 | + $this->storeLDAPUserName($ldapEntry['uid'][0]); |
|
| 225 | + } else if(isset($ldapEntry['samaccountname'])) { |
|
| 226 | + $this->storeLDAPUserName($ldapEntry['samaccountname'][0]); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + //homePath |
|
| 230 | + if(strpos($this->connection->homeFolderNamingRule, 'attr:') === 0) { |
|
| 231 | + $attr = strtolower(substr($this->connection->homeFolderNamingRule, strlen('attr:'))); |
|
| 232 | + if(isset($ldapEntry[$attr])) { |
|
| 233 | + $this->access->cacheUserHome( |
|
| 234 | + $this->getUsername(), $this->getHomePath($ldapEntry[$attr][0])); |
|
| 235 | + } |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + //memberOf groups |
|
| 239 | + $cacheKey = 'getMemberOf'.$this->getUsername(); |
|
| 240 | + $groups = false; |
|
| 241 | + if(isset($ldapEntry['memberof'])) { |
|
| 242 | + $groups = $ldapEntry['memberof']; |
|
| 243 | + } |
|
| 244 | + $this->connection->writeToCache($cacheKey, $groups); |
|
| 245 | + |
|
| 246 | + //Avatar |
|
| 247 | + $attrs = array('jpegphoto', 'thumbnailphoto'); |
|
| 248 | + foreach ($attrs as $attr) { |
|
| 249 | + if(isset($ldapEntry[$attr])) { |
|
| 250 | + $this->avatarImage = $ldapEntry[$attr][0]; |
|
| 251 | + // the call to the method that saves the avatar in the file |
|
| 252 | + // system must be postponed after the login. It is to ensure |
|
| 253 | + // external mounts are mounted properly (e.g. with login |
|
| 254 | + // credentials from the session). |
|
| 255 | + \OCP\Util::connectHook('OC_User', 'post_login', $this, 'updateAvatarPostLogin'); |
|
| 256 | + break; |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * @brief returns the LDAP DN of the user |
|
| 263 | + * @return string |
|
| 264 | + */ |
|
| 265 | + public function getDN() { |
|
| 266 | + return $this->dn; |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + /** |
|
| 270 | + * @brief returns the Nextcloud internal username of the user |
|
| 271 | + * @return string |
|
| 272 | + */ |
|
| 273 | + public function getUsername() { |
|
| 274 | + return $this->uid; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * returns the home directory of the user if specified by LDAP settings |
|
| 279 | + * @param string $valueFromLDAP |
|
| 280 | + * @return bool|string |
|
| 281 | + * @throws \Exception |
|
| 282 | + */ |
|
| 283 | + public function getHomePath($valueFromLDAP = null) { |
|
| 284 | + $path = (string)$valueFromLDAP; |
|
| 285 | + $attr = null; |
|
| 286 | + |
|
| 287 | + if (is_null($valueFromLDAP) |
|
| 288 | + && strpos($this->access->connection->homeFolderNamingRule, 'attr:') === 0 |
|
| 289 | + && $this->access->connection->homeFolderNamingRule !== 'attr:') |
|
| 290 | + { |
|
| 291 | + $attr = substr($this->access->connection->homeFolderNamingRule, strlen('attr:')); |
|
| 292 | + $homedir = $this->access->readAttribute( |
|
| 293 | + $this->access->username2dn($this->getUsername()), $attr); |
|
| 294 | + if ($homedir && isset($homedir[0])) { |
|
| 295 | + $path = $homedir[0]; |
|
| 296 | + } |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + if ($path !== '') { |
|
| 300 | + //if attribute's value is an absolute path take this, otherwise append it to data dir |
|
| 301 | + //check for / at the beginning or pattern c:\ resp. c:/ |
|
| 302 | + if( '/' !== $path[0] |
|
| 303 | + && !(3 < strlen($path) && ctype_alpha($path[0]) |
|
| 304 | + && $path[1] === ':' && ('\\' === $path[2] || '/' === $path[2])) |
|
| 305 | + ) { |
|
| 306 | + $path = $this->config->getSystemValue('datadirectory', |
|
| 307 | + \OC::$SERVERROOT.'/data' ) . '/' . $path; |
|
| 308 | + } |
|
| 309 | + //we need it to store it in the DB as well in case a user gets |
|
| 310 | + //deleted so we can clean up afterwards |
|
| 311 | + $this->config->setUserValue( |
|
| 312 | + $this->getUsername(), 'user_ldap', 'homePath', $path |
|
| 313 | + ); |
|
| 314 | + return $path; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + if( !is_null($attr) |
|
| 318 | + && $this->config->getAppValue('user_ldap', 'enforce_home_folder_naming_rule', true) |
|
| 319 | + ) { |
|
| 320 | + // a naming rule attribute is defined, but it doesn't exist for that LDAP user |
|
| 321 | + throw new \Exception('Home dir attribute can\'t be read from LDAP for uid: ' . $this->getUsername()); |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + //false will apply default behaviour as defined and done by OC_User |
|
| 325 | + $this->config->setUserValue($this->getUsername(), 'user_ldap', 'homePath', ''); |
|
| 326 | + return false; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + public function getMemberOfGroups() { |
|
| 330 | + $cacheKey = 'getMemberOf'.$this->getUsername(); |
|
| 331 | + $memberOfGroups = $this->connection->getFromCache($cacheKey); |
|
| 332 | + if(!is_null($memberOfGroups)) { |
|
| 333 | + return $memberOfGroups; |
|
| 334 | + } |
|
| 335 | + $groupDNs = $this->access->readAttribute($this->getDN(), 'memberOf'); |
|
| 336 | + $this->connection->writeToCache($cacheKey, $groupDNs); |
|
| 337 | + return $groupDNs; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * @brief reads the image from LDAP that shall be used as Avatar |
|
| 342 | + * @return string data (provided by LDAP) | false |
|
| 343 | + */ |
|
| 344 | + public function getAvatarImage() { |
|
| 345 | + if(!is_null($this->avatarImage)) { |
|
| 346 | + return $this->avatarImage; |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + $this->avatarImage = false; |
|
| 350 | + $attributes = array('jpegPhoto', 'thumbnailPhoto'); |
|
| 351 | + foreach($attributes as $attribute) { |
|
| 352 | + $result = $this->access->readAttribute($this->dn, $attribute); |
|
| 353 | + if($result !== false && is_array($result) && isset($result[0])) { |
|
| 354 | + $this->avatarImage = $result[0]; |
|
| 355 | + break; |
|
| 356 | + } |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + return $this->avatarImage; |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * @brief marks the user as having logged in at least once |
|
| 364 | + * @return null |
|
| 365 | + */ |
|
| 366 | + public function markLogin() { |
|
| 367 | + $this->config->setUserValue( |
|
| 368 | + $this->uid, 'user_ldap', self::USER_PREFKEY_FIRSTLOGIN, 1); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * @brief marks the time when user features like email have been updated |
|
| 373 | + * @return null |
|
| 374 | + */ |
|
| 375 | + public function markRefreshTime() { |
|
| 376 | + $this->config->setUserValue( |
|
| 377 | + $this->uid, 'user_ldap', self::USER_PREFKEY_LASTREFRESH, time()); |
|
| 378 | + } |
|
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * @brief checks whether user features needs to be updated again by |
|
| 382 | + * comparing the difference of time of the last refresh to now with the |
|
| 383 | + * desired interval |
|
| 384 | + * @return bool |
|
| 385 | + */ |
|
| 386 | + private function needsRefresh() { |
|
| 387 | + $lastChecked = $this->config->getUserValue($this->uid, 'user_ldap', |
|
| 388 | + self::USER_PREFKEY_LASTREFRESH, 0); |
|
| 389 | + |
|
| 390 | + if((time() - (int)$lastChecked) < (int)$this->config->getAppValue('user_ldap', 'updateAttributesInterval', 86400)) { |
|
| 391 | + return false; |
|
| 392 | + } |
|
| 393 | + return true; |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + /** |
|
| 397 | + * Stores a key-value pair in relation to this user |
|
| 398 | + * |
|
| 399 | + * @param string $key |
|
| 400 | + * @param string $value |
|
| 401 | + */ |
|
| 402 | + private function store($key, $value) { |
|
| 403 | + $this->config->setUserValue($this->uid, 'user_ldap', $key, $value); |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + /** |
|
| 407 | + * Composes the display name and stores it in the database. The final |
|
| 408 | + * display name is returned. |
|
| 409 | + * |
|
| 410 | + * @param string $displayName |
|
| 411 | + * @param string $displayName2 |
|
| 412 | + * @returns string the effective display name |
|
| 413 | + */ |
|
| 414 | + public function composeAndStoreDisplayName($displayName, $displayName2 = '') { |
|
| 415 | + $displayName2 = (string)$displayName2; |
|
| 416 | + if($displayName2 !== '') { |
|
| 417 | + $displayName .= ' (' . $displayName2 . ')'; |
|
| 418 | + } |
|
| 419 | + $this->store('displayName', $displayName); |
|
| 420 | + return $displayName; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * Stores the LDAP Username in the Database |
|
| 425 | + * @param string $userName |
|
| 426 | + */ |
|
| 427 | + public function storeLDAPUserName($userName) { |
|
| 428 | + $this->store('uid', $userName); |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + /** |
|
| 432 | + * @brief checks whether an update method specified by feature was run |
|
| 433 | + * already. If not, it will marked like this, because it is expected that |
|
| 434 | + * the method will be run, when false is returned. |
|
| 435 | + * @param string $feature email | quota | avatar (can be extended) |
|
| 436 | + * @return bool |
|
| 437 | + */ |
|
| 438 | + private function wasRefreshed($feature) { |
|
| 439 | + if(isset($this->refreshedFeatures[$feature])) { |
|
| 440 | + return true; |
|
| 441 | + } |
|
| 442 | + $this->refreshedFeatures[$feature] = 1; |
|
| 443 | + return false; |
|
| 444 | + } |
|
| 445 | + |
|
| 446 | + /** |
|
| 447 | + * fetches the email from LDAP and stores it as Nextcloud user value |
|
| 448 | + * @param string $valueFromLDAP if known, to save an LDAP read request |
|
| 449 | + * @return null |
|
| 450 | + */ |
|
| 451 | + public function updateEmail($valueFromLDAP = null) { |
|
| 452 | + if($this->wasRefreshed('email')) { |
|
| 453 | + return; |
|
| 454 | + } |
|
| 455 | + $email = (string)$valueFromLDAP; |
|
| 456 | + if(is_null($valueFromLDAP)) { |
|
| 457 | + $emailAttribute = $this->connection->ldapEmailAttribute; |
|
| 458 | + if ($emailAttribute !== '') { |
|
| 459 | + $aEmail = $this->access->readAttribute($this->dn, $emailAttribute); |
|
| 460 | + if(is_array($aEmail) && (count($aEmail) > 0)) { |
|
| 461 | + $email = (string)$aEmail[0]; |
|
| 462 | + } |
|
| 463 | + } |
|
| 464 | + } |
|
| 465 | + if ($email !== '') { |
|
| 466 | + $user = $this->userManager->get($this->uid); |
|
| 467 | + if (!is_null($user)) { |
|
| 468 | + $currentEmail = (string)$user->getEMailAddress(); |
|
| 469 | + if ($currentEmail !== $email) { |
|
| 470 | + $user->setEMailAddress($email); |
|
| 471 | + } |
|
| 472 | + } |
|
| 473 | + } |
|
| 474 | + } |
|
| 475 | + |
|
| 476 | + /** |
|
| 477 | + * Overall process goes as follow: |
|
| 478 | + * 1. fetch the quota from LDAP and check if it's parseable with the "verifyQuotaValue" function |
|
| 479 | + * 2. if the value can't be fetched, is empty or not parseable, use the default LDAP quota |
|
| 480 | + * 3. if the default LDAP quota can't be parsed, use the Nextcloud's default quota (use 'default') |
|
| 481 | + * 4. check if the target user exists and set the quota for the user. |
|
| 482 | + * |
|
| 483 | + * In order to improve performance and prevent an unwanted extra LDAP call, the $valueFromLDAP |
|
| 484 | + * parameter can be passed with the value of the attribute. This value will be considered as the |
|
| 485 | + * quota for the user coming from the LDAP server (step 1 of the process) It can be useful to |
|
| 486 | + * fetch all the user's attributes in one call and use the fetched values in this function. |
|
| 487 | + * The expected value for that parameter is a string describing the quota for the user. Valid |
|
| 488 | + * values are 'none' (unlimited), 'default' (the Nextcloud's default quota), '1234' (quota in |
|
| 489 | + * bytes), '1234 MB' (quota in MB - check the \OC_Helper::computerFileSize method for more info) |
|
| 490 | + * |
|
| 491 | + * fetches the quota from LDAP and stores it as Nextcloud user value |
|
| 492 | + * @param string $valueFromLDAP the quota attribute's value can be passed, |
|
| 493 | + * to save the readAttribute request |
|
| 494 | + * @return null |
|
| 495 | + */ |
|
| 496 | + public function updateQuota($valueFromLDAP = null) { |
|
| 497 | + if($this->wasRefreshed('quota')) { |
|
| 498 | + return; |
|
| 499 | + } |
|
| 500 | + |
|
| 501 | + $quota = false; |
|
| 502 | + if(is_null($valueFromLDAP)) { |
|
| 503 | + $quotaAttribute = $this->connection->ldapQuotaAttribute; |
|
| 504 | + if ($quotaAttribute !== '') { |
|
| 505 | + $aQuota = $this->access->readAttribute($this->dn, $quotaAttribute); |
|
| 506 | + if($aQuota && (count($aQuota) > 0)) { |
|
| 507 | + if ($this->verifyQuotaValue($aQuota[0])) { |
|
| 508 | + $quota = $aQuota[0]; |
|
| 509 | + } else { |
|
| 510 | + $this->log->log('not suitable LDAP quota found for user ' . $this->uid . ': [' . $aQuota[0] . ']', \OCP\Util::WARN); |
|
| 511 | + } |
|
| 512 | + } |
|
| 513 | + } |
|
| 514 | + } else { |
|
| 515 | + if ($this->verifyQuotaValue($valueFromLDAP)) { |
|
| 516 | + $quota = $valueFromLDAP; |
|
| 517 | + } else { |
|
| 518 | + $this->log->log('not suitable LDAP quota found for user ' . $this->uid . ': [' . $valueFromLDAP . ']', \OCP\Util::WARN); |
|
| 519 | + } |
|
| 520 | + } |
|
| 521 | + |
|
| 522 | + if ($quota === false) { |
|
| 523 | + // quota not found using the LDAP attribute (or not parseable). Try the default quota |
|
| 524 | + $defaultQuota = $this->connection->ldapQuotaDefault; |
|
| 525 | + if ($this->verifyQuotaValue($defaultQuota)) { |
|
| 526 | + $quota = $defaultQuota; |
|
| 527 | + } |
|
| 528 | + } |
|
| 529 | + |
|
| 530 | + $targetUser = $this->userManager->get($this->uid); |
|
| 531 | + if ($targetUser) { |
|
| 532 | + if($quota !== false) { |
|
| 533 | + $targetUser->setQuota($quota); |
|
| 534 | + } else { |
|
| 535 | + $this->log->log('not suitable default quota found for user ' . $this->uid . ': [' . $defaultQuota . ']', \OCP\Util::WARN); |
|
| 536 | + } |
|
| 537 | + } else { |
|
| 538 | + $this->log->log('trying to set a quota for user ' . $this->uid . ' but the user is missing', \OCP\Util::ERROR); |
|
| 539 | + } |
|
| 540 | + } |
|
| 541 | + |
|
| 542 | + private function verifyQuotaValue($quotaValue) { |
|
| 543 | + return $quotaValue === 'none' || $quotaValue === 'default' || \OC_Helper::computerFileSize($quotaValue) !== false; |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + /** |
|
| 547 | + * called by a post_login hook to save the avatar picture |
|
| 548 | + * |
|
| 549 | + * @param array $params |
|
| 550 | + */ |
|
| 551 | + public function updateAvatarPostLogin($params) { |
|
| 552 | + if(isset($params['uid']) && $params['uid'] === $this->getUsername()) { |
|
| 553 | + $this->updateAvatar(); |
|
| 554 | + } |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + /** |
|
| 558 | + * @brief attempts to get an image from LDAP and sets it as Nextcloud avatar |
|
| 559 | + * @return null |
|
| 560 | + */ |
|
| 561 | + public function updateAvatar() { |
|
| 562 | + if($this->wasRefreshed('avatar')) { |
|
| 563 | + return; |
|
| 564 | + } |
|
| 565 | + $avatarImage = $this->getAvatarImage(); |
|
| 566 | + if($avatarImage === false) { |
|
| 567 | + //not set, nothing left to do; |
|
| 568 | + return; |
|
| 569 | + } |
|
| 570 | + $this->image->loadFromBase64(base64_encode($avatarImage)); |
|
| 571 | + $this->setOwnCloudAvatar(); |
|
| 572 | + } |
|
| 573 | + |
|
| 574 | + /** |
|
| 575 | + * @brief sets an image as Nextcloud avatar |
|
| 576 | + * @return null |
|
| 577 | + */ |
|
| 578 | + private function setOwnCloudAvatar() { |
|
| 579 | + if(!$this->image->valid()) { |
|
| 580 | + $this->log->log('jpegPhoto data invalid for '.$this->dn, \OCP\Util::ERROR); |
|
| 581 | + return; |
|
| 582 | + } |
|
| 583 | + //make sure it is a square and not bigger than 128x128 |
|
| 584 | + $size = min(array($this->image->width(), $this->image->height(), 128)); |
|
| 585 | + if(!$this->image->centerCrop($size)) { |
|
| 586 | + $this->log->log('croping image for avatar failed for '.$this->dn, \OCP\Util::ERROR); |
|
| 587 | + return; |
|
| 588 | + } |
|
| 589 | + |
|
| 590 | + if(!$this->fs->isLoaded()) { |
|
| 591 | + $this->fs->setup($this->uid); |
|
| 592 | + } |
|
| 593 | + |
|
| 594 | + try { |
|
| 595 | + $avatar = $this->avatarManager->getAvatar($this->uid); |
|
| 596 | + $avatar->set($this->image); |
|
| 597 | + } catch (\Exception $e) { |
|
| 598 | + \OC::$server->getLogger()->logException($e, [ |
|
| 599 | + 'message' => 'Could not set avatar for ' . $this->dn, |
|
| 600 | + 'level' => \OCP\Util::INFO, |
|
| 601 | + 'app' => 'user_ldap', |
|
| 602 | + ]); |
|
| 603 | + } |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + /** |
|
| 607 | + * called by a post_login hook to handle password expiry |
|
| 608 | + * |
|
| 609 | + * @param array $params |
|
| 610 | + */ |
|
| 611 | + public function handlePasswordExpiry($params) { |
|
| 612 | + $ppolicyDN = $this->connection->ldapDefaultPPolicyDN; |
|
| 613 | + if (empty($ppolicyDN) || ((int)$this->connection->turnOnPasswordChange !== 1)) { |
|
| 614 | + return;//password expiry handling disabled |
|
| 615 | + } |
|
| 616 | + $uid = $params['uid']; |
|
| 617 | + if(isset($uid) && $uid === $this->getUsername()) { |
|
| 618 | + //retrieve relevant user attributes |
|
| 619 | + $result = $this->access->search('objectclass=*', $this->dn, ['pwdpolicysubentry', 'pwdgraceusetime', 'pwdreset', 'pwdchangedtime']); |
|
| 620 | 620 | |
| 621 | - if(array_key_exists('pwdpolicysubentry', $result[0])) { |
|
| 622 | - $pwdPolicySubentry = $result[0]['pwdpolicysubentry']; |
|
| 623 | - if($pwdPolicySubentry && (count($pwdPolicySubentry) > 0)){ |
|
| 624 | - $ppolicyDN = $pwdPolicySubentry[0];//custom ppolicy DN |
|
| 625 | - } |
|
| 626 | - } |
|
| 621 | + if(array_key_exists('pwdpolicysubentry', $result[0])) { |
|
| 622 | + $pwdPolicySubentry = $result[0]['pwdpolicysubentry']; |
|
| 623 | + if($pwdPolicySubentry && (count($pwdPolicySubentry) > 0)){ |
|
| 624 | + $ppolicyDN = $pwdPolicySubentry[0];//custom ppolicy DN |
|
| 625 | + } |
|
| 626 | + } |
|
| 627 | 627 | |
| 628 | - $pwdGraceUseTime = array_key_exists('pwdgraceusetime', $result[0]) ? $result[0]['pwdgraceusetime'] : null; |
|
| 629 | - $pwdReset = array_key_exists('pwdreset', $result[0]) ? $result[0]['pwdreset'] : null; |
|
| 630 | - $pwdChangedTime = array_key_exists('pwdchangedtime', $result[0]) ? $result[0]['pwdchangedtime'] : null; |
|
| 628 | + $pwdGraceUseTime = array_key_exists('pwdgraceusetime', $result[0]) ? $result[0]['pwdgraceusetime'] : null; |
|
| 629 | + $pwdReset = array_key_exists('pwdreset', $result[0]) ? $result[0]['pwdreset'] : null; |
|
| 630 | + $pwdChangedTime = array_key_exists('pwdchangedtime', $result[0]) ? $result[0]['pwdchangedtime'] : null; |
|
| 631 | 631 | |
| 632 | - //retrieve relevant password policy attributes |
|
| 633 | - $cacheKey = 'ppolicyAttributes' . $ppolicyDN; |
|
| 634 | - $result = $this->connection->getFromCache($cacheKey); |
|
| 635 | - if(is_null($result)) { |
|
| 636 | - $result = $this->access->search('objectclass=*', $ppolicyDN, ['pwdgraceauthnlimit', 'pwdmaxage', 'pwdexpirewarning']); |
|
| 637 | - $this->connection->writeToCache($cacheKey, $result); |
|
| 638 | - } |
|
| 632 | + //retrieve relevant password policy attributes |
|
| 633 | + $cacheKey = 'ppolicyAttributes' . $ppolicyDN; |
|
| 634 | + $result = $this->connection->getFromCache($cacheKey); |
|
| 635 | + if(is_null($result)) { |
|
| 636 | + $result = $this->access->search('objectclass=*', $ppolicyDN, ['pwdgraceauthnlimit', 'pwdmaxage', 'pwdexpirewarning']); |
|
| 637 | + $this->connection->writeToCache($cacheKey, $result); |
|
| 638 | + } |
|
| 639 | 639 | |
| 640 | - $pwdGraceAuthNLimit = array_key_exists('pwdgraceauthnlimit', $result[0]) ? $result[0]['pwdgraceauthnlimit'] : null; |
|
| 641 | - $pwdMaxAge = array_key_exists('pwdmaxage', $result[0]) ? $result[0]['pwdmaxage'] : null; |
|
| 642 | - $pwdExpireWarning = array_key_exists('pwdexpirewarning', $result[0]) ? $result[0]['pwdexpirewarning'] : null; |
|
| 640 | + $pwdGraceAuthNLimit = array_key_exists('pwdgraceauthnlimit', $result[0]) ? $result[0]['pwdgraceauthnlimit'] : null; |
|
| 641 | + $pwdMaxAge = array_key_exists('pwdmaxage', $result[0]) ? $result[0]['pwdmaxage'] : null; |
|
| 642 | + $pwdExpireWarning = array_key_exists('pwdexpirewarning', $result[0]) ? $result[0]['pwdexpirewarning'] : null; |
|
| 643 | 643 | |
| 644 | - //handle grace login |
|
| 645 | - $pwdGraceUseTimeCount = count($pwdGraceUseTime); |
|
| 646 | - if($pwdGraceUseTime && $pwdGraceUseTimeCount > 0) { //was this a grace login? |
|
| 647 | - if($pwdGraceAuthNLimit |
|
| 648 | - && (count($pwdGraceAuthNLimit) > 0) |
|
| 649 | - &&($pwdGraceUseTimeCount < (int)$pwdGraceAuthNLimit[0])) { //at least one more grace login available? |
|
| 650 | - $this->config->setUserValue($uid, 'user_ldap', 'needsPasswordReset', 'true'); |
|
| 651 | - header('Location: '.\OC::$server->getURLGenerator()->linkToRouteAbsolute( |
|
| 652 | - 'user_ldap.renewPassword.showRenewPasswordForm', array('user' => $uid))); |
|
| 653 | - } else { //no more grace login available |
|
| 654 | - header('Location: '.\OC::$server->getURLGenerator()->linkToRouteAbsolute( |
|
| 655 | - 'user_ldap.renewPassword.showLoginFormInvalidPassword', array('user' => $uid))); |
|
| 656 | - } |
|
| 657 | - exit(); |
|
| 658 | - } |
|
| 659 | - //handle pwdReset attribute |
|
| 660 | - if($pwdReset && (count($pwdReset) > 0) && $pwdReset[0] === 'TRUE') { //user must change his password |
|
| 661 | - $this->config->setUserValue($uid, 'user_ldap', 'needsPasswordReset', 'true'); |
|
| 662 | - header('Location: '.\OC::$server->getURLGenerator()->linkToRouteAbsolute( |
|
| 663 | - 'user_ldap.renewPassword.showRenewPasswordForm', array('user' => $uid))); |
|
| 664 | - exit(); |
|
| 665 | - } |
|
| 666 | - //handle password expiry warning |
|
| 667 | - if($pwdChangedTime && (count($pwdChangedTime) > 0)) { |
|
| 668 | - if($pwdMaxAge && (count($pwdMaxAge) > 0) |
|
| 669 | - && $pwdExpireWarning && (count($pwdExpireWarning) > 0)) { |
|
| 670 | - $pwdMaxAgeInt = (int)$pwdMaxAge[0]; |
|
| 671 | - $pwdExpireWarningInt = (int)$pwdExpireWarning[0]; |
|
| 672 | - if($pwdMaxAgeInt > 0 && $pwdExpireWarningInt > 0){ |
|
| 673 | - $pwdChangedTimeDt = \DateTime::createFromFormat('YmdHisZ', $pwdChangedTime[0]); |
|
| 674 | - $pwdChangedTimeDt->add(new \DateInterval('PT'.$pwdMaxAgeInt.'S')); |
|
| 675 | - $currentDateTime = new \DateTime(); |
|
| 676 | - $secondsToExpiry = $pwdChangedTimeDt->getTimestamp() - $currentDateTime->getTimestamp(); |
|
| 677 | - if($secondsToExpiry <= $pwdExpireWarningInt) { |
|
| 678 | - //remove last password expiry warning if any |
|
| 679 | - $notification = $this->notificationManager->createNotification(); |
|
| 680 | - $notification->setApp('user_ldap') |
|
| 681 | - ->setUser($uid) |
|
| 682 | - ->setObject('pwd_exp_warn', $uid) |
|
| 683 | - ; |
|
| 684 | - $this->notificationManager->markProcessed($notification); |
|
| 685 | - //create new password expiry warning |
|
| 686 | - $notification = $this->notificationManager->createNotification(); |
|
| 687 | - $notification->setApp('user_ldap') |
|
| 688 | - ->setUser($uid) |
|
| 689 | - ->setDateTime($currentDateTime) |
|
| 690 | - ->setObject('pwd_exp_warn', $uid) |
|
| 691 | - ->setSubject('pwd_exp_warn_days', [(int) ceil($secondsToExpiry / 60 / 60 / 24)]) |
|
| 692 | - ; |
|
| 693 | - $this->notificationManager->notify($notification); |
|
| 694 | - } |
|
| 695 | - } |
|
| 696 | - } |
|
| 697 | - } |
|
| 698 | - } |
|
| 699 | - } |
|
| 644 | + //handle grace login |
|
| 645 | + $pwdGraceUseTimeCount = count($pwdGraceUseTime); |
|
| 646 | + if($pwdGraceUseTime && $pwdGraceUseTimeCount > 0) { //was this a grace login? |
|
| 647 | + if($pwdGraceAuthNLimit |
|
| 648 | + && (count($pwdGraceAuthNLimit) > 0) |
|
| 649 | + &&($pwdGraceUseTimeCount < (int)$pwdGraceAuthNLimit[0])) { //at least one more grace login available? |
|
| 650 | + $this->config->setUserValue($uid, 'user_ldap', 'needsPasswordReset', 'true'); |
|
| 651 | + header('Location: '.\OC::$server->getURLGenerator()->linkToRouteAbsolute( |
|
| 652 | + 'user_ldap.renewPassword.showRenewPasswordForm', array('user' => $uid))); |
|
| 653 | + } else { //no more grace login available |
|
| 654 | + header('Location: '.\OC::$server->getURLGenerator()->linkToRouteAbsolute( |
|
| 655 | + 'user_ldap.renewPassword.showLoginFormInvalidPassword', array('user' => $uid))); |
|
| 656 | + } |
|
| 657 | + exit(); |
|
| 658 | + } |
|
| 659 | + //handle pwdReset attribute |
|
| 660 | + if($pwdReset && (count($pwdReset) > 0) && $pwdReset[0] === 'TRUE') { //user must change his password |
|
| 661 | + $this->config->setUserValue($uid, 'user_ldap', 'needsPasswordReset', 'true'); |
|
| 662 | + header('Location: '.\OC::$server->getURLGenerator()->linkToRouteAbsolute( |
|
| 663 | + 'user_ldap.renewPassword.showRenewPasswordForm', array('user' => $uid))); |
|
| 664 | + exit(); |
|
| 665 | + } |
|
| 666 | + //handle password expiry warning |
|
| 667 | + if($pwdChangedTime && (count($pwdChangedTime) > 0)) { |
|
| 668 | + if($pwdMaxAge && (count($pwdMaxAge) > 0) |
|
| 669 | + && $pwdExpireWarning && (count($pwdExpireWarning) > 0)) { |
|
| 670 | + $pwdMaxAgeInt = (int)$pwdMaxAge[0]; |
|
| 671 | + $pwdExpireWarningInt = (int)$pwdExpireWarning[0]; |
|
| 672 | + if($pwdMaxAgeInt > 0 && $pwdExpireWarningInt > 0){ |
|
| 673 | + $pwdChangedTimeDt = \DateTime::createFromFormat('YmdHisZ', $pwdChangedTime[0]); |
|
| 674 | + $pwdChangedTimeDt->add(new \DateInterval('PT'.$pwdMaxAgeInt.'S')); |
|
| 675 | + $currentDateTime = new \DateTime(); |
|
| 676 | + $secondsToExpiry = $pwdChangedTimeDt->getTimestamp() - $currentDateTime->getTimestamp(); |
|
| 677 | + if($secondsToExpiry <= $pwdExpireWarningInt) { |
|
| 678 | + //remove last password expiry warning if any |
|
| 679 | + $notification = $this->notificationManager->createNotification(); |
|
| 680 | + $notification->setApp('user_ldap') |
|
| 681 | + ->setUser($uid) |
|
| 682 | + ->setObject('pwd_exp_warn', $uid) |
|
| 683 | + ; |
|
| 684 | + $this->notificationManager->markProcessed($notification); |
|
| 685 | + //create new password expiry warning |
|
| 686 | + $notification = $this->notificationManager->createNotification(); |
|
| 687 | + $notification->setApp('user_ldap') |
|
| 688 | + ->setUser($uid) |
|
| 689 | + ->setDateTime($currentDateTime) |
|
| 690 | + ->setObject('pwd_exp_warn', $uid) |
|
| 691 | + ->setSubject('pwd_exp_warn_days', [(int) ceil($secondsToExpiry / 60 / 60 / 24)]) |
|
| 692 | + ; |
|
| 693 | + $this->notificationManager->notify($notification); |
|
| 694 | + } |
|
| 695 | + } |
|
| 696 | + } |
|
| 697 | + } |
|
| 698 | + } |
|
| 699 | + } |
|
| 700 | 700 | } |
@@ -152,17 +152,17 @@ discard block |
||
| 152 | 152 | * @return null |
| 153 | 153 | */ |
| 154 | 154 | public function update() { |
| 155 | - if(is_null($this->dn)) { |
|
| 155 | + if (is_null($this->dn)) { |
|
| 156 | 156 | return null; |
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | $hasLoggedIn = $this->config->getUserValue($this->uid, 'user_ldap', |
| 160 | 160 | self::USER_PREFKEY_FIRSTLOGIN, 0); |
| 161 | 161 | |
| 162 | - if($this->needsRefresh()) { |
|
| 162 | + if ($this->needsRefresh()) { |
|
| 163 | 163 | $this->updateEmail(); |
| 164 | 164 | $this->updateQuota(); |
| 165 | - if($hasLoggedIn !== 0) { |
|
| 165 | + if ($hasLoggedIn !== 0) { |
|
| 166 | 166 | //we do not need to try it, when the user has not been logged in |
| 167 | 167 | //before, because the file system will not be ready. |
| 168 | 168 | $this->updateAvatar(); |
@@ -181,7 +181,7 @@ discard block |
||
| 181 | 181 | $this->markRefreshTime(); |
| 182 | 182 | //Quota |
| 183 | 183 | $attr = strtolower($this->connection->ldapQuotaAttribute); |
| 184 | - if(isset($ldapEntry[$attr])) { |
|
| 184 | + if (isset($ldapEntry[$attr])) { |
|
| 185 | 185 | $this->updateQuota($ldapEntry[$attr][0]); |
| 186 | 186 | } else { |
| 187 | 187 | if ($this->connection->ldapQuotaDefault !== '') { |
@@ -193,12 +193,12 @@ discard block |
||
| 193 | 193 | //displayName |
| 194 | 194 | $displayName = $displayName2 = ''; |
| 195 | 195 | $attr = strtolower($this->connection->ldapUserDisplayName); |
| 196 | - if(isset($ldapEntry[$attr])) { |
|
| 197 | - $displayName = (string)$ldapEntry[$attr][0]; |
|
| 196 | + if (isset($ldapEntry[$attr])) { |
|
| 197 | + $displayName = (string) $ldapEntry[$attr][0]; |
|
| 198 | 198 | } |
| 199 | 199 | $attr = strtolower($this->connection->ldapUserDisplayName2); |
| 200 | - if(isset($ldapEntry[$attr])) { |
|
| 201 | - $displayName2 = (string)$ldapEntry[$attr][0]; |
|
| 200 | + if (isset($ldapEntry[$attr])) { |
|
| 201 | + $displayName2 = (string) $ldapEntry[$attr][0]; |
|
| 202 | 202 | } |
| 203 | 203 | if ($displayName !== '') { |
| 204 | 204 | $this->composeAndStoreDisplayName($displayName); |
@@ -214,22 +214,22 @@ discard block |
||
| 214 | 214 | //email must be stored after displayname, because it would cause a user |
| 215 | 215 | //change event that will trigger fetching the display name again |
| 216 | 216 | $attr = strtolower($this->connection->ldapEmailAttribute); |
| 217 | - if(isset($ldapEntry[$attr])) { |
|
| 217 | + if (isset($ldapEntry[$attr])) { |
|
| 218 | 218 | $this->updateEmail($ldapEntry[$attr][0]); |
| 219 | 219 | } |
| 220 | 220 | unset($attr); |
| 221 | 221 | |
| 222 | 222 | // LDAP Username, needed for s2s sharing |
| 223 | - if(isset($ldapEntry['uid'])) { |
|
| 223 | + if (isset($ldapEntry['uid'])) { |
|
| 224 | 224 | $this->storeLDAPUserName($ldapEntry['uid'][0]); |
| 225 | - } else if(isset($ldapEntry['samaccountname'])) { |
|
| 225 | + } else if (isset($ldapEntry['samaccountname'])) { |
|
| 226 | 226 | $this->storeLDAPUserName($ldapEntry['samaccountname'][0]); |
| 227 | 227 | } |
| 228 | 228 | |
| 229 | 229 | //homePath |
| 230 | - if(strpos($this->connection->homeFolderNamingRule, 'attr:') === 0) { |
|
| 230 | + if (strpos($this->connection->homeFolderNamingRule, 'attr:') === 0) { |
|
| 231 | 231 | $attr = strtolower(substr($this->connection->homeFolderNamingRule, strlen('attr:'))); |
| 232 | - if(isset($ldapEntry[$attr])) { |
|
| 232 | + if (isset($ldapEntry[$attr])) { |
|
| 233 | 233 | $this->access->cacheUserHome( |
| 234 | 234 | $this->getUsername(), $this->getHomePath($ldapEntry[$attr][0])); |
| 235 | 235 | } |
@@ -238,15 +238,15 @@ discard block |
||
| 238 | 238 | //memberOf groups |
| 239 | 239 | $cacheKey = 'getMemberOf'.$this->getUsername(); |
| 240 | 240 | $groups = false; |
| 241 | - if(isset($ldapEntry['memberof'])) { |
|
| 241 | + if (isset($ldapEntry['memberof'])) { |
|
| 242 | 242 | $groups = $ldapEntry['memberof']; |
| 243 | 243 | } |
| 244 | 244 | $this->connection->writeToCache($cacheKey, $groups); |
| 245 | 245 | |
| 246 | 246 | //Avatar |
| 247 | 247 | $attrs = array('jpegphoto', 'thumbnailphoto'); |
| 248 | - foreach ($attrs as $attr) { |
|
| 249 | - if(isset($ldapEntry[$attr])) { |
|
| 248 | + foreach ($attrs as $attr) { |
|
| 249 | + if (isset($ldapEntry[$attr])) { |
|
| 250 | 250 | $this->avatarImage = $ldapEntry[$attr][0]; |
| 251 | 251 | // the call to the method that saves the avatar in the file |
| 252 | 252 | // system must be postponed after the login. It is to ensure |
@@ -281,7 +281,7 @@ discard block |
||
| 281 | 281 | * @throws \Exception |
| 282 | 282 | */ |
| 283 | 283 | public function getHomePath($valueFromLDAP = null) { |
| 284 | - $path = (string)$valueFromLDAP; |
|
| 284 | + $path = (string) $valueFromLDAP; |
|
| 285 | 285 | $attr = null; |
| 286 | 286 | |
| 287 | 287 | if (is_null($valueFromLDAP) |
@@ -299,12 +299,12 @@ discard block |
||
| 299 | 299 | if ($path !== '') { |
| 300 | 300 | //if attribute's value is an absolute path take this, otherwise append it to data dir |
| 301 | 301 | //check for / at the beginning or pattern c:\ resp. c:/ |
| 302 | - if( '/' !== $path[0] |
|
| 302 | + if ('/' !== $path[0] |
|
| 303 | 303 | && !(3 < strlen($path) && ctype_alpha($path[0]) |
| 304 | 304 | && $path[1] === ':' && ('\\' === $path[2] || '/' === $path[2])) |
| 305 | 305 | ) { |
| 306 | 306 | $path = $this->config->getSystemValue('datadirectory', |
| 307 | - \OC::$SERVERROOT.'/data' ) . '/' . $path; |
|
| 307 | + \OC::$SERVERROOT.'/data').'/'.$path; |
|
| 308 | 308 | } |
| 309 | 309 | //we need it to store it in the DB as well in case a user gets |
| 310 | 310 | //deleted so we can clean up afterwards |
@@ -314,11 +314,11 @@ discard block |
||
| 314 | 314 | return $path; |
| 315 | 315 | } |
| 316 | 316 | |
| 317 | - if( !is_null($attr) |
|
| 317 | + if (!is_null($attr) |
|
| 318 | 318 | && $this->config->getAppValue('user_ldap', 'enforce_home_folder_naming_rule', true) |
| 319 | 319 | ) { |
| 320 | 320 | // a naming rule attribute is defined, but it doesn't exist for that LDAP user |
| 321 | - throw new \Exception('Home dir attribute can\'t be read from LDAP for uid: ' . $this->getUsername()); |
|
| 321 | + throw new \Exception('Home dir attribute can\'t be read from LDAP for uid: '.$this->getUsername()); |
|
| 322 | 322 | } |
| 323 | 323 | |
| 324 | 324 | //false will apply default behaviour as defined and done by OC_User |
@@ -329,7 +329,7 @@ discard block |
||
| 329 | 329 | public function getMemberOfGroups() { |
| 330 | 330 | $cacheKey = 'getMemberOf'.$this->getUsername(); |
| 331 | 331 | $memberOfGroups = $this->connection->getFromCache($cacheKey); |
| 332 | - if(!is_null($memberOfGroups)) { |
|
| 332 | + if (!is_null($memberOfGroups)) { |
|
| 333 | 333 | return $memberOfGroups; |
| 334 | 334 | } |
| 335 | 335 | $groupDNs = $this->access->readAttribute($this->getDN(), 'memberOf'); |
@@ -342,15 +342,15 @@ discard block |
||
| 342 | 342 | * @return string data (provided by LDAP) | false |
| 343 | 343 | */ |
| 344 | 344 | public function getAvatarImage() { |
| 345 | - if(!is_null($this->avatarImage)) { |
|
| 345 | + if (!is_null($this->avatarImage)) { |
|
| 346 | 346 | return $this->avatarImage; |
| 347 | 347 | } |
| 348 | 348 | |
| 349 | 349 | $this->avatarImage = false; |
| 350 | 350 | $attributes = array('jpegPhoto', 'thumbnailPhoto'); |
| 351 | - foreach($attributes as $attribute) { |
|
| 351 | + foreach ($attributes as $attribute) { |
|
| 352 | 352 | $result = $this->access->readAttribute($this->dn, $attribute); |
| 353 | - if($result !== false && is_array($result) && isset($result[0])) { |
|
| 353 | + if ($result !== false && is_array($result) && isset($result[0])) { |
|
| 354 | 354 | $this->avatarImage = $result[0]; |
| 355 | 355 | break; |
| 356 | 356 | } |
@@ -387,7 +387,7 @@ discard block |
||
| 387 | 387 | $lastChecked = $this->config->getUserValue($this->uid, 'user_ldap', |
| 388 | 388 | self::USER_PREFKEY_LASTREFRESH, 0); |
| 389 | 389 | |
| 390 | - if((time() - (int)$lastChecked) < (int)$this->config->getAppValue('user_ldap', 'updateAttributesInterval', 86400)) { |
|
| 390 | + if ((time() - (int) $lastChecked) < (int) $this->config->getAppValue('user_ldap', 'updateAttributesInterval', 86400)) { |
|
| 391 | 391 | return false; |
| 392 | 392 | } |
| 393 | 393 | return true; |
@@ -412,9 +412,9 @@ discard block |
||
| 412 | 412 | * @returns string the effective display name |
| 413 | 413 | */ |
| 414 | 414 | public function composeAndStoreDisplayName($displayName, $displayName2 = '') { |
| 415 | - $displayName2 = (string)$displayName2; |
|
| 416 | - if($displayName2 !== '') { |
|
| 417 | - $displayName .= ' (' . $displayName2 . ')'; |
|
| 415 | + $displayName2 = (string) $displayName2; |
|
| 416 | + if ($displayName2 !== '') { |
|
| 417 | + $displayName .= ' ('.$displayName2.')'; |
|
| 418 | 418 | } |
| 419 | 419 | $this->store('displayName', $displayName); |
| 420 | 420 | return $displayName; |
@@ -436,7 +436,7 @@ discard block |
||
| 436 | 436 | * @return bool |
| 437 | 437 | */ |
| 438 | 438 | private function wasRefreshed($feature) { |
| 439 | - if(isset($this->refreshedFeatures[$feature])) { |
|
| 439 | + if (isset($this->refreshedFeatures[$feature])) { |
|
| 440 | 440 | return true; |
| 441 | 441 | } |
| 442 | 442 | $this->refreshedFeatures[$feature] = 1; |
@@ -449,23 +449,23 @@ discard block |
||
| 449 | 449 | * @return null |
| 450 | 450 | */ |
| 451 | 451 | public function updateEmail($valueFromLDAP = null) { |
| 452 | - if($this->wasRefreshed('email')) { |
|
| 452 | + if ($this->wasRefreshed('email')) { |
|
| 453 | 453 | return; |
| 454 | 454 | } |
| 455 | - $email = (string)$valueFromLDAP; |
|
| 456 | - if(is_null($valueFromLDAP)) { |
|
| 455 | + $email = (string) $valueFromLDAP; |
|
| 456 | + if (is_null($valueFromLDAP)) { |
|
| 457 | 457 | $emailAttribute = $this->connection->ldapEmailAttribute; |
| 458 | 458 | if ($emailAttribute !== '') { |
| 459 | 459 | $aEmail = $this->access->readAttribute($this->dn, $emailAttribute); |
| 460 | - if(is_array($aEmail) && (count($aEmail) > 0)) { |
|
| 461 | - $email = (string)$aEmail[0]; |
|
| 460 | + if (is_array($aEmail) && (count($aEmail) > 0)) { |
|
| 461 | + $email = (string) $aEmail[0]; |
|
| 462 | 462 | } |
| 463 | 463 | } |
| 464 | 464 | } |
| 465 | 465 | if ($email !== '') { |
| 466 | 466 | $user = $this->userManager->get($this->uid); |
| 467 | 467 | if (!is_null($user)) { |
| 468 | - $currentEmail = (string)$user->getEMailAddress(); |
|
| 468 | + $currentEmail = (string) $user->getEMailAddress(); |
|
| 469 | 469 | if ($currentEmail !== $email) { |
| 470 | 470 | $user->setEMailAddress($email); |
| 471 | 471 | } |
@@ -494,20 +494,20 @@ discard block |
||
| 494 | 494 | * @return null |
| 495 | 495 | */ |
| 496 | 496 | public function updateQuota($valueFromLDAP = null) { |
| 497 | - if($this->wasRefreshed('quota')) { |
|
| 497 | + if ($this->wasRefreshed('quota')) { |
|
| 498 | 498 | return; |
| 499 | 499 | } |
| 500 | 500 | |
| 501 | 501 | $quota = false; |
| 502 | - if(is_null($valueFromLDAP)) { |
|
| 502 | + if (is_null($valueFromLDAP)) { |
|
| 503 | 503 | $quotaAttribute = $this->connection->ldapQuotaAttribute; |
| 504 | 504 | if ($quotaAttribute !== '') { |
| 505 | 505 | $aQuota = $this->access->readAttribute($this->dn, $quotaAttribute); |
| 506 | - if($aQuota && (count($aQuota) > 0)) { |
|
| 506 | + if ($aQuota && (count($aQuota) > 0)) { |
|
| 507 | 507 | if ($this->verifyQuotaValue($aQuota[0])) { |
| 508 | 508 | $quota = $aQuota[0]; |
| 509 | 509 | } else { |
| 510 | - $this->log->log('not suitable LDAP quota found for user ' . $this->uid . ': [' . $aQuota[0] . ']', \OCP\Util::WARN); |
|
| 510 | + $this->log->log('not suitable LDAP quota found for user '.$this->uid.': ['.$aQuota[0].']', \OCP\Util::WARN); |
|
| 511 | 511 | } |
| 512 | 512 | } |
| 513 | 513 | } |
@@ -515,7 +515,7 @@ discard block |
||
| 515 | 515 | if ($this->verifyQuotaValue($valueFromLDAP)) { |
| 516 | 516 | $quota = $valueFromLDAP; |
| 517 | 517 | } else { |
| 518 | - $this->log->log('not suitable LDAP quota found for user ' . $this->uid . ': [' . $valueFromLDAP . ']', \OCP\Util::WARN); |
|
| 518 | + $this->log->log('not suitable LDAP quota found for user '.$this->uid.': ['.$valueFromLDAP.']', \OCP\Util::WARN); |
|
| 519 | 519 | } |
| 520 | 520 | } |
| 521 | 521 | |
@@ -529,13 +529,13 @@ discard block |
||
| 529 | 529 | |
| 530 | 530 | $targetUser = $this->userManager->get($this->uid); |
| 531 | 531 | if ($targetUser) { |
| 532 | - if($quota !== false) { |
|
| 532 | + if ($quota !== false) { |
|
| 533 | 533 | $targetUser->setQuota($quota); |
| 534 | 534 | } else { |
| 535 | - $this->log->log('not suitable default quota found for user ' . $this->uid . ': [' . $defaultQuota . ']', \OCP\Util::WARN); |
|
| 535 | + $this->log->log('not suitable default quota found for user '.$this->uid.': ['.$defaultQuota.']', \OCP\Util::WARN); |
|
| 536 | 536 | } |
| 537 | 537 | } else { |
| 538 | - $this->log->log('trying to set a quota for user ' . $this->uid . ' but the user is missing', \OCP\Util::ERROR); |
|
| 538 | + $this->log->log('trying to set a quota for user '.$this->uid.' but the user is missing', \OCP\Util::ERROR); |
|
| 539 | 539 | } |
| 540 | 540 | } |
| 541 | 541 | |
@@ -549,7 +549,7 @@ discard block |
||
| 549 | 549 | * @param array $params |
| 550 | 550 | */ |
| 551 | 551 | public function updateAvatarPostLogin($params) { |
| 552 | - if(isset($params['uid']) && $params['uid'] === $this->getUsername()) { |
|
| 552 | + if (isset($params['uid']) && $params['uid'] === $this->getUsername()) { |
|
| 553 | 553 | $this->updateAvatar(); |
| 554 | 554 | } |
| 555 | 555 | } |
@@ -559,11 +559,11 @@ discard block |
||
| 559 | 559 | * @return null |
| 560 | 560 | */ |
| 561 | 561 | public function updateAvatar() { |
| 562 | - if($this->wasRefreshed('avatar')) { |
|
| 562 | + if ($this->wasRefreshed('avatar')) { |
|
| 563 | 563 | return; |
| 564 | 564 | } |
| 565 | 565 | $avatarImage = $this->getAvatarImage(); |
| 566 | - if($avatarImage === false) { |
|
| 566 | + if ($avatarImage === false) { |
|
| 567 | 567 | //not set, nothing left to do; |
| 568 | 568 | return; |
| 569 | 569 | } |
@@ -576,18 +576,18 @@ discard block |
||
| 576 | 576 | * @return null |
| 577 | 577 | */ |
| 578 | 578 | private function setOwnCloudAvatar() { |
| 579 | - if(!$this->image->valid()) { |
|
| 579 | + if (!$this->image->valid()) { |
|
| 580 | 580 | $this->log->log('jpegPhoto data invalid for '.$this->dn, \OCP\Util::ERROR); |
| 581 | 581 | return; |
| 582 | 582 | } |
| 583 | 583 | //make sure it is a square and not bigger than 128x128 |
| 584 | 584 | $size = min(array($this->image->width(), $this->image->height(), 128)); |
| 585 | - if(!$this->image->centerCrop($size)) { |
|
| 585 | + if (!$this->image->centerCrop($size)) { |
|
| 586 | 586 | $this->log->log('croping image for avatar failed for '.$this->dn, \OCP\Util::ERROR); |
| 587 | 587 | return; |
| 588 | 588 | } |
| 589 | 589 | |
| 590 | - if(!$this->fs->isLoaded()) { |
|
| 590 | + if (!$this->fs->isLoaded()) { |
|
| 591 | 591 | $this->fs->setup($this->uid); |
| 592 | 592 | } |
| 593 | 593 | |
@@ -596,7 +596,7 @@ discard block |
||
| 596 | 596 | $avatar->set($this->image); |
| 597 | 597 | } catch (\Exception $e) { |
| 598 | 598 | \OC::$server->getLogger()->logException($e, [ |
| 599 | - 'message' => 'Could not set avatar for ' . $this->dn, |
|
| 599 | + 'message' => 'Could not set avatar for '.$this->dn, |
|
| 600 | 600 | 'level' => \OCP\Util::INFO, |
| 601 | 601 | 'app' => 'user_ldap', |
| 602 | 602 | ]); |
@@ -610,18 +610,18 @@ discard block |
||
| 610 | 610 | */ |
| 611 | 611 | public function handlePasswordExpiry($params) { |
| 612 | 612 | $ppolicyDN = $this->connection->ldapDefaultPPolicyDN; |
| 613 | - if (empty($ppolicyDN) || ((int)$this->connection->turnOnPasswordChange !== 1)) { |
|
| 614 | - return;//password expiry handling disabled |
|
| 613 | + if (empty($ppolicyDN) || ((int) $this->connection->turnOnPasswordChange !== 1)) { |
|
| 614 | + return; //password expiry handling disabled |
|
| 615 | 615 | } |
| 616 | 616 | $uid = $params['uid']; |
| 617 | - if(isset($uid) && $uid === $this->getUsername()) { |
|
| 617 | + if (isset($uid) && $uid === $this->getUsername()) { |
|
| 618 | 618 | //retrieve relevant user attributes |
| 619 | 619 | $result = $this->access->search('objectclass=*', $this->dn, ['pwdpolicysubentry', 'pwdgraceusetime', 'pwdreset', 'pwdchangedtime']); |
| 620 | 620 | |
| 621 | - if(array_key_exists('pwdpolicysubentry', $result[0])) { |
|
| 621 | + if (array_key_exists('pwdpolicysubentry', $result[0])) { |
|
| 622 | 622 | $pwdPolicySubentry = $result[0]['pwdpolicysubentry']; |
| 623 | - if($pwdPolicySubentry && (count($pwdPolicySubentry) > 0)){ |
|
| 624 | - $ppolicyDN = $pwdPolicySubentry[0];//custom ppolicy DN |
|
| 623 | + if ($pwdPolicySubentry && (count($pwdPolicySubentry) > 0)) { |
|
| 624 | + $ppolicyDN = $pwdPolicySubentry[0]; //custom ppolicy DN |
|
| 625 | 625 | } |
| 626 | 626 | } |
| 627 | 627 | |
@@ -630,9 +630,9 @@ discard block |
||
| 630 | 630 | $pwdChangedTime = array_key_exists('pwdchangedtime', $result[0]) ? $result[0]['pwdchangedtime'] : null; |
| 631 | 631 | |
| 632 | 632 | //retrieve relevant password policy attributes |
| 633 | - $cacheKey = 'ppolicyAttributes' . $ppolicyDN; |
|
| 633 | + $cacheKey = 'ppolicyAttributes'.$ppolicyDN; |
|
| 634 | 634 | $result = $this->connection->getFromCache($cacheKey); |
| 635 | - if(is_null($result)) { |
|
| 635 | + if (is_null($result)) { |
|
| 636 | 636 | $result = $this->access->search('objectclass=*', $ppolicyDN, ['pwdgraceauthnlimit', 'pwdmaxage', 'pwdexpirewarning']); |
| 637 | 637 | $this->connection->writeToCache($cacheKey, $result); |
| 638 | 638 | } |
@@ -643,10 +643,10 @@ discard block |
||
| 643 | 643 | |
| 644 | 644 | //handle grace login |
| 645 | 645 | $pwdGraceUseTimeCount = count($pwdGraceUseTime); |
| 646 | - if($pwdGraceUseTime && $pwdGraceUseTimeCount > 0) { //was this a grace login? |
|
| 647 | - if($pwdGraceAuthNLimit |
|
| 646 | + if ($pwdGraceUseTime && $pwdGraceUseTimeCount > 0) { //was this a grace login? |
|
| 647 | + if ($pwdGraceAuthNLimit |
|
| 648 | 648 | && (count($pwdGraceAuthNLimit) > 0) |
| 649 | - &&($pwdGraceUseTimeCount < (int)$pwdGraceAuthNLimit[0])) { //at least one more grace login available? |
|
| 649 | + &&($pwdGraceUseTimeCount < (int) $pwdGraceAuthNLimit[0])) { //at least one more grace login available? |
|
| 650 | 650 | $this->config->setUserValue($uid, 'user_ldap', 'needsPasswordReset', 'true'); |
| 651 | 651 | header('Location: '.\OC::$server->getURLGenerator()->linkToRouteAbsolute( |
| 652 | 652 | 'user_ldap.renewPassword.showRenewPasswordForm', array('user' => $uid))); |
@@ -657,24 +657,24 @@ discard block |
||
| 657 | 657 | exit(); |
| 658 | 658 | } |
| 659 | 659 | //handle pwdReset attribute |
| 660 | - if($pwdReset && (count($pwdReset) > 0) && $pwdReset[0] === 'TRUE') { //user must change his password |
|
| 660 | + if ($pwdReset && (count($pwdReset) > 0) && $pwdReset[0] === 'TRUE') { //user must change his password |
|
| 661 | 661 | $this->config->setUserValue($uid, 'user_ldap', 'needsPasswordReset', 'true'); |
| 662 | 662 | header('Location: '.\OC::$server->getURLGenerator()->linkToRouteAbsolute( |
| 663 | 663 | 'user_ldap.renewPassword.showRenewPasswordForm', array('user' => $uid))); |
| 664 | 664 | exit(); |
| 665 | 665 | } |
| 666 | 666 | //handle password expiry warning |
| 667 | - if($pwdChangedTime && (count($pwdChangedTime) > 0)) { |
|
| 668 | - if($pwdMaxAge && (count($pwdMaxAge) > 0) |
|
| 667 | + if ($pwdChangedTime && (count($pwdChangedTime) > 0)) { |
|
| 668 | + if ($pwdMaxAge && (count($pwdMaxAge) > 0) |
|
| 669 | 669 | && $pwdExpireWarning && (count($pwdExpireWarning) > 0)) { |
| 670 | - $pwdMaxAgeInt = (int)$pwdMaxAge[0]; |
|
| 671 | - $pwdExpireWarningInt = (int)$pwdExpireWarning[0]; |
|
| 672 | - if($pwdMaxAgeInt > 0 && $pwdExpireWarningInt > 0){ |
|
| 670 | + $pwdMaxAgeInt = (int) $pwdMaxAge[0]; |
|
| 671 | + $pwdExpireWarningInt = (int) $pwdExpireWarning[0]; |
|
| 672 | + if ($pwdMaxAgeInt > 0 && $pwdExpireWarningInt > 0) { |
|
| 673 | 673 | $pwdChangedTimeDt = \DateTime::createFromFormat('YmdHisZ', $pwdChangedTime[0]); |
| 674 | 674 | $pwdChangedTimeDt->add(new \DateInterval('PT'.$pwdMaxAgeInt.'S')); |
| 675 | 675 | $currentDateTime = new \DateTime(); |
| 676 | 676 | $secondsToExpiry = $pwdChangedTimeDt->getTimestamp() - $currentDateTime->getTimestamp(); |
| 677 | - if($secondsToExpiry <= $pwdExpireWarningInt) { |
|
| 677 | + if ($secondsToExpiry <= $pwdExpireWarningInt) { |
|
| 678 | 678 | //remove last password expiry warning if any |
| 679 | 679 | $notification = $this->notificationManager->createNotification(); |
| 680 | 680 | $notification->setApp('user_ldap') |
@@ -45,1148 +45,1148 @@ |
||
| 45 | 45 | use OCP\GroupInterface; |
| 46 | 46 | |
| 47 | 47 | class Group_LDAP extends BackendUtility implements \OCP\GroupInterface, IGroupLDAP { |
| 48 | - protected $enabled = false; |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @var string[] $cachedGroupMembers array of users with gid as key |
|
| 52 | - */ |
|
| 53 | - protected $cachedGroupMembers; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @var string[] $cachedGroupsByMember array of groups with uid as key |
|
| 57 | - */ |
|
| 58 | - protected $cachedGroupsByMember; |
|
| 59 | - |
|
| 60 | - /** @var GroupPluginManager */ |
|
| 61 | - protected $groupPluginManager; |
|
| 62 | - |
|
| 63 | - public function __construct(Access $access, GroupPluginManager $groupPluginManager) { |
|
| 64 | - parent::__construct($access); |
|
| 65 | - $filter = $this->access->connection->ldapGroupFilter; |
|
| 66 | - $gassoc = $this->access->connection->ldapGroupMemberAssocAttr; |
|
| 67 | - if(!empty($filter) && !empty($gassoc)) { |
|
| 68 | - $this->enabled = true; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - $this->cachedGroupMembers = new CappedMemoryCache(); |
|
| 72 | - $this->cachedGroupsByMember = new CappedMemoryCache(); |
|
| 73 | - $this->groupPluginManager = $groupPluginManager; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * is user in group? |
|
| 78 | - * @param string $uid uid of the user |
|
| 79 | - * @param string $gid gid of the group |
|
| 80 | - * @return bool |
|
| 81 | - * |
|
| 82 | - * Checks whether the user is member of a group or not. |
|
| 83 | - */ |
|
| 84 | - public function inGroup($uid, $gid) { |
|
| 85 | - if(!$this->enabled) { |
|
| 86 | - return false; |
|
| 87 | - } |
|
| 88 | - $cacheKey = 'inGroup'.$uid.':'.$gid; |
|
| 89 | - $inGroup = $this->access->connection->getFromCache($cacheKey); |
|
| 90 | - if(!is_null($inGroup)) { |
|
| 91 | - return (bool)$inGroup; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - $userDN = $this->access->username2dn($uid); |
|
| 95 | - |
|
| 96 | - if(isset($this->cachedGroupMembers[$gid])) { |
|
| 97 | - $isInGroup = in_array($userDN, $this->cachedGroupMembers[$gid]); |
|
| 98 | - return $isInGroup; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - $cacheKeyMembers = 'inGroup-members:'.$gid; |
|
| 102 | - $members = $this->access->connection->getFromCache($cacheKeyMembers); |
|
| 103 | - if(!is_null($members)) { |
|
| 104 | - $this->cachedGroupMembers[$gid] = $members; |
|
| 105 | - $isInGroup = in_array($userDN, $members); |
|
| 106 | - $this->access->connection->writeToCache($cacheKey, $isInGroup); |
|
| 107 | - return $isInGroup; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - $groupDN = $this->access->groupname2dn($gid); |
|
| 111 | - // just in case |
|
| 112 | - if(!$groupDN || !$userDN) { |
|
| 113 | - $this->access->connection->writeToCache($cacheKey, false); |
|
| 114 | - return false; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - //check primary group first |
|
| 118 | - if($gid === $this->getUserPrimaryGroup($userDN)) { |
|
| 119 | - $this->access->connection->writeToCache($cacheKey, true); |
|
| 120 | - return true; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - //usually, LDAP attributes are said to be case insensitive. But there are exceptions of course. |
|
| 124 | - $members = $this->_groupMembers($groupDN); |
|
| 125 | - $members = array_keys($members); // uids are returned as keys |
|
| 126 | - if(!is_array($members) || count($members) === 0) { |
|
| 127 | - $this->access->connection->writeToCache($cacheKey, false); |
|
| 128 | - return false; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - //extra work if we don't get back user DNs |
|
| 132 | - if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
| 133 | - $dns = array(); |
|
| 134 | - $filterParts = array(); |
|
| 135 | - $bytes = 0; |
|
| 136 | - foreach($members as $mid) { |
|
| 137 | - $filter = str_replace('%uid', $mid, $this->access->connection->ldapLoginFilter); |
|
| 138 | - $filterParts[] = $filter; |
|
| 139 | - $bytes += strlen($filter); |
|
| 140 | - if($bytes >= 9000000) { |
|
| 141 | - // AD has a default input buffer of 10 MB, we do not want |
|
| 142 | - // to take even the chance to exceed it |
|
| 143 | - $filter = $this->access->combineFilterWithOr($filterParts); |
|
| 144 | - $bytes = 0; |
|
| 145 | - $filterParts = array(); |
|
| 146 | - $users = $this->access->fetchListOfUsers($filter, 'dn', count($filterParts)); |
|
| 147 | - $dns = array_merge($dns, $users); |
|
| 148 | - } |
|
| 149 | - } |
|
| 150 | - if(count($filterParts) > 0) { |
|
| 151 | - $filter = $this->access->combineFilterWithOr($filterParts); |
|
| 152 | - $users = $this->access->fetchListOfUsers($filter, 'dn', count($filterParts)); |
|
| 153 | - $dns = array_merge($dns, $users); |
|
| 154 | - } |
|
| 155 | - $members = $dns; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - $isInGroup = in_array($userDN, $members); |
|
| 159 | - $this->access->connection->writeToCache($cacheKey, $isInGroup); |
|
| 160 | - $this->access->connection->writeToCache($cacheKeyMembers, $members); |
|
| 161 | - $this->cachedGroupMembers[$gid] = $members; |
|
| 162 | - |
|
| 163 | - return $isInGroup; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * @param string $dnGroup |
|
| 168 | - * @return array |
|
| 169 | - * |
|
| 170 | - * For a group that has user membership defined by an LDAP search url attribute returns the users |
|
| 171 | - * that match the search url otherwise returns an empty array. |
|
| 172 | - */ |
|
| 173 | - public function getDynamicGroupMembers($dnGroup) { |
|
| 174 | - $dynamicGroupMemberURL = strtolower($this->access->connection->ldapDynamicGroupMemberURL); |
|
| 175 | - |
|
| 176 | - if (empty($dynamicGroupMemberURL)) { |
|
| 177 | - return array(); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - $dynamicMembers = array(); |
|
| 181 | - $memberURLs = $this->access->readAttribute( |
|
| 182 | - $dnGroup, |
|
| 183 | - $dynamicGroupMemberURL, |
|
| 184 | - $this->access->connection->ldapGroupFilter |
|
| 185 | - ); |
|
| 186 | - if ($memberURLs !== false) { |
|
| 187 | - // this group has the 'memberURL' attribute so this is a dynamic group |
|
| 188 | - // example 1: ldap:///cn=users,cn=accounts,dc=dcsubbase,dc=dcbase??one?(o=HeadOffice) |
|
| 189 | - // example 2: ldap:///cn=users,cn=accounts,dc=dcsubbase,dc=dcbase??one?(&(o=HeadOffice)(uidNumber>=500)) |
|
| 190 | - $pos = strpos($memberURLs[0], '('); |
|
| 191 | - if ($pos !== false) { |
|
| 192 | - $memberUrlFilter = substr($memberURLs[0], $pos); |
|
| 193 | - $foundMembers = $this->access->searchUsers($memberUrlFilter,'dn'); |
|
| 194 | - $dynamicMembers = array(); |
|
| 195 | - foreach($foundMembers as $value) { |
|
| 196 | - $dynamicMembers[$value['dn'][0]] = 1; |
|
| 197 | - } |
|
| 198 | - } else { |
|
| 199 | - \OCP\Util::writeLog('user_ldap', 'No search filter found on member url '. |
|
| 200 | - 'of group ' . $dnGroup, \OCP\Util::DEBUG); |
|
| 201 | - } |
|
| 202 | - } |
|
| 203 | - return $dynamicMembers; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * @param string $dnGroup |
|
| 208 | - * @param array|null &$seen |
|
| 209 | - * @return array|mixed|null |
|
| 210 | - */ |
|
| 211 | - private function _groupMembers($dnGroup, &$seen = null) { |
|
| 212 | - if ($seen === null) { |
|
| 213 | - $seen = array(); |
|
| 214 | - } |
|
| 215 | - $allMembers = array(); |
|
| 216 | - if (array_key_exists($dnGroup, $seen)) { |
|
| 217 | - // avoid loops |
|
| 218 | - return array(); |
|
| 219 | - } |
|
| 220 | - // used extensively in cron job, caching makes sense for nested groups |
|
| 221 | - $cacheKey = '_groupMembers'.$dnGroup; |
|
| 222 | - $groupMembers = $this->access->connection->getFromCache($cacheKey); |
|
| 223 | - if(!is_null($groupMembers)) { |
|
| 224 | - return $groupMembers; |
|
| 225 | - } |
|
| 226 | - $seen[$dnGroup] = 1; |
|
| 227 | - $members = $this->access->readAttribute($dnGroup, $this->access->connection->ldapGroupMemberAssocAttr, |
|
| 228 | - $this->access->connection->ldapGroupFilter); |
|
| 229 | - if (is_array($members)) { |
|
| 230 | - foreach ($members as $memberDN) { |
|
| 231 | - $allMembers[$memberDN] = 1; |
|
| 232 | - $nestedGroups = $this->access->connection->ldapNestedGroups; |
|
| 233 | - if (!empty($nestedGroups)) { |
|
| 234 | - $subMembers = $this->_groupMembers($memberDN, $seen); |
|
| 235 | - if ($subMembers) { |
|
| 236 | - $allMembers = array_merge($allMembers, $subMembers); |
|
| 237 | - } |
|
| 238 | - } |
|
| 239 | - } |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - $allMembers = array_merge($allMembers, $this->getDynamicGroupMembers($dnGroup)); |
|
| 243 | - |
|
| 244 | - $this->access->connection->writeToCache($cacheKey, $allMembers); |
|
| 245 | - return $allMembers; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - /** |
|
| 249 | - * @param string $DN |
|
| 250 | - * @param array|null &$seen |
|
| 251 | - * @return array |
|
| 252 | - */ |
|
| 253 | - private function _getGroupDNsFromMemberOf($DN, &$seen = null) { |
|
| 254 | - if ($seen === null) { |
|
| 255 | - $seen = array(); |
|
| 256 | - } |
|
| 257 | - if (array_key_exists($DN, $seen)) { |
|
| 258 | - // avoid loops |
|
| 259 | - return array(); |
|
| 260 | - } |
|
| 261 | - $seen[$DN] = 1; |
|
| 262 | - $groups = $this->access->readAttribute($DN, 'memberOf'); |
|
| 263 | - if (!is_array($groups)) { |
|
| 264 | - return array(); |
|
| 265 | - } |
|
| 266 | - $groups = $this->access->groupsMatchFilter($groups); |
|
| 267 | - $allGroups = $groups; |
|
| 268 | - $nestedGroups = $this->access->connection->ldapNestedGroups; |
|
| 269 | - if ((int)$nestedGroups === 1) { |
|
| 270 | - foreach ($groups as $group) { |
|
| 271 | - $subGroups = $this->_getGroupDNsFromMemberOf($group, $seen); |
|
| 272 | - $allGroups = array_merge($allGroups, $subGroups); |
|
| 273 | - } |
|
| 274 | - } |
|
| 275 | - return $allGroups; |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * translates a gidNumber into an ownCloud internal name |
|
| 280 | - * @param string $gid as given by gidNumber on POSIX LDAP |
|
| 281 | - * @param string $dn a DN that belongs to the same domain as the group |
|
| 282 | - * @return string|bool |
|
| 283 | - */ |
|
| 284 | - public function gidNumber2Name($gid, $dn) { |
|
| 285 | - $cacheKey = 'gidNumberToName' . $gid; |
|
| 286 | - $groupName = $this->access->connection->getFromCache($cacheKey); |
|
| 287 | - if(!is_null($groupName) && isset($groupName)) { |
|
| 288 | - return $groupName; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - //we need to get the DN from LDAP |
|
| 292 | - $filter = $this->access->combineFilterWithAnd([ |
|
| 293 | - $this->access->connection->ldapGroupFilter, |
|
| 294 | - 'objectClass=posixGroup', |
|
| 295 | - $this->access->connection->ldapGidNumber . '=' . $gid |
|
| 296 | - ]); |
|
| 297 | - $result = $this->access->searchGroups($filter, array('dn'), 1); |
|
| 298 | - if(empty($result)) { |
|
| 299 | - return false; |
|
| 300 | - } |
|
| 301 | - $dn = $result[0]['dn'][0]; |
|
| 302 | - |
|
| 303 | - //and now the group name |
|
| 304 | - //NOTE once we have separate ownCloud group IDs and group names we can |
|
| 305 | - //directly read the display name attribute instead of the DN |
|
| 306 | - $name = $this->access->dn2groupname($dn); |
|
| 307 | - |
|
| 308 | - $this->access->connection->writeToCache($cacheKey, $name); |
|
| 309 | - |
|
| 310 | - return $name; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * returns the entry's gidNumber |
|
| 315 | - * @param string $dn |
|
| 316 | - * @param string $attribute |
|
| 317 | - * @return string|bool |
|
| 318 | - */ |
|
| 319 | - private function getEntryGidNumber($dn, $attribute) { |
|
| 320 | - $value = $this->access->readAttribute($dn, $attribute); |
|
| 321 | - if(is_array($value) && !empty($value)) { |
|
| 322 | - return $value[0]; |
|
| 323 | - } |
|
| 324 | - return false; |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - /** |
|
| 328 | - * returns the group's primary ID |
|
| 329 | - * @param string $dn |
|
| 330 | - * @return string|bool |
|
| 331 | - */ |
|
| 332 | - public function getGroupGidNumber($dn) { |
|
| 333 | - return $this->getEntryGidNumber($dn, 'gidNumber'); |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * returns the user's gidNumber |
|
| 338 | - * @param string $dn |
|
| 339 | - * @return string|bool |
|
| 340 | - */ |
|
| 341 | - public function getUserGidNumber($dn) { |
|
| 342 | - $gidNumber = false; |
|
| 343 | - if($this->access->connection->hasGidNumber) { |
|
| 344 | - $gidNumber = $this->getEntryGidNumber($dn, $this->access->connection->ldapGidNumber); |
|
| 345 | - if($gidNumber === false) { |
|
| 346 | - $this->access->connection->hasGidNumber = false; |
|
| 347 | - } |
|
| 348 | - } |
|
| 349 | - return $gidNumber; |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * returns a filter for a "users has specific gid" search or count operation |
|
| 354 | - * |
|
| 355 | - * @param string $groupDN |
|
| 356 | - * @param string $search |
|
| 357 | - * @return string |
|
| 358 | - * @throws \Exception |
|
| 359 | - */ |
|
| 360 | - private function prepareFilterForUsersHasGidNumber($groupDN, $search = '') { |
|
| 361 | - $groupID = $this->getGroupGidNumber($groupDN); |
|
| 362 | - if($groupID === false) { |
|
| 363 | - throw new \Exception('Not a valid group'); |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - $filterParts = []; |
|
| 367 | - $filterParts[] = $this->access->getFilterForUserCount(); |
|
| 368 | - if ($search !== '') { |
|
| 369 | - $filterParts[] = $this->access->getFilterPartForUserSearch($search); |
|
| 370 | - } |
|
| 371 | - $filterParts[] = $this->access->connection->ldapGidNumber .'=' . $groupID; |
|
| 372 | - |
|
| 373 | - $filter = $this->access->combineFilterWithAnd($filterParts); |
|
| 374 | - |
|
| 375 | - return $filter; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * returns a list of users that have the given group as gid number |
|
| 380 | - * |
|
| 381 | - * @param string $groupDN |
|
| 382 | - * @param string $search |
|
| 383 | - * @param int $limit |
|
| 384 | - * @param int $offset |
|
| 385 | - * @return string[] |
|
| 386 | - */ |
|
| 387 | - public function getUsersInGidNumber($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
| 388 | - try { |
|
| 389 | - $filter = $this->prepareFilterForUsersHasGidNumber($groupDN, $search); |
|
| 390 | - $users = $this->access->fetchListOfUsers( |
|
| 391 | - $filter, |
|
| 392 | - [$this->access->connection->ldapUserDisplayName, 'dn'], |
|
| 393 | - $limit, |
|
| 394 | - $offset |
|
| 395 | - ); |
|
| 396 | - return $this->access->nextcloudUserNames($users); |
|
| 397 | - } catch (\Exception $e) { |
|
| 398 | - return []; |
|
| 399 | - } |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - /** |
|
| 403 | - * returns the number of users that have the given group as gid number |
|
| 404 | - * |
|
| 405 | - * @param string $groupDN |
|
| 406 | - * @param string $search |
|
| 407 | - * @param int $limit |
|
| 408 | - * @param int $offset |
|
| 409 | - * @return int |
|
| 410 | - */ |
|
| 411 | - public function countUsersInGidNumber($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
| 412 | - try { |
|
| 413 | - $filter = $this->prepareFilterForUsersHasGidNumber($groupDN, $search); |
|
| 414 | - $users = $this->access->countUsers($filter, ['dn'], $limit, $offset); |
|
| 415 | - return (int)$users; |
|
| 416 | - } catch (\Exception $e) { |
|
| 417 | - return 0; |
|
| 418 | - } |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - /** |
|
| 422 | - * gets the gidNumber of a user |
|
| 423 | - * @param string $dn |
|
| 424 | - * @return string |
|
| 425 | - */ |
|
| 426 | - public function getUserGroupByGid($dn) { |
|
| 427 | - $groupID = $this->getUserGidNumber($dn); |
|
| 428 | - if($groupID !== false) { |
|
| 429 | - $groupName = $this->gidNumber2Name($groupID, $dn); |
|
| 430 | - if($groupName !== false) { |
|
| 431 | - return $groupName; |
|
| 432 | - } |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - return false; |
|
| 436 | - } |
|
| 437 | - |
|
| 438 | - /** |
|
| 439 | - * translates a primary group ID into an Nextcloud internal name |
|
| 440 | - * @param string $gid as given by primaryGroupID on AD |
|
| 441 | - * @param string $dn a DN that belongs to the same domain as the group |
|
| 442 | - * @return string|bool |
|
| 443 | - */ |
|
| 444 | - public function primaryGroupID2Name($gid, $dn) { |
|
| 445 | - $cacheKey = 'primaryGroupIDtoName'; |
|
| 446 | - $groupNames = $this->access->connection->getFromCache($cacheKey); |
|
| 447 | - if(!is_null($groupNames) && isset($groupNames[$gid])) { |
|
| 448 | - return $groupNames[$gid]; |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - $domainObjectSid = $this->access->getSID($dn); |
|
| 452 | - if($domainObjectSid === false) { |
|
| 453 | - return false; |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - //we need to get the DN from LDAP |
|
| 457 | - $filter = $this->access->combineFilterWithAnd(array( |
|
| 458 | - $this->access->connection->ldapGroupFilter, |
|
| 459 | - 'objectsid=' . $domainObjectSid . '-' . $gid |
|
| 460 | - )); |
|
| 461 | - $result = $this->access->searchGroups($filter, array('dn'), 1); |
|
| 462 | - if(empty($result)) { |
|
| 463 | - return false; |
|
| 464 | - } |
|
| 465 | - $dn = $result[0]['dn'][0]; |
|
| 466 | - |
|
| 467 | - //and now the group name |
|
| 468 | - //NOTE once we have separate Nextcloud group IDs and group names we can |
|
| 469 | - //directly read the display name attribute instead of the DN |
|
| 470 | - $name = $this->access->dn2groupname($dn); |
|
| 471 | - |
|
| 472 | - $this->access->connection->writeToCache($cacheKey, $name); |
|
| 473 | - |
|
| 474 | - return $name; |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - /** |
|
| 478 | - * returns the entry's primary group ID |
|
| 479 | - * @param string $dn |
|
| 480 | - * @param string $attribute |
|
| 481 | - * @return string|bool |
|
| 482 | - */ |
|
| 483 | - private function getEntryGroupID($dn, $attribute) { |
|
| 484 | - $value = $this->access->readAttribute($dn, $attribute); |
|
| 485 | - if(is_array($value) && !empty($value)) { |
|
| 486 | - return $value[0]; |
|
| 487 | - } |
|
| 488 | - return false; |
|
| 489 | - } |
|
| 490 | - |
|
| 491 | - /** |
|
| 492 | - * returns the group's primary ID |
|
| 493 | - * @param string $dn |
|
| 494 | - * @return string|bool |
|
| 495 | - */ |
|
| 496 | - public function getGroupPrimaryGroupID($dn) { |
|
| 497 | - return $this->getEntryGroupID($dn, 'primaryGroupToken'); |
|
| 498 | - } |
|
| 499 | - |
|
| 500 | - /** |
|
| 501 | - * returns the user's primary group ID |
|
| 502 | - * @param string $dn |
|
| 503 | - * @return string|bool |
|
| 504 | - */ |
|
| 505 | - public function getUserPrimaryGroupIDs($dn) { |
|
| 506 | - $primaryGroupID = false; |
|
| 507 | - if($this->access->connection->hasPrimaryGroups) { |
|
| 508 | - $primaryGroupID = $this->getEntryGroupID($dn, 'primaryGroupID'); |
|
| 509 | - if($primaryGroupID === false) { |
|
| 510 | - $this->access->connection->hasPrimaryGroups = false; |
|
| 511 | - } |
|
| 512 | - } |
|
| 513 | - return $primaryGroupID; |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - /** |
|
| 517 | - * returns a filter for a "users in primary group" search or count operation |
|
| 518 | - * |
|
| 519 | - * @param string $groupDN |
|
| 520 | - * @param string $search |
|
| 521 | - * @return string |
|
| 522 | - * @throws \Exception |
|
| 523 | - */ |
|
| 524 | - private function prepareFilterForUsersInPrimaryGroup($groupDN, $search = '') { |
|
| 525 | - $groupID = $this->getGroupPrimaryGroupID($groupDN); |
|
| 526 | - if($groupID === false) { |
|
| 527 | - throw new \Exception('Not a valid group'); |
|
| 528 | - } |
|
| 529 | - |
|
| 530 | - $filterParts = []; |
|
| 531 | - $filterParts[] = $this->access->getFilterForUserCount(); |
|
| 532 | - if ($search !== '') { |
|
| 533 | - $filterParts[] = $this->access->getFilterPartForUserSearch($search); |
|
| 534 | - } |
|
| 535 | - $filterParts[] = 'primaryGroupID=' . $groupID; |
|
| 536 | - |
|
| 537 | - $filter = $this->access->combineFilterWithAnd($filterParts); |
|
| 538 | - |
|
| 539 | - return $filter; |
|
| 540 | - } |
|
| 541 | - |
|
| 542 | - /** |
|
| 543 | - * returns a list of users that have the given group as primary group |
|
| 544 | - * |
|
| 545 | - * @param string $groupDN |
|
| 546 | - * @param string $search |
|
| 547 | - * @param int $limit |
|
| 548 | - * @param int $offset |
|
| 549 | - * @return string[] |
|
| 550 | - */ |
|
| 551 | - public function getUsersInPrimaryGroup($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
| 552 | - try { |
|
| 553 | - $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search); |
|
| 554 | - $users = $this->access->fetchListOfUsers( |
|
| 555 | - $filter, |
|
| 556 | - array($this->access->connection->ldapUserDisplayName, 'dn'), |
|
| 557 | - $limit, |
|
| 558 | - $offset |
|
| 559 | - ); |
|
| 560 | - return $this->access->nextcloudUserNames($users); |
|
| 561 | - } catch (\Exception $e) { |
|
| 562 | - return array(); |
|
| 563 | - } |
|
| 564 | - } |
|
| 565 | - |
|
| 566 | - /** |
|
| 567 | - * returns the number of users that have the given group as primary group |
|
| 568 | - * |
|
| 569 | - * @param string $groupDN |
|
| 570 | - * @param string $search |
|
| 571 | - * @param int $limit |
|
| 572 | - * @param int $offset |
|
| 573 | - * @return int |
|
| 574 | - */ |
|
| 575 | - public function countUsersInPrimaryGroup($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
| 576 | - try { |
|
| 577 | - $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search); |
|
| 578 | - $users = $this->access->countUsers($filter, array('dn'), $limit, $offset); |
|
| 579 | - return (int)$users; |
|
| 580 | - } catch (\Exception $e) { |
|
| 581 | - return 0; |
|
| 582 | - } |
|
| 583 | - } |
|
| 584 | - |
|
| 585 | - /** |
|
| 586 | - * gets the primary group of a user |
|
| 587 | - * @param string $dn |
|
| 588 | - * @return string |
|
| 589 | - */ |
|
| 590 | - public function getUserPrimaryGroup($dn) { |
|
| 591 | - $groupID = $this->getUserPrimaryGroupIDs($dn); |
|
| 592 | - if($groupID !== false) { |
|
| 593 | - $groupName = $this->primaryGroupID2Name($groupID, $dn); |
|
| 594 | - if($groupName !== false) { |
|
| 595 | - return $groupName; |
|
| 596 | - } |
|
| 597 | - } |
|
| 598 | - |
|
| 599 | - return false; |
|
| 600 | - } |
|
| 601 | - |
|
| 602 | - /** |
|
| 603 | - * Get all groups a user belongs to |
|
| 604 | - * @param string $uid Name of the user |
|
| 605 | - * @return array with group names |
|
| 606 | - * |
|
| 607 | - * This function fetches all groups a user belongs to. It does not check |
|
| 608 | - * if the user exists at all. |
|
| 609 | - * |
|
| 610 | - * This function includes groups based on dynamic group membership. |
|
| 611 | - */ |
|
| 612 | - public function getUserGroups($uid) { |
|
| 613 | - if(!$this->enabled) { |
|
| 614 | - return array(); |
|
| 615 | - } |
|
| 616 | - $cacheKey = 'getUserGroups'.$uid; |
|
| 617 | - $userGroups = $this->access->connection->getFromCache($cacheKey); |
|
| 618 | - if(!is_null($userGroups)) { |
|
| 619 | - return $userGroups; |
|
| 620 | - } |
|
| 621 | - $userDN = $this->access->username2dn($uid); |
|
| 622 | - if(!$userDN) { |
|
| 623 | - $this->access->connection->writeToCache($cacheKey, array()); |
|
| 624 | - return array(); |
|
| 625 | - } |
|
| 626 | - |
|
| 627 | - $groups = []; |
|
| 628 | - $primaryGroup = $this->getUserPrimaryGroup($userDN); |
|
| 629 | - $gidGroupName = $this->getUserGroupByGid($userDN); |
|
| 630 | - |
|
| 631 | - $dynamicGroupMemberURL = strtolower($this->access->connection->ldapDynamicGroupMemberURL); |
|
| 632 | - |
|
| 633 | - if (!empty($dynamicGroupMemberURL)) { |
|
| 634 | - // look through dynamic groups to add them to the result array if needed |
|
| 635 | - $groupsToMatch = $this->access->fetchListOfGroups( |
|
| 636 | - $this->access->connection->ldapGroupFilter,array('dn',$dynamicGroupMemberURL)); |
|
| 637 | - foreach($groupsToMatch as $dynamicGroup) { |
|
| 638 | - if (!array_key_exists($dynamicGroupMemberURL, $dynamicGroup)) { |
|
| 639 | - continue; |
|
| 640 | - } |
|
| 641 | - $pos = strpos($dynamicGroup[$dynamicGroupMemberURL][0], '('); |
|
| 642 | - if ($pos !== false) { |
|
| 643 | - $memberUrlFilter = substr($dynamicGroup[$dynamicGroupMemberURL][0],$pos); |
|
| 644 | - // apply filter via ldap search to see if this user is in this |
|
| 645 | - // dynamic group |
|
| 646 | - $userMatch = $this->access->readAttribute( |
|
| 647 | - $userDN, |
|
| 648 | - $this->access->connection->ldapUserDisplayName, |
|
| 649 | - $memberUrlFilter |
|
| 650 | - ); |
|
| 651 | - if ($userMatch !== false) { |
|
| 652 | - // match found so this user is in this group |
|
| 653 | - $groupName = $this->access->dn2groupname($dynamicGroup['dn'][0]); |
|
| 654 | - if(is_string($groupName)) { |
|
| 655 | - // be sure to never return false if the dn could not be |
|
| 656 | - // resolved to a name, for whatever reason. |
|
| 657 | - $groups[] = $groupName; |
|
| 658 | - } |
|
| 659 | - } |
|
| 660 | - } else { |
|
| 661 | - \OCP\Util::writeLog('user_ldap', 'No search filter found on member url '. |
|
| 662 | - 'of group ' . print_r($dynamicGroup, true), \OCP\Util::DEBUG); |
|
| 663 | - } |
|
| 664 | - } |
|
| 665 | - } |
|
| 666 | - |
|
| 667 | - // if possible, read out membership via memberOf. It's far faster than |
|
| 668 | - // performing a search, which still is a fallback later. |
|
| 669 | - // memberof doesn't support memberuid, so skip it here. |
|
| 670 | - if((int)$this->access->connection->hasMemberOfFilterSupport === 1 |
|
| 671 | - && (int)$this->access->connection->useMemberOfToDetectMembership === 1 |
|
| 672 | - && strtolower($this->access->connection->ldapGroupMemberAssocAttr) !== 'memberuid' |
|
| 673 | - ) { |
|
| 674 | - $groupDNs = $this->_getGroupDNsFromMemberOf($userDN); |
|
| 675 | - if (is_array($groupDNs)) { |
|
| 676 | - foreach ($groupDNs as $dn) { |
|
| 677 | - $groupName = $this->access->dn2groupname($dn); |
|
| 678 | - if(is_string($groupName)) { |
|
| 679 | - // be sure to never return false if the dn could not be |
|
| 680 | - // resolved to a name, for whatever reason. |
|
| 681 | - $groups[] = $groupName; |
|
| 682 | - } |
|
| 683 | - } |
|
| 684 | - } |
|
| 685 | - |
|
| 686 | - if($primaryGroup !== false) { |
|
| 687 | - $groups[] = $primaryGroup; |
|
| 688 | - } |
|
| 689 | - if($gidGroupName !== false) { |
|
| 690 | - $groups[] = $gidGroupName; |
|
| 691 | - } |
|
| 692 | - $this->access->connection->writeToCache($cacheKey, $groups); |
|
| 693 | - return $groups; |
|
| 694 | - } |
|
| 695 | - |
|
| 696 | - //uniqueMember takes DN, memberuid the uid, so we need to distinguish |
|
| 697 | - if((strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'uniquemember') |
|
| 698 | - || (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'member') |
|
| 699 | - ) { |
|
| 700 | - $uid = $userDN; |
|
| 701 | - } else if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
| 702 | - $result = $this->access->readAttribute($userDN, 'uid'); |
|
| 703 | - if ($result === false) { |
|
| 704 | - \OCP\Util::writeLog('user_ldap', 'No uid attribute found for DN ' . $userDN . ' on '. |
|
| 705 | - $this->access->connection->ldapHost, \OCP\Util::DEBUG); |
|
| 706 | - } |
|
| 707 | - $uid = $result[0]; |
|
| 708 | - } else { |
|
| 709 | - // just in case |
|
| 710 | - $uid = $userDN; |
|
| 711 | - } |
|
| 712 | - |
|
| 713 | - if(isset($this->cachedGroupsByMember[$uid])) { |
|
| 714 | - $groups = array_merge($groups, $this->cachedGroupsByMember[$uid]); |
|
| 715 | - } else { |
|
| 716 | - $groupsByMember = array_values($this->getGroupsByMember($uid)); |
|
| 717 | - $groupsByMember = $this->access->nextcloudGroupNames($groupsByMember); |
|
| 718 | - $this->cachedGroupsByMember[$uid] = $groupsByMember; |
|
| 719 | - $groups = array_merge($groups, $groupsByMember); |
|
| 720 | - } |
|
| 721 | - |
|
| 722 | - if($primaryGroup !== false) { |
|
| 723 | - $groups[] = $primaryGroup; |
|
| 724 | - } |
|
| 725 | - if($gidGroupName !== false) { |
|
| 726 | - $groups[] = $gidGroupName; |
|
| 727 | - } |
|
| 728 | - |
|
| 729 | - $groups = array_unique($groups, SORT_LOCALE_STRING); |
|
| 730 | - $this->access->connection->writeToCache($cacheKey, $groups); |
|
| 731 | - |
|
| 732 | - return $groups; |
|
| 733 | - } |
|
| 734 | - |
|
| 735 | - /** |
|
| 736 | - * @param string $dn |
|
| 737 | - * @param array|null &$seen |
|
| 738 | - * @return array |
|
| 739 | - */ |
|
| 740 | - private function getGroupsByMember($dn, &$seen = null) { |
|
| 741 | - if ($seen === null) { |
|
| 742 | - $seen = array(); |
|
| 743 | - } |
|
| 744 | - $allGroups = array(); |
|
| 745 | - if (array_key_exists($dn, $seen)) { |
|
| 746 | - // avoid loops |
|
| 747 | - return array(); |
|
| 748 | - } |
|
| 749 | - $seen[$dn] = true; |
|
| 750 | - $filter = $this->access->combineFilterWithAnd(array( |
|
| 751 | - $this->access->connection->ldapGroupFilter, |
|
| 752 | - $this->access->connection->ldapGroupMemberAssocAttr.'='.$dn |
|
| 753 | - )); |
|
| 754 | - $groups = $this->access->fetchListOfGroups($filter, |
|
| 755 | - array($this->access->connection->ldapGroupDisplayName, 'dn')); |
|
| 756 | - if (is_array($groups)) { |
|
| 757 | - foreach ($groups as $groupobj) { |
|
| 758 | - $groupDN = $groupobj['dn'][0]; |
|
| 759 | - $allGroups[$groupDN] = $groupobj; |
|
| 760 | - $nestedGroups = $this->access->connection->ldapNestedGroups; |
|
| 761 | - if (!empty($nestedGroups)) { |
|
| 762 | - $supergroups = $this->getGroupsByMember($groupDN, $seen); |
|
| 763 | - if (is_array($supergroups) && (count($supergroups)>0)) { |
|
| 764 | - $allGroups = array_merge($allGroups, $supergroups); |
|
| 765 | - } |
|
| 766 | - } |
|
| 767 | - } |
|
| 768 | - } |
|
| 769 | - return $allGroups; |
|
| 770 | - } |
|
| 771 | - |
|
| 772 | - /** |
|
| 773 | - * get a list of all users in a group |
|
| 774 | - * |
|
| 775 | - * @param string $gid |
|
| 776 | - * @param string $search |
|
| 777 | - * @param int $limit |
|
| 778 | - * @param int $offset |
|
| 779 | - * @return array with user ids |
|
| 780 | - */ |
|
| 781 | - public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
| 782 | - if(!$this->enabled) { |
|
| 783 | - return array(); |
|
| 784 | - } |
|
| 785 | - if(!$this->groupExists($gid)) { |
|
| 786 | - return array(); |
|
| 787 | - } |
|
| 788 | - $search = $this->access->escapeFilterPart($search, true); |
|
| 789 | - $cacheKey = 'usersInGroup-'.$gid.'-'.$search.'-'.$limit.'-'.$offset; |
|
| 790 | - // check for cache of the exact query |
|
| 791 | - $groupUsers = $this->access->connection->getFromCache($cacheKey); |
|
| 792 | - if(!is_null($groupUsers)) { |
|
| 793 | - return $groupUsers; |
|
| 794 | - } |
|
| 795 | - |
|
| 796 | - // check for cache of the query without limit and offset |
|
| 797 | - $groupUsers = $this->access->connection->getFromCache('usersInGroup-'.$gid.'-'.$search); |
|
| 798 | - if(!is_null($groupUsers)) { |
|
| 799 | - $groupUsers = array_slice($groupUsers, $offset, $limit); |
|
| 800 | - $this->access->connection->writeToCache($cacheKey, $groupUsers); |
|
| 801 | - return $groupUsers; |
|
| 802 | - } |
|
| 803 | - |
|
| 804 | - if($limit === -1) { |
|
| 805 | - $limit = null; |
|
| 806 | - } |
|
| 807 | - $groupDN = $this->access->groupname2dn($gid); |
|
| 808 | - if(!$groupDN) { |
|
| 809 | - // group couldn't be found, return empty resultset |
|
| 810 | - $this->access->connection->writeToCache($cacheKey, array()); |
|
| 811 | - return array(); |
|
| 812 | - } |
|
| 813 | - |
|
| 814 | - $primaryUsers = $this->getUsersInPrimaryGroup($groupDN, $search, $limit, $offset); |
|
| 815 | - $posixGroupUsers = $this->getUsersInGidNumber($groupDN, $search, $limit, $offset); |
|
| 816 | - $members = array_keys($this->_groupMembers($groupDN)); |
|
| 817 | - if(!$members && empty($posixGroupUsers) && empty($primaryUsers)) { |
|
| 818 | - //in case users could not be retrieved, return empty result set |
|
| 819 | - $this->access->connection->writeToCache($cacheKey, []); |
|
| 820 | - return []; |
|
| 821 | - } |
|
| 822 | - |
|
| 823 | - $groupUsers = array(); |
|
| 824 | - $isMemberUid = (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid'); |
|
| 825 | - $attrs = $this->access->userManager->getAttributes(true); |
|
| 826 | - foreach($members as $member) { |
|
| 827 | - if($isMemberUid) { |
|
| 828 | - //we got uids, need to get their DNs to 'translate' them to user names |
|
| 829 | - $filter = $this->access->combineFilterWithAnd(array( |
|
| 830 | - str_replace('%uid', $member, $this->access->connection->ldapLoginFilter), |
|
| 831 | - $this->access->getFilterPartForUserSearch($search) |
|
| 832 | - )); |
|
| 833 | - $ldap_users = $this->access->fetchListOfUsers($filter, $attrs, 1); |
|
| 834 | - if(count($ldap_users) < 1) { |
|
| 835 | - continue; |
|
| 836 | - } |
|
| 837 | - $groupUsers[] = $this->access->dn2username($ldap_users[0]['dn'][0]); |
|
| 838 | - } else { |
|
| 839 | - //we got DNs, check if we need to filter by search or we can give back all of them |
|
| 840 | - if ($search !== '') { |
|
| 841 | - if(!$this->access->readAttribute($member, |
|
| 842 | - $this->access->connection->ldapUserDisplayName, |
|
| 843 | - $this->access->getFilterPartForUserSearch($search))) { |
|
| 844 | - continue; |
|
| 845 | - } |
|
| 846 | - } |
|
| 847 | - // dn2username will also check if the users belong to the allowed base |
|
| 848 | - if($ocname = $this->access->dn2username($member)) { |
|
| 849 | - $groupUsers[] = $ocname; |
|
| 850 | - } |
|
| 851 | - } |
|
| 852 | - } |
|
| 853 | - |
|
| 854 | - $groupUsers = array_unique(array_merge($groupUsers, $primaryUsers, $posixGroupUsers)); |
|
| 855 | - natsort($groupUsers); |
|
| 856 | - $this->access->connection->writeToCache('usersInGroup-'.$gid.'-'.$search, $groupUsers); |
|
| 857 | - $groupUsers = array_slice($groupUsers, $offset, $limit); |
|
| 858 | - |
|
| 859 | - $this->access->connection->writeToCache($cacheKey, $groupUsers); |
|
| 860 | - |
|
| 861 | - return $groupUsers; |
|
| 862 | - } |
|
| 863 | - |
|
| 864 | - /** |
|
| 865 | - * returns the number of users in a group, who match the search term |
|
| 866 | - * @param string $gid the internal group name |
|
| 867 | - * @param string $search optional, a search string |
|
| 868 | - * @return int|bool |
|
| 869 | - */ |
|
| 870 | - public function countUsersInGroup($gid, $search = '') { |
|
| 871 | - if ($this->groupPluginManager->implementsActions(GroupInterface::COUNT_USERS)) { |
|
| 872 | - return $this->groupPluginManager->countUsersInGroup($gid, $search); |
|
| 873 | - } |
|
| 874 | - |
|
| 875 | - $cacheKey = 'countUsersInGroup-'.$gid.'-'.$search; |
|
| 876 | - if(!$this->enabled || !$this->groupExists($gid)) { |
|
| 877 | - return false; |
|
| 878 | - } |
|
| 879 | - $groupUsers = $this->access->connection->getFromCache($cacheKey); |
|
| 880 | - if(!is_null($groupUsers)) { |
|
| 881 | - return $groupUsers; |
|
| 882 | - } |
|
| 883 | - |
|
| 884 | - $groupDN = $this->access->groupname2dn($gid); |
|
| 885 | - if(!$groupDN) { |
|
| 886 | - // group couldn't be found, return empty result set |
|
| 887 | - $this->access->connection->writeToCache($cacheKey, false); |
|
| 888 | - return false; |
|
| 889 | - } |
|
| 890 | - |
|
| 891 | - $members = array_keys($this->_groupMembers($groupDN)); |
|
| 892 | - $primaryUserCount = $this->countUsersInPrimaryGroup($groupDN, ''); |
|
| 893 | - if(!$members && $primaryUserCount === 0) { |
|
| 894 | - //in case users could not be retrieved, return empty result set |
|
| 895 | - $this->access->connection->writeToCache($cacheKey, false); |
|
| 896 | - return false; |
|
| 897 | - } |
|
| 898 | - |
|
| 899 | - if ($search === '') { |
|
| 900 | - $groupUsers = count($members) + $primaryUserCount; |
|
| 901 | - $this->access->connection->writeToCache($cacheKey, $groupUsers); |
|
| 902 | - return $groupUsers; |
|
| 903 | - } |
|
| 904 | - $search = $this->access->escapeFilterPart($search, true); |
|
| 905 | - $isMemberUid = |
|
| 906 | - (strtolower($this->access->connection->ldapGroupMemberAssocAttr) |
|
| 907 | - === 'memberuid'); |
|
| 908 | - |
|
| 909 | - //we need to apply the search filter |
|
| 910 | - //alternatives that need to be checked: |
|
| 911 | - //a) get all users by search filter and array_intersect them |
|
| 912 | - //b) a, but only when less than 1k 10k ?k users like it is |
|
| 913 | - //c) put all DNs|uids in a LDAP filter, combine with the search string |
|
| 914 | - // and let it count. |
|
| 915 | - //For now this is not important, because the only use of this method |
|
| 916 | - //does not supply a search string |
|
| 917 | - $groupUsers = array(); |
|
| 918 | - foreach($members as $member) { |
|
| 919 | - if($isMemberUid) { |
|
| 920 | - //we got uids, need to get their DNs to 'translate' them to user names |
|
| 921 | - $filter = $this->access->combineFilterWithAnd(array( |
|
| 922 | - str_replace('%uid', $member, $this->access->connection->ldapLoginFilter), |
|
| 923 | - $this->access->getFilterPartForUserSearch($search) |
|
| 924 | - )); |
|
| 925 | - $ldap_users = $this->access->fetchListOfUsers($filter, 'dn', 1); |
|
| 926 | - if(count($ldap_users) < 1) { |
|
| 927 | - continue; |
|
| 928 | - } |
|
| 929 | - $groupUsers[] = $this->access->dn2username($ldap_users[0]); |
|
| 930 | - } else { |
|
| 931 | - //we need to apply the search filter now |
|
| 932 | - if(!$this->access->readAttribute($member, |
|
| 933 | - $this->access->connection->ldapUserDisplayName, |
|
| 934 | - $this->access->getFilterPartForUserSearch($search))) { |
|
| 935 | - continue; |
|
| 936 | - } |
|
| 937 | - // dn2username will also check if the users belong to the allowed base |
|
| 938 | - if($ocname = $this->access->dn2username($member)) { |
|
| 939 | - $groupUsers[] = $ocname; |
|
| 940 | - } |
|
| 941 | - } |
|
| 942 | - } |
|
| 943 | - |
|
| 944 | - //and get users that have the group as primary |
|
| 945 | - $primaryUsers = $this->countUsersInPrimaryGroup($groupDN, $search); |
|
| 946 | - |
|
| 947 | - return count($groupUsers) + $primaryUsers; |
|
| 948 | - } |
|
| 949 | - |
|
| 950 | - /** |
|
| 951 | - * get a list of all groups |
|
| 952 | - * |
|
| 953 | - * @param string $search |
|
| 954 | - * @param $limit |
|
| 955 | - * @param int $offset |
|
| 956 | - * @return array with group names |
|
| 957 | - * |
|
| 958 | - * Returns a list with all groups (used by getGroups) |
|
| 959 | - */ |
|
| 960 | - protected function getGroupsChunk($search = '', $limit = -1, $offset = 0) { |
|
| 961 | - if(!$this->enabled) { |
|
| 962 | - return array(); |
|
| 963 | - } |
|
| 964 | - $cacheKey = 'getGroups-'.$search.'-'.$limit.'-'.$offset; |
|
| 965 | - |
|
| 966 | - //Check cache before driving unnecessary searches |
|
| 967 | - \OCP\Util::writeLog('user_ldap', 'getGroups '.$cacheKey, \OCP\Util::DEBUG); |
|
| 968 | - $ldap_groups = $this->access->connection->getFromCache($cacheKey); |
|
| 969 | - if(!is_null($ldap_groups)) { |
|
| 970 | - return $ldap_groups; |
|
| 971 | - } |
|
| 972 | - |
|
| 973 | - // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
|
| 974 | - // error. With a limit of 0, we get 0 results. So we pass null. |
|
| 975 | - if($limit <= 0) { |
|
| 976 | - $limit = null; |
|
| 977 | - } |
|
| 978 | - $filter = $this->access->combineFilterWithAnd(array( |
|
| 979 | - $this->access->connection->ldapGroupFilter, |
|
| 980 | - $this->access->getFilterPartForGroupSearch($search) |
|
| 981 | - )); |
|
| 982 | - \OCP\Util::writeLog('user_ldap', 'getGroups Filter '.$filter, \OCP\Util::DEBUG); |
|
| 983 | - $ldap_groups = $this->access->fetchListOfGroups($filter, |
|
| 984 | - array($this->access->connection->ldapGroupDisplayName, 'dn'), |
|
| 985 | - $limit, |
|
| 986 | - $offset); |
|
| 987 | - $ldap_groups = $this->access->nextcloudGroupNames($ldap_groups); |
|
| 988 | - |
|
| 989 | - $this->access->connection->writeToCache($cacheKey, $ldap_groups); |
|
| 990 | - return $ldap_groups; |
|
| 991 | - } |
|
| 992 | - |
|
| 993 | - /** |
|
| 994 | - * get a list of all groups using a paged search |
|
| 995 | - * |
|
| 996 | - * @param string $search |
|
| 997 | - * @param int $limit |
|
| 998 | - * @param int $offset |
|
| 999 | - * @return array with group names |
|
| 1000 | - * |
|
| 1001 | - * Returns a list with all groups |
|
| 1002 | - * Uses a paged search if available to override a |
|
| 1003 | - * server side search limit. |
|
| 1004 | - * (active directory has a limit of 1000 by default) |
|
| 1005 | - */ |
|
| 1006 | - public function getGroups($search = '', $limit = -1, $offset = 0) { |
|
| 1007 | - if(!$this->enabled) { |
|
| 1008 | - return array(); |
|
| 1009 | - } |
|
| 1010 | - $search = $this->access->escapeFilterPart($search, true); |
|
| 1011 | - $pagingSize = (int)$this->access->connection->ldapPagingSize; |
|
| 1012 | - if (!$this->access->connection->hasPagedResultSupport || $pagingSize <= 0) { |
|
| 1013 | - return $this->getGroupsChunk($search, $limit, $offset); |
|
| 1014 | - } |
|
| 1015 | - $maxGroups = 100000; // limit max results (just for safety reasons) |
|
| 1016 | - if ($limit > -1) { |
|
| 1017 | - $overallLimit = min($limit + $offset, $maxGroups); |
|
| 1018 | - } else { |
|
| 1019 | - $overallLimit = $maxGroups; |
|
| 1020 | - } |
|
| 1021 | - $chunkOffset = $offset; |
|
| 1022 | - $allGroups = array(); |
|
| 1023 | - while ($chunkOffset < $overallLimit) { |
|
| 1024 | - $chunkLimit = min($pagingSize, $overallLimit - $chunkOffset); |
|
| 1025 | - $ldapGroups = $this->getGroupsChunk($search, $chunkLimit, $chunkOffset); |
|
| 1026 | - $nread = count($ldapGroups); |
|
| 1027 | - \OCP\Util::writeLog('user_ldap', 'getGroups('.$search.'): read '.$nread.' at offset '.$chunkOffset.' (limit: '.$chunkLimit.')', \OCP\Util::DEBUG); |
|
| 1028 | - if ($nread) { |
|
| 1029 | - $allGroups = array_merge($allGroups, $ldapGroups); |
|
| 1030 | - $chunkOffset += $nread; |
|
| 1031 | - } |
|
| 1032 | - if ($nread < $chunkLimit) { |
|
| 1033 | - break; |
|
| 1034 | - } |
|
| 1035 | - } |
|
| 1036 | - return $allGroups; |
|
| 1037 | - } |
|
| 1038 | - |
|
| 1039 | - /** |
|
| 1040 | - * @param string $group |
|
| 1041 | - * @return bool |
|
| 1042 | - */ |
|
| 1043 | - public function groupMatchesFilter($group) { |
|
| 1044 | - return (strripos($group, $this->groupSearch) !== false); |
|
| 1045 | - } |
|
| 1046 | - |
|
| 1047 | - /** |
|
| 1048 | - * check if a group exists |
|
| 1049 | - * @param string $gid |
|
| 1050 | - * @return bool |
|
| 1051 | - */ |
|
| 1052 | - public function groupExists($gid) { |
|
| 1053 | - $groupExists = $this->access->connection->getFromCache('groupExists'.$gid); |
|
| 1054 | - if(!is_null($groupExists)) { |
|
| 1055 | - return (bool)$groupExists; |
|
| 1056 | - } |
|
| 1057 | - |
|
| 1058 | - //getting dn, if false the group does not exist. If dn, it may be mapped |
|
| 1059 | - //only, requires more checking. |
|
| 1060 | - $dn = $this->access->groupname2dn($gid); |
|
| 1061 | - if(!$dn) { |
|
| 1062 | - $this->access->connection->writeToCache('groupExists'.$gid, false); |
|
| 1063 | - return false; |
|
| 1064 | - } |
|
| 1065 | - |
|
| 1066 | - //if group really still exists, we will be able to read its objectclass |
|
| 1067 | - if(!is_array($this->access->readAttribute($dn, ''))) { |
|
| 1068 | - $this->access->connection->writeToCache('groupExists'.$gid, false); |
|
| 1069 | - return false; |
|
| 1070 | - } |
|
| 1071 | - |
|
| 1072 | - $this->access->connection->writeToCache('groupExists'.$gid, true); |
|
| 1073 | - return true; |
|
| 1074 | - } |
|
| 1075 | - |
|
| 1076 | - /** |
|
| 1077 | - * Check if backend implements actions |
|
| 1078 | - * @param int $actions bitwise-or'ed actions |
|
| 1079 | - * @return boolean |
|
| 1080 | - * |
|
| 1081 | - * Returns the supported actions as int to be |
|
| 1082 | - * compared with GroupInterface::CREATE_GROUP etc. |
|
| 1083 | - */ |
|
| 1084 | - public function implementsActions($actions) { |
|
| 1085 | - return (bool)((GroupInterface::COUNT_USERS | |
|
| 1086 | - $this->groupPluginManager->getImplementedActions()) & $actions); |
|
| 1087 | - } |
|
| 1088 | - |
|
| 1089 | - /** |
|
| 1090 | - * Return access for LDAP interaction. |
|
| 1091 | - * @return Access instance of Access for LDAP interaction |
|
| 1092 | - */ |
|
| 1093 | - public function getLDAPAccess($gid) { |
|
| 1094 | - return $this->access; |
|
| 1095 | - } |
|
| 1096 | - |
|
| 1097 | - /** |
|
| 1098 | - * create a group |
|
| 1099 | - * @param string $gid |
|
| 1100 | - * @return bool |
|
| 1101 | - * @throws \Exception |
|
| 1102 | - */ |
|
| 1103 | - public function createGroup($gid) { |
|
| 1104 | - if ($this->groupPluginManager->implementsActions(GroupInterface::CREATE_GROUP)) { |
|
| 1105 | - if ($dn = $this->groupPluginManager->createGroup($gid)) { |
|
| 1106 | - //updates group mapping |
|
| 1107 | - $this->access->dn2ocname($dn, $gid, false); |
|
| 1108 | - $this->access->connection->writeToCache("groupExists".$gid, true); |
|
| 1109 | - } |
|
| 1110 | - return $dn != null; |
|
| 1111 | - } |
|
| 1112 | - throw new \Exception('Could not create group in LDAP backend.'); |
|
| 1113 | - } |
|
| 1114 | - |
|
| 1115 | - /** |
|
| 1116 | - * delete a group |
|
| 1117 | - * @param string $gid gid of the group to delete |
|
| 1118 | - * @return bool |
|
| 1119 | - * @throws \Exception |
|
| 1120 | - */ |
|
| 1121 | - public function deleteGroup($gid) { |
|
| 1122 | - if ($this->groupPluginManager->implementsActions(GroupInterface::DELETE_GROUP)) { |
|
| 1123 | - if ($ret = $this->groupPluginManager->deleteGroup($gid)) { |
|
| 1124 | - #delete group in nextcloud internal db |
|
| 1125 | - $this->access->getGroupMapper()->unmap($gid); |
|
| 1126 | - $this->access->connection->writeToCache("groupExists".$gid, false); |
|
| 1127 | - } |
|
| 1128 | - return $ret; |
|
| 1129 | - } |
|
| 1130 | - throw new \Exception('Could not delete group in LDAP backend.'); |
|
| 1131 | - } |
|
| 1132 | - |
|
| 1133 | - /** |
|
| 1134 | - * Add a user to a group |
|
| 1135 | - * @param string $uid Name of the user to add to group |
|
| 1136 | - * @param string $gid Name of the group in which add the user |
|
| 1137 | - * @return bool |
|
| 1138 | - * @throws \Exception |
|
| 1139 | - */ |
|
| 1140 | - public function addToGroup($uid, $gid) { |
|
| 1141 | - if ($this->groupPluginManager->implementsActions(GroupInterface::ADD_TO_GROUP)) { |
|
| 1142 | - if ($ret = $this->groupPluginManager->addToGroup($uid, $gid)) { |
|
| 1143 | - $this->access->connection->clearCache(); |
|
| 1144 | - } |
|
| 1145 | - return $ret; |
|
| 1146 | - } |
|
| 1147 | - throw new \Exception('Could not add user to group in LDAP backend.'); |
|
| 1148 | - } |
|
| 1149 | - |
|
| 1150 | - /** |
|
| 1151 | - * Removes a user from a group |
|
| 1152 | - * @param string $uid Name of the user to remove from group |
|
| 1153 | - * @param string $gid Name of the group from which remove the user |
|
| 1154 | - * @return bool |
|
| 1155 | - * @throws \Exception |
|
| 1156 | - */ |
|
| 1157 | - public function removeFromGroup($uid, $gid) { |
|
| 1158 | - if ($this->groupPluginManager->implementsActions(GroupInterface::REMOVE_FROM_GROUP)) { |
|
| 1159 | - if ($ret = $this->groupPluginManager->removeFromGroup($uid, $gid)) { |
|
| 1160 | - $this->access->connection->clearCache(); |
|
| 1161 | - } |
|
| 1162 | - return $ret; |
|
| 1163 | - } |
|
| 1164 | - throw new \Exception('Could not remove user from group in LDAP backend.'); |
|
| 1165 | - } |
|
| 1166 | - |
|
| 1167 | - /** |
|
| 1168 | - * Gets group details |
|
| 1169 | - * @param string $gid Name of the group |
|
| 1170 | - * @return array | false |
|
| 1171 | - * @throws \Exception |
|
| 1172 | - */ |
|
| 1173 | - public function getGroupDetails($gid) { |
|
| 1174 | - if ($this->groupPluginManager->implementsActions(GroupInterface::GROUP_DETAILS)) { |
|
| 1175 | - return $this->groupPluginManager->getGroupDetails($gid); |
|
| 1176 | - } |
|
| 1177 | - throw new \Exception('Could not get group details in LDAP backend.'); |
|
| 1178 | - } |
|
| 1179 | - |
|
| 1180 | - /** |
|
| 1181 | - * Return LDAP connection resource from a cloned connection. |
|
| 1182 | - * The cloned connection needs to be closed manually. |
|
| 1183 | - * of the current access. |
|
| 1184 | - * @param string $gid |
|
| 1185 | - * @return resource of the LDAP connection |
|
| 1186 | - */ |
|
| 1187 | - public function getNewLDAPConnection($gid) { |
|
| 1188 | - $connection = clone $this->access->getConnection(); |
|
| 1189 | - return $connection->getConnectionResource(); |
|
| 1190 | - } |
|
| 48 | + protected $enabled = false; |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @var string[] $cachedGroupMembers array of users with gid as key |
|
| 52 | + */ |
|
| 53 | + protected $cachedGroupMembers; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @var string[] $cachedGroupsByMember array of groups with uid as key |
|
| 57 | + */ |
|
| 58 | + protected $cachedGroupsByMember; |
|
| 59 | + |
|
| 60 | + /** @var GroupPluginManager */ |
|
| 61 | + protected $groupPluginManager; |
|
| 62 | + |
|
| 63 | + public function __construct(Access $access, GroupPluginManager $groupPluginManager) { |
|
| 64 | + parent::__construct($access); |
|
| 65 | + $filter = $this->access->connection->ldapGroupFilter; |
|
| 66 | + $gassoc = $this->access->connection->ldapGroupMemberAssocAttr; |
|
| 67 | + if(!empty($filter) && !empty($gassoc)) { |
|
| 68 | + $this->enabled = true; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + $this->cachedGroupMembers = new CappedMemoryCache(); |
|
| 72 | + $this->cachedGroupsByMember = new CappedMemoryCache(); |
|
| 73 | + $this->groupPluginManager = $groupPluginManager; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * is user in group? |
|
| 78 | + * @param string $uid uid of the user |
|
| 79 | + * @param string $gid gid of the group |
|
| 80 | + * @return bool |
|
| 81 | + * |
|
| 82 | + * Checks whether the user is member of a group or not. |
|
| 83 | + */ |
|
| 84 | + public function inGroup($uid, $gid) { |
|
| 85 | + if(!$this->enabled) { |
|
| 86 | + return false; |
|
| 87 | + } |
|
| 88 | + $cacheKey = 'inGroup'.$uid.':'.$gid; |
|
| 89 | + $inGroup = $this->access->connection->getFromCache($cacheKey); |
|
| 90 | + if(!is_null($inGroup)) { |
|
| 91 | + return (bool)$inGroup; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + $userDN = $this->access->username2dn($uid); |
|
| 95 | + |
|
| 96 | + if(isset($this->cachedGroupMembers[$gid])) { |
|
| 97 | + $isInGroup = in_array($userDN, $this->cachedGroupMembers[$gid]); |
|
| 98 | + return $isInGroup; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + $cacheKeyMembers = 'inGroup-members:'.$gid; |
|
| 102 | + $members = $this->access->connection->getFromCache($cacheKeyMembers); |
|
| 103 | + if(!is_null($members)) { |
|
| 104 | + $this->cachedGroupMembers[$gid] = $members; |
|
| 105 | + $isInGroup = in_array($userDN, $members); |
|
| 106 | + $this->access->connection->writeToCache($cacheKey, $isInGroup); |
|
| 107 | + return $isInGroup; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + $groupDN = $this->access->groupname2dn($gid); |
|
| 111 | + // just in case |
|
| 112 | + if(!$groupDN || !$userDN) { |
|
| 113 | + $this->access->connection->writeToCache($cacheKey, false); |
|
| 114 | + return false; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + //check primary group first |
|
| 118 | + if($gid === $this->getUserPrimaryGroup($userDN)) { |
|
| 119 | + $this->access->connection->writeToCache($cacheKey, true); |
|
| 120 | + return true; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + //usually, LDAP attributes are said to be case insensitive. But there are exceptions of course. |
|
| 124 | + $members = $this->_groupMembers($groupDN); |
|
| 125 | + $members = array_keys($members); // uids are returned as keys |
|
| 126 | + if(!is_array($members) || count($members) === 0) { |
|
| 127 | + $this->access->connection->writeToCache($cacheKey, false); |
|
| 128 | + return false; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + //extra work if we don't get back user DNs |
|
| 132 | + if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
| 133 | + $dns = array(); |
|
| 134 | + $filterParts = array(); |
|
| 135 | + $bytes = 0; |
|
| 136 | + foreach($members as $mid) { |
|
| 137 | + $filter = str_replace('%uid', $mid, $this->access->connection->ldapLoginFilter); |
|
| 138 | + $filterParts[] = $filter; |
|
| 139 | + $bytes += strlen($filter); |
|
| 140 | + if($bytes >= 9000000) { |
|
| 141 | + // AD has a default input buffer of 10 MB, we do not want |
|
| 142 | + // to take even the chance to exceed it |
|
| 143 | + $filter = $this->access->combineFilterWithOr($filterParts); |
|
| 144 | + $bytes = 0; |
|
| 145 | + $filterParts = array(); |
|
| 146 | + $users = $this->access->fetchListOfUsers($filter, 'dn', count($filterParts)); |
|
| 147 | + $dns = array_merge($dns, $users); |
|
| 148 | + } |
|
| 149 | + } |
|
| 150 | + if(count($filterParts) > 0) { |
|
| 151 | + $filter = $this->access->combineFilterWithOr($filterParts); |
|
| 152 | + $users = $this->access->fetchListOfUsers($filter, 'dn', count($filterParts)); |
|
| 153 | + $dns = array_merge($dns, $users); |
|
| 154 | + } |
|
| 155 | + $members = $dns; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + $isInGroup = in_array($userDN, $members); |
|
| 159 | + $this->access->connection->writeToCache($cacheKey, $isInGroup); |
|
| 160 | + $this->access->connection->writeToCache($cacheKeyMembers, $members); |
|
| 161 | + $this->cachedGroupMembers[$gid] = $members; |
|
| 162 | + |
|
| 163 | + return $isInGroup; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * @param string $dnGroup |
|
| 168 | + * @return array |
|
| 169 | + * |
|
| 170 | + * For a group that has user membership defined by an LDAP search url attribute returns the users |
|
| 171 | + * that match the search url otherwise returns an empty array. |
|
| 172 | + */ |
|
| 173 | + public function getDynamicGroupMembers($dnGroup) { |
|
| 174 | + $dynamicGroupMemberURL = strtolower($this->access->connection->ldapDynamicGroupMemberURL); |
|
| 175 | + |
|
| 176 | + if (empty($dynamicGroupMemberURL)) { |
|
| 177 | + return array(); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + $dynamicMembers = array(); |
|
| 181 | + $memberURLs = $this->access->readAttribute( |
|
| 182 | + $dnGroup, |
|
| 183 | + $dynamicGroupMemberURL, |
|
| 184 | + $this->access->connection->ldapGroupFilter |
|
| 185 | + ); |
|
| 186 | + if ($memberURLs !== false) { |
|
| 187 | + // this group has the 'memberURL' attribute so this is a dynamic group |
|
| 188 | + // example 1: ldap:///cn=users,cn=accounts,dc=dcsubbase,dc=dcbase??one?(o=HeadOffice) |
|
| 189 | + // example 2: ldap:///cn=users,cn=accounts,dc=dcsubbase,dc=dcbase??one?(&(o=HeadOffice)(uidNumber>=500)) |
|
| 190 | + $pos = strpos($memberURLs[0], '('); |
|
| 191 | + if ($pos !== false) { |
|
| 192 | + $memberUrlFilter = substr($memberURLs[0], $pos); |
|
| 193 | + $foundMembers = $this->access->searchUsers($memberUrlFilter,'dn'); |
|
| 194 | + $dynamicMembers = array(); |
|
| 195 | + foreach($foundMembers as $value) { |
|
| 196 | + $dynamicMembers[$value['dn'][0]] = 1; |
|
| 197 | + } |
|
| 198 | + } else { |
|
| 199 | + \OCP\Util::writeLog('user_ldap', 'No search filter found on member url '. |
|
| 200 | + 'of group ' . $dnGroup, \OCP\Util::DEBUG); |
|
| 201 | + } |
|
| 202 | + } |
|
| 203 | + return $dynamicMembers; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * @param string $dnGroup |
|
| 208 | + * @param array|null &$seen |
|
| 209 | + * @return array|mixed|null |
|
| 210 | + */ |
|
| 211 | + private function _groupMembers($dnGroup, &$seen = null) { |
|
| 212 | + if ($seen === null) { |
|
| 213 | + $seen = array(); |
|
| 214 | + } |
|
| 215 | + $allMembers = array(); |
|
| 216 | + if (array_key_exists($dnGroup, $seen)) { |
|
| 217 | + // avoid loops |
|
| 218 | + return array(); |
|
| 219 | + } |
|
| 220 | + // used extensively in cron job, caching makes sense for nested groups |
|
| 221 | + $cacheKey = '_groupMembers'.$dnGroup; |
|
| 222 | + $groupMembers = $this->access->connection->getFromCache($cacheKey); |
|
| 223 | + if(!is_null($groupMembers)) { |
|
| 224 | + return $groupMembers; |
|
| 225 | + } |
|
| 226 | + $seen[$dnGroup] = 1; |
|
| 227 | + $members = $this->access->readAttribute($dnGroup, $this->access->connection->ldapGroupMemberAssocAttr, |
|
| 228 | + $this->access->connection->ldapGroupFilter); |
|
| 229 | + if (is_array($members)) { |
|
| 230 | + foreach ($members as $memberDN) { |
|
| 231 | + $allMembers[$memberDN] = 1; |
|
| 232 | + $nestedGroups = $this->access->connection->ldapNestedGroups; |
|
| 233 | + if (!empty($nestedGroups)) { |
|
| 234 | + $subMembers = $this->_groupMembers($memberDN, $seen); |
|
| 235 | + if ($subMembers) { |
|
| 236 | + $allMembers = array_merge($allMembers, $subMembers); |
|
| 237 | + } |
|
| 238 | + } |
|
| 239 | + } |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + $allMembers = array_merge($allMembers, $this->getDynamicGroupMembers($dnGroup)); |
|
| 243 | + |
|
| 244 | + $this->access->connection->writeToCache($cacheKey, $allMembers); |
|
| 245 | + return $allMembers; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + /** |
|
| 249 | + * @param string $DN |
|
| 250 | + * @param array|null &$seen |
|
| 251 | + * @return array |
|
| 252 | + */ |
|
| 253 | + private function _getGroupDNsFromMemberOf($DN, &$seen = null) { |
|
| 254 | + if ($seen === null) { |
|
| 255 | + $seen = array(); |
|
| 256 | + } |
|
| 257 | + if (array_key_exists($DN, $seen)) { |
|
| 258 | + // avoid loops |
|
| 259 | + return array(); |
|
| 260 | + } |
|
| 261 | + $seen[$DN] = 1; |
|
| 262 | + $groups = $this->access->readAttribute($DN, 'memberOf'); |
|
| 263 | + if (!is_array($groups)) { |
|
| 264 | + return array(); |
|
| 265 | + } |
|
| 266 | + $groups = $this->access->groupsMatchFilter($groups); |
|
| 267 | + $allGroups = $groups; |
|
| 268 | + $nestedGroups = $this->access->connection->ldapNestedGroups; |
|
| 269 | + if ((int)$nestedGroups === 1) { |
|
| 270 | + foreach ($groups as $group) { |
|
| 271 | + $subGroups = $this->_getGroupDNsFromMemberOf($group, $seen); |
|
| 272 | + $allGroups = array_merge($allGroups, $subGroups); |
|
| 273 | + } |
|
| 274 | + } |
|
| 275 | + return $allGroups; |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * translates a gidNumber into an ownCloud internal name |
|
| 280 | + * @param string $gid as given by gidNumber on POSIX LDAP |
|
| 281 | + * @param string $dn a DN that belongs to the same domain as the group |
|
| 282 | + * @return string|bool |
|
| 283 | + */ |
|
| 284 | + public function gidNumber2Name($gid, $dn) { |
|
| 285 | + $cacheKey = 'gidNumberToName' . $gid; |
|
| 286 | + $groupName = $this->access->connection->getFromCache($cacheKey); |
|
| 287 | + if(!is_null($groupName) && isset($groupName)) { |
|
| 288 | + return $groupName; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + //we need to get the DN from LDAP |
|
| 292 | + $filter = $this->access->combineFilterWithAnd([ |
|
| 293 | + $this->access->connection->ldapGroupFilter, |
|
| 294 | + 'objectClass=posixGroup', |
|
| 295 | + $this->access->connection->ldapGidNumber . '=' . $gid |
|
| 296 | + ]); |
|
| 297 | + $result = $this->access->searchGroups($filter, array('dn'), 1); |
|
| 298 | + if(empty($result)) { |
|
| 299 | + return false; |
|
| 300 | + } |
|
| 301 | + $dn = $result[0]['dn'][0]; |
|
| 302 | + |
|
| 303 | + //and now the group name |
|
| 304 | + //NOTE once we have separate ownCloud group IDs and group names we can |
|
| 305 | + //directly read the display name attribute instead of the DN |
|
| 306 | + $name = $this->access->dn2groupname($dn); |
|
| 307 | + |
|
| 308 | + $this->access->connection->writeToCache($cacheKey, $name); |
|
| 309 | + |
|
| 310 | + return $name; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * returns the entry's gidNumber |
|
| 315 | + * @param string $dn |
|
| 316 | + * @param string $attribute |
|
| 317 | + * @return string|bool |
|
| 318 | + */ |
|
| 319 | + private function getEntryGidNumber($dn, $attribute) { |
|
| 320 | + $value = $this->access->readAttribute($dn, $attribute); |
|
| 321 | + if(is_array($value) && !empty($value)) { |
|
| 322 | + return $value[0]; |
|
| 323 | + } |
|
| 324 | + return false; |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + /** |
|
| 328 | + * returns the group's primary ID |
|
| 329 | + * @param string $dn |
|
| 330 | + * @return string|bool |
|
| 331 | + */ |
|
| 332 | + public function getGroupGidNumber($dn) { |
|
| 333 | + return $this->getEntryGidNumber($dn, 'gidNumber'); |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * returns the user's gidNumber |
|
| 338 | + * @param string $dn |
|
| 339 | + * @return string|bool |
|
| 340 | + */ |
|
| 341 | + public function getUserGidNumber($dn) { |
|
| 342 | + $gidNumber = false; |
|
| 343 | + if($this->access->connection->hasGidNumber) { |
|
| 344 | + $gidNumber = $this->getEntryGidNumber($dn, $this->access->connection->ldapGidNumber); |
|
| 345 | + if($gidNumber === false) { |
|
| 346 | + $this->access->connection->hasGidNumber = false; |
|
| 347 | + } |
|
| 348 | + } |
|
| 349 | + return $gidNumber; |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * returns a filter for a "users has specific gid" search or count operation |
|
| 354 | + * |
|
| 355 | + * @param string $groupDN |
|
| 356 | + * @param string $search |
|
| 357 | + * @return string |
|
| 358 | + * @throws \Exception |
|
| 359 | + */ |
|
| 360 | + private function prepareFilterForUsersHasGidNumber($groupDN, $search = '') { |
|
| 361 | + $groupID = $this->getGroupGidNumber($groupDN); |
|
| 362 | + if($groupID === false) { |
|
| 363 | + throw new \Exception('Not a valid group'); |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + $filterParts = []; |
|
| 367 | + $filterParts[] = $this->access->getFilterForUserCount(); |
|
| 368 | + if ($search !== '') { |
|
| 369 | + $filterParts[] = $this->access->getFilterPartForUserSearch($search); |
|
| 370 | + } |
|
| 371 | + $filterParts[] = $this->access->connection->ldapGidNumber .'=' . $groupID; |
|
| 372 | + |
|
| 373 | + $filter = $this->access->combineFilterWithAnd($filterParts); |
|
| 374 | + |
|
| 375 | + return $filter; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * returns a list of users that have the given group as gid number |
|
| 380 | + * |
|
| 381 | + * @param string $groupDN |
|
| 382 | + * @param string $search |
|
| 383 | + * @param int $limit |
|
| 384 | + * @param int $offset |
|
| 385 | + * @return string[] |
|
| 386 | + */ |
|
| 387 | + public function getUsersInGidNumber($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
| 388 | + try { |
|
| 389 | + $filter = $this->prepareFilterForUsersHasGidNumber($groupDN, $search); |
|
| 390 | + $users = $this->access->fetchListOfUsers( |
|
| 391 | + $filter, |
|
| 392 | + [$this->access->connection->ldapUserDisplayName, 'dn'], |
|
| 393 | + $limit, |
|
| 394 | + $offset |
|
| 395 | + ); |
|
| 396 | + return $this->access->nextcloudUserNames($users); |
|
| 397 | + } catch (\Exception $e) { |
|
| 398 | + return []; |
|
| 399 | + } |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + /** |
|
| 403 | + * returns the number of users that have the given group as gid number |
|
| 404 | + * |
|
| 405 | + * @param string $groupDN |
|
| 406 | + * @param string $search |
|
| 407 | + * @param int $limit |
|
| 408 | + * @param int $offset |
|
| 409 | + * @return int |
|
| 410 | + */ |
|
| 411 | + public function countUsersInGidNumber($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
| 412 | + try { |
|
| 413 | + $filter = $this->prepareFilterForUsersHasGidNumber($groupDN, $search); |
|
| 414 | + $users = $this->access->countUsers($filter, ['dn'], $limit, $offset); |
|
| 415 | + return (int)$users; |
|
| 416 | + } catch (\Exception $e) { |
|
| 417 | + return 0; |
|
| 418 | + } |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + /** |
|
| 422 | + * gets the gidNumber of a user |
|
| 423 | + * @param string $dn |
|
| 424 | + * @return string |
|
| 425 | + */ |
|
| 426 | + public function getUserGroupByGid($dn) { |
|
| 427 | + $groupID = $this->getUserGidNumber($dn); |
|
| 428 | + if($groupID !== false) { |
|
| 429 | + $groupName = $this->gidNumber2Name($groupID, $dn); |
|
| 430 | + if($groupName !== false) { |
|
| 431 | + return $groupName; |
|
| 432 | + } |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + return false; |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + /** |
|
| 439 | + * translates a primary group ID into an Nextcloud internal name |
|
| 440 | + * @param string $gid as given by primaryGroupID on AD |
|
| 441 | + * @param string $dn a DN that belongs to the same domain as the group |
|
| 442 | + * @return string|bool |
|
| 443 | + */ |
|
| 444 | + public function primaryGroupID2Name($gid, $dn) { |
|
| 445 | + $cacheKey = 'primaryGroupIDtoName'; |
|
| 446 | + $groupNames = $this->access->connection->getFromCache($cacheKey); |
|
| 447 | + if(!is_null($groupNames) && isset($groupNames[$gid])) { |
|
| 448 | + return $groupNames[$gid]; |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + $domainObjectSid = $this->access->getSID($dn); |
|
| 452 | + if($domainObjectSid === false) { |
|
| 453 | + return false; |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + //we need to get the DN from LDAP |
|
| 457 | + $filter = $this->access->combineFilterWithAnd(array( |
|
| 458 | + $this->access->connection->ldapGroupFilter, |
|
| 459 | + 'objectsid=' . $domainObjectSid . '-' . $gid |
|
| 460 | + )); |
|
| 461 | + $result = $this->access->searchGroups($filter, array('dn'), 1); |
|
| 462 | + if(empty($result)) { |
|
| 463 | + return false; |
|
| 464 | + } |
|
| 465 | + $dn = $result[0]['dn'][0]; |
|
| 466 | + |
|
| 467 | + //and now the group name |
|
| 468 | + //NOTE once we have separate Nextcloud group IDs and group names we can |
|
| 469 | + //directly read the display name attribute instead of the DN |
|
| 470 | + $name = $this->access->dn2groupname($dn); |
|
| 471 | + |
|
| 472 | + $this->access->connection->writeToCache($cacheKey, $name); |
|
| 473 | + |
|
| 474 | + return $name; |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + /** |
|
| 478 | + * returns the entry's primary group ID |
|
| 479 | + * @param string $dn |
|
| 480 | + * @param string $attribute |
|
| 481 | + * @return string|bool |
|
| 482 | + */ |
|
| 483 | + private function getEntryGroupID($dn, $attribute) { |
|
| 484 | + $value = $this->access->readAttribute($dn, $attribute); |
|
| 485 | + if(is_array($value) && !empty($value)) { |
|
| 486 | + return $value[0]; |
|
| 487 | + } |
|
| 488 | + return false; |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + /** |
|
| 492 | + * returns the group's primary ID |
|
| 493 | + * @param string $dn |
|
| 494 | + * @return string|bool |
|
| 495 | + */ |
|
| 496 | + public function getGroupPrimaryGroupID($dn) { |
|
| 497 | + return $this->getEntryGroupID($dn, 'primaryGroupToken'); |
|
| 498 | + } |
|
| 499 | + |
|
| 500 | + /** |
|
| 501 | + * returns the user's primary group ID |
|
| 502 | + * @param string $dn |
|
| 503 | + * @return string|bool |
|
| 504 | + */ |
|
| 505 | + public function getUserPrimaryGroupIDs($dn) { |
|
| 506 | + $primaryGroupID = false; |
|
| 507 | + if($this->access->connection->hasPrimaryGroups) { |
|
| 508 | + $primaryGroupID = $this->getEntryGroupID($dn, 'primaryGroupID'); |
|
| 509 | + if($primaryGroupID === false) { |
|
| 510 | + $this->access->connection->hasPrimaryGroups = false; |
|
| 511 | + } |
|
| 512 | + } |
|
| 513 | + return $primaryGroupID; |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + /** |
|
| 517 | + * returns a filter for a "users in primary group" search or count operation |
|
| 518 | + * |
|
| 519 | + * @param string $groupDN |
|
| 520 | + * @param string $search |
|
| 521 | + * @return string |
|
| 522 | + * @throws \Exception |
|
| 523 | + */ |
|
| 524 | + private function prepareFilterForUsersInPrimaryGroup($groupDN, $search = '') { |
|
| 525 | + $groupID = $this->getGroupPrimaryGroupID($groupDN); |
|
| 526 | + if($groupID === false) { |
|
| 527 | + throw new \Exception('Not a valid group'); |
|
| 528 | + } |
|
| 529 | + |
|
| 530 | + $filterParts = []; |
|
| 531 | + $filterParts[] = $this->access->getFilterForUserCount(); |
|
| 532 | + if ($search !== '') { |
|
| 533 | + $filterParts[] = $this->access->getFilterPartForUserSearch($search); |
|
| 534 | + } |
|
| 535 | + $filterParts[] = 'primaryGroupID=' . $groupID; |
|
| 536 | + |
|
| 537 | + $filter = $this->access->combineFilterWithAnd($filterParts); |
|
| 538 | + |
|
| 539 | + return $filter; |
|
| 540 | + } |
|
| 541 | + |
|
| 542 | + /** |
|
| 543 | + * returns a list of users that have the given group as primary group |
|
| 544 | + * |
|
| 545 | + * @param string $groupDN |
|
| 546 | + * @param string $search |
|
| 547 | + * @param int $limit |
|
| 548 | + * @param int $offset |
|
| 549 | + * @return string[] |
|
| 550 | + */ |
|
| 551 | + public function getUsersInPrimaryGroup($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
| 552 | + try { |
|
| 553 | + $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search); |
|
| 554 | + $users = $this->access->fetchListOfUsers( |
|
| 555 | + $filter, |
|
| 556 | + array($this->access->connection->ldapUserDisplayName, 'dn'), |
|
| 557 | + $limit, |
|
| 558 | + $offset |
|
| 559 | + ); |
|
| 560 | + return $this->access->nextcloudUserNames($users); |
|
| 561 | + } catch (\Exception $e) { |
|
| 562 | + return array(); |
|
| 563 | + } |
|
| 564 | + } |
|
| 565 | + |
|
| 566 | + /** |
|
| 567 | + * returns the number of users that have the given group as primary group |
|
| 568 | + * |
|
| 569 | + * @param string $groupDN |
|
| 570 | + * @param string $search |
|
| 571 | + * @param int $limit |
|
| 572 | + * @param int $offset |
|
| 573 | + * @return int |
|
| 574 | + */ |
|
| 575 | + public function countUsersInPrimaryGroup($groupDN, $search = '', $limit = -1, $offset = 0) { |
|
| 576 | + try { |
|
| 577 | + $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search); |
|
| 578 | + $users = $this->access->countUsers($filter, array('dn'), $limit, $offset); |
|
| 579 | + return (int)$users; |
|
| 580 | + } catch (\Exception $e) { |
|
| 581 | + return 0; |
|
| 582 | + } |
|
| 583 | + } |
|
| 584 | + |
|
| 585 | + /** |
|
| 586 | + * gets the primary group of a user |
|
| 587 | + * @param string $dn |
|
| 588 | + * @return string |
|
| 589 | + */ |
|
| 590 | + public function getUserPrimaryGroup($dn) { |
|
| 591 | + $groupID = $this->getUserPrimaryGroupIDs($dn); |
|
| 592 | + if($groupID !== false) { |
|
| 593 | + $groupName = $this->primaryGroupID2Name($groupID, $dn); |
|
| 594 | + if($groupName !== false) { |
|
| 595 | + return $groupName; |
|
| 596 | + } |
|
| 597 | + } |
|
| 598 | + |
|
| 599 | + return false; |
|
| 600 | + } |
|
| 601 | + |
|
| 602 | + /** |
|
| 603 | + * Get all groups a user belongs to |
|
| 604 | + * @param string $uid Name of the user |
|
| 605 | + * @return array with group names |
|
| 606 | + * |
|
| 607 | + * This function fetches all groups a user belongs to. It does not check |
|
| 608 | + * if the user exists at all. |
|
| 609 | + * |
|
| 610 | + * This function includes groups based on dynamic group membership. |
|
| 611 | + */ |
|
| 612 | + public function getUserGroups($uid) { |
|
| 613 | + if(!$this->enabled) { |
|
| 614 | + return array(); |
|
| 615 | + } |
|
| 616 | + $cacheKey = 'getUserGroups'.$uid; |
|
| 617 | + $userGroups = $this->access->connection->getFromCache($cacheKey); |
|
| 618 | + if(!is_null($userGroups)) { |
|
| 619 | + return $userGroups; |
|
| 620 | + } |
|
| 621 | + $userDN = $this->access->username2dn($uid); |
|
| 622 | + if(!$userDN) { |
|
| 623 | + $this->access->connection->writeToCache($cacheKey, array()); |
|
| 624 | + return array(); |
|
| 625 | + } |
|
| 626 | + |
|
| 627 | + $groups = []; |
|
| 628 | + $primaryGroup = $this->getUserPrimaryGroup($userDN); |
|
| 629 | + $gidGroupName = $this->getUserGroupByGid($userDN); |
|
| 630 | + |
|
| 631 | + $dynamicGroupMemberURL = strtolower($this->access->connection->ldapDynamicGroupMemberURL); |
|
| 632 | + |
|
| 633 | + if (!empty($dynamicGroupMemberURL)) { |
|
| 634 | + // look through dynamic groups to add them to the result array if needed |
|
| 635 | + $groupsToMatch = $this->access->fetchListOfGroups( |
|
| 636 | + $this->access->connection->ldapGroupFilter,array('dn',$dynamicGroupMemberURL)); |
|
| 637 | + foreach($groupsToMatch as $dynamicGroup) { |
|
| 638 | + if (!array_key_exists($dynamicGroupMemberURL, $dynamicGroup)) { |
|
| 639 | + continue; |
|
| 640 | + } |
|
| 641 | + $pos = strpos($dynamicGroup[$dynamicGroupMemberURL][0], '('); |
|
| 642 | + if ($pos !== false) { |
|
| 643 | + $memberUrlFilter = substr($dynamicGroup[$dynamicGroupMemberURL][0],$pos); |
|
| 644 | + // apply filter via ldap search to see if this user is in this |
|
| 645 | + // dynamic group |
|
| 646 | + $userMatch = $this->access->readAttribute( |
|
| 647 | + $userDN, |
|
| 648 | + $this->access->connection->ldapUserDisplayName, |
|
| 649 | + $memberUrlFilter |
|
| 650 | + ); |
|
| 651 | + if ($userMatch !== false) { |
|
| 652 | + // match found so this user is in this group |
|
| 653 | + $groupName = $this->access->dn2groupname($dynamicGroup['dn'][0]); |
|
| 654 | + if(is_string($groupName)) { |
|
| 655 | + // be sure to never return false if the dn could not be |
|
| 656 | + // resolved to a name, for whatever reason. |
|
| 657 | + $groups[] = $groupName; |
|
| 658 | + } |
|
| 659 | + } |
|
| 660 | + } else { |
|
| 661 | + \OCP\Util::writeLog('user_ldap', 'No search filter found on member url '. |
|
| 662 | + 'of group ' . print_r($dynamicGroup, true), \OCP\Util::DEBUG); |
|
| 663 | + } |
|
| 664 | + } |
|
| 665 | + } |
|
| 666 | + |
|
| 667 | + // if possible, read out membership via memberOf. It's far faster than |
|
| 668 | + // performing a search, which still is a fallback later. |
|
| 669 | + // memberof doesn't support memberuid, so skip it here. |
|
| 670 | + if((int)$this->access->connection->hasMemberOfFilterSupport === 1 |
|
| 671 | + && (int)$this->access->connection->useMemberOfToDetectMembership === 1 |
|
| 672 | + && strtolower($this->access->connection->ldapGroupMemberAssocAttr) !== 'memberuid' |
|
| 673 | + ) { |
|
| 674 | + $groupDNs = $this->_getGroupDNsFromMemberOf($userDN); |
|
| 675 | + if (is_array($groupDNs)) { |
|
| 676 | + foreach ($groupDNs as $dn) { |
|
| 677 | + $groupName = $this->access->dn2groupname($dn); |
|
| 678 | + if(is_string($groupName)) { |
|
| 679 | + // be sure to never return false if the dn could not be |
|
| 680 | + // resolved to a name, for whatever reason. |
|
| 681 | + $groups[] = $groupName; |
|
| 682 | + } |
|
| 683 | + } |
|
| 684 | + } |
|
| 685 | + |
|
| 686 | + if($primaryGroup !== false) { |
|
| 687 | + $groups[] = $primaryGroup; |
|
| 688 | + } |
|
| 689 | + if($gidGroupName !== false) { |
|
| 690 | + $groups[] = $gidGroupName; |
|
| 691 | + } |
|
| 692 | + $this->access->connection->writeToCache($cacheKey, $groups); |
|
| 693 | + return $groups; |
|
| 694 | + } |
|
| 695 | + |
|
| 696 | + //uniqueMember takes DN, memberuid the uid, so we need to distinguish |
|
| 697 | + if((strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'uniquemember') |
|
| 698 | + || (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'member') |
|
| 699 | + ) { |
|
| 700 | + $uid = $userDN; |
|
| 701 | + } else if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
| 702 | + $result = $this->access->readAttribute($userDN, 'uid'); |
|
| 703 | + if ($result === false) { |
|
| 704 | + \OCP\Util::writeLog('user_ldap', 'No uid attribute found for DN ' . $userDN . ' on '. |
|
| 705 | + $this->access->connection->ldapHost, \OCP\Util::DEBUG); |
|
| 706 | + } |
|
| 707 | + $uid = $result[0]; |
|
| 708 | + } else { |
|
| 709 | + // just in case |
|
| 710 | + $uid = $userDN; |
|
| 711 | + } |
|
| 712 | + |
|
| 713 | + if(isset($this->cachedGroupsByMember[$uid])) { |
|
| 714 | + $groups = array_merge($groups, $this->cachedGroupsByMember[$uid]); |
|
| 715 | + } else { |
|
| 716 | + $groupsByMember = array_values($this->getGroupsByMember($uid)); |
|
| 717 | + $groupsByMember = $this->access->nextcloudGroupNames($groupsByMember); |
|
| 718 | + $this->cachedGroupsByMember[$uid] = $groupsByMember; |
|
| 719 | + $groups = array_merge($groups, $groupsByMember); |
|
| 720 | + } |
|
| 721 | + |
|
| 722 | + if($primaryGroup !== false) { |
|
| 723 | + $groups[] = $primaryGroup; |
|
| 724 | + } |
|
| 725 | + if($gidGroupName !== false) { |
|
| 726 | + $groups[] = $gidGroupName; |
|
| 727 | + } |
|
| 728 | + |
|
| 729 | + $groups = array_unique($groups, SORT_LOCALE_STRING); |
|
| 730 | + $this->access->connection->writeToCache($cacheKey, $groups); |
|
| 731 | + |
|
| 732 | + return $groups; |
|
| 733 | + } |
|
| 734 | + |
|
| 735 | + /** |
|
| 736 | + * @param string $dn |
|
| 737 | + * @param array|null &$seen |
|
| 738 | + * @return array |
|
| 739 | + */ |
|
| 740 | + private function getGroupsByMember($dn, &$seen = null) { |
|
| 741 | + if ($seen === null) { |
|
| 742 | + $seen = array(); |
|
| 743 | + } |
|
| 744 | + $allGroups = array(); |
|
| 745 | + if (array_key_exists($dn, $seen)) { |
|
| 746 | + // avoid loops |
|
| 747 | + return array(); |
|
| 748 | + } |
|
| 749 | + $seen[$dn] = true; |
|
| 750 | + $filter = $this->access->combineFilterWithAnd(array( |
|
| 751 | + $this->access->connection->ldapGroupFilter, |
|
| 752 | + $this->access->connection->ldapGroupMemberAssocAttr.'='.$dn |
|
| 753 | + )); |
|
| 754 | + $groups = $this->access->fetchListOfGroups($filter, |
|
| 755 | + array($this->access->connection->ldapGroupDisplayName, 'dn')); |
|
| 756 | + if (is_array($groups)) { |
|
| 757 | + foreach ($groups as $groupobj) { |
|
| 758 | + $groupDN = $groupobj['dn'][0]; |
|
| 759 | + $allGroups[$groupDN] = $groupobj; |
|
| 760 | + $nestedGroups = $this->access->connection->ldapNestedGroups; |
|
| 761 | + if (!empty($nestedGroups)) { |
|
| 762 | + $supergroups = $this->getGroupsByMember($groupDN, $seen); |
|
| 763 | + if (is_array($supergroups) && (count($supergroups)>0)) { |
|
| 764 | + $allGroups = array_merge($allGroups, $supergroups); |
|
| 765 | + } |
|
| 766 | + } |
|
| 767 | + } |
|
| 768 | + } |
|
| 769 | + return $allGroups; |
|
| 770 | + } |
|
| 771 | + |
|
| 772 | + /** |
|
| 773 | + * get a list of all users in a group |
|
| 774 | + * |
|
| 775 | + * @param string $gid |
|
| 776 | + * @param string $search |
|
| 777 | + * @param int $limit |
|
| 778 | + * @param int $offset |
|
| 779 | + * @return array with user ids |
|
| 780 | + */ |
|
| 781 | + public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
|
| 782 | + if(!$this->enabled) { |
|
| 783 | + return array(); |
|
| 784 | + } |
|
| 785 | + if(!$this->groupExists($gid)) { |
|
| 786 | + return array(); |
|
| 787 | + } |
|
| 788 | + $search = $this->access->escapeFilterPart($search, true); |
|
| 789 | + $cacheKey = 'usersInGroup-'.$gid.'-'.$search.'-'.$limit.'-'.$offset; |
|
| 790 | + // check for cache of the exact query |
|
| 791 | + $groupUsers = $this->access->connection->getFromCache($cacheKey); |
|
| 792 | + if(!is_null($groupUsers)) { |
|
| 793 | + return $groupUsers; |
|
| 794 | + } |
|
| 795 | + |
|
| 796 | + // check for cache of the query without limit and offset |
|
| 797 | + $groupUsers = $this->access->connection->getFromCache('usersInGroup-'.$gid.'-'.$search); |
|
| 798 | + if(!is_null($groupUsers)) { |
|
| 799 | + $groupUsers = array_slice($groupUsers, $offset, $limit); |
|
| 800 | + $this->access->connection->writeToCache($cacheKey, $groupUsers); |
|
| 801 | + return $groupUsers; |
|
| 802 | + } |
|
| 803 | + |
|
| 804 | + if($limit === -1) { |
|
| 805 | + $limit = null; |
|
| 806 | + } |
|
| 807 | + $groupDN = $this->access->groupname2dn($gid); |
|
| 808 | + if(!$groupDN) { |
|
| 809 | + // group couldn't be found, return empty resultset |
|
| 810 | + $this->access->connection->writeToCache($cacheKey, array()); |
|
| 811 | + return array(); |
|
| 812 | + } |
|
| 813 | + |
|
| 814 | + $primaryUsers = $this->getUsersInPrimaryGroup($groupDN, $search, $limit, $offset); |
|
| 815 | + $posixGroupUsers = $this->getUsersInGidNumber($groupDN, $search, $limit, $offset); |
|
| 816 | + $members = array_keys($this->_groupMembers($groupDN)); |
|
| 817 | + if(!$members && empty($posixGroupUsers) && empty($primaryUsers)) { |
|
| 818 | + //in case users could not be retrieved, return empty result set |
|
| 819 | + $this->access->connection->writeToCache($cacheKey, []); |
|
| 820 | + return []; |
|
| 821 | + } |
|
| 822 | + |
|
| 823 | + $groupUsers = array(); |
|
| 824 | + $isMemberUid = (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid'); |
|
| 825 | + $attrs = $this->access->userManager->getAttributes(true); |
|
| 826 | + foreach($members as $member) { |
|
| 827 | + if($isMemberUid) { |
|
| 828 | + //we got uids, need to get their DNs to 'translate' them to user names |
|
| 829 | + $filter = $this->access->combineFilterWithAnd(array( |
|
| 830 | + str_replace('%uid', $member, $this->access->connection->ldapLoginFilter), |
|
| 831 | + $this->access->getFilterPartForUserSearch($search) |
|
| 832 | + )); |
|
| 833 | + $ldap_users = $this->access->fetchListOfUsers($filter, $attrs, 1); |
|
| 834 | + if(count($ldap_users) < 1) { |
|
| 835 | + continue; |
|
| 836 | + } |
|
| 837 | + $groupUsers[] = $this->access->dn2username($ldap_users[0]['dn'][0]); |
|
| 838 | + } else { |
|
| 839 | + //we got DNs, check if we need to filter by search or we can give back all of them |
|
| 840 | + if ($search !== '') { |
|
| 841 | + if(!$this->access->readAttribute($member, |
|
| 842 | + $this->access->connection->ldapUserDisplayName, |
|
| 843 | + $this->access->getFilterPartForUserSearch($search))) { |
|
| 844 | + continue; |
|
| 845 | + } |
|
| 846 | + } |
|
| 847 | + // dn2username will also check if the users belong to the allowed base |
|
| 848 | + if($ocname = $this->access->dn2username($member)) { |
|
| 849 | + $groupUsers[] = $ocname; |
|
| 850 | + } |
|
| 851 | + } |
|
| 852 | + } |
|
| 853 | + |
|
| 854 | + $groupUsers = array_unique(array_merge($groupUsers, $primaryUsers, $posixGroupUsers)); |
|
| 855 | + natsort($groupUsers); |
|
| 856 | + $this->access->connection->writeToCache('usersInGroup-'.$gid.'-'.$search, $groupUsers); |
|
| 857 | + $groupUsers = array_slice($groupUsers, $offset, $limit); |
|
| 858 | + |
|
| 859 | + $this->access->connection->writeToCache($cacheKey, $groupUsers); |
|
| 860 | + |
|
| 861 | + return $groupUsers; |
|
| 862 | + } |
|
| 863 | + |
|
| 864 | + /** |
|
| 865 | + * returns the number of users in a group, who match the search term |
|
| 866 | + * @param string $gid the internal group name |
|
| 867 | + * @param string $search optional, a search string |
|
| 868 | + * @return int|bool |
|
| 869 | + */ |
|
| 870 | + public function countUsersInGroup($gid, $search = '') { |
|
| 871 | + if ($this->groupPluginManager->implementsActions(GroupInterface::COUNT_USERS)) { |
|
| 872 | + return $this->groupPluginManager->countUsersInGroup($gid, $search); |
|
| 873 | + } |
|
| 874 | + |
|
| 875 | + $cacheKey = 'countUsersInGroup-'.$gid.'-'.$search; |
|
| 876 | + if(!$this->enabled || !$this->groupExists($gid)) { |
|
| 877 | + return false; |
|
| 878 | + } |
|
| 879 | + $groupUsers = $this->access->connection->getFromCache($cacheKey); |
|
| 880 | + if(!is_null($groupUsers)) { |
|
| 881 | + return $groupUsers; |
|
| 882 | + } |
|
| 883 | + |
|
| 884 | + $groupDN = $this->access->groupname2dn($gid); |
|
| 885 | + if(!$groupDN) { |
|
| 886 | + // group couldn't be found, return empty result set |
|
| 887 | + $this->access->connection->writeToCache($cacheKey, false); |
|
| 888 | + return false; |
|
| 889 | + } |
|
| 890 | + |
|
| 891 | + $members = array_keys($this->_groupMembers($groupDN)); |
|
| 892 | + $primaryUserCount = $this->countUsersInPrimaryGroup($groupDN, ''); |
|
| 893 | + if(!$members && $primaryUserCount === 0) { |
|
| 894 | + //in case users could not be retrieved, return empty result set |
|
| 895 | + $this->access->connection->writeToCache($cacheKey, false); |
|
| 896 | + return false; |
|
| 897 | + } |
|
| 898 | + |
|
| 899 | + if ($search === '') { |
|
| 900 | + $groupUsers = count($members) + $primaryUserCount; |
|
| 901 | + $this->access->connection->writeToCache($cacheKey, $groupUsers); |
|
| 902 | + return $groupUsers; |
|
| 903 | + } |
|
| 904 | + $search = $this->access->escapeFilterPart($search, true); |
|
| 905 | + $isMemberUid = |
|
| 906 | + (strtolower($this->access->connection->ldapGroupMemberAssocAttr) |
|
| 907 | + === 'memberuid'); |
|
| 908 | + |
|
| 909 | + //we need to apply the search filter |
|
| 910 | + //alternatives that need to be checked: |
|
| 911 | + //a) get all users by search filter and array_intersect them |
|
| 912 | + //b) a, but only when less than 1k 10k ?k users like it is |
|
| 913 | + //c) put all DNs|uids in a LDAP filter, combine with the search string |
|
| 914 | + // and let it count. |
|
| 915 | + //For now this is not important, because the only use of this method |
|
| 916 | + //does not supply a search string |
|
| 917 | + $groupUsers = array(); |
|
| 918 | + foreach($members as $member) { |
|
| 919 | + if($isMemberUid) { |
|
| 920 | + //we got uids, need to get their DNs to 'translate' them to user names |
|
| 921 | + $filter = $this->access->combineFilterWithAnd(array( |
|
| 922 | + str_replace('%uid', $member, $this->access->connection->ldapLoginFilter), |
|
| 923 | + $this->access->getFilterPartForUserSearch($search) |
|
| 924 | + )); |
|
| 925 | + $ldap_users = $this->access->fetchListOfUsers($filter, 'dn', 1); |
|
| 926 | + if(count($ldap_users) < 1) { |
|
| 927 | + continue; |
|
| 928 | + } |
|
| 929 | + $groupUsers[] = $this->access->dn2username($ldap_users[0]); |
|
| 930 | + } else { |
|
| 931 | + //we need to apply the search filter now |
|
| 932 | + if(!$this->access->readAttribute($member, |
|
| 933 | + $this->access->connection->ldapUserDisplayName, |
|
| 934 | + $this->access->getFilterPartForUserSearch($search))) { |
|
| 935 | + continue; |
|
| 936 | + } |
|
| 937 | + // dn2username will also check if the users belong to the allowed base |
|
| 938 | + if($ocname = $this->access->dn2username($member)) { |
|
| 939 | + $groupUsers[] = $ocname; |
|
| 940 | + } |
|
| 941 | + } |
|
| 942 | + } |
|
| 943 | + |
|
| 944 | + //and get users that have the group as primary |
|
| 945 | + $primaryUsers = $this->countUsersInPrimaryGroup($groupDN, $search); |
|
| 946 | + |
|
| 947 | + return count($groupUsers) + $primaryUsers; |
|
| 948 | + } |
|
| 949 | + |
|
| 950 | + /** |
|
| 951 | + * get a list of all groups |
|
| 952 | + * |
|
| 953 | + * @param string $search |
|
| 954 | + * @param $limit |
|
| 955 | + * @param int $offset |
|
| 956 | + * @return array with group names |
|
| 957 | + * |
|
| 958 | + * Returns a list with all groups (used by getGroups) |
|
| 959 | + */ |
|
| 960 | + protected function getGroupsChunk($search = '', $limit = -1, $offset = 0) { |
|
| 961 | + if(!$this->enabled) { |
|
| 962 | + return array(); |
|
| 963 | + } |
|
| 964 | + $cacheKey = 'getGroups-'.$search.'-'.$limit.'-'.$offset; |
|
| 965 | + |
|
| 966 | + //Check cache before driving unnecessary searches |
|
| 967 | + \OCP\Util::writeLog('user_ldap', 'getGroups '.$cacheKey, \OCP\Util::DEBUG); |
|
| 968 | + $ldap_groups = $this->access->connection->getFromCache($cacheKey); |
|
| 969 | + if(!is_null($ldap_groups)) { |
|
| 970 | + return $ldap_groups; |
|
| 971 | + } |
|
| 972 | + |
|
| 973 | + // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
|
| 974 | + // error. With a limit of 0, we get 0 results. So we pass null. |
|
| 975 | + if($limit <= 0) { |
|
| 976 | + $limit = null; |
|
| 977 | + } |
|
| 978 | + $filter = $this->access->combineFilterWithAnd(array( |
|
| 979 | + $this->access->connection->ldapGroupFilter, |
|
| 980 | + $this->access->getFilterPartForGroupSearch($search) |
|
| 981 | + )); |
|
| 982 | + \OCP\Util::writeLog('user_ldap', 'getGroups Filter '.$filter, \OCP\Util::DEBUG); |
|
| 983 | + $ldap_groups = $this->access->fetchListOfGroups($filter, |
|
| 984 | + array($this->access->connection->ldapGroupDisplayName, 'dn'), |
|
| 985 | + $limit, |
|
| 986 | + $offset); |
|
| 987 | + $ldap_groups = $this->access->nextcloudGroupNames($ldap_groups); |
|
| 988 | + |
|
| 989 | + $this->access->connection->writeToCache($cacheKey, $ldap_groups); |
|
| 990 | + return $ldap_groups; |
|
| 991 | + } |
|
| 992 | + |
|
| 993 | + /** |
|
| 994 | + * get a list of all groups using a paged search |
|
| 995 | + * |
|
| 996 | + * @param string $search |
|
| 997 | + * @param int $limit |
|
| 998 | + * @param int $offset |
|
| 999 | + * @return array with group names |
|
| 1000 | + * |
|
| 1001 | + * Returns a list with all groups |
|
| 1002 | + * Uses a paged search if available to override a |
|
| 1003 | + * server side search limit. |
|
| 1004 | + * (active directory has a limit of 1000 by default) |
|
| 1005 | + */ |
|
| 1006 | + public function getGroups($search = '', $limit = -1, $offset = 0) { |
|
| 1007 | + if(!$this->enabled) { |
|
| 1008 | + return array(); |
|
| 1009 | + } |
|
| 1010 | + $search = $this->access->escapeFilterPart($search, true); |
|
| 1011 | + $pagingSize = (int)$this->access->connection->ldapPagingSize; |
|
| 1012 | + if (!$this->access->connection->hasPagedResultSupport || $pagingSize <= 0) { |
|
| 1013 | + return $this->getGroupsChunk($search, $limit, $offset); |
|
| 1014 | + } |
|
| 1015 | + $maxGroups = 100000; // limit max results (just for safety reasons) |
|
| 1016 | + if ($limit > -1) { |
|
| 1017 | + $overallLimit = min($limit + $offset, $maxGroups); |
|
| 1018 | + } else { |
|
| 1019 | + $overallLimit = $maxGroups; |
|
| 1020 | + } |
|
| 1021 | + $chunkOffset = $offset; |
|
| 1022 | + $allGroups = array(); |
|
| 1023 | + while ($chunkOffset < $overallLimit) { |
|
| 1024 | + $chunkLimit = min($pagingSize, $overallLimit - $chunkOffset); |
|
| 1025 | + $ldapGroups = $this->getGroupsChunk($search, $chunkLimit, $chunkOffset); |
|
| 1026 | + $nread = count($ldapGroups); |
|
| 1027 | + \OCP\Util::writeLog('user_ldap', 'getGroups('.$search.'): read '.$nread.' at offset '.$chunkOffset.' (limit: '.$chunkLimit.')', \OCP\Util::DEBUG); |
|
| 1028 | + if ($nread) { |
|
| 1029 | + $allGroups = array_merge($allGroups, $ldapGroups); |
|
| 1030 | + $chunkOffset += $nread; |
|
| 1031 | + } |
|
| 1032 | + if ($nread < $chunkLimit) { |
|
| 1033 | + break; |
|
| 1034 | + } |
|
| 1035 | + } |
|
| 1036 | + return $allGroups; |
|
| 1037 | + } |
|
| 1038 | + |
|
| 1039 | + /** |
|
| 1040 | + * @param string $group |
|
| 1041 | + * @return bool |
|
| 1042 | + */ |
|
| 1043 | + public function groupMatchesFilter($group) { |
|
| 1044 | + return (strripos($group, $this->groupSearch) !== false); |
|
| 1045 | + } |
|
| 1046 | + |
|
| 1047 | + /** |
|
| 1048 | + * check if a group exists |
|
| 1049 | + * @param string $gid |
|
| 1050 | + * @return bool |
|
| 1051 | + */ |
|
| 1052 | + public function groupExists($gid) { |
|
| 1053 | + $groupExists = $this->access->connection->getFromCache('groupExists'.$gid); |
|
| 1054 | + if(!is_null($groupExists)) { |
|
| 1055 | + return (bool)$groupExists; |
|
| 1056 | + } |
|
| 1057 | + |
|
| 1058 | + //getting dn, if false the group does not exist. If dn, it may be mapped |
|
| 1059 | + //only, requires more checking. |
|
| 1060 | + $dn = $this->access->groupname2dn($gid); |
|
| 1061 | + if(!$dn) { |
|
| 1062 | + $this->access->connection->writeToCache('groupExists'.$gid, false); |
|
| 1063 | + return false; |
|
| 1064 | + } |
|
| 1065 | + |
|
| 1066 | + //if group really still exists, we will be able to read its objectclass |
|
| 1067 | + if(!is_array($this->access->readAttribute($dn, ''))) { |
|
| 1068 | + $this->access->connection->writeToCache('groupExists'.$gid, false); |
|
| 1069 | + return false; |
|
| 1070 | + } |
|
| 1071 | + |
|
| 1072 | + $this->access->connection->writeToCache('groupExists'.$gid, true); |
|
| 1073 | + return true; |
|
| 1074 | + } |
|
| 1075 | + |
|
| 1076 | + /** |
|
| 1077 | + * Check if backend implements actions |
|
| 1078 | + * @param int $actions bitwise-or'ed actions |
|
| 1079 | + * @return boolean |
|
| 1080 | + * |
|
| 1081 | + * Returns the supported actions as int to be |
|
| 1082 | + * compared with GroupInterface::CREATE_GROUP etc. |
|
| 1083 | + */ |
|
| 1084 | + public function implementsActions($actions) { |
|
| 1085 | + return (bool)((GroupInterface::COUNT_USERS | |
|
| 1086 | + $this->groupPluginManager->getImplementedActions()) & $actions); |
|
| 1087 | + } |
|
| 1088 | + |
|
| 1089 | + /** |
|
| 1090 | + * Return access for LDAP interaction. |
|
| 1091 | + * @return Access instance of Access for LDAP interaction |
|
| 1092 | + */ |
|
| 1093 | + public function getLDAPAccess($gid) { |
|
| 1094 | + return $this->access; |
|
| 1095 | + } |
|
| 1096 | + |
|
| 1097 | + /** |
|
| 1098 | + * create a group |
|
| 1099 | + * @param string $gid |
|
| 1100 | + * @return bool |
|
| 1101 | + * @throws \Exception |
|
| 1102 | + */ |
|
| 1103 | + public function createGroup($gid) { |
|
| 1104 | + if ($this->groupPluginManager->implementsActions(GroupInterface::CREATE_GROUP)) { |
|
| 1105 | + if ($dn = $this->groupPluginManager->createGroup($gid)) { |
|
| 1106 | + //updates group mapping |
|
| 1107 | + $this->access->dn2ocname($dn, $gid, false); |
|
| 1108 | + $this->access->connection->writeToCache("groupExists".$gid, true); |
|
| 1109 | + } |
|
| 1110 | + return $dn != null; |
|
| 1111 | + } |
|
| 1112 | + throw new \Exception('Could not create group in LDAP backend.'); |
|
| 1113 | + } |
|
| 1114 | + |
|
| 1115 | + /** |
|
| 1116 | + * delete a group |
|
| 1117 | + * @param string $gid gid of the group to delete |
|
| 1118 | + * @return bool |
|
| 1119 | + * @throws \Exception |
|
| 1120 | + */ |
|
| 1121 | + public function deleteGroup($gid) { |
|
| 1122 | + if ($this->groupPluginManager->implementsActions(GroupInterface::DELETE_GROUP)) { |
|
| 1123 | + if ($ret = $this->groupPluginManager->deleteGroup($gid)) { |
|
| 1124 | + #delete group in nextcloud internal db |
|
| 1125 | + $this->access->getGroupMapper()->unmap($gid); |
|
| 1126 | + $this->access->connection->writeToCache("groupExists".$gid, false); |
|
| 1127 | + } |
|
| 1128 | + return $ret; |
|
| 1129 | + } |
|
| 1130 | + throw new \Exception('Could not delete group in LDAP backend.'); |
|
| 1131 | + } |
|
| 1132 | + |
|
| 1133 | + /** |
|
| 1134 | + * Add a user to a group |
|
| 1135 | + * @param string $uid Name of the user to add to group |
|
| 1136 | + * @param string $gid Name of the group in which add the user |
|
| 1137 | + * @return bool |
|
| 1138 | + * @throws \Exception |
|
| 1139 | + */ |
|
| 1140 | + public function addToGroup($uid, $gid) { |
|
| 1141 | + if ($this->groupPluginManager->implementsActions(GroupInterface::ADD_TO_GROUP)) { |
|
| 1142 | + if ($ret = $this->groupPluginManager->addToGroup($uid, $gid)) { |
|
| 1143 | + $this->access->connection->clearCache(); |
|
| 1144 | + } |
|
| 1145 | + return $ret; |
|
| 1146 | + } |
|
| 1147 | + throw new \Exception('Could not add user to group in LDAP backend.'); |
|
| 1148 | + } |
|
| 1149 | + |
|
| 1150 | + /** |
|
| 1151 | + * Removes a user from a group |
|
| 1152 | + * @param string $uid Name of the user to remove from group |
|
| 1153 | + * @param string $gid Name of the group from which remove the user |
|
| 1154 | + * @return bool |
|
| 1155 | + * @throws \Exception |
|
| 1156 | + */ |
|
| 1157 | + public function removeFromGroup($uid, $gid) { |
|
| 1158 | + if ($this->groupPluginManager->implementsActions(GroupInterface::REMOVE_FROM_GROUP)) { |
|
| 1159 | + if ($ret = $this->groupPluginManager->removeFromGroup($uid, $gid)) { |
|
| 1160 | + $this->access->connection->clearCache(); |
|
| 1161 | + } |
|
| 1162 | + return $ret; |
|
| 1163 | + } |
|
| 1164 | + throw new \Exception('Could not remove user from group in LDAP backend.'); |
|
| 1165 | + } |
|
| 1166 | + |
|
| 1167 | + /** |
|
| 1168 | + * Gets group details |
|
| 1169 | + * @param string $gid Name of the group |
|
| 1170 | + * @return array | false |
|
| 1171 | + * @throws \Exception |
|
| 1172 | + */ |
|
| 1173 | + public function getGroupDetails($gid) { |
|
| 1174 | + if ($this->groupPluginManager->implementsActions(GroupInterface::GROUP_DETAILS)) { |
|
| 1175 | + return $this->groupPluginManager->getGroupDetails($gid); |
|
| 1176 | + } |
|
| 1177 | + throw new \Exception('Could not get group details in LDAP backend.'); |
|
| 1178 | + } |
|
| 1179 | + |
|
| 1180 | + /** |
|
| 1181 | + * Return LDAP connection resource from a cloned connection. |
|
| 1182 | + * The cloned connection needs to be closed manually. |
|
| 1183 | + * of the current access. |
|
| 1184 | + * @param string $gid |
|
| 1185 | + * @return resource of the LDAP connection |
|
| 1186 | + */ |
|
| 1187 | + public function getNewLDAPConnection($gid) { |
|
| 1188 | + $connection = clone $this->access->getConnection(); |
|
| 1189 | + return $connection->getConnectionResource(); |
|
| 1190 | + } |
|
| 1191 | 1191 | |
| 1192 | 1192 | } |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | parent::__construct($access); |
| 65 | 65 | $filter = $this->access->connection->ldapGroupFilter; |
| 66 | 66 | $gassoc = $this->access->connection->ldapGroupMemberAssocAttr; |
| 67 | - if(!empty($filter) && !empty($gassoc)) { |
|
| 67 | + if (!empty($filter) && !empty($gassoc)) { |
|
| 68 | 68 | $this->enabled = true; |
| 69 | 69 | } |
| 70 | 70 | |
@@ -82,25 +82,25 @@ discard block |
||
| 82 | 82 | * Checks whether the user is member of a group or not. |
| 83 | 83 | */ |
| 84 | 84 | public function inGroup($uid, $gid) { |
| 85 | - if(!$this->enabled) { |
|
| 85 | + if (!$this->enabled) { |
|
| 86 | 86 | return false; |
| 87 | 87 | } |
| 88 | 88 | $cacheKey = 'inGroup'.$uid.':'.$gid; |
| 89 | 89 | $inGroup = $this->access->connection->getFromCache($cacheKey); |
| 90 | - if(!is_null($inGroup)) { |
|
| 91 | - return (bool)$inGroup; |
|
| 90 | + if (!is_null($inGroup)) { |
|
| 91 | + return (bool) $inGroup; |
|
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | $userDN = $this->access->username2dn($uid); |
| 95 | 95 | |
| 96 | - if(isset($this->cachedGroupMembers[$gid])) { |
|
| 96 | + if (isset($this->cachedGroupMembers[$gid])) { |
|
| 97 | 97 | $isInGroup = in_array($userDN, $this->cachedGroupMembers[$gid]); |
| 98 | 98 | return $isInGroup; |
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | $cacheKeyMembers = 'inGroup-members:'.$gid; |
| 102 | 102 | $members = $this->access->connection->getFromCache($cacheKeyMembers); |
| 103 | - if(!is_null($members)) { |
|
| 103 | + if (!is_null($members)) { |
|
| 104 | 104 | $this->cachedGroupMembers[$gid] = $members; |
| 105 | 105 | $isInGroup = in_array($userDN, $members); |
| 106 | 106 | $this->access->connection->writeToCache($cacheKey, $isInGroup); |
@@ -109,13 +109,13 @@ discard block |
||
| 109 | 109 | |
| 110 | 110 | $groupDN = $this->access->groupname2dn($gid); |
| 111 | 111 | // just in case |
| 112 | - if(!$groupDN || !$userDN) { |
|
| 112 | + if (!$groupDN || !$userDN) { |
|
| 113 | 113 | $this->access->connection->writeToCache($cacheKey, false); |
| 114 | 114 | return false; |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | //check primary group first |
| 118 | - if($gid === $this->getUserPrimaryGroup($userDN)) { |
|
| 118 | + if ($gid === $this->getUserPrimaryGroup($userDN)) { |
|
| 119 | 119 | $this->access->connection->writeToCache($cacheKey, true); |
| 120 | 120 | return true; |
| 121 | 121 | } |
@@ -123,21 +123,21 @@ discard block |
||
| 123 | 123 | //usually, LDAP attributes are said to be case insensitive. But there are exceptions of course. |
| 124 | 124 | $members = $this->_groupMembers($groupDN); |
| 125 | 125 | $members = array_keys($members); // uids are returned as keys |
| 126 | - if(!is_array($members) || count($members) === 0) { |
|
| 126 | + if (!is_array($members) || count($members) === 0) { |
|
| 127 | 127 | $this->access->connection->writeToCache($cacheKey, false); |
| 128 | 128 | return false; |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | //extra work if we don't get back user DNs |
| 132 | - if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
| 132 | + if (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
| 133 | 133 | $dns = array(); |
| 134 | 134 | $filterParts = array(); |
| 135 | 135 | $bytes = 0; |
| 136 | - foreach($members as $mid) { |
|
| 136 | + foreach ($members as $mid) { |
|
| 137 | 137 | $filter = str_replace('%uid', $mid, $this->access->connection->ldapLoginFilter); |
| 138 | 138 | $filterParts[] = $filter; |
| 139 | 139 | $bytes += strlen($filter); |
| 140 | - if($bytes >= 9000000) { |
|
| 140 | + if ($bytes >= 9000000) { |
|
| 141 | 141 | // AD has a default input buffer of 10 MB, we do not want |
| 142 | 142 | // to take even the chance to exceed it |
| 143 | 143 | $filter = $this->access->combineFilterWithOr($filterParts); |
@@ -147,7 +147,7 @@ discard block |
||
| 147 | 147 | $dns = array_merge($dns, $users); |
| 148 | 148 | } |
| 149 | 149 | } |
| 150 | - if(count($filterParts) > 0) { |
|
| 150 | + if (count($filterParts) > 0) { |
|
| 151 | 151 | $filter = $this->access->combineFilterWithOr($filterParts); |
| 152 | 152 | $users = $this->access->fetchListOfUsers($filter, 'dn', count($filterParts)); |
| 153 | 153 | $dns = array_merge($dns, $users); |
@@ -190,14 +190,14 @@ discard block |
||
| 190 | 190 | $pos = strpos($memberURLs[0], '('); |
| 191 | 191 | if ($pos !== false) { |
| 192 | 192 | $memberUrlFilter = substr($memberURLs[0], $pos); |
| 193 | - $foundMembers = $this->access->searchUsers($memberUrlFilter,'dn'); |
|
| 193 | + $foundMembers = $this->access->searchUsers($memberUrlFilter, 'dn'); |
|
| 194 | 194 | $dynamicMembers = array(); |
| 195 | - foreach($foundMembers as $value) { |
|
| 195 | + foreach ($foundMembers as $value) { |
|
| 196 | 196 | $dynamicMembers[$value['dn'][0]] = 1; |
| 197 | 197 | } |
| 198 | 198 | } else { |
| 199 | 199 | \OCP\Util::writeLog('user_ldap', 'No search filter found on member url '. |
| 200 | - 'of group ' . $dnGroup, \OCP\Util::DEBUG); |
|
| 200 | + 'of group '.$dnGroup, \OCP\Util::DEBUG); |
|
| 201 | 201 | } |
| 202 | 202 | } |
| 203 | 203 | return $dynamicMembers; |
@@ -220,7 +220,7 @@ discard block |
||
| 220 | 220 | // used extensively in cron job, caching makes sense for nested groups |
| 221 | 221 | $cacheKey = '_groupMembers'.$dnGroup; |
| 222 | 222 | $groupMembers = $this->access->connection->getFromCache($cacheKey); |
| 223 | - if(!is_null($groupMembers)) { |
|
| 223 | + if (!is_null($groupMembers)) { |
|
| 224 | 224 | return $groupMembers; |
| 225 | 225 | } |
| 226 | 226 | $seen[$dnGroup] = 1; |
@@ -264,9 +264,9 @@ discard block |
||
| 264 | 264 | return array(); |
| 265 | 265 | } |
| 266 | 266 | $groups = $this->access->groupsMatchFilter($groups); |
| 267 | - $allGroups = $groups; |
|
| 267 | + $allGroups = $groups; |
|
| 268 | 268 | $nestedGroups = $this->access->connection->ldapNestedGroups; |
| 269 | - if ((int)$nestedGroups === 1) { |
|
| 269 | + if ((int) $nestedGroups === 1) { |
|
| 270 | 270 | foreach ($groups as $group) { |
| 271 | 271 | $subGroups = $this->_getGroupDNsFromMemberOf($group, $seen); |
| 272 | 272 | $allGroups = array_merge($allGroups, $subGroups); |
@@ -282,9 +282,9 @@ discard block |
||
| 282 | 282 | * @return string|bool |
| 283 | 283 | */ |
| 284 | 284 | public function gidNumber2Name($gid, $dn) { |
| 285 | - $cacheKey = 'gidNumberToName' . $gid; |
|
| 285 | + $cacheKey = 'gidNumberToName'.$gid; |
|
| 286 | 286 | $groupName = $this->access->connection->getFromCache($cacheKey); |
| 287 | - if(!is_null($groupName) && isset($groupName)) { |
|
| 287 | + if (!is_null($groupName) && isset($groupName)) { |
|
| 288 | 288 | return $groupName; |
| 289 | 289 | } |
| 290 | 290 | |
@@ -292,10 +292,10 @@ discard block |
||
| 292 | 292 | $filter = $this->access->combineFilterWithAnd([ |
| 293 | 293 | $this->access->connection->ldapGroupFilter, |
| 294 | 294 | 'objectClass=posixGroup', |
| 295 | - $this->access->connection->ldapGidNumber . '=' . $gid |
|
| 295 | + $this->access->connection->ldapGidNumber.'='.$gid |
|
| 296 | 296 | ]); |
| 297 | 297 | $result = $this->access->searchGroups($filter, array('dn'), 1); |
| 298 | - if(empty($result)) { |
|
| 298 | + if (empty($result)) { |
|
| 299 | 299 | return false; |
| 300 | 300 | } |
| 301 | 301 | $dn = $result[0]['dn'][0]; |
@@ -318,7 +318,7 @@ discard block |
||
| 318 | 318 | */ |
| 319 | 319 | private function getEntryGidNumber($dn, $attribute) { |
| 320 | 320 | $value = $this->access->readAttribute($dn, $attribute); |
| 321 | - if(is_array($value) && !empty($value)) { |
|
| 321 | + if (is_array($value) && !empty($value)) { |
|
| 322 | 322 | return $value[0]; |
| 323 | 323 | } |
| 324 | 324 | return false; |
@@ -340,9 +340,9 @@ discard block |
||
| 340 | 340 | */ |
| 341 | 341 | public function getUserGidNumber($dn) { |
| 342 | 342 | $gidNumber = false; |
| 343 | - if($this->access->connection->hasGidNumber) { |
|
| 343 | + if ($this->access->connection->hasGidNumber) { |
|
| 344 | 344 | $gidNumber = $this->getEntryGidNumber($dn, $this->access->connection->ldapGidNumber); |
| 345 | - if($gidNumber === false) { |
|
| 345 | + if ($gidNumber === false) { |
|
| 346 | 346 | $this->access->connection->hasGidNumber = false; |
| 347 | 347 | } |
| 348 | 348 | } |
@@ -359,7 +359,7 @@ discard block |
||
| 359 | 359 | */ |
| 360 | 360 | private function prepareFilterForUsersHasGidNumber($groupDN, $search = '') { |
| 361 | 361 | $groupID = $this->getGroupGidNumber($groupDN); |
| 362 | - if($groupID === false) { |
|
| 362 | + if ($groupID === false) { |
|
| 363 | 363 | throw new \Exception('Not a valid group'); |
| 364 | 364 | } |
| 365 | 365 | |
@@ -368,7 +368,7 @@ discard block |
||
| 368 | 368 | if ($search !== '') { |
| 369 | 369 | $filterParts[] = $this->access->getFilterPartForUserSearch($search); |
| 370 | 370 | } |
| 371 | - $filterParts[] = $this->access->connection->ldapGidNumber .'=' . $groupID; |
|
| 371 | + $filterParts[] = $this->access->connection->ldapGidNumber.'='.$groupID; |
|
| 372 | 372 | |
| 373 | 373 | $filter = $this->access->combineFilterWithAnd($filterParts); |
| 374 | 374 | |
@@ -412,7 +412,7 @@ discard block |
||
| 412 | 412 | try { |
| 413 | 413 | $filter = $this->prepareFilterForUsersHasGidNumber($groupDN, $search); |
| 414 | 414 | $users = $this->access->countUsers($filter, ['dn'], $limit, $offset); |
| 415 | - return (int)$users; |
|
| 415 | + return (int) $users; |
|
| 416 | 416 | } catch (\Exception $e) { |
| 417 | 417 | return 0; |
| 418 | 418 | } |
@@ -425,9 +425,9 @@ discard block |
||
| 425 | 425 | */ |
| 426 | 426 | public function getUserGroupByGid($dn) { |
| 427 | 427 | $groupID = $this->getUserGidNumber($dn); |
| 428 | - if($groupID !== false) { |
|
| 428 | + if ($groupID !== false) { |
|
| 429 | 429 | $groupName = $this->gidNumber2Name($groupID, $dn); |
| 430 | - if($groupName !== false) { |
|
| 430 | + if ($groupName !== false) { |
|
| 431 | 431 | return $groupName; |
| 432 | 432 | } |
| 433 | 433 | } |
@@ -444,22 +444,22 @@ discard block |
||
| 444 | 444 | public function primaryGroupID2Name($gid, $dn) { |
| 445 | 445 | $cacheKey = 'primaryGroupIDtoName'; |
| 446 | 446 | $groupNames = $this->access->connection->getFromCache($cacheKey); |
| 447 | - if(!is_null($groupNames) && isset($groupNames[$gid])) { |
|
| 447 | + if (!is_null($groupNames) && isset($groupNames[$gid])) { |
|
| 448 | 448 | return $groupNames[$gid]; |
| 449 | 449 | } |
| 450 | 450 | |
| 451 | 451 | $domainObjectSid = $this->access->getSID($dn); |
| 452 | - if($domainObjectSid === false) { |
|
| 452 | + if ($domainObjectSid === false) { |
|
| 453 | 453 | return false; |
| 454 | 454 | } |
| 455 | 455 | |
| 456 | 456 | //we need to get the DN from LDAP |
| 457 | 457 | $filter = $this->access->combineFilterWithAnd(array( |
| 458 | 458 | $this->access->connection->ldapGroupFilter, |
| 459 | - 'objectsid=' . $domainObjectSid . '-' . $gid |
|
| 459 | + 'objectsid='.$domainObjectSid.'-'.$gid |
|
| 460 | 460 | )); |
| 461 | 461 | $result = $this->access->searchGroups($filter, array('dn'), 1); |
| 462 | - if(empty($result)) { |
|
| 462 | + if (empty($result)) { |
|
| 463 | 463 | return false; |
| 464 | 464 | } |
| 465 | 465 | $dn = $result[0]['dn'][0]; |
@@ -482,7 +482,7 @@ discard block |
||
| 482 | 482 | */ |
| 483 | 483 | private function getEntryGroupID($dn, $attribute) { |
| 484 | 484 | $value = $this->access->readAttribute($dn, $attribute); |
| 485 | - if(is_array($value) && !empty($value)) { |
|
| 485 | + if (is_array($value) && !empty($value)) { |
|
| 486 | 486 | return $value[0]; |
| 487 | 487 | } |
| 488 | 488 | return false; |
@@ -504,9 +504,9 @@ discard block |
||
| 504 | 504 | */ |
| 505 | 505 | public function getUserPrimaryGroupIDs($dn) { |
| 506 | 506 | $primaryGroupID = false; |
| 507 | - if($this->access->connection->hasPrimaryGroups) { |
|
| 507 | + if ($this->access->connection->hasPrimaryGroups) { |
|
| 508 | 508 | $primaryGroupID = $this->getEntryGroupID($dn, 'primaryGroupID'); |
| 509 | - if($primaryGroupID === false) { |
|
| 509 | + if ($primaryGroupID === false) { |
|
| 510 | 510 | $this->access->connection->hasPrimaryGroups = false; |
| 511 | 511 | } |
| 512 | 512 | } |
@@ -523,7 +523,7 @@ discard block |
||
| 523 | 523 | */ |
| 524 | 524 | private function prepareFilterForUsersInPrimaryGroup($groupDN, $search = '') { |
| 525 | 525 | $groupID = $this->getGroupPrimaryGroupID($groupDN); |
| 526 | - if($groupID === false) { |
|
| 526 | + if ($groupID === false) { |
|
| 527 | 527 | throw new \Exception('Not a valid group'); |
| 528 | 528 | } |
| 529 | 529 | |
@@ -532,7 +532,7 @@ discard block |
||
| 532 | 532 | if ($search !== '') { |
| 533 | 533 | $filterParts[] = $this->access->getFilterPartForUserSearch($search); |
| 534 | 534 | } |
| 535 | - $filterParts[] = 'primaryGroupID=' . $groupID; |
|
| 535 | + $filterParts[] = 'primaryGroupID='.$groupID; |
|
| 536 | 536 | |
| 537 | 537 | $filter = $this->access->combineFilterWithAnd($filterParts); |
| 538 | 538 | |
@@ -576,7 +576,7 @@ discard block |
||
| 576 | 576 | try { |
| 577 | 577 | $filter = $this->prepareFilterForUsersInPrimaryGroup($groupDN, $search); |
| 578 | 578 | $users = $this->access->countUsers($filter, array('dn'), $limit, $offset); |
| 579 | - return (int)$users; |
|
| 579 | + return (int) $users; |
|
| 580 | 580 | } catch (\Exception $e) { |
| 581 | 581 | return 0; |
| 582 | 582 | } |
@@ -589,9 +589,9 @@ discard block |
||
| 589 | 589 | */ |
| 590 | 590 | public function getUserPrimaryGroup($dn) { |
| 591 | 591 | $groupID = $this->getUserPrimaryGroupIDs($dn); |
| 592 | - if($groupID !== false) { |
|
| 592 | + if ($groupID !== false) { |
|
| 593 | 593 | $groupName = $this->primaryGroupID2Name($groupID, $dn); |
| 594 | - if($groupName !== false) { |
|
| 594 | + if ($groupName !== false) { |
|
| 595 | 595 | return $groupName; |
| 596 | 596 | } |
| 597 | 597 | } |
@@ -610,16 +610,16 @@ discard block |
||
| 610 | 610 | * This function includes groups based on dynamic group membership. |
| 611 | 611 | */ |
| 612 | 612 | public function getUserGroups($uid) { |
| 613 | - if(!$this->enabled) { |
|
| 613 | + if (!$this->enabled) { |
|
| 614 | 614 | return array(); |
| 615 | 615 | } |
| 616 | 616 | $cacheKey = 'getUserGroups'.$uid; |
| 617 | 617 | $userGroups = $this->access->connection->getFromCache($cacheKey); |
| 618 | - if(!is_null($userGroups)) { |
|
| 618 | + if (!is_null($userGroups)) { |
|
| 619 | 619 | return $userGroups; |
| 620 | 620 | } |
| 621 | 621 | $userDN = $this->access->username2dn($uid); |
| 622 | - if(!$userDN) { |
|
| 622 | + if (!$userDN) { |
|
| 623 | 623 | $this->access->connection->writeToCache($cacheKey, array()); |
| 624 | 624 | return array(); |
| 625 | 625 | } |
@@ -633,14 +633,14 @@ discard block |
||
| 633 | 633 | if (!empty($dynamicGroupMemberURL)) { |
| 634 | 634 | // look through dynamic groups to add them to the result array if needed |
| 635 | 635 | $groupsToMatch = $this->access->fetchListOfGroups( |
| 636 | - $this->access->connection->ldapGroupFilter,array('dn',$dynamicGroupMemberURL)); |
|
| 637 | - foreach($groupsToMatch as $dynamicGroup) { |
|
| 636 | + $this->access->connection->ldapGroupFilter, array('dn', $dynamicGroupMemberURL)); |
|
| 637 | + foreach ($groupsToMatch as $dynamicGroup) { |
|
| 638 | 638 | if (!array_key_exists($dynamicGroupMemberURL, $dynamicGroup)) { |
| 639 | 639 | continue; |
| 640 | 640 | } |
| 641 | 641 | $pos = strpos($dynamicGroup[$dynamicGroupMemberURL][0], '('); |
| 642 | 642 | if ($pos !== false) { |
| 643 | - $memberUrlFilter = substr($dynamicGroup[$dynamicGroupMemberURL][0],$pos); |
|
| 643 | + $memberUrlFilter = substr($dynamicGroup[$dynamicGroupMemberURL][0], $pos); |
|
| 644 | 644 | // apply filter via ldap search to see if this user is in this |
| 645 | 645 | // dynamic group |
| 646 | 646 | $userMatch = $this->access->readAttribute( |
@@ -651,7 +651,7 @@ discard block |
||
| 651 | 651 | if ($userMatch !== false) { |
| 652 | 652 | // match found so this user is in this group |
| 653 | 653 | $groupName = $this->access->dn2groupname($dynamicGroup['dn'][0]); |
| 654 | - if(is_string($groupName)) { |
|
| 654 | + if (is_string($groupName)) { |
|
| 655 | 655 | // be sure to never return false if the dn could not be |
| 656 | 656 | // resolved to a name, for whatever reason. |
| 657 | 657 | $groups[] = $groupName; |
@@ -659,7 +659,7 @@ discard block |
||
| 659 | 659 | } |
| 660 | 660 | } else { |
| 661 | 661 | \OCP\Util::writeLog('user_ldap', 'No search filter found on member url '. |
| 662 | - 'of group ' . print_r($dynamicGroup, true), \OCP\Util::DEBUG); |
|
| 662 | + 'of group '.print_r($dynamicGroup, true), \OCP\Util::DEBUG); |
|
| 663 | 663 | } |
| 664 | 664 | } |
| 665 | 665 | } |
@@ -667,15 +667,15 @@ discard block |
||
| 667 | 667 | // if possible, read out membership via memberOf. It's far faster than |
| 668 | 668 | // performing a search, which still is a fallback later. |
| 669 | 669 | // memberof doesn't support memberuid, so skip it here. |
| 670 | - if((int)$this->access->connection->hasMemberOfFilterSupport === 1 |
|
| 671 | - && (int)$this->access->connection->useMemberOfToDetectMembership === 1 |
|
| 670 | + if ((int) $this->access->connection->hasMemberOfFilterSupport === 1 |
|
| 671 | + && (int) $this->access->connection->useMemberOfToDetectMembership === 1 |
|
| 672 | 672 | && strtolower($this->access->connection->ldapGroupMemberAssocAttr) !== 'memberuid' |
| 673 | 673 | ) { |
| 674 | 674 | $groupDNs = $this->_getGroupDNsFromMemberOf($userDN); |
| 675 | 675 | if (is_array($groupDNs)) { |
| 676 | 676 | foreach ($groupDNs as $dn) { |
| 677 | 677 | $groupName = $this->access->dn2groupname($dn); |
| 678 | - if(is_string($groupName)) { |
|
| 678 | + if (is_string($groupName)) { |
|
| 679 | 679 | // be sure to never return false if the dn could not be |
| 680 | 680 | // resolved to a name, for whatever reason. |
| 681 | 681 | $groups[] = $groupName; |
@@ -683,10 +683,10 @@ discard block |
||
| 683 | 683 | } |
| 684 | 684 | } |
| 685 | 685 | |
| 686 | - if($primaryGroup !== false) { |
|
| 686 | + if ($primaryGroup !== false) { |
|
| 687 | 687 | $groups[] = $primaryGroup; |
| 688 | 688 | } |
| 689 | - if($gidGroupName !== false) { |
|
| 689 | + if ($gidGroupName !== false) { |
|
| 690 | 690 | $groups[] = $gidGroupName; |
| 691 | 691 | } |
| 692 | 692 | $this->access->connection->writeToCache($cacheKey, $groups); |
@@ -694,14 +694,14 @@ discard block |
||
| 694 | 694 | } |
| 695 | 695 | |
| 696 | 696 | //uniqueMember takes DN, memberuid the uid, so we need to distinguish |
| 697 | - if((strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'uniquemember') |
|
| 697 | + if ((strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'uniquemember') |
|
| 698 | 698 | || (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'member') |
| 699 | 699 | ) { |
| 700 | 700 | $uid = $userDN; |
| 701 | - } else if(strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
| 701 | + } else if (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid') { |
|
| 702 | 702 | $result = $this->access->readAttribute($userDN, 'uid'); |
| 703 | 703 | if ($result === false) { |
| 704 | - \OCP\Util::writeLog('user_ldap', 'No uid attribute found for DN ' . $userDN . ' on '. |
|
| 704 | + \OCP\Util::writeLog('user_ldap', 'No uid attribute found for DN '.$userDN.' on '. |
|
| 705 | 705 | $this->access->connection->ldapHost, \OCP\Util::DEBUG); |
| 706 | 706 | } |
| 707 | 707 | $uid = $result[0]; |
@@ -710,7 +710,7 @@ discard block |
||
| 710 | 710 | $uid = $userDN; |
| 711 | 711 | } |
| 712 | 712 | |
| 713 | - if(isset($this->cachedGroupsByMember[$uid])) { |
|
| 713 | + if (isset($this->cachedGroupsByMember[$uid])) { |
|
| 714 | 714 | $groups = array_merge($groups, $this->cachedGroupsByMember[$uid]); |
| 715 | 715 | } else { |
| 716 | 716 | $groupsByMember = array_values($this->getGroupsByMember($uid)); |
@@ -719,10 +719,10 @@ discard block |
||
| 719 | 719 | $groups = array_merge($groups, $groupsByMember); |
| 720 | 720 | } |
| 721 | 721 | |
| 722 | - if($primaryGroup !== false) { |
|
| 722 | + if ($primaryGroup !== false) { |
|
| 723 | 723 | $groups[] = $primaryGroup; |
| 724 | 724 | } |
| 725 | - if($gidGroupName !== false) { |
|
| 725 | + if ($gidGroupName !== false) { |
|
| 726 | 726 | $groups[] = $gidGroupName; |
| 727 | 727 | } |
| 728 | 728 | |
@@ -760,7 +760,7 @@ discard block |
||
| 760 | 760 | $nestedGroups = $this->access->connection->ldapNestedGroups; |
| 761 | 761 | if (!empty($nestedGroups)) { |
| 762 | 762 | $supergroups = $this->getGroupsByMember($groupDN, $seen); |
| 763 | - if (is_array($supergroups) && (count($supergroups)>0)) { |
|
| 763 | + if (is_array($supergroups) && (count($supergroups) > 0)) { |
|
| 764 | 764 | $allGroups = array_merge($allGroups, $supergroups); |
| 765 | 765 | } |
| 766 | 766 | } |
@@ -779,33 +779,33 @@ discard block |
||
| 779 | 779 | * @return array with user ids |
| 780 | 780 | */ |
| 781 | 781 | public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { |
| 782 | - if(!$this->enabled) { |
|
| 782 | + if (!$this->enabled) { |
|
| 783 | 783 | return array(); |
| 784 | 784 | } |
| 785 | - if(!$this->groupExists($gid)) { |
|
| 785 | + if (!$this->groupExists($gid)) { |
|
| 786 | 786 | return array(); |
| 787 | 787 | } |
| 788 | 788 | $search = $this->access->escapeFilterPart($search, true); |
| 789 | 789 | $cacheKey = 'usersInGroup-'.$gid.'-'.$search.'-'.$limit.'-'.$offset; |
| 790 | 790 | // check for cache of the exact query |
| 791 | 791 | $groupUsers = $this->access->connection->getFromCache($cacheKey); |
| 792 | - if(!is_null($groupUsers)) { |
|
| 792 | + if (!is_null($groupUsers)) { |
|
| 793 | 793 | return $groupUsers; |
| 794 | 794 | } |
| 795 | 795 | |
| 796 | 796 | // check for cache of the query without limit and offset |
| 797 | 797 | $groupUsers = $this->access->connection->getFromCache('usersInGroup-'.$gid.'-'.$search); |
| 798 | - if(!is_null($groupUsers)) { |
|
| 798 | + if (!is_null($groupUsers)) { |
|
| 799 | 799 | $groupUsers = array_slice($groupUsers, $offset, $limit); |
| 800 | 800 | $this->access->connection->writeToCache($cacheKey, $groupUsers); |
| 801 | 801 | return $groupUsers; |
| 802 | 802 | } |
| 803 | 803 | |
| 804 | - if($limit === -1) { |
|
| 804 | + if ($limit === -1) { |
|
| 805 | 805 | $limit = null; |
| 806 | 806 | } |
| 807 | 807 | $groupDN = $this->access->groupname2dn($gid); |
| 808 | - if(!$groupDN) { |
|
| 808 | + if (!$groupDN) { |
|
| 809 | 809 | // group couldn't be found, return empty resultset |
| 810 | 810 | $this->access->connection->writeToCache($cacheKey, array()); |
| 811 | 811 | return array(); |
@@ -814,7 +814,7 @@ discard block |
||
| 814 | 814 | $primaryUsers = $this->getUsersInPrimaryGroup($groupDN, $search, $limit, $offset); |
| 815 | 815 | $posixGroupUsers = $this->getUsersInGidNumber($groupDN, $search, $limit, $offset); |
| 816 | 816 | $members = array_keys($this->_groupMembers($groupDN)); |
| 817 | - if(!$members && empty($posixGroupUsers) && empty($primaryUsers)) { |
|
| 817 | + if (!$members && empty($posixGroupUsers) && empty($primaryUsers)) { |
|
| 818 | 818 | //in case users could not be retrieved, return empty result set |
| 819 | 819 | $this->access->connection->writeToCache($cacheKey, []); |
| 820 | 820 | return []; |
@@ -823,29 +823,29 @@ discard block |
||
| 823 | 823 | $groupUsers = array(); |
| 824 | 824 | $isMemberUid = (strtolower($this->access->connection->ldapGroupMemberAssocAttr) === 'memberuid'); |
| 825 | 825 | $attrs = $this->access->userManager->getAttributes(true); |
| 826 | - foreach($members as $member) { |
|
| 827 | - if($isMemberUid) { |
|
| 826 | + foreach ($members as $member) { |
|
| 827 | + if ($isMemberUid) { |
|
| 828 | 828 | //we got uids, need to get their DNs to 'translate' them to user names |
| 829 | 829 | $filter = $this->access->combineFilterWithAnd(array( |
| 830 | 830 | str_replace('%uid', $member, $this->access->connection->ldapLoginFilter), |
| 831 | 831 | $this->access->getFilterPartForUserSearch($search) |
| 832 | 832 | )); |
| 833 | 833 | $ldap_users = $this->access->fetchListOfUsers($filter, $attrs, 1); |
| 834 | - if(count($ldap_users) < 1) { |
|
| 834 | + if (count($ldap_users) < 1) { |
|
| 835 | 835 | continue; |
| 836 | 836 | } |
| 837 | 837 | $groupUsers[] = $this->access->dn2username($ldap_users[0]['dn'][0]); |
| 838 | 838 | } else { |
| 839 | 839 | //we got DNs, check if we need to filter by search or we can give back all of them |
| 840 | 840 | if ($search !== '') { |
| 841 | - if(!$this->access->readAttribute($member, |
|
| 841 | + if (!$this->access->readAttribute($member, |
|
| 842 | 842 | $this->access->connection->ldapUserDisplayName, |
| 843 | 843 | $this->access->getFilterPartForUserSearch($search))) { |
| 844 | 844 | continue; |
| 845 | 845 | } |
| 846 | 846 | } |
| 847 | 847 | // dn2username will also check if the users belong to the allowed base |
| 848 | - if($ocname = $this->access->dn2username($member)) { |
|
| 848 | + if ($ocname = $this->access->dn2username($member)) { |
|
| 849 | 849 | $groupUsers[] = $ocname; |
| 850 | 850 | } |
| 851 | 851 | } |
@@ -873,16 +873,16 @@ discard block |
||
| 873 | 873 | } |
| 874 | 874 | |
| 875 | 875 | $cacheKey = 'countUsersInGroup-'.$gid.'-'.$search; |
| 876 | - if(!$this->enabled || !$this->groupExists($gid)) { |
|
| 876 | + if (!$this->enabled || !$this->groupExists($gid)) { |
|
| 877 | 877 | return false; |
| 878 | 878 | } |
| 879 | 879 | $groupUsers = $this->access->connection->getFromCache($cacheKey); |
| 880 | - if(!is_null($groupUsers)) { |
|
| 880 | + if (!is_null($groupUsers)) { |
|
| 881 | 881 | return $groupUsers; |
| 882 | 882 | } |
| 883 | 883 | |
| 884 | 884 | $groupDN = $this->access->groupname2dn($gid); |
| 885 | - if(!$groupDN) { |
|
| 885 | + if (!$groupDN) { |
|
| 886 | 886 | // group couldn't be found, return empty result set |
| 887 | 887 | $this->access->connection->writeToCache($cacheKey, false); |
| 888 | 888 | return false; |
@@ -890,7 +890,7 @@ discard block |
||
| 890 | 890 | |
| 891 | 891 | $members = array_keys($this->_groupMembers($groupDN)); |
| 892 | 892 | $primaryUserCount = $this->countUsersInPrimaryGroup($groupDN, ''); |
| 893 | - if(!$members && $primaryUserCount === 0) { |
|
| 893 | + if (!$members && $primaryUserCount === 0) { |
|
| 894 | 894 | //in case users could not be retrieved, return empty result set |
| 895 | 895 | $this->access->connection->writeToCache($cacheKey, false); |
| 896 | 896 | return false; |
@@ -915,27 +915,27 @@ discard block |
||
| 915 | 915 | //For now this is not important, because the only use of this method |
| 916 | 916 | //does not supply a search string |
| 917 | 917 | $groupUsers = array(); |
| 918 | - foreach($members as $member) { |
|
| 919 | - if($isMemberUid) { |
|
| 918 | + foreach ($members as $member) { |
|
| 919 | + if ($isMemberUid) { |
|
| 920 | 920 | //we got uids, need to get their DNs to 'translate' them to user names |
| 921 | 921 | $filter = $this->access->combineFilterWithAnd(array( |
| 922 | 922 | str_replace('%uid', $member, $this->access->connection->ldapLoginFilter), |
| 923 | 923 | $this->access->getFilterPartForUserSearch($search) |
| 924 | 924 | )); |
| 925 | 925 | $ldap_users = $this->access->fetchListOfUsers($filter, 'dn', 1); |
| 926 | - if(count($ldap_users) < 1) { |
|
| 926 | + if (count($ldap_users) < 1) { |
|
| 927 | 927 | continue; |
| 928 | 928 | } |
| 929 | 929 | $groupUsers[] = $this->access->dn2username($ldap_users[0]); |
| 930 | 930 | } else { |
| 931 | 931 | //we need to apply the search filter now |
| 932 | - if(!$this->access->readAttribute($member, |
|
| 932 | + if (!$this->access->readAttribute($member, |
|
| 933 | 933 | $this->access->connection->ldapUserDisplayName, |
| 934 | 934 | $this->access->getFilterPartForUserSearch($search))) { |
| 935 | 935 | continue; |
| 936 | 936 | } |
| 937 | 937 | // dn2username will also check if the users belong to the allowed base |
| 938 | - if($ocname = $this->access->dn2username($member)) { |
|
| 938 | + if ($ocname = $this->access->dn2username($member)) { |
|
| 939 | 939 | $groupUsers[] = $ocname; |
| 940 | 940 | } |
| 941 | 941 | } |
@@ -958,7 +958,7 @@ discard block |
||
| 958 | 958 | * Returns a list with all groups (used by getGroups) |
| 959 | 959 | */ |
| 960 | 960 | protected function getGroupsChunk($search = '', $limit = -1, $offset = 0) { |
| 961 | - if(!$this->enabled) { |
|
| 961 | + if (!$this->enabled) { |
|
| 962 | 962 | return array(); |
| 963 | 963 | } |
| 964 | 964 | $cacheKey = 'getGroups-'.$search.'-'.$limit.'-'.$offset; |
@@ -966,13 +966,13 @@ discard block |
||
| 966 | 966 | //Check cache before driving unnecessary searches |
| 967 | 967 | \OCP\Util::writeLog('user_ldap', 'getGroups '.$cacheKey, \OCP\Util::DEBUG); |
| 968 | 968 | $ldap_groups = $this->access->connection->getFromCache($cacheKey); |
| 969 | - if(!is_null($ldap_groups)) { |
|
| 969 | + if (!is_null($ldap_groups)) { |
|
| 970 | 970 | return $ldap_groups; |
| 971 | 971 | } |
| 972 | 972 | |
| 973 | 973 | // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
| 974 | 974 | // error. With a limit of 0, we get 0 results. So we pass null. |
| 975 | - if($limit <= 0) { |
|
| 975 | + if ($limit <= 0) { |
|
| 976 | 976 | $limit = null; |
| 977 | 977 | } |
| 978 | 978 | $filter = $this->access->combineFilterWithAnd(array( |
@@ -1004,11 +1004,11 @@ discard block |
||
| 1004 | 1004 | * (active directory has a limit of 1000 by default) |
| 1005 | 1005 | */ |
| 1006 | 1006 | public function getGroups($search = '', $limit = -1, $offset = 0) { |
| 1007 | - if(!$this->enabled) { |
|
| 1007 | + if (!$this->enabled) { |
|
| 1008 | 1008 | return array(); |
| 1009 | 1009 | } |
| 1010 | 1010 | $search = $this->access->escapeFilterPart($search, true); |
| 1011 | - $pagingSize = (int)$this->access->connection->ldapPagingSize; |
|
| 1011 | + $pagingSize = (int) $this->access->connection->ldapPagingSize; |
|
| 1012 | 1012 | if (!$this->access->connection->hasPagedResultSupport || $pagingSize <= 0) { |
| 1013 | 1013 | return $this->getGroupsChunk($search, $limit, $offset); |
| 1014 | 1014 | } |
@@ -1051,20 +1051,20 @@ discard block |
||
| 1051 | 1051 | */ |
| 1052 | 1052 | public function groupExists($gid) { |
| 1053 | 1053 | $groupExists = $this->access->connection->getFromCache('groupExists'.$gid); |
| 1054 | - if(!is_null($groupExists)) { |
|
| 1055 | - return (bool)$groupExists; |
|
| 1054 | + if (!is_null($groupExists)) { |
|
| 1055 | + return (bool) $groupExists; |
|
| 1056 | 1056 | } |
| 1057 | 1057 | |
| 1058 | 1058 | //getting dn, if false the group does not exist. If dn, it may be mapped |
| 1059 | 1059 | //only, requires more checking. |
| 1060 | 1060 | $dn = $this->access->groupname2dn($gid); |
| 1061 | - if(!$dn) { |
|
| 1061 | + if (!$dn) { |
|
| 1062 | 1062 | $this->access->connection->writeToCache('groupExists'.$gid, false); |
| 1063 | 1063 | return false; |
| 1064 | 1064 | } |
| 1065 | 1065 | |
| 1066 | 1066 | //if group really still exists, we will be able to read its objectclass |
| 1067 | - if(!is_array($this->access->readAttribute($dn, ''))) { |
|
| 1067 | + if (!is_array($this->access->readAttribute($dn, ''))) { |
|
| 1068 | 1068 | $this->access->connection->writeToCache('groupExists'.$gid, false); |
| 1069 | 1069 | return false; |
| 1070 | 1070 | } |
@@ -1082,7 +1082,7 @@ discard block |
||
| 1082 | 1082 | * compared with GroupInterface::CREATE_GROUP etc. |
| 1083 | 1083 | */ |
| 1084 | 1084 | public function implementsActions($actions) { |
| 1085 | - return (bool)((GroupInterface::COUNT_USERS | |
|
| 1085 | + return (bool) ((GroupInterface::COUNT_USERS | |
|
| 1086 | 1086 | $this->groupPluginManager->getImplementedActions()) & $actions); |
| 1087 | 1087 | } |
| 1088 | 1088 | |
@@ -41,1311 +41,1311 @@ |
||
| 41 | 41 | use OC\ServerNotAvailableException; |
| 42 | 42 | |
| 43 | 43 | class Wizard extends LDAPUtility { |
| 44 | - /** @var \OCP\IL10N */ |
|
| 45 | - static protected $l; |
|
| 46 | - protected $access; |
|
| 47 | - protected $cr; |
|
| 48 | - protected $configuration; |
|
| 49 | - protected $result; |
|
| 50 | - protected $resultCache = array(); |
|
| 51 | - |
|
| 52 | - const LRESULT_PROCESSED_OK = 2; |
|
| 53 | - const LRESULT_PROCESSED_INVALID = 3; |
|
| 54 | - const LRESULT_PROCESSED_SKIP = 4; |
|
| 55 | - |
|
| 56 | - const LFILTER_LOGIN = 2; |
|
| 57 | - const LFILTER_USER_LIST = 3; |
|
| 58 | - const LFILTER_GROUP_LIST = 4; |
|
| 59 | - |
|
| 60 | - const LFILTER_MODE_ASSISTED = 2; |
|
| 61 | - const LFILTER_MODE_RAW = 1; |
|
| 62 | - |
|
| 63 | - const LDAP_NW_TIMEOUT = 4; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Constructor |
|
| 67 | - * @param Configuration $configuration an instance of Configuration |
|
| 68 | - * @param ILDAPWrapper $ldap an instance of ILDAPWrapper |
|
| 69 | - * @param Access $access |
|
| 70 | - */ |
|
| 71 | - public function __construct(Configuration $configuration, ILDAPWrapper $ldap, Access $access) { |
|
| 72 | - parent::__construct($ldap); |
|
| 73 | - $this->configuration = $configuration; |
|
| 74 | - if(is_null(Wizard::$l)) { |
|
| 75 | - Wizard::$l = \OC::$server->getL10N('user_ldap'); |
|
| 76 | - } |
|
| 77 | - $this->access = $access; |
|
| 78 | - $this->result = new WizardResult(); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - public function __destruct() { |
|
| 82 | - if($this->result->hasChanges()) { |
|
| 83 | - $this->configuration->saveConfiguration(); |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * counts entries in the LDAP directory |
|
| 89 | - * |
|
| 90 | - * @param string $filter the LDAP search filter |
|
| 91 | - * @param string $type a string being either 'users' or 'groups'; |
|
| 92 | - * @return bool|int |
|
| 93 | - * @throws \Exception |
|
| 94 | - */ |
|
| 95 | - public function countEntries($filter, $type) { |
|
| 96 | - $reqs = array('ldapHost', 'ldapPort', 'ldapBase'); |
|
| 97 | - if($type === 'users') { |
|
| 98 | - $reqs[] = 'ldapUserFilter'; |
|
| 99 | - } |
|
| 100 | - if(!$this->checkRequirements($reqs)) { |
|
| 101 | - throw new \Exception('Requirements not met', 400); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - $attr = array('dn'); // default |
|
| 105 | - $limit = 1001; |
|
| 106 | - if($type === 'groups') { |
|
| 107 | - $result = $this->access->countGroups($filter, $attr, $limit); |
|
| 108 | - } else if($type === 'users') { |
|
| 109 | - $result = $this->access->countUsers($filter, $attr, $limit); |
|
| 110 | - } else if ($type === 'objects') { |
|
| 111 | - $result = $this->access->countObjects($limit); |
|
| 112 | - } else { |
|
| 113 | - throw new \Exception('Internal error: Invalid object type', 500); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - return $result; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * formats the return value of a count operation to the string to be |
|
| 121 | - * inserted. |
|
| 122 | - * |
|
| 123 | - * @param bool|int $count |
|
| 124 | - * @return int|string |
|
| 125 | - */ |
|
| 126 | - private function formatCountResult($count) { |
|
| 127 | - $formatted = ($count !== false) ? $count : 0; |
|
| 128 | - if($formatted > 1000) { |
|
| 129 | - $formatted = '> 1000'; |
|
| 130 | - } |
|
| 131 | - return $formatted; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - public function countGroups() { |
|
| 135 | - $filter = $this->configuration->ldapGroupFilter; |
|
| 136 | - |
|
| 137 | - if(empty($filter)) { |
|
| 138 | - $output = self::$l->n('%s group found', '%s groups found', 0, array(0)); |
|
| 139 | - $this->result->addChange('ldap_group_count', $output); |
|
| 140 | - return $this->result; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - try { |
|
| 144 | - $groupsTotal = $this->formatCountResult($this->countEntries($filter, 'groups')); |
|
| 145 | - } catch (\Exception $e) { |
|
| 146 | - //400 can be ignored, 500 is forwarded |
|
| 147 | - if($e->getCode() === 500) { |
|
| 148 | - throw $e; |
|
| 149 | - } |
|
| 150 | - return false; |
|
| 151 | - } |
|
| 152 | - $output = self::$l->n('%s group found', '%s groups found', $groupsTotal, array($groupsTotal)); |
|
| 153 | - $this->result->addChange('ldap_group_count', $output); |
|
| 154 | - return $this->result; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * @return WizardResult |
|
| 159 | - * @throws \Exception |
|
| 160 | - */ |
|
| 161 | - public function countUsers() { |
|
| 162 | - $filter = $this->access->getFilterForUserCount(); |
|
| 163 | - |
|
| 164 | - $usersTotal = $this->formatCountResult($this->countEntries($filter, 'users')); |
|
| 165 | - $output = self::$l->n('%s user found', '%s users found', $usersTotal, array($usersTotal)); |
|
| 166 | - $this->result->addChange('ldap_user_count', $output); |
|
| 167 | - return $this->result; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * counts any objects in the currently set base dn |
|
| 172 | - * |
|
| 173 | - * @return WizardResult |
|
| 174 | - * @throws \Exception |
|
| 175 | - */ |
|
| 176 | - public function countInBaseDN() { |
|
| 177 | - // we don't need to provide a filter in this case |
|
| 178 | - $total = $this->countEntries(null, 'objects'); |
|
| 179 | - if($total === false) { |
|
| 180 | - throw new \Exception('invalid results received'); |
|
| 181 | - } |
|
| 182 | - $this->result->addChange('ldap_test_base', $total); |
|
| 183 | - return $this->result; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * counts users with a specified attribute |
|
| 188 | - * @param string $attr |
|
| 189 | - * @param bool $existsCheck |
|
| 190 | - * @return int|bool |
|
| 191 | - */ |
|
| 192 | - public function countUsersWithAttribute($attr, $existsCheck = false) { |
|
| 193 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 194 | - 'ldapPort', |
|
| 195 | - 'ldapBase', |
|
| 196 | - 'ldapUserFilter', |
|
| 197 | - ))) { |
|
| 198 | - return false; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - $filter = $this->access->combineFilterWithAnd(array( |
|
| 202 | - $this->configuration->ldapUserFilter, |
|
| 203 | - $attr . '=*' |
|
| 204 | - )); |
|
| 205 | - |
|
| 206 | - $limit = ($existsCheck === false) ? null : 1; |
|
| 207 | - |
|
| 208 | - return $this->access->countUsers($filter, array('dn'), $limit); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * detects the display name attribute. If a setting is already present that |
|
| 213 | - * returns at least one hit, the detection will be canceled. |
|
| 214 | - * @return WizardResult|bool |
|
| 215 | - * @throws \Exception |
|
| 216 | - */ |
|
| 217 | - public function detectUserDisplayNameAttribute() { |
|
| 218 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 219 | - 'ldapPort', |
|
| 220 | - 'ldapBase', |
|
| 221 | - 'ldapUserFilter', |
|
| 222 | - ))) { |
|
| 223 | - return false; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - $attr = $this->configuration->ldapUserDisplayName; |
|
| 227 | - if ($attr !== '' && $attr !== 'displayName') { |
|
| 228 | - // most likely not the default value with upper case N, |
|
| 229 | - // verify it still produces a result |
|
| 230 | - $count = (int)$this->countUsersWithAttribute($attr, true); |
|
| 231 | - if($count > 0) { |
|
| 232 | - //no change, but we sent it back to make sure the user interface |
|
| 233 | - //is still correct, even if the ajax call was cancelled meanwhile |
|
| 234 | - $this->result->addChange('ldap_display_name', $attr); |
|
| 235 | - return $this->result; |
|
| 236 | - } |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - // first attribute that has at least one result wins |
|
| 240 | - $displayNameAttrs = array('displayname', 'cn'); |
|
| 241 | - foreach ($displayNameAttrs as $attr) { |
|
| 242 | - $count = (int)$this->countUsersWithAttribute($attr, true); |
|
| 243 | - |
|
| 244 | - if($count > 0) { |
|
| 245 | - $this->applyFind('ldap_display_name', $attr); |
|
| 246 | - return $this->result; |
|
| 247 | - } |
|
| 248 | - }; |
|
| 249 | - |
|
| 250 | - throw new \Exception(self::$l->t('Could not detect user display name attribute. Please specify it yourself in advanced LDAP settings.')); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * detects the most often used email attribute for users applying to the |
|
| 255 | - * user list filter. If a setting is already present that returns at least |
|
| 256 | - * one hit, the detection will be canceled. |
|
| 257 | - * @return WizardResult|bool |
|
| 258 | - */ |
|
| 259 | - public function detectEmailAttribute() { |
|
| 260 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 261 | - 'ldapPort', |
|
| 262 | - 'ldapBase', |
|
| 263 | - 'ldapUserFilter', |
|
| 264 | - ))) { |
|
| 265 | - return false; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - $attr = $this->configuration->ldapEmailAttribute; |
|
| 269 | - if ($attr !== '') { |
|
| 270 | - $count = (int)$this->countUsersWithAttribute($attr, true); |
|
| 271 | - if($count > 0) { |
|
| 272 | - return false; |
|
| 273 | - } |
|
| 274 | - $writeLog = true; |
|
| 275 | - } else { |
|
| 276 | - $writeLog = false; |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - $emailAttributes = array('mail', 'mailPrimaryAddress'); |
|
| 280 | - $winner = ''; |
|
| 281 | - $maxUsers = 0; |
|
| 282 | - foreach($emailAttributes as $attr) { |
|
| 283 | - $count = $this->countUsersWithAttribute($attr); |
|
| 284 | - if($count > $maxUsers) { |
|
| 285 | - $maxUsers = $count; |
|
| 286 | - $winner = $attr; |
|
| 287 | - } |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - if($winner !== '') { |
|
| 291 | - $this->applyFind('ldap_email_attr', $winner); |
|
| 292 | - if($writeLog) { |
|
| 293 | - \OCP\Util::writeLog('user_ldap', 'The mail attribute has ' . |
|
| 294 | - 'automatically been reset, because the original value ' . |
|
| 295 | - 'did not return any results.', \OCP\Util::INFO); |
|
| 296 | - } |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - return $this->result; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * @return WizardResult |
|
| 304 | - * @throws \Exception |
|
| 305 | - */ |
|
| 306 | - public function determineAttributes() { |
|
| 307 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 308 | - 'ldapPort', |
|
| 309 | - 'ldapBase', |
|
| 310 | - 'ldapUserFilter', |
|
| 311 | - ))) { |
|
| 312 | - return false; |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - $attributes = $this->getUserAttributes(); |
|
| 316 | - |
|
| 317 | - natcasesort($attributes); |
|
| 318 | - $attributes = array_values($attributes); |
|
| 319 | - |
|
| 320 | - $this->result->addOptions('ldap_loginfilter_attributes', $attributes); |
|
| 321 | - |
|
| 322 | - $selected = $this->configuration->ldapLoginFilterAttributes; |
|
| 323 | - if(is_array($selected) && !empty($selected)) { |
|
| 324 | - $this->result->addChange('ldap_loginfilter_attributes', $selected); |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - return $this->result; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * detects the available LDAP attributes |
|
| 332 | - * @return array|false The instance's WizardResult instance |
|
| 333 | - * @throws \Exception |
|
| 334 | - */ |
|
| 335 | - private function getUserAttributes() { |
|
| 336 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 337 | - 'ldapPort', |
|
| 338 | - 'ldapBase', |
|
| 339 | - 'ldapUserFilter', |
|
| 340 | - ))) { |
|
| 341 | - return false; |
|
| 342 | - } |
|
| 343 | - $cr = $this->getConnection(); |
|
| 344 | - if(!$cr) { |
|
| 345 | - throw new \Exception('Could not connect to LDAP'); |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - $base = $this->configuration->ldapBase[0]; |
|
| 349 | - $filter = $this->configuration->ldapUserFilter; |
|
| 350 | - $rr = $this->ldap->search($cr, $base, $filter, array(), 1, 1); |
|
| 351 | - if(!$this->ldap->isResource($rr)) { |
|
| 352 | - return false; |
|
| 353 | - } |
|
| 354 | - $er = $this->ldap->firstEntry($cr, $rr); |
|
| 355 | - $attributes = $this->ldap->getAttributes($cr, $er); |
|
| 356 | - $pureAttributes = array(); |
|
| 357 | - for($i = 0; $i < $attributes['count']; $i++) { |
|
| 358 | - $pureAttributes[] = $attributes[$i]; |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - return $pureAttributes; |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - /** |
|
| 365 | - * detects the available LDAP groups |
|
| 366 | - * @return WizardResult|false the instance's WizardResult instance |
|
| 367 | - */ |
|
| 368 | - public function determineGroupsForGroups() { |
|
| 369 | - return $this->determineGroups('ldap_groupfilter_groups', |
|
| 370 | - 'ldapGroupFilterGroups', |
|
| 371 | - false); |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - /** |
|
| 375 | - * detects the available LDAP groups |
|
| 376 | - * @return WizardResult|false the instance's WizardResult instance |
|
| 377 | - */ |
|
| 378 | - public function determineGroupsForUsers() { |
|
| 379 | - return $this->determineGroups('ldap_userfilter_groups', |
|
| 380 | - 'ldapUserFilterGroups'); |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - /** |
|
| 384 | - * detects the available LDAP groups |
|
| 385 | - * @param string $dbKey |
|
| 386 | - * @param string $confKey |
|
| 387 | - * @param bool $testMemberOf |
|
| 388 | - * @return WizardResult|false the instance's WizardResult instance |
|
| 389 | - * @throws \Exception |
|
| 390 | - */ |
|
| 391 | - private function determineGroups($dbKey, $confKey, $testMemberOf = true) { |
|
| 392 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 393 | - 'ldapPort', |
|
| 394 | - 'ldapBase', |
|
| 395 | - ))) { |
|
| 396 | - return false; |
|
| 397 | - } |
|
| 398 | - $cr = $this->getConnection(); |
|
| 399 | - if(!$cr) { |
|
| 400 | - throw new \Exception('Could not connect to LDAP'); |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - $this->fetchGroups($dbKey, $confKey); |
|
| 404 | - |
|
| 405 | - if($testMemberOf) { |
|
| 406 | - $this->configuration->hasMemberOfFilterSupport = $this->testMemberOf(); |
|
| 407 | - $this->result->markChange(); |
|
| 408 | - if(!$this->configuration->hasMemberOfFilterSupport) { |
|
| 409 | - throw new \Exception('memberOf is not supported by the server'); |
|
| 410 | - } |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - return $this->result; |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * fetches all groups from LDAP and adds them to the result object |
|
| 418 | - * |
|
| 419 | - * @param string $dbKey |
|
| 420 | - * @param string $confKey |
|
| 421 | - * @return array $groupEntries |
|
| 422 | - * @throws \Exception |
|
| 423 | - */ |
|
| 424 | - public function fetchGroups($dbKey, $confKey) { |
|
| 425 | - $obclasses = array('posixGroup', 'group', 'zimbraDistributionList', 'groupOfNames', 'groupOfUniqueNames'); |
|
| 426 | - |
|
| 427 | - $filterParts = array(); |
|
| 428 | - foreach($obclasses as $obclass) { |
|
| 429 | - $filterParts[] = 'objectclass='.$obclass; |
|
| 430 | - } |
|
| 431 | - //we filter for everything |
|
| 432 | - //- that looks like a group and |
|
| 433 | - //- has the group display name set |
|
| 434 | - $filter = $this->access->combineFilterWithOr($filterParts); |
|
| 435 | - $filter = $this->access->combineFilterWithAnd(array($filter, 'cn=*')); |
|
| 436 | - |
|
| 437 | - $groupNames = array(); |
|
| 438 | - $groupEntries = array(); |
|
| 439 | - $limit = 400; |
|
| 440 | - $offset = 0; |
|
| 441 | - do { |
|
| 442 | - // we need to request dn additionally here, otherwise memberOf |
|
| 443 | - // detection will fail later |
|
| 444 | - $result = $this->access->searchGroups($filter, array('cn', 'dn'), $limit, $offset); |
|
| 445 | - foreach($result as $item) { |
|
| 446 | - if(!isset($item['cn']) && !is_array($item['cn']) && !isset($item['cn'][0])) { |
|
| 447 | - // just in case - no issue known |
|
| 448 | - continue; |
|
| 449 | - } |
|
| 450 | - $groupNames[] = $item['cn'][0]; |
|
| 451 | - $groupEntries[] = $item; |
|
| 452 | - } |
|
| 453 | - $offset += $limit; |
|
| 454 | - } while ($this->access->hasMoreResults()); |
|
| 455 | - |
|
| 456 | - if(count($groupNames) > 0) { |
|
| 457 | - natsort($groupNames); |
|
| 458 | - $this->result->addOptions($dbKey, array_values($groupNames)); |
|
| 459 | - } else { |
|
| 460 | - throw new \Exception(self::$l->t('Could not find the desired feature')); |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - $setFeatures = $this->configuration->$confKey; |
|
| 464 | - if(is_array($setFeatures) && !empty($setFeatures)) { |
|
| 465 | - //something is already configured? pre-select it. |
|
| 466 | - $this->result->addChange($dbKey, $setFeatures); |
|
| 467 | - } |
|
| 468 | - return $groupEntries; |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - public function determineGroupMemberAssoc() { |
|
| 472 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 473 | - 'ldapPort', |
|
| 474 | - 'ldapGroupFilter', |
|
| 475 | - ))) { |
|
| 476 | - return false; |
|
| 477 | - } |
|
| 478 | - $attribute = $this->detectGroupMemberAssoc(); |
|
| 479 | - if($attribute === false) { |
|
| 480 | - return false; |
|
| 481 | - } |
|
| 482 | - $this->configuration->setConfiguration(array('ldapGroupMemberAssocAttr' => $attribute)); |
|
| 483 | - $this->result->addChange('ldap_group_member_assoc_attribute', $attribute); |
|
| 484 | - |
|
| 485 | - return $this->result; |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - /** |
|
| 489 | - * Detects the available object classes |
|
| 490 | - * @return WizardResult|false the instance's WizardResult instance |
|
| 491 | - * @throws \Exception |
|
| 492 | - */ |
|
| 493 | - public function determineGroupObjectClasses() { |
|
| 494 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 495 | - 'ldapPort', |
|
| 496 | - 'ldapBase', |
|
| 497 | - ))) { |
|
| 498 | - return false; |
|
| 499 | - } |
|
| 500 | - $cr = $this->getConnection(); |
|
| 501 | - if(!$cr) { |
|
| 502 | - throw new \Exception('Could not connect to LDAP'); |
|
| 503 | - } |
|
| 504 | - |
|
| 505 | - $obclasses = array('groupOfNames', 'groupOfUniqueNames', 'group', 'posixGroup', '*'); |
|
| 506 | - $this->determineFeature($obclasses, |
|
| 507 | - 'objectclass', |
|
| 508 | - 'ldap_groupfilter_objectclass', |
|
| 509 | - 'ldapGroupFilterObjectclass', |
|
| 510 | - false); |
|
| 511 | - |
|
| 512 | - return $this->result; |
|
| 513 | - } |
|
| 514 | - |
|
| 515 | - /** |
|
| 516 | - * detects the available object classes |
|
| 517 | - * @return WizardResult |
|
| 518 | - * @throws \Exception |
|
| 519 | - */ |
|
| 520 | - public function determineUserObjectClasses() { |
|
| 521 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 522 | - 'ldapPort', |
|
| 523 | - 'ldapBase', |
|
| 524 | - ))) { |
|
| 525 | - return false; |
|
| 526 | - } |
|
| 527 | - $cr = $this->getConnection(); |
|
| 528 | - if(!$cr) { |
|
| 529 | - throw new \Exception('Could not connect to LDAP'); |
|
| 530 | - } |
|
| 531 | - |
|
| 532 | - $obclasses = array('inetOrgPerson', 'person', 'organizationalPerson', |
|
| 533 | - 'user', 'posixAccount', '*'); |
|
| 534 | - $filter = $this->configuration->ldapUserFilter; |
|
| 535 | - //if filter is empty, it is probably the first time the wizard is called |
|
| 536 | - //then, apply suggestions. |
|
| 537 | - $this->determineFeature($obclasses, |
|
| 538 | - 'objectclass', |
|
| 539 | - 'ldap_userfilter_objectclass', |
|
| 540 | - 'ldapUserFilterObjectclass', |
|
| 541 | - empty($filter)); |
|
| 542 | - |
|
| 543 | - return $this->result; |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - /** |
|
| 547 | - * @return WizardResult|false |
|
| 548 | - * @throws \Exception |
|
| 549 | - */ |
|
| 550 | - public function getGroupFilter() { |
|
| 551 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 552 | - 'ldapPort', |
|
| 553 | - 'ldapBase', |
|
| 554 | - ))) { |
|
| 555 | - return false; |
|
| 556 | - } |
|
| 557 | - //make sure the use display name is set |
|
| 558 | - $displayName = $this->configuration->ldapGroupDisplayName; |
|
| 559 | - if ($displayName === '') { |
|
| 560 | - $d = $this->configuration->getDefaults(); |
|
| 561 | - $this->applyFind('ldap_group_display_name', |
|
| 562 | - $d['ldap_group_display_name']); |
|
| 563 | - } |
|
| 564 | - $filter = $this->composeLdapFilter(self::LFILTER_GROUP_LIST); |
|
| 565 | - |
|
| 566 | - $this->applyFind('ldap_group_filter', $filter); |
|
| 567 | - return $this->result; |
|
| 568 | - } |
|
| 569 | - |
|
| 570 | - /** |
|
| 571 | - * @return WizardResult|false |
|
| 572 | - * @throws \Exception |
|
| 573 | - */ |
|
| 574 | - public function getUserListFilter() { |
|
| 575 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 576 | - 'ldapPort', |
|
| 577 | - 'ldapBase', |
|
| 578 | - ))) { |
|
| 579 | - return false; |
|
| 580 | - } |
|
| 581 | - //make sure the use display name is set |
|
| 582 | - $displayName = $this->configuration->ldapUserDisplayName; |
|
| 583 | - if ($displayName === '') { |
|
| 584 | - $d = $this->configuration->getDefaults(); |
|
| 585 | - $this->applyFind('ldap_display_name', $d['ldap_display_name']); |
|
| 586 | - } |
|
| 587 | - $filter = $this->composeLdapFilter(self::LFILTER_USER_LIST); |
|
| 588 | - if(!$filter) { |
|
| 589 | - throw new \Exception('Cannot create filter'); |
|
| 590 | - } |
|
| 591 | - |
|
| 592 | - $this->applyFind('ldap_userlist_filter', $filter); |
|
| 593 | - return $this->result; |
|
| 594 | - } |
|
| 595 | - |
|
| 596 | - /** |
|
| 597 | - * @return bool|WizardResult |
|
| 598 | - * @throws \Exception |
|
| 599 | - */ |
|
| 600 | - public function getUserLoginFilter() { |
|
| 601 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 602 | - 'ldapPort', |
|
| 603 | - 'ldapBase', |
|
| 604 | - 'ldapUserFilter', |
|
| 605 | - ))) { |
|
| 606 | - return false; |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - $filter = $this->composeLdapFilter(self::LFILTER_LOGIN); |
|
| 610 | - if(!$filter) { |
|
| 611 | - throw new \Exception('Cannot create filter'); |
|
| 612 | - } |
|
| 613 | - |
|
| 614 | - $this->applyFind('ldap_login_filter', $filter); |
|
| 615 | - return $this->result; |
|
| 616 | - } |
|
| 617 | - |
|
| 618 | - /** |
|
| 619 | - * @return bool|WizardResult |
|
| 620 | - * @param string $loginName |
|
| 621 | - * @throws \Exception |
|
| 622 | - */ |
|
| 623 | - public function testLoginName($loginName) { |
|
| 624 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 625 | - 'ldapPort', |
|
| 626 | - 'ldapBase', |
|
| 627 | - 'ldapLoginFilter', |
|
| 628 | - ))) { |
|
| 629 | - return false; |
|
| 630 | - } |
|
| 631 | - |
|
| 632 | - $cr = $this->access->connection->getConnectionResource(); |
|
| 633 | - if(!$this->ldap->isResource($cr)) { |
|
| 634 | - throw new \Exception('connection error'); |
|
| 635 | - } |
|
| 636 | - |
|
| 637 | - if(mb_strpos($this->access->connection->ldapLoginFilter, '%uid', 0, 'UTF-8') |
|
| 638 | - === false) { |
|
| 639 | - throw new \Exception('missing placeholder'); |
|
| 640 | - } |
|
| 641 | - |
|
| 642 | - $users = $this->access->countUsersByLoginName($loginName); |
|
| 643 | - if($this->ldap->errno($cr) !== 0) { |
|
| 644 | - throw new \Exception($this->ldap->error($cr)); |
|
| 645 | - } |
|
| 646 | - $filter = str_replace('%uid', $loginName, $this->access->connection->ldapLoginFilter); |
|
| 647 | - $this->result->addChange('ldap_test_loginname', $users); |
|
| 648 | - $this->result->addChange('ldap_test_effective_filter', $filter); |
|
| 649 | - return $this->result; |
|
| 650 | - } |
|
| 651 | - |
|
| 652 | - /** |
|
| 653 | - * Tries to determine the port, requires given Host, User DN and Password |
|
| 654 | - * @return WizardResult|false WizardResult on success, false otherwise |
|
| 655 | - * @throws \Exception |
|
| 656 | - */ |
|
| 657 | - public function guessPortAndTLS() { |
|
| 658 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 659 | - ))) { |
|
| 660 | - return false; |
|
| 661 | - } |
|
| 662 | - $this->checkHost(); |
|
| 663 | - $portSettings = $this->getPortSettingsToTry(); |
|
| 664 | - |
|
| 665 | - if(!is_array($portSettings)) { |
|
| 666 | - throw new \Exception(print_r($portSettings, true)); |
|
| 667 | - } |
|
| 668 | - |
|
| 669 | - //proceed from the best configuration and return on first success |
|
| 670 | - foreach($portSettings as $setting) { |
|
| 671 | - $p = $setting['port']; |
|
| 672 | - $t = $setting['tls']; |
|
| 673 | - \OCP\Util::writeLog('user_ldap', 'Wiz: trying port '. $p . ', TLS '. $t, \OCP\Util::DEBUG); |
|
| 674 | - //connectAndBind may throw Exception, it needs to be catched by the |
|
| 675 | - //callee of this method |
|
| 676 | - |
|
| 677 | - try { |
|
| 678 | - $settingsFound = $this->connectAndBind($p, $t); |
|
| 679 | - } catch (\Exception $e) { |
|
| 680 | - // any reply other than -1 (= cannot connect) is already okay, |
|
| 681 | - // because then we found the server |
|
| 682 | - // unavailable startTLS returns -11 |
|
| 683 | - if($e->getCode() > 0) { |
|
| 684 | - $settingsFound = true; |
|
| 685 | - } else { |
|
| 686 | - throw $e; |
|
| 687 | - } |
|
| 688 | - } |
|
| 689 | - |
|
| 690 | - if ($settingsFound === true) { |
|
| 691 | - $config = array( |
|
| 692 | - 'ldapPort' => $p, |
|
| 693 | - 'ldapTLS' => (int)$t |
|
| 694 | - ); |
|
| 695 | - $this->configuration->setConfiguration($config); |
|
| 696 | - \OCP\Util::writeLog('user_ldap', 'Wiz: detected Port ' . $p, \OCP\Util::DEBUG); |
|
| 697 | - $this->result->addChange('ldap_port', $p); |
|
| 698 | - return $this->result; |
|
| 699 | - } |
|
| 700 | - } |
|
| 701 | - |
|
| 702 | - //custom port, undetected (we do not brute force) |
|
| 703 | - return false; |
|
| 704 | - } |
|
| 705 | - |
|
| 706 | - /** |
|
| 707 | - * tries to determine a base dn from User DN or LDAP Host |
|
| 708 | - * @return WizardResult|false WizardResult on success, false otherwise |
|
| 709 | - */ |
|
| 710 | - public function guessBaseDN() { |
|
| 711 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 712 | - 'ldapPort', |
|
| 713 | - ))) { |
|
| 714 | - return false; |
|
| 715 | - } |
|
| 716 | - |
|
| 717 | - //check whether a DN is given in the agent name (99.9% of all cases) |
|
| 718 | - $base = null; |
|
| 719 | - $i = stripos($this->configuration->ldapAgentName, 'dc='); |
|
| 720 | - if($i !== false) { |
|
| 721 | - $base = substr($this->configuration->ldapAgentName, $i); |
|
| 722 | - if($this->testBaseDN($base)) { |
|
| 723 | - $this->applyFind('ldap_base', $base); |
|
| 724 | - return $this->result; |
|
| 725 | - } |
|
| 726 | - } |
|
| 727 | - |
|
| 728 | - //this did not help :( |
|
| 729 | - //Let's see whether we can parse the Host URL and convert the domain to |
|
| 730 | - //a base DN |
|
| 731 | - $helper = new Helper(\OC::$server->getConfig()); |
|
| 732 | - $domain = $helper->getDomainFromURL($this->configuration->ldapHost); |
|
| 733 | - if(!$domain) { |
|
| 734 | - return false; |
|
| 735 | - } |
|
| 736 | - |
|
| 737 | - $dparts = explode('.', $domain); |
|
| 738 | - while(count($dparts) > 0) { |
|
| 739 | - $base2 = 'dc=' . implode(',dc=', $dparts); |
|
| 740 | - if ($base !== $base2 && $this->testBaseDN($base2)) { |
|
| 741 | - $this->applyFind('ldap_base', $base2); |
|
| 742 | - return $this->result; |
|
| 743 | - } |
|
| 744 | - array_shift($dparts); |
|
| 745 | - } |
|
| 746 | - |
|
| 747 | - return false; |
|
| 748 | - } |
|
| 749 | - |
|
| 750 | - /** |
|
| 751 | - * sets the found value for the configuration key in the WizardResult |
|
| 752 | - * as well as in the Configuration instance |
|
| 753 | - * @param string $key the configuration key |
|
| 754 | - * @param string $value the (detected) value |
|
| 755 | - * |
|
| 756 | - */ |
|
| 757 | - private function applyFind($key, $value) { |
|
| 758 | - $this->result->addChange($key, $value); |
|
| 759 | - $this->configuration->setConfiguration(array($key => $value)); |
|
| 760 | - } |
|
| 761 | - |
|
| 762 | - /** |
|
| 763 | - * Checks, whether a port was entered in the Host configuration |
|
| 764 | - * field. In this case the port will be stripped off, but also stored as |
|
| 765 | - * setting. |
|
| 766 | - */ |
|
| 767 | - private function checkHost() { |
|
| 768 | - $host = $this->configuration->ldapHost; |
|
| 769 | - $hostInfo = parse_url($host); |
|
| 770 | - |
|
| 771 | - //removes Port from Host |
|
| 772 | - if(is_array($hostInfo) && isset($hostInfo['port'])) { |
|
| 773 | - $port = $hostInfo['port']; |
|
| 774 | - $host = str_replace(':'.$port, '', $host); |
|
| 775 | - $this->applyFind('ldap_host', $host); |
|
| 776 | - $this->applyFind('ldap_port', $port); |
|
| 777 | - } |
|
| 778 | - } |
|
| 779 | - |
|
| 780 | - /** |
|
| 781 | - * tries to detect the group member association attribute which is |
|
| 782 | - * one of 'uniqueMember', 'memberUid', 'member', 'gidNumber' |
|
| 783 | - * @return string|false, string with the attribute name, false on error |
|
| 784 | - * @throws \Exception |
|
| 785 | - */ |
|
| 786 | - private function detectGroupMemberAssoc() { |
|
| 787 | - $possibleAttrs = array('uniqueMember', 'memberUid', 'member', 'gidNumber'); |
|
| 788 | - $filter = $this->configuration->ldapGroupFilter; |
|
| 789 | - if(empty($filter)) { |
|
| 790 | - return false; |
|
| 791 | - } |
|
| 792 | - $cr = $this->getConnection(); |
|
| 793 | - if(!$cr) { |
|
| 794 | - throw new \Exception('Could not connect to LDAP'); |
|
| 795 | - } |
|
| 796 | - $base = $this->configuration->ldapBase[0]; |
|
| 797 | - $rr = $this->ldap->search($cr, $base, $filter, $possibleAttrs, 0, 1000); |
|
| 798 | - if(!$this->ldap->isResource($rr)) { |
|
| 799 | - return false; |
|
| 800 | - } |
|
| 801 | - $er = $this->ldap->firstEntry($cr, $rr); |
|
| 802 | - while(is_resource($er)) { |
|
| 803 | - $this->ldap->getDN($cr, $er); |
|
| 804 | - $attrs = $this->ldap->getAttributes($cr, $er); |
|
| 805 | - $result = array(); |
|
| 806 | - $possibleAttrsCount = count($possibleAttrs); |
|
| 807 | - for($i = 0; $i < $possibleAttrsCount; $i++) { |
|
| 808 | - if(isset($attrs[$possibleAttrs[$i]])) { |
|
| 809 | - $result[$possibleAttrs[$i]] = $attrs[$possibleAttrs[$i]]['count']; |
|
| 810 | - } |
|
| 811 | - } |
|
| 812 | - if(!empty($result)) { |
|
| 813 | - natsort($result); |
|
| 814 | - return key($result); |
|
| 815 | - } |
|
| 816 | - |
|
| 817 | - $er = $this->ldap->nextEntry($cr, $er); |
|
| 818 | - } |
|
| 819 | - |
|
| 820 | - return false; |
|
| 821 | - } |
|
| 822 | - |
|
| 823 | - /** |
|
| 824 | - * Checks whether for a given BaseDN results will be returned |
|
| 825 | - * @param string $base the BaseDN to test |
|
| 826 | - * @return bool true on success, false otherwise |
|
| 827 | - * @throws \Exception |
|
| 828 | - */ |
|
| 829 | - private function testBaseDN($base) { |
|
| 830 | - $cr = $this->getConnection(); |
|
| 831 | - if(!$cr) { |
|
| 832 | - throw new \Exception('Could not connect to LDAP'); |
|
| 833 | - } |
|
| 834 | - |
|
| 835 | - //base is there, let's validate it. If we search for anything, we should |
|
| 836 | - //get a result set > 0 on a proper base |
|
| 837 | - $rr = $this->ldap->search($cr, $base, 'objectClass=*', array('dn'), 0, 1); |
|
| 838 | - if(!$this->ldap->isResource($rr)) { |
|
| 839 | - $errorNo = $this->ldap->errno($cr); |
|
| 840 | - $errorMsg = $this->ldap->error($cr); |
|
| 841 | - \OCP\Util::writeLog('user_ldap', 'Wiz: Could not search base '.$base. |
|
| 842 | - ' Error '.$errorNo.': '.$errorMsg, \OCP\Util::INFO); |
|
| 843 | - return false; |
|
| 844 | - } |
|
| 845 | - $entries = $this->ldap->countEntries($cr, $rr); |
|
| 846 | - return ($entries !== false) && ($entries > 0); |
|
| 847 | - } |
|
| 848 | - |
|
| 849 | - /** |
|
| 850 | - * Checks whether the server supports memberOf in LDAP Filter. |
|
| 851 | - * Note: at least in OpenLDAP, availability of memberOf is dependent on |
|
| 852 | - * a configured objectClass. I.e. not necessarily for all available groups |
|
| 853 | - * memberOf does work. |
|
| 854 | - * |
|
| 855 | - * @return bool true if it does, false otherwise |
|
| 856 | - * @throws \Exception |
|
| 857 | - */ |
|
| 858 | - private function testMemberOf() { |
|
| 859 | - $cr = $this->getConnection(); |
|
| 860 | - if(!$cr) { |
|
| 861 | - throw new \Exception('Could not connect to LDAP'); |
|
| 862 | - } |
|
| 863 | - $result = $this->access->countUsers('memberOf=*', array('memberOf'), 1); |
|
| 864 | - if(is_int($result) && $result > 0) { |
|
| 865 | - return true; |
|
| 866 | - } |
|
| 867 | - return false; |
|
| 868 | - } |
|
| 869 | - |
|
| 870 | - /** |
|
| 871 | - * creates an LDAP Filter from given configuration |
|
| 872 | - * @param integer $filterType int, for which use case the filter shall be created |
|
| 873 | - * can be any of self::LFILTER_USER_LIST, self::LFILTER_LOGIN or |
|
| 874 | - * self::LFILTER_GROUP_LIST |
|
| 875 | - * @return string|false string with the filter on success, false otherwise |
|
| 876 | - * @throws \Exception |
|
| 877 | - */ |
|
| 878 | - private function composeLdapFilter($filterType) { |
|
| 879 | - $filter = ''; |
|
| 880 | - $parts = 0; |
|
| 881 | - switch ($filterType) { |
|
| 882 | - case self::LFILTER_USER_LIST: |
|
| 883 | - $objcs = $this->configuration->ldapUserFilterObjectclass; |
|
| 884 | - //glue objectclasses |
|
| 885 | - if(is_array($objcs) && count($objcs) > 0) { |
|
| 886 | - $filter .= '(|'; |
|
| 887 | - foreach($objcs as $objc) { |
|
| 888 | - $filter .= '(objectclass=' . $objc . ')'; |
|
| 889 | - } |
|
| 890 | - $filter .= ')'; |
|
| 891 | - $parts++; |
|
| 892 | - } |
|
| 893 | - //glue group memberships |
|
| 894 | - if($this->configuration->hasMemberOfFilterSupport) { |
|
| 895 | - $cns = $this->configuration->ldapUserFilterGroups; |
|
| 896 | - if(is_array($cns) && count($cns) > 0) { |
|
| 897 | - $filter .= '(|'; |
|
| 898 | - $cr = $this->getConnection(); |
|
| 899 | - if(!$cr) { |
|
| 900 | - throw new \Exception('Could not connect to LDAP'); |
|
| 901 | - } |
|
| 902 | - $base = $this->configuration->ldapBase[0]; |
|
| 903 | - foreach($cns as $cn) { |
|
| 904 | - $rr = $this->ldap->search($cr, $base, 'cn=' . $cn, array('dn', 'primaryGroupToken')); |
|
| 905 | - if(!$this->ldap->isResource($rr)) { |
|
| 906 | - continue; |
|
| 907 | - } |
|
| 908 | - $er = $this->ldap->firstEntry($cr, $rr); |
|
| 909 | - $attrs = $this->ldap->getAttributes($cr, $er); |
|
| 910 | - $dn = $this->ldap->getDN($cr, $er); |
|
| 911 | - if ($dn === false || $dn === '') { |
|
| 912 | - continue; |
|
| 913 | - } |
|
| 914 | - $filterPart = '(memberof=' . $dn . ')'; |
|
| 915 | - if(isset($attrs['primaryGroupToken'])) { |
|
| 916 | - $pgt = $attrs['primaryGroupToken'][0]; |
|
| 917 | - $primaryFilterPart = '(primaryGroupID=' . $pgt .')'; |
|
| 918 | - $filterPart = '(|' . $filterPart . $primaryFilterPart . ')'; |
|
| 919 | - } |
|
| 920 | - $filter .= $filterPart; |
|
| 921 | - } |
|
| 922 | - $filter .= ')'; |
|
| 923 | - } |
|
| 924 | - $parts++; |
|
| 925 | - } |
|
| 926 | - //wrap parts in AND condition |
|
| 927 | - if($parts > 1) { |
|
| 928 | - $filter = '(&' . $filter . ')'; |
|
| 929 | - } |
|
| 930 | - if ($filter === '') { |
|
| 931 | - $filter = '(objectclass=*)'; |
|
| 932 | - } |
|
| 933 | - break; |
|
| 934 | - |
|
| 935 | - case self::LFILTER_GROUP_LIST: |
|
| 936 | - $objcs = $this->configuration->ldapGroupFilterObjectclass; |
|
| 937 | - //glue objectclasses |
|
| 938 | - if(is_array($objcs) && count($objcs) > 0) { |
|
| 939 | - $filter .= '(|'; |
|
| 940 | - foreach($objcs as $objc) { |
|
| 941 | - $filter .= '(objectclass=' . $objc . ')'; |
|
| 942 | - } |
|
| 943 | - $filter .= ')'; |
|
| 944 | - $parts++; |
|
| 945 | - } |
|
| 946 | - //glue group memberships |
|
| 947 | - $cns = $this->configuration->ldapGroupFilterGroups; |
|
| 948 | - if(is_array($cns) && count($cns) > 0) { |
|
| 949 | - $filter .= '(|'; |
|
| 950 | - foreach($cns as $cn) { |
|
| 951 | - $filter .= '(cn=' . $cn . ')'; |
|
| 952 | - } |
|
| 953 | - $filter .= ')'; |
|
| 954 | - } |
|
| 955 | - $parts++; |
|
| 956 | - //wrap parts in AND condition |
|
| 957 | - if($parts > 1) { |
|
| 958 | - $filter = '(&' . $filter . ')'; |
|
| 959 | - } |
|
| 960 | - break; |
|
| 961 | - |
|
| 962 | - case self::LFILTER_LOGIN: |
|
| 963 | - $ulf = $this->configuration->ldapUserFilter; |
|
| 964 | - $loginpart = '=%uid'; |
|
| 965 | - $filterUsername = ''; |
|
| 966 | - $userAttributes = $this->getUserAttributes(); |
|
| 967 | - $userAttributes = array_change_key_case(array_flip($userAttributes)); |
|
| 968 | - $parts = 0; |
|
| 969 | - |
|
| 970 | - if($this->configuration->ldapLoginFilterUsername === '1') { |
|
| 971 | - $attr = ''; |
|
| 972 | - if(isset($userAttributes['uid'])) { |
|
| 973 | - $attr = 'uid'; |
|
| 974 | - } else if(isset($userAttributes['samaccountname'])) { |
|
| 975 | - $attr = 'samaccountname'; |
|
| 976 | - } else if(isset($userAttributes['cn'])) { |
|
| 977 | - //fallback |
|
| 978 | - $attr = 'cn'; |
|
| 979 | - } |
|
| 980 | - if ($attr !== '') { |
|
| 981 | - $filterUsername = '(' . $attr . $loginpart . ')'; |
|
| 982 | - $parts++; |
|
| 983 | - } |
|
| 984 | - } |
|
| 985 | - |
|
| 986 | - $filterEmail = ''; |
|
| 987 | - if($this->configuration->ldapLoginFilterEmail === '1') { |
|
| 988 | - $filterEmail = '(|(mailPrimaryAddress=%uid)(mail=%uid))'; |
|
| 989 | - $parts++; |
|
| 990 | - } |
|
| 991 | - |
|
| 992 | - $filterAttributes = ''; |
|
| 993 | - $attrsToFilter = $this->configuration->ldapLoginFilterAttributes; |
|
| 994 | - if(is_array($attrsToFilter) && count($attrsToFilter) > 0) { |
|
| 995 | - $filterAttributes = '(|'; |
|
| 996 | - foreach($attrsToFilter as $attribute) { |
|
| 997 | - $filterAttributes .= '(' . $attribute . $loginpart . ')'; |
|
| 998 | - } |
|
| 999 | - $filterAttributes .= ')'; |
|
| 1000 | - $parts++; |
|
| 1001 | - } |
|
| 1002 | - |
|
| 1003 | - $filterLogin = ''; |
|
| 1004 | - if($parts > 1) { |
|
| 1005 | - $filterLogin = '(|'; |
|
| 1006 | - } |
|
| 1007 | - $filterLogin .= $filterUsername; |
|
| 1008 | - $filterLogin .= $filterEmail; |
|
| 1009 | - $filterLogin .= $filterAttributes; |
|
| 1010 | - if($parts > 1) { |
|
| 1011 | - $filterLogin .= ')'; |
|
| 1012 | - } |
|
| 1013 | - |
|
| 1014 | - $filter = '(&'.$ulf.$filterLogin.')'; |
|
| 1015 | - break; |
|
| 1016 | - } |
|
| 1017 | - |
|
| 1018 | - \OCP\Util::writeLog('user_ldap', 'Wiz: Final filter '.$filter, \OCP\Util::DEBUG); |
|
| 1019 | - |
|
| 1020 | - return $filter; |
|
| 1021 | - } |
|
| 1022 | - |
|
| 1023 | - /** |
|
| 1024 | - * Connects and Binds to an LDAP Server |
|
| 1025 | - * |
|
| 1026 | - * @param int $port the port to connect with |
|
| 1027 | - * @param bool $tls whether startTLS is to be used |
|
| 1028 | - * @return bool |
|
| 1029 | - * @throws \Exception |
|
| 1030 | - */ |
|
| 1031 | - private function connectAndBind($port, $tls) { |
|
| 1032 | - //connect, does not really trigger any server communication |
|
| 1033 | - $host = $this->configuration->ldapHost; |
|
| 1034 | - $hostInfo = parse_url($host); |
|
| 1035 | - if(!$hostInfo) { |
|
| 1036 | - throw new \Exception(self::$l->t('Invalid Host')); |
|
| 1037 | - } |
|
| 1038 | - \OCP\Util::writeLog('user_ldap', 'Wiz: Attempting to connect ', \OCP\Util::DEBUG); |
|
| 1039 | - $cr = $this->ldap->connect($host, $port); |
|
| 1040 | - if(!is_resource($cr)) { |
|
| 1041 | - throw new \Exception(self::$l->t('Invalid Host')); |
|
| 1042 | - } |
|
| 1043 | - |
|
| 1044 | - //set LDAP options |
|
| 1045 | - $this->ldap->setOption($cr, LDAP_OPT_PROTOCOL_VERSION, 3); |
|
| 1046 | - $this->ldap->setOption($cr, LDAP_OPT_REFERRALS, 0); |
|
| 1047 | - $this->ldap->setOption($cr, LDAP_OPT_NETWORK_TIMEOUT, self::LDAP_NW_TIMEOUT); |
|
| 1048 | - |
|
| 1049 | - try { |
|
| 1050 | - if($tls) { |
|
| 1051 | - $isTlsWorking = @$this->ldap->startTls($cr); |
|
| 1052 | - if(!$isTlsWorking) { |
|
| 1053 | - return false; |
|
| 1054 | - } |
|
| 1055 | - } |
|
| 1056 | - |
|
| 1057 | - \OCP\Util::writeLog('user_ldap', 'Wiz: Attemping to Bind ', \OCP\Util::DEBUG); |
|
| 1058 | - //interesting part: do the bind! |
|
| 1059 | - $login = $this->ldap->bind($cr, |
|
| 1060 | - $this->configuration->ldapAgentName, |
|
| 1061 | - $this->configuration->ldapAgentPassword |
|
| 1062 | - ); |
|
| 1063 | - $errNo = $this->ldap->errno($cr); |
|
| 1064 | - $error = ldap_error($cr); |
|
| 1065 | - $this->ldap->unbind($cr); |
|
| 1066 | - } catch(ServerNotAvailableException $e) { |
|
| 1067 | - return false; |
|
| 1068 | - } |
|
| 1069 | - |
|
| 1070 | - if($login === true) { |
|
| 1071 | - $this->ldap->unbind($cr); |
|
| 1072 | - \OCP\Util::writeLog('user_ldap', 'Wiz: Bind successful to Port '. $port . ' TLS ' . (int)$tls, \OCP\Util::DEBUG); |
|
| 1073 | - return true; |
|
| 1074 | - } |
|
| 1075 | - |
|
| 1076 | - if($errNo === -1) { |
|
| 1077 | - //host, port or TLS wrong |
|
| 1078 | - return false; |
|
| 1079 | - } |
|
| 1080 | - throw new \Exception($error, $errNo); |
|
| 1081 | - } |
|
| 1082 | - |
|
| 1083 | - /** |
|
| 1084 | - * checks whether a valid combination of agent and password has been |
|
| 1085 | - * provided (either two values or nothing for anonymous connect) |
|
| 1086 | - * @return bool, true if everything is fine, false otherwise |
|
| 1087 | - */ |
|
| 1088 | - private function checkAgentRequirements() { |
|
| 1089 | - $agent = $this->configuration->ldapAgentName; |
|
| 1090 | - $pwd = $this->configuration->ldapAgentPassword; |
|
| 1091 | - |
|
| 1092 | - return |
|
| 1093 | - ($agent !== '' && $pwd !== '') |
|
| 1094 | - || ($agent === '' && $pwd === '') |
|
| 1095 | - ; |
|
| 1096 | - } |
|
| 1097 | - |
|
| 1098 | - /** |
|
| 1099 | - * @param array $reqs |
|
| 1100 | - * @return bool |
|
| 1101 | - */ |
|
| 1102 | - private function checkRequirements($reqs) { |
|
| 1103 | - $this->checkAgentRequirements(); |
|
| 1104 | - foreach($reqs as $option) { |
|
| 1105 | - $value = $this->configuration->$option; |
|
| 1106 | - if(empty($value)) { |
|
| 1107 | - return false; |
|
| 1108 | - } |
|
| 1109 | - } |
|
| 1110 | - return true; |
|
| 1111 | - } |
|
| 1112 | - |
|
| 1113 | - /** |
|
| 1114 | - * does a cumulativeSearch on LDAP to get different values of a |
|
| 1115 | - * specified attribute |
|
| 1116 | - * @param string[] $filters array, the filters that shall be used in the search |
|
| 1117 | - * @param string $attr the attribute of which a list of values shall be returned |
|
| 1118 | - * @param int $dnReadLimit the amount of how many DNs should be analyzed. |
|
| 1119 | - * The lower, the faster |
|
| 1120 | - * @param string $maxF string. if not null, this variable will have the filter that |
|
| 1121 | - * yields most result entries |
|
| 1122 | - * @return array|false an array with the values on success, false otherwise |
|
| 1123 | - */ |
|
| 1124 | - public function cumulativeSearchOnAttribute($filters, $attr, $dnReadLimit = 3, &$maxF = null) { |
|
| 1125 | - $dnRead = array(); |
|
| 1126 | - $foundItems = array(); |
|
| 1127 | - $maxEntries = 0; |
|
| 1128 | - if(!is_array($this->configuration->ldapBase) |
|
| 1129 | - || !isset($this->configuration->ldapBase[0])) { |
|
| 1130 | - return false; |
|
| 1131 | - } |
|
| 1132 | - $base = $this->configuration->ldapBase[0]; |
|
| 1133 | - $cr = $this->getConnection(); |
|
| 1134 | - if(!$this->ldap->isResource($cr)) { |
|
| 1135 | - return false; |
|
| 1136 | - } |
|
| 1137 | - $lastFilter = null; |
|
| 1138 | - if(isset($filters[count($filters)-1])) { |
|
| 1139 | - $lastFilter = $filters[count($filters)-1]; |
|
| 1140 | - } |
|
| 1141 | - foreach($filters as $filter) { |
|
| 1142 | - if($lastFilter === $filter && count($foundItems) > 0) { |
|
| 1143 | - //skip when the filter is a wildcard and results were found |
|
| 1144 | - continue; |
|
| 1145 | - } |
|
| 1146 | - // 20k limit for performance and reason |
|
| 1147 | - $rr = $this->ldap->search($cr, $base, $filter, array($attr), 0, 20000); |
|
| 1148 | - if(!$this->ldap->isResource($rr)) { |
|
| 1149 | - continue; |
|
| 1150 | - } |
|
| 1151 | - $entries = $this->ldap->countEntries($cr, $rr); |
|
| 1152 | - $getEntryFunc = 'firstEntry'; |
|
| 1153 | - if(($entries !== false) && ($entries > 0)) { |
|
| 1154 | - if(!is_null($maxF) && $entries > $maxEntries) { |
|
| 1155 | - $maxEntries = $entries; |
|
| 1156 | - $maxF = $filter; |
|
| 1157 | - } |
|
| 1158 | - $dnReadCount = 0; |
|
| 1159 | - do { |
|
| 1160 | - $entry = $this->ldap->$getEntryFunc($cr, $rr); |
|
| 1161 | - $getEntryFunc = 'nextEntry'; |
|
| 1162 | - if(!$this->ldap->isResource($entry)) { |
|
| 1163 | - continue 2; |
|
| 1164 | - } |
|
| 1165 | - $rr = $entry; //will be expected by nextEntry next round |
|
| 1166 | - $attributes = $this->ldap->getAttributes($cr, $entry); |
|
| 1167 | - $dn = $this->ldap->getDN($cr, $entry); |
|
| 1168 | - if($dn === false || in_array($dn, $dnRead)) { |
|
| 1169 | - continue; |
|
| 1170 | - } |
|
| 1171 | - $newItems = array(); |
|
| 1172 | - $state = $this->getAttributeValuesFromEntry($attributes, |
|
| 1173 | - $attr, |
|
| 1174 | - $newItems); |
|
| 1175 | - $dnReadCount++; |
|
| 1176 | - $foundItems = array_merge($foundItems, $newItems); |
|
| 1177 | - $this->resultCache[$dn][$attr] = $newItems; |
|
| 1178 | - $dnRead[] = $dn; |
|
| 1179 | - } while(($state === self::LRESULT_PROCESSED_SKIP |
|
| 1180 | - || $this->ldap->isResource($entry)) |
|
| 1181 | - && ($dnReadLimit === 0 || $dnReadCount < $dnReadLimit)); |
|
| 1182 | - } |
|
| 1183 | - } |
|
| 1184 | - |
|
| 1185 | - return array_unique($foundItems); |
|
| 1186 | - } |
|
| 1187 | - |
|
| 1188 | - /** |
|
| 1189 | - * determines if and which $attr are available on the LDAP server |
|
| 1190 | - * @param string[] $objectclasses the objectclasses to use as search filter |
|
| 1191 | - * @param string $attr the attribute to look for |
|
| 1192 | - * @param string $dbkey the dbkey of the setting the feature is connected to |
|
| 1193 | - * @param string $confkey the confkey counterpart for the $dbkey as used in the |
|
| 1194 | - * Configuration class |
|
| 1195 | - * @param bool $po whether the objectClass with most result entries |
|
| 1196 | - * shall be pre-selected via the result |
|
| 1197 | - * @return array|false list of found items. |
|
| 1198 | - * @throws \Exception |
|
| 1199 | - */ |
|
| 1200 | - private function determineFeature($objectclasses, $attr, $dbkey, $confkey, $po = false) { |
|
| 1201 | - $cr = $this->getConnection(); |
|
| 1202 | - if(!$cr) { |
|
| 1203 | - throw new \Exception('Could not connect to LDAP'); |
|
| 1204 | - } |
|
| 1205 | - $p = 'objectclass='; |
|
| 1206 | - foreach($objectclasses as $key => $value) { |
|
| 1207 | - $objectclasses[$key] = $p.$value; |
|
| 1208 | - } |
|
| 1209 | - $maxEntryObjC = ''; |
|
| 1210 | - |
|
| 1211 | - //how deep to dig? |
|
| 1212 | - //When looking for objectclasses, testing few entries is sufficient, |
|
| 1213 | - $dig = 3; |
|
| 1214 | - |
|
| 1215 | - $availableFeatures = |
|
| 1216 | - $this->cumulativeSearchOnAttribute($objectclasses, $attr, |
|
| 1217 | - $dig, $maxEntryObjC); |
|
| 1218 | - if(is_array($availableFeatures) |
|
| 1219 | - && count($availableFeatures) > 0) { |
|
| 1220 | - natcasesort($availableFeatures); |
|
| 1221 | - //natcasesort keeps indices, but we must get rid of them for proper |
|
| 1222 | - //sorting in the web UI. Therefore: array_values |
|
| 1223 | - $this->result->addOptions($dbkey, array_values($availableFeatures)); |
|
| 1224 | - } else { |
|
| 1225 | - throw new \Exception(self::$l->t('Could not find the desired feature')); |
|
| 1226 | - } |
|
| 1227 | - |
|
| 1228 | - $setFeatures = $this->configuration->$confkey; |
|
| 1229 | - if(is_array($setFeatures) && !empty($setFeatures)) { |
|
| 1230 | - //something is already configured? pre-select it. |
|
| 1231 | - $this->result->addChange($dbkey, $setFeatures); |
|
| 1232 | - } else if ($po && $maxEntryObjC !== '') { |
|
| 1233 | - //pre-select objectclass with most result entries |
|
| 1234 | - $maxEntryObjC = str_replace($p, '', $maxEntryObjC); |
|
| 1235 | - $this->applyFind($dbkey, $maxEntryObjC); |
|
| 1236 | - $this->result->addChange($dbkey, $maxEntryObjC); |
|
| 1237 | - } |
|
| 1238 | - |
|
| 1239 | - return $availableFeatures; |
|
| 1240 | - } |
|
| 1241 | - |
|
| 1242 | - /** |
|
| 1243 | - * appends a list of values fr |
|
| 1244 | - * @param resource $result the return value from ldap_get_attributes |
|
| 1245 | - * @param string $attribute the attribute values to look for |
|
| 1246 | - * @param array &$known new values will be appended here |
|
| 1247 | - * @return int, state on of the class constants LRESULT_PROCESSED_OK, |
|
| 1248 | - * LRESULT_PROCESSED_INVALID or LRESULT_PROCESSED_SKIP |
|
| 1249 | - */ |
|
| 1250 | - private function getAttributeValuesFromEntry($result, $attribute, &$known) { |
|
| 1251 | - if(!is_array($result) |
|
| 1252 | - || !isset($result['count']) |
|
| 1253 | - || !$result['count'] > 0) { |
|
| 1254 | - return self::LRESULT_PROCESSED_INVALID; |
|
| 1255 | - } |
|
| 1256 | - |
|
| 1257 | - // strtolower on all keys for proper comparison |
|
| 1258 | - $result = \OCP\Util::mb_array_change_key_case($result); |
|
| 1259 | - $attribute = strtolower($attribute); |
|
| 1260 | - if(isset($result[$attribute])) { |
|
| 1261 | - foreach($result[$attribute] as $key => $val) { |
|
| 1262 | - if($key === 'count') { |
|
| 1263 | - continue; |
|
| 1264 | - } |
|
| 1265 | - if(!in_array($val, $known)) { |
|
| 1266 | - $known[] = $val; |
|
| 1267 | - } |
|
| 1268 | - } |
|
| 1269 | - return self::LRESULT_PROCESSED_OK; |
|
| 1270 | - } else { |
|
| 1271 | - return self::LRESULT_PROCESSED_SKIP; |
|
| 1272 | - } |
|
| 1273 | - } |
|
| 1274 | - |
|
| 1275 | - /** |
|
| 1276 | - * @return bool|mixed |
|
| 1277 | - */ |
|
| 1278 | - private function getConnection() { |
|
| 1279 | - if(!is_null($this->cr)) { |
|
| 1280 | - return $this->cr; |
|
| 1281 | - } |
|
| 1282 | - |
|
| 1283 | - $cr = $this->ldap->connect( |
|
| 1284 | - $this->configuration->ldapHost, |
|
| 1285 | - $this->configuration->ldapPort |
|
| 1286 | - ); |
|
| 1287 | - |
|
| 1288 | - $this->ldap->setOption($cr, LDAP_OPT_PROTOCOL_VERSION, 3); |
|
| 1289 | - $this->ldap->setOption($cr, LDAP_OPT_REFERRALS, 0); |
|
| 1290 | - $this->ldap->setOption($cr, LDAP_OPT_NETWORK_TIMEOUT, self::LDAP_NW_TIMEOUT); |
|
| 1291 | - if($this->configuration->ldapTLS === 1) { |
|
| 1292 | - $this->ldap->startTls($cr); |
|
| 1293 | - } |
|
| 1294 | - |
|
| 1295 | - $lo = @$this->ldap->bind($cr, |
|
| 1296 | - $this->configuration->ldapAgentName, |
|
| 1297 | - $this->configuration->ldapAgentPassword); |
|
| 1298 | - if($lo === true) { |
|
| 1299 | - $this->$cr = $cr; |
|
| 1300 | - return $cr; |
|
| 1301 | - } |
|
| 1302 | - |
|
| 1303 | - return false; |
|
| 1304 | - } |
|
| 1305 | - |
|
| 1306 | - /** |
|
| 1307 | - * @return array |
|
| 1308 | - */ |
|
| 1309 | - private function getDefaultLdapPortSettings() { |
|
| 1310 | - static $settings = array( |
|
| 1311 | - array('port' => 7636, 'tls' => false), |
|
| 1312 | - array('port' => 636, 'tls' => false), |
|
| 1313 | - array('port' => 7389, 'tls' => true), |
|
| 1314 | - array('port' => 389, 'tls' => true), |
|
| 1315 | - array('port' => 7389, 'tls' => false), |
|
| 1316 | - array('port' => 389, 'tls' => false), |
|
| 1317 | - ); |
|
| 1318 | - return $settings; |
|
| 1319 | - } |
|
| 1320 | - |
|
| 1321 | - /** |
|
| 1322 | - * @return array |
|
| 1323 | - */ |
|
| 1324 | - private function getPortSettingsToTry() { |
|
| 1325 | - //389 ← LDAP / Unencrypted or StartTLS |
|
| 1326 | - //636 ← LDAPS / SSL |
|
| 1327 | - //7xxx ← UCS. need to be checked first, because both ports may be open |
|
| 1328 | - $host = $this->configuration->ldapHost; |
|
| 1329 | - $port = (int)$this->configuration->ldapPort; |
|
| 1330 | - $portSettings = array(); |
|
| 1331 | - |
|
| 1332 | - //In case the port is already provided, we will check this first |
|
| 1333 | - if($port > 0) { |
|
| 1334 | - $hostInfo = parse_url($host); |
|
| 1335 | - if(!(is_array($hostInfo) |
|
| 1336 | - && isset($hostInfo['scheme']) |
|
| 1337 | - && stripos($hostInfo['scheme'], 'ldaps') !== false)) { |
|
| 1338 | - $portSettings[] = array('port' => $port, 'tls' => true); |
|
| 1339 | - } |
|
| 1340 | - $portSettings[] =array('port' => $port, 'tls' => false); |
|
| 1341 | - } |
|
| 1342 | - |
|
| 1343 | - //default ports |
|
| 1344 | - $portSettings = array_merge($portSettings, |
|
| 1345 | - $this->getDefaultLdapPortSettings()); |
|
| 1346 | - |
|
| 1347 | - return $portSettings; |
|
| 1348 | - } |
|
| 44 | + /** @var \OCP\IL10N */ |
|
| 45 | + static protected $l; |
|
| 46 | + protected $access; |
|
| 47 | + protected $cr; |
|
| 48 | + protected $configuration; |
|
| 49 | + protected $result; |
|
| 50 | + protected $resultCache = array(); |
|
| 51 | + |
|
| 52 | + const LRESULT_PROCESSED_OK = 2; |
|
| 53 | + const LRESULT_PROCESSED_INVALID = 3; |
|
| 54 | + const LRESULT_PROCESSED_SKIP = 4; |
|
| 55 | + |
|
| 56 | + const LFILTER_LOGIN = 2; |
|
| 57 | + const LFILTER_USER_LIST = 3; |
|
| 58 | + const LFILTER_GROUP_LIST = 4; |
|
| 59 | + |
|
| 60 | + const LFILTER_MODE_ASSISTED = 2; |
|
| 61 | + const LFILTER_MODE_RAW = 1; |
|
| 62 | + |
|
| 63 | + const LDAP_NW_TIMEOUT = 4; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Constructor |
|
| 67 | + * @param Configuration $configuration an instance of Configuration |
|
| 68 | + * @param ILDAPWrapper $ldap an instance of ILDAPWrapper |
|
| 69 | + * @param Access $access |
|
| 70 | + */ |
|
| 71 | + public function __construct(Configuration $configuration, ILDAPWrapper $ldap, Access $access) { |
|
| 72 | + parent::__construct($ldap); |
|
| 73 | + $this->configuration = $configuration; |
|
| 74 | + if(is_null(Wizard::$l)) { |
|
| 75 | + Wizard::$l = \OC::$server->getL10N('user_ldap'); |
|
| 76 | + } |
|
| 77 | + $this->access = $access; |
|
| 78 | + $this->result = new WizardResult(); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + public function __destruct() { |
|
| 82 | + if($this->result->hasChanges()) { |
|
| 83 | + $this->configuration->saveConfiguration(); |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * counts entries in the LDAP directory |
|
| 89 | + * |
|
| 90 | + * @param string $filter the LDAP search filter |
|
| 91 | + * @param string $type a string being either 'users' or 'groups'; |
|
| 92 | + * @return bool|int |
|
| 93 | + * @throws \Exception |
|
| 94 | + */ |
|
| 95 | + public function countEntries($filter, $type) { |
|
| 96 | + $reqs = array('ldapHost', 'ldapPort', 'ldapBase'); |
|
| 97 | + if($type === 'users') { |
|
| 98 | + $reqs[] = 'ldapUserFilter'; |
|
| 99 | + } |
|
| 100 | + if(!$this->checkRequirements($reqs)) { |
|
| 101 | + throw new \Exception('Requirements not met', 400); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + $attr = array('dn'); // default |
|
| 105 | + $limit = 1001; |
|
| 106 | + if($type === 'groups') { |
|
| 107 | + $result = $this->access->countGroups($filter, $attr, $limit); |
|
| 108 | + } else if($type === 'users') { |
|
| 109 | + $result = $this->access->countUsers($filter, $attr, $limit); |
|
| 110 | + } else if ($type === 'objects') { |
|
| 111 | + $result = $this->access->countObjects($limit); |
|
| 112 | + } else { |
|
| 113 | + throw new \Exception('Internal error: Invalid object type', 500); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + return $result; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * formats the return value of a count operation to the string to be |
|
| 121 | + * inserted. |
|
| 122 | + * |
|
| 123 | + * @param bool|int $count |
|
| 124 | + * @return int|string |
|
| 125 | + */ |
|
| 126 | + private function formatCountResult($count) { |
|
| 127 | + $formatted = ($count !== false) ? $count : 0; |
|
| 128 | + if($formatted > 1000) { |
|
| 129 | + $formatted = '> 1000'; |
|
| 130 | + } |
|
| 131 | + return $formatted; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + public function countGroups() { |
|
| 135 | + $filter = $this->configuration->ldapGroupFilter; |
|
| 136 | + |
|
| 137 | + if(empty($filter)) { |
|
| 138 | + $output = self::$l->n('%s group found', '%s groups found', 0, array(0)); |
|
| 139 | + $this->result->addChange('ldap_group_count', $output); |
|
| 140 | + return $this->result; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + try { |
|
| 144 | + $groupsTotal = $this->formatCountResult($this->countEntries($filter, 'groups')); |
|
| 145 | + } catch (\Exception $e) { |
|
| 146 | + //400 can be ignored, 500 is forwarded |
|
| 147 | + if($e->getCode() === 500) { |
|
| 148 | + throw $e; |
|
| 149 | + } |
|
| 150 | + return false; |
|
| 151 | + } |
|
| 152 | + $output = self::$l->n('%s group found', '%s groups found', $groupsTotal, array($groupsTotal)); |
|
| 153 | + $this->result->addChange('ldap_group_count', $output); |
|
| 154 | + return $this->result; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * @return WizardResult |
|
| 159 | + * @throws \Exception |
|
| 160 | + */ |
|
| 161 | + public function countUsers() { |
|
| 162 | + $filter = $this->access->getFilterForUserCount(); |
|
| 163 | + |
|
| 164 | + $usersTotal = $this->formatCountResult($this->countEntries($filter, 'users')); |
|
| 165 | + $output = self::$l->n('%s user found', '%s users found', $usersTotal, array($usersTotal)); |
|
| 166 | + $this->result->addChange('ldap_user_count', $output); |
|
| 167 | + return $this->result; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * counts any objects in the currently set base dn |
|
| 172 | + * |
|
| 173 | + * @return WizardResult |
|
| 174 | + * @throws \Exception |
|
| 175 | + */ |
|
| 176 | + public function countInBaseDN() { |
|
| 177 | + // we don't need to provide a filter in this case |
|
| 178 | + $total = $this->countEntries(null, 'objects'); |
|
| 179 | + if($total === false) { |
|
| 180 | + throw new \Exception('invalid results received'); |
|
| 181 | + } |
|
| 182 | + $this->result->addChange('ldap_test_base', $total); |
|
| 183 | + return $this->result; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * counts users with a specified attribute |
|
| 188 | + * @param string $attr |
|
| 189 | + * @param bool $existsCheck |
|
| 190 | + * @return int|bool |
|
| 191 | + */ |
|
| 192 | + public function countUsersWithAttribute($attr, $existsCheck = false) { |
|
| 193 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 194 | + 'ldapPort', |
|
| 195 | + 'ldapBase', |
|
| 196 | + 'ldapUserFilter', |
|
| 197 | + ))) { |
|
| 198 | + return false; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + $filter = $this->access->combineFilterWithAnd(array( |
|
| 202 | + $this->configuration->ldapUserFilter, |
|
| 203 | + $attr . '=*' |
|
| 204 | + )); |
|
| 205 | + |
|
| 206 | + $limit = ($existsCheck === false) ? null : 1; |
|
| 207 | + |
|
| 208 | + return $this->access->countUsers($filter, array('dn'), $limit); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * detects the display name attribute. If a setting is already present that |
|
| 213 | + * returns at least one hit, the detection will be canceled. |
|
| 214 | + * @return WizardResult|bool |
|
| 215 | + * @throws \Exception |
|
| 216 | + */ |
|
| 217 | + public function detectUserDisplayNameAttribute() { |
|
| 218 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 219 | + 'ldapPort', |
|
| 220 | + 'ldapBase', |
|
| 221 | + 'ldapUserFilter', |
|
| 222 | + ))) { |
|
| 223 | + return false; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + $attr = $this->configuration->ldapUserDisplayName; |
|
| 227 | + if ($attr !== '' && $attr !== 'displayName') { |
|
| 228 | + // most likely not the default value with upper case N, |
|
| 229 | + // verify it still produces a result |
|
| 230 | + $count = (int)$this->countUsersWithAttribute($attr, true); |
|
| 231 | + if($count > 0) { |
|
| 232 | + //no change, but we sent it back to make sure the user interface |
|
| 233 | + //is still correct, even if the ajax call was cancelled meanwhile |
|
| 234 | + $this->result->addChange('ldap_display_name', $attr); |
|
| 235 | + return $this->result; |
|
| 236 | + } |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + // first attribute that has at least one result wins |
|
| 240 | + $displayNameAttrs = array('displayname', 'cn'); |
|
| 241 | + foreach ($displayNameAttrs as $attr) { |
|
| 242 | + $count = (int)$this->countUsersWithAttribute($attr, true); |
|
| 243 | + |
|
| 244 | + if($count > 0) { |
|
| 245 | + $this->applyFind('ldap_display_name', $attr); |
|
| 246 | + return $this->result; |
|
| 247 | + } |
|
| 248 | + }; |
|
| 249 | + |
|
| 250 | + throw new \Exception(self::$l->t('Could not detect user display name attribute. Please specify it yourself in advanced LDAP settings.')); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * detects the most often used email attribute for users applying to the |
|
| 255 | + * user list filter. If a setting is already present that returns at least |
|
| 256 | + * one hit, the detection will be canceled. |
|
| 257 | + * @return WizardResult|bool |
|
| 258 | + */ |
|
| 259 | + public function detectEmailAttribute() { |
|
| 260 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 261 | + 'ldapPort', |
|
| 262 | + 'ldapBase', |
|
| 263 | + 'ldapUserFilter', |
|
| 264 | + ))) { |
|
| 265 | + return false; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + $attr = $this->configuration->ldapEmailAttribute; |
|
| 269 | + if ($attr !== '') { |
|
| 270 | + $count = (int)$this->countUsersWithAttribute($attr, true); |
|
| 271 | + if($count > 0) { |
|
| 272 | + return false; |
|
| 273 | + } |
|
| 274 | + $writeLog = true; |
|
| 275 | + } else { |
|
| 276 | + $writeLog = false; |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + $emailAttributes = array('mail', 'mailPrimaryAddress'); |
|
| 280 | + $winner = ''; |
|
| 281 | + $maxUsers = 0; |
|
| 282 | + foreach($emailAttributes as $attr) { |
|
| 283 | + $count = $this->countUsersWithAttribute($attr); |
|
| 284 | + if($count > $maxUsers) { |
|
| 285 | + $maxUsers = $count; |
|
| 286 | + $winner = $attr; |
|
| 287 | + } |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + if($winner !== '') { |
|
| 291 | + $this->applyFind('ldap_email_attr', $winner); |
|
| 292 | + if($writeLog) { |
|
| 293 | + \OCP\Util::writeLog('user_ldap', 'The mail attribute has ' . |
|
| 294 | + 'automatically been reset, because the original value ' . |
|
| 295 | + 'did not return any results.', \OCP\Util::INFO); |
|
| 296 | + } |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + return $this->result; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * @return WizardResult |
|
| 304 | + * @throws \Exception |
|
| 305 | + */ |
|
| 306 | + public function determineAttributes() { |
|
| 307 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 308 | + 'ldapPort', |
|
| 309 | + 'ldapBase', |
|
| 310 | + 'ldapUserFilter', |
|
| 311 | + ))) { |
|
| 312 | + return false; |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + $attributes = $this->getUserAttributes(); |
|
| 316 | + |
|
| 317 | + natcasesort($attributes); |
|
| 318 | + $attributes = array_values($attributes); |
|
| 319 | + |
|
| 320 | + $this->result->addOptions('ldap_loginfilter_attributes', $attributes); |
|
| 321 | + |
|
| 322 | + $selected = $this->configuration->ldapLoginFilterAttributes; |
|
| 323 | + if(is_array($selected) && !empty($selected)) { |
|
| 324 | + $this->result->addChange('ldap_loginfilter_attributes', $selected); |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + return $this->result; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * detects the available LDAP attributes |
|
| 332 | + * @return array|false The instance's WizardResult instance |
|
| 333 | + * @throws \Exception |
|
| 334 | + */ |
|
| 335 | + private function getUserAttributes() { |
|
| 336 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 337 | + 'ldapPort', |
|
| 338 | + 'ldapBase', |
|
| 339 | + 'ldapUserFilter', |
|
| 340 | + ))) { |
|
| 341 | + return false; |
|
| 342 | + } |
|
| 343 | + $cr = $this->getConnection(); |
|
| 344 | + if(!$cr) { |
|
| 345 | + throw new \Exception('Could not connect to LDAP'); |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + $base = $this->configuration->ldapBase[0]; |
|
| 349 | + $filter = $this->configuration->ldapUserFilter; |
|
| 350 | + $rr = $this->ldap->search($cr, $base, $filter, array(), 1, 1); |
|
| 351 | + if(!$this->ldap->isResource($rr)) { |
|
| 352 | + return false; |
|
| 353 | + } |
|
| 354 | + $er = $this->ldap->firstEntry($cr, $rr); |
|
| 355 | + $attributes = $this->ldap->getAttributes($cr, $er); |
|
| 356 | + $pureAttributes = array(); |
|
| 357 | + for($i = 0; $i < $attributes['count']; $i++) { |
|
| 358 | + $pureAttributes[] = $attributes[$i]; |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + return $pureAttributes; |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + /** |
|
| 365 | + * detects the available LDAP groups |
|
| 366 | + * @return WizardResult|false the instance's WizardResult instance |
|
| 367 | + */ |
|
| 368 | + public function determineGroupsForGroups() { |
|
| 369 | + return $this->determineGroups('ldap_groupfilter_groups', |
|
| 370 | + 'ldapGroupFilterGroups', |
|
| 371 | + false); |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + /** |
|
| 375 | + * detects the available LDAP groups |
|
| 376 | + * @return WizardResult|false the instance's WizardResult instance |
|
| 377 | + */ |
|
| 378 | + public function determineGroupsForUsers() { |
|
| 379 | + return $this->determineGroups('ldap_userfilter_groups', |
|
| 380 | + 'ldapUserFilterGroups'); |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + /** |
|
| 384 | + * detects the available LDAP groups |
|
| 385 | + * @param string $dbKey |
|
| 386 | + * @param string $confKey |
|
| 387 | + * @param bool $testMemberOf |
|
| 388 | + * @return WizardResult|false the instance's WizardResult instance |
|
| 389 | + * @throws \Exception |
|
| 390 | + */ |
|
| 391 | + private function determineGroups($dbKey, $confKey, $testMemberOf = true) { |
|
| 392 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 393 | + 'ldapPort', |
|
| 394 | + 'ldapBase', |
|
| 395 | + ))) { |
|
| 396 | + return false; |
|
| 397 | + } |
|
| 398 | + $cr = $this->getConnection(); |
|
| 399 | + if(!$cr) { |
|
| 400 | + throw new \Exception('Could not connect to LDAP'); |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + $this->fetchGroups($dbKey, $confKey); |
|
| 404 | + |
|
| 405 | + if($testMemberOf) { |
|
| 406 | + $this->configuration->hasMemberOfFilterSupport = $this->testMemberOf(); |
|
| 407 | + $this->result->markChange(); |
|
| 408 | + if(!$this->configuration->hasMemberOfFilterSupport) { |
|
| 409 | + throw new \Exception('memberOf is not supported by the server'); |
|
| 410 | + } |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + return $this->result; |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * fetches all groups from LDAP and adds them to the result object |
|
| 418 | + * |
|
| 419 | + * @param string $dbKey |
|
| 420 | + * @param string $confKey |
|
| 421 | + * @return array $groupEntries |
|
| 422 | + * @throws \Exception |
|
| 423 | + */ |
|
| 424 | + public function fetchGroups($dbKey, $confKey) { |
|
| 425 | + $obclasses = array('posixGroup', 'group', 'zimbraDistributionList', 'groupOfNames', 'groupOfUniqueNames'); |
|
| 426 | + |
|
| 427 | + $filterParts = array(); |
|
| 428 | + foreach($obclasses as $obclass) { |
|
| 429 | + $filterParts[] = 'objectclass='.$obclass; |
|
| 430 | + } |
|
| 431 | + //we filter for everything |
|
| 432 | + //- that looks like a group and |
|
| 433 | + //- has the group display name set |
|
| 434 | + $filter = $this->access->combineFilterWithOr($filterParts); |
|
| 435 | + $filter = $this->access->combineFilterWithAnd(array($filter, 'cn=*')); |
|
| 436 | + |
|
| 437 | + $groupNames = array(); |
|
| 438 | + $groupEntries = array(); |
|
| 439 | + $limit = 400; |
|
| 440 | + $offset = 0; |
|
| 441 | + do { |
|
| 442 | + // we need to request dn additionally here, otherwise memberOf |
|
| 443 | + // detection will fail later |
|
| 444 | + $result = $this->access->searchGroups($filter, array('cn', 'dn'), $limit, $offset); |
|
| 445 | + foreach($result as $item) { |
|
| 446 | + if(!isset($item['cn']) && !is_array($item['cn']) && !isset($item['cn'][0])) { |
|
| 447 | + // just in case - no issue known |
|
| 448 | + continue; |
|
| 449 | + } |
|
| 450 | + $groupNames[] = $item['cn'][0]; |
|
| 451 | + $groupEntries[] = $item; |
|
| 452 | + } |
|
| 453 | + $offset += $limit; |
|
| 454 | + } while ($this->access->hasMoreResults()); |
|
| 455 | + |
|
| 456 | + if(count($groupNames) > 0) { |
|
| 457 | + natsort($groupNames); |
|
| 458 | + $this->result->addOptions($dbKey, array_values($groupNames)); |
|
| 459 | + } else { |
|
| 460 | + throw new \Exception(self::$l->t('Could not find the desired feature')); |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + $setFeatures = $this->configuration->$confKey; |
|
| 464 | + if(is_array($setFeatures) && !empty($setFeatures)) { |
|
| 465 | + //something is already configured? pre-select it. |
|
| 466 | + $this->result->addChange($dbKey, $setFeatures); |
|
| 467 | + } |
|
| 468 | + return $groupEntries; |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + public function determineGroupMemberAssoc() { |
|
| 472 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 473 | + 'ldapPort', |
|
| 474 | + 'ldapGroupFilter', |
|
| 475 | + ))) { |
|
| 476 | + return false; |
|
| 477 | + } |
|
| 478 | + $attribute = $this->detectGroupMemberAssoc(); |
|
| 479 | + if($attribute === false) { |
|
| 480 | + return false; |
|
| 481 | + } |
|
| 482 | + $this->configuration->setConfiguration(array('ldapGroupMemberAssocAttr' => $attribute)); |
|
| 483 | + $this->result->addChange('ldap_group_member_assoc_attribute', $attribute); |
|
| 484 | + |
|
| 485 | + return $this->result; |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + /** |
|
| 489 | + * Detects the available object classes |
|
| 490 | + * @return WizardResult|false the instance's WizardResult instance |
|
| 491 | + * @throws \Exception |
|
| 492 | + */ |
|
| 493 | + public function determineGroupObjectClasses() { |
|
| 494 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 495 | + 'ldapPort', |
|
| 496 | + 'ldapBase', |
|
| 497 | + ))) { |
|
| 498 | + return false; |
|
| 499 | + } |
|
| 500 | + $cr = $this->getConnection(); |
|
| 501 | + if(!$cr) { |
|
| 502 | + throw new \Exception('Could not connect to LDAP'); |
|
| 503 | + } |
|
| 504 | + |
|
| 505 | + $obclasses = array('groupOfNames', 'groupOfUniqueNames', 'group', 'posixGroup', '*'); |
|
| 506 | + $this->determineFeature($obclasses, |
|
| 507 | + 'objectclass', |
|
| 508 | + 'ldap_groupfilter_objectclass', |
|
| 509 | + 'ldapGroupFilterObjectclass', |
|
| 510 | + false); |
|
| 511 | + |
|
| 512 | + return $this->result; |
|
| 513 | + } |
|
| 514 | + |
|
| 515 | + /** |
|
| 516 | + * detects the available object classes |
|
| 517 | + * @return WizardResult |
|
| 518 | + * @throws \Exception |
|
| 519 | + */ |
|
| 520 | + public function determineUserObjectClasses() { |
|
| 521 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 522 | + 'ldapPort', |
|
| 523 | + 'ldapBase', |
|
| 524 | + ))) { |
|
| 525 | + return false; |
|
| 526 | + } |
|
| 527 | + $cr = $this->getConnection(); |
|
| 528 | + if(!$cr) { |
|
| 529 | + throw new \Exception('Could not connect to LDAP'); |
|
| 530 | + } |
|
| 531 | + |
|
| 532 | + $obclasses = array('inetOrgPerson', 'person', 'organizationalPerson', |
|
| 533 | + 'user', 'posixAccount', '*'); |
|
| 534 | + $filter = $this->configuration->ldapUserFilter; |
|
| 535 | + //if filter is empty, it is probably the first time the wizard is called |
|
| 536 | + //then, apply suggestions. |
|
| 537 | + $this->determineFeature($obclasses, |
|
| 538 | + 'objectclass', |
|
| 539 | + 'ldap_userfilter_objectclass', |
|
| 540 | + 'ldapUserFilterObjectclass', |
|
| 541 | + empty($filter)); |
|
| 542 | + |
|
| 543 | + return $this->result; |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + /** |
|
| 547 | + * @return WizardResult|false |
|
| 548 | + * @throws \Exception |
|
| 549 | + */ |
|
| 550 | + public function getGroupFilter() { |
|
| 551 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 552 | + 'ldapPort', |
|
| 553 | + 'ldapBase', |
|
| 554 | + ))) { |
|
| 555 | + return false; |
|
| 556 | + } |
|
| 557 | + //make sure the use display name is set |
|
| 558 | + $displayName = $this->configuration->ldapGroupDisplayName; |
|
| 559 | + if ($displayName === '') { |
|
| 560 | + $d = $this->configuration->getDefaults(); |
|
| 561 | + $this->applyFind('ldap_group_display_name', |
|
| 562 | + $d['ldap_group_display_name']); |
|
| 563 | + } |
|
| 564 | + $filter = $this->composeLdapFilter(self::LFILTER_GROUP_LIST); |
|
| 565 | + |
|
| 566 | + $this->applyFind('ldap_group_filter', $filter); |
|
| 567 | + return $this->result; |
|
| 568 | + } |
|
| 569 | + |
|
| 570 | + /** |
|
| 571 | + * @return WizardResult|false |
|
| 572 | + * @throws \Exception |
|
| 573 | + */ |
|
| 574 | + public function getUserListFilter() { |
|
| 575 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 576 | + 'ldapPort', |
|
| 577 | + 'ldapBase', |
|
| 578 | + ))) { |
|
| 579 | + return false; |
|
| 580 | + } |
|
| 581 | + //make sure the use display name is set |
|
| 582 | + $displayName = $this->configuration->ldapUserDisplayName; |
|
| 583 | + if ($displayName === '') { |
|
| 584 | + $d = $this->configuration->getDefaults(); |
|
| 585 | + $this->applyFind('ldap_display_name', $d['ldap_display_name']); |
|
| 586 | + } |
|
| 587 | + $filter = $this->composeLdapFilter(self::LFILTER_USER_LIST); |
|
| 588 | + if(!$filter) { |
|
| 589 | + throw new \Exception('Cannot create filter'); |
|
| 590 | + } |
|
| 591 | + |
|
| 592 | + $this->applyFind('ldap_userlist_filter', $filter); |
|
| 593 | + return $this->result; |
|
| 594 | + } |
|
| 595 | + |
|
| 596 | + /** |
|
| 597 | + * @return bool|WizardResult |
|
| 598 | + * @throws \Exception |
|
| 599 | + */ |
|
| 600 | + public function getUserLoginFilter() { |
|
| 601 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 602 | + 'ldapPort', |
|
| 603 | + 'ldapBase', |
|
| 604 | + 'ldapUserFilter', |
|
| 605 | + ))) { |
|
| 606 | + return false; |
|
| 607 | + } |
|
| 608 | + |
|
| 609 | + $filter = $this->composeLdapFilter(self::LFILTER_LOGIN); |
|
| 610 | + if(!$filter) { |
|
| 611 | + throw new \Exception('Cannot create filter'); |
|
| 612 | + } |
|
| 613 | + |
|
| 614 | + $this->applyFind('ldap_login_filter', $filter); |
|
| 615 | + return $this->result; |
|
| 616 | + } |
|
| 617 | + |
|
| 618 | + /** |
|
| 619 | + * @return bool|WizardResult |
|
| 620 | + * @param string $loginName |
|
| 621 | + * @throws \Exception |
|
| 622 | + */ |
|
| 623 | + public function testLoginName($loginName) { |
|
| 624 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 625 | + 'ldapPort', |
|
| 626 | + 'ldapBase', |
|
| 627 | + 'ldapLoginFilter', |
|
| 628 | + ))) { |
|
| 629 | + return false; |
|
| 630 | + } |
|
| 631 | + |
|
| 632 | + $cr = $this->access->connection->getConnectionResource(); |
|
| 633 | + if(!$this->ldap->isResource($cr)) { |
|
| 634 | + throw new \Exception('connection error'); |
|
| 635 | + } |
|
| 636 | + |
|
| 637 | + if(mb_strpos($this->access->connection->ldapLoginFilter, '%uid', 0, 'UTF-8') |
|
| 638 | + === false) { |
|
| 639 | + throw new \Exception('missing placeholder'); |
|
| 640 | + } |
|
| 641 | + |
|
| 642 | + $users = $this->access->countUsersByLoginName($loginName); |
|
| 643 | + if($this->ldap->errno($cr) !== 0) { |
|
| 644 | + throw new \Exception($this->ldap->error($cr)); |
|
| 645 | + } |
|
| 646 | + $filter = str_replace('%uid', $loginName, $this->access->connection->ldapLoginFilter); |
|
| 647 | + $this->result->addChange('ldap_test_loginname', $users); |
|
| 648 | + $this->result->addChange('ldap_test_effective_filter', $filter); |
|
| 649 | + return $this->result; |
|
| 650 | + } |
|
| 651 | + |
|
| 652 | + /** |
|
| 653 | + * Tries to determine the port, requires given Host, User DN and Password |
|
| 654 | + * @return WizardResult|false WizardResult on success, false otherwise |
|
| 655 | + * @throws \Exception |
|
| 656 | + */ |
|
| 657 | + public function guessPortAndTLS() { |
|
| 658 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 659 | + ))) { |
|
| 660 | + return false; |
|
| 661 | + } |
|
| 662 | + $this->checkHost(); |
|
| 663 | + $portSettings = $this->getPortSettingsToTry(); |
|
| 664 | + |
|
| 665 | + if(!is_array($portSettings)) { |
|
| 666 | + throw new \Exception(print_r($portSettings, true)); |
|
| 667 | + } |
|
| 668 | + |
|
| 669 | + //proceed from the best configuration and return on first success |
|
| 670 | + foreach($portSettings as $setting) { |
|
| 671 | + $p = $setting['port']; |
|
| 672 | + $t = $setting['tls']; |
|
| 673 | + \OCP\Util::writeLog('user_ldap', 'Wiz: trying port '. $p . ', TLS '. $t, \OCP\Util::DEBUG); |
|
| 674 | + //connectAndBind may throw Exception, it needs to be catched by the |
|
| 675 | + //callee of this method |
|
| 676 | + |
|
| 677 | + try { |
|
| 678 | + $settingsFound = $this->connectAndBind($p, $t); |
|
| 679 | + } catch (\Exception $e) { |
|
| 680 | + // any reply other than -1 (= cannot connect) is already okay, |
|
| 681 | + // because then we found the server |
|
| 682 | + // unavailable startTLS returns -11 |
|
| 683 | + if($e->getCode() > 0) { |
|
| 684 | + $settingsFound = true; |
|
| 685 | + } else { |
|
| 686 | + throw $e; |
|
| 687 | + } |
|
| 688 | + } |
|
| 689 | + |
|
| 690 | + if ($settingsFound === true) { |
|
| 691 | + $config = array( |
|
| 692 | + 'ldapPort' => $p, |
|
| 693 | + 'ldapTLS' => (int)$t |
|
| 694 | + ); |
|
| 695 | + $this->configuration->setConfiguration($config); |
|
| 696 | + \OCP\Util::writeLog('user_ldap', 'Wiz: detected Port ' . $p, \OCP\Util::DEBUG); |
|
| 697 | + $this->result->addChange('ldap_port', $p); |
|
| 698 | + return $this->result; |
|
| 699 | + } |
|
| 700 | + } |
|
| 701 | + |
|
| 702 | + //custom port, undetected (we do not brute force) |
|
| 703 | + return false; |
|
| 704 | + } |
|
| 705 | + |
|
| 706 | + /** |
|
| 707 | + * tries to determine a base dn from User DN or LDAP Host |
|
| 708 | + * @return WizardResult|false WizardResult on success, false otherwise |
|
| 709 | + */ |
|
| 710 | + public function guessBaseDN() { |
|
| 711 | + if(!$this->checkRequirements(array('ldapHost', |
|
| 712 | + 'ldapPort', |
|
| 713 | + ))) { |
|
| 714 | + return false; |
|
| 715 | + } |
|
| 716 | + |
|
| 717 | + //check whether a DN is given in the agent name (99.9% of all cases) |
|
| 718 | + $base = null; |
|
| 719 | + $i = stripos($this->configuration->ldapAgentName, 'dc='); |
|
| 720 | + if($i !== false) { |
|
| 721 | + $base = substr($this->configuration->ldapAgentName, $i); |
|
| 722 | + if($this->testBaseDN($base)) { |
|
| 723 | + $this->applyFind('ldap_base', $base); |
|
| 724 | + return $this->result; |
|
| 725 | + } |
|
| 726 | + } |
|
| 727 | + |
|
| 728 | + //this did not help :( |
|
| 729 | + //Let's see whether we can parse the Host URL and convert the domain to |
|
| 730 | + //a base DN |
|
| 731 | + $helper = new Helper(\OC::$server->getConfig()); |
|
| 732 | + $domain = $helper->getDomainFromURL($this->configuration->ldapHost); |
|
| 733 | + if(!$domain) { |
|
| 734 | + return false; |
|
| 735 | + } |
|
| 736 | + |
|
| 737 | + $dparts = explode('.', $domain); |
|
| 738 | + while(count($dparts) > 0) { |
|
| 739 | + $base2 = 'dc=' . implode(',dc=', $dparts); |
|
| 740 | + if ($base !== $base2 && $this->testBaseDN($base2)) { |
|
| 741 | + $this->applyFind('ldap_base', $base2); |
|
| 742 | + return $this->result; |
|
| 743 | + } |
|
| 744 | + array_shift($dparts); |
|
| 745 | + } |
|
| 746 | + |
|
| 747 | + return false; |
|
| 748 | + } |
|
| 749 | + |
|
| 750 | + /** |
|
| 751 | + * sets the found value for the configuration key in the WizardResult |
|
| 752 | + * as well as in the Configuration instance |
|
| 753 | + * @param string $key the configuration key |
|
| 754 | + * @param string $value the (detected) value |
|
| 755 | + * |
|
| 756 | + */ |
|
| 757 | + private function applyFind($key, $value) { |
|
| 758 | + $this->result->addChange($key, $value); |
|
| 759 | + $this->configuration->setConfiguration(array($key => $value)); |
|
| 760 | + } |
|
| 761 | + |
|
| 762 | + /** |
|
| 763 | + * Checks, whether a port was entered in the Host configuration |
|
| 764 | + * field. In this case the port will be stripped off, but also stored as |
|
| 765 | + * setting. |
|
| 766 | + */ |
|
| 767 | + private function checkHost() { |
|
| 768 | + $host = $this->configuration->ldapHost; |
|
| 769 | + $hostInfo = parse_url($host); |
|
| 770 | + |
|
| 771 | + //removes Port from Host |
|
| 772 | + if(is_array($hostInfo) && isset($hostInfo['port'])) { |
|
| 773 | + $port = $hostInfo['port']; |
|
| 774 | + $host = str_replace(':'.$port, '', $host); |
|
| 775 | + $this->applyFind('ldap_host', $host); |
|
| 776 | + $this->applyFind('ldap_port', $port); |
|
| 777 | + } |
|
| 778 | + } |
|
| 779 | + |
|
| 780 | + /** |
|
| 781 | + * tries to detect the group member association attribute which is |
|
| 782 | + * one of 'uniqueMember', 'memberUid', 'member', 'gidNumber' |
|
| 783 | + * @return string|false, string with the attribute name, false on error |
|
| 784 | + * @throws \Exception |
|
| 785 | + */ |
|
| 786 | + private function detectGroupMemberAssoc() { |
|
| 787 | + $possibleAttrs = array('uniqueMember', 'memberUid', 'member', 'gidNumber'); |
|
| 788 | + $filter = $this->configuration->ldapGroupFilter; |
|
| 789 | + if(empty($filter)) { |
|
| 790 | + return false; |
|
| 791 | + } |
|
| 792 | + $cr = $this->getConnection(); |
|
| 793 | + if(!$cr) { |
|
| 794 | + throw new \Exception('Could not connect to LDAP'); |
|
| 795 | + } |
|
| 796 | + $base = $this->configuration->ldapBase[0]; |
|
| 797 | + $rr = $this->ldap->search($cr, $base, $filter, $possibleAttrs, 0, 1000); |
|
| 798 | + if(!$this->ldap->isResource($rr)) { |
|
| 799 | + return false; |
|
| 800 | + } |
|
| 801 | + $er = $this->ldap->firstEntry($cr, $rr); |
|
| 802 | + while(is_resource($er)) { |
|
| 803 | + $this->ldap->getDN($cr, $er); |
|
| 804 | + $attrs = $this->ldap->getAttributes($cr, $er); |
|
| 805 | + $result = array(); |
|
| 806 | + $possibleAttrsCount = count($possibleAttrs); |
|
| 807 | + for($i = 0; $i < $possibleAttrsCount; $i++) { |
|
| 808 | + if(isset($attrs[$possibleAttrs[$i]])) { |
|
| 809 | + $result[$possibleAttrs[$i]] = $attrs[$possibleAttrs[$i]]['count']; |
|
| 810 | + } |
|
| 811 | + } |
|
| 812 | + if(!empty($result)) { |
|
| 813 | + natsort($result); |
|
| 814 | + return key($result); |
|
| 815 | + } |
|
| 816 | + |
|
| 817 | + $er = $this->ldap->nextEntry($cr, $er); |
|
| 818 | + } |
|
| 819 | + |
|
| 820 | + return false; |
|
| 821 | + } |
|
| 822 | + |
|
| 823 | + /** |
|
| 824 | + * Checks whether for a given BaseDN results will be returned |
|
| 825 | + * @param string $base the BaseDN to test |
|
| 826 | + * @return bool true on success, false otherwise |
|
| 827 | + * @throws \Exception |
|
| 828 | + */ |
|
| 829 | + private function testBaseDN($base) { |
|
| 830 | + $cr = $this->getConnection(); |
|
| 831 | + if(!$cr) { |
|
| 832 | + throw new \Exception('Could not connect to LDAP'); |
|
| 833 | + } |
|
| 834 | + |
|
| 835 | + //base is there, let's validate it. If we search for anything, we should |
|
| 836 | + //get a result set > 0 on a proper base |
|
| 837 | + $rr = $this->ldap->search($cr, $base, 'objectClass=*', array('dn'), 0, 1); |
|
| 838 | + if(!$this->ldap->isResource($rr)) { |
|
| 839 | + $errorNo = $this->ldap->errno($cr); |
|
| 840 | + $errorMsg = $this->ldap->error($cr); |
|
| 841 | + \OCP\Util::writeLog('user_ldap', 'Wiz: Could not search base '.$base. |
|
| 842 | + ' Error '.$errorNo.': '.$errorMsg, \OCP\Util::INFO); |
|
| 843 | + return false; |
|
| 844 | + } |
|
| 845 | + $entries = $this->ldap->countEntries($cr, $rr); |
|
| 846 | + return ($entries !== false) && ($entries > 0); |
|
| 847 | + } |
|
| 848 | + |
|
| 849 | + /** |
|
| 850 | + * Checks whether the server supports memberOf in LDAP Filter. |
|
| 851 | + * Note: at least in OpenLDAP, availability of memberOf is dependent on |
|
| 852 | + * a configured objectClass. I.e. not necessarily for all available groups |
|
| 853 | + * memberOf does work. |
|
| 854 | + * |
|
| 855 | + * @return bool true if it does, false otherwise |
|
| 856 | + * @throws \Exception |
|
| 857 | + */ |
|
| 858 | + private function testMemberOf() { |
|
| 859 | + $cr = $this->getConnection(); |
|
| 860 | + if(!$cr) { |
|
| 861 | + throw new \Exception('Could not connect to LDAP'); |
|
| 862 | + } |
|
| 863 | + $result = $this->access->countUsers('memberOf=*', array('memberOf'), 1); |
|
| 864 | + if(is_int($result) && $result > 0) { |
|
| 865 | + return true; |
|
| 866 | + } |
|
| 867 | + return false; |
|
| 868 | + } |
|
| 869 | + |
|
| 870 | + /** |
|
| 871 | + * creates an LDAP Filter from given configuration |
|
| 872 | + * @param integer $filterType int, for which use case the filter shall be created |
|
| 873 | + * can be any of self::LFILTER_USER_LIST, self::LFILTER_LOGIN or |
|
| 874 | + * self::LFILTER_GROUP_LIST |
|
| 875 | + * @return string|false string with the filter on success, false otherwise |
|
| 876 | + * @throws \Exception |
|
| 877 | + */ |
|
| 878 | + private function composeLdapFilter($filterType) { |
|
| 879 | + $filter = ''; |
|
| 880 | + $parts = 0; |
|
| 881 | + switch ($filterType) { |
|
| 882 | + case self::LFILTER_USER_LIST: |
|
| 883 | + $objcs = $this->configuration->ldapUserFilterObjectclass; |
|
| 884 | + //glue objectclasses |
|
| 885 | + if(is_array($objcs) && count($objcs) > 0) { |
|
| 886 | + $filter .= '(|'; |
|
| 887 | + foreach($objcs as $objc) { |
|
| 888 | + $filter .= '(objectclass=' . $objc . ')'; |
|
| 889 | + } |
|
| 890 | + $filter .= ')'; |
|
| 891 | + $parts++; |
|
| 892 | + } |
|
| 893 | + //glue group memberships |
|
| 894 | + if($this->configuration->hasMemberOfFilterSupport) { |
|
| 895 | + $cns = $this->configuration->ldapUserFilterGroups; |
|
| 896 | + if(is_array($cns) && count($cns) > 0) { |
|
| 897 | + $filter .= '(|'; |
|
| 898 | + $cr = $this->getConnection(); |
|
| 899 | + if(!$cr) { |
|
| 900 | + throw new \Exception('Could not connect to LDAP'); |
|
| 901 | + } |
|
| 902 | + $base = $this->configuration->ldapBase[0]; |
|
| 903 | + foreach($cns as $cn) { |
|
| 904 | + $rr = $this->ldap->search($cr, $base, 'cn=' . $cn, array('dn', 'primaryGroupToken')); |
|
| 905 | + if(!$this->ldap->isResource($rr)) { |
|
| 906 | + continue; |
|
| 907 | + } |
|
| 908 | + $er = $this->ldap->firstEntry($cr, $rr); |
|
| 909 | + $attrs = $this->ldap->getAttributes($cr, $er); |
|
| 910 | + $dn = $this->ldap->getDN($cr, $er); |
|
| 911 | + if ($dn === false || $dn === '') { |
|
| 912 | + continue; |
|
| 913 | + } |
|
| 914 | + $filterPart = '(memberof=' . $dn . ')'; |
|
| 915 | + if(isset($attrs['primaryGroupToken'])) { |
|
| 916 | + $pgt = $attrs['primaryGroupToken'][0]; |
|
| 917 | + $primaryFilterPart = '(primaryGroupID=' . $pgt .')'; |
|
| 918 | + $filterPart = '(|' . $filterPart . $primaryFilterPart . ')'; |
|
| 919 | + } |
|
| 920 | + $filter .= $filterPart; |
|
| 921 | + } |
|
| 922 | + $filter .= ')'; |
|
| 923 | + } |
|
| 924 | + $parts++; |
|
| 925 | + } |
|
| 926 | + //wrap parts in AND condition |
|
| 927 | + if($parts > 1) { |
|
| 928 | + $filter = '(&' . $filter . ')'; |
|
| 929 | + } |
|
| 930 | + if ($filter === '') { |
|
| 931 | + $filter = '(objectclass=*)'; |
|
| 932 | + } |
|
| 933 | + break; |
|
| 934 | + |
|
| 935 | + case self::LFILTER_GROUP_LIST: |
|
| 936 | + $objcs = $this->configuration->ldapGroupFilterObjectclass; |
|
| 937 | + //glue objectclasses |
|
| 938 | + if(is_array($objcs) && count($objcs) > 0) { |
|
| 939 | + $filter .= '(|'; |
|
| 940 | + foreach($objcs as $objc) { |
|
| 941 | + $filter .= '(objectclass=' . $objc . ')'; |
|
| 942 | + } |
|
| 943 | + $filter .= ')'; |
|
| 944 | + $parts++; |
|
| 945 | + } |
|
| 946 | + //glue group memberships |
|
| 947 | + $cns = $this->configuration->ldapGroupFilterGroups; |
|
| 948 | + if(is_array($cns) && count($cns) > 0) { |
|
| 949 | + $filter .= '(|'; |
|
| 950 | + foreach($cns as $cn) { |
|
| 951 | + $filter .= '(cn=' . $cn . ')'; |
|
| 952 | + } |
|
| 953 | + $filter .= ')'; |
|
| 954 | + } |
|
| 955 | + $parts++; |
|
| 956 | + //wrap parts in AND condition |
|
| 957 | + if($parts > 1) { |
|
| 958 | + $filter = '(&' . $filter . ')'; |
|
| 959 | + } |
|
| 960 | + break; |
|
| 961 | + |
|
| 962 | + case self::LFILTER_LOGIN: |
|
| 963 | + $ulf = $this->configuration->ldapUserFilter; |
|
| 964 | + $loginpart = '=%uid'; |
|
| 965 | + $filterUsername = ''; |
|
| 966 | + $userAttributes = $this->getUserAttributes(); |
|
| 967 | + $userAttributes = array_change_key_case(array_flip($userAttributes)); |
|
| 968 | + $parts = 0; |
|
| 969 | + |
|
| 970 | + if($this->configuration->ldapLoginFilterUsername === '1') { |
|
| 971 | + $attr = ''; |
|
| 972 | + if(isset($userAttributes['uid'])) { |
|
| 973 | + $attr = 'uid'; |
|
| 974 | + } else if(isset($userAttributes['samaccountname'])) { |
|
| 975 | + $attr = 'samaccountname'; |
|
| 976 | + } else if(isset($userAttributes['cn'])) { |
|
| 977 | + //fallback |
|
| 978 | + $attr = 'cn'; |
|
| 979 | + } |
|
| 980 | + if ($attr !== '') { |
|
| 981 | + $filterUsername = '(' . $attr . $loginpart . ')'; |
|
| 982 | + $parts++; |
|
| 983 | + } |
|
| 984 | + } |
|
| 985 | + |
|
| 986 | + $filterEmail = ''; |
|
| 987 | + if($this->configuration->ldapLoginFilterEmail === '1') { |
|
| 988 | + $filterEmail = '(|(mailPrimaryAddress=%uid)(mail=%uid))'; |
|
| 989 | + $parts++; |
|
| 990 | + } |
|
| 991 | + |
|
| 992 | + $filterAttributes = ''; |
|
| 993 | + $attrsToFilter = $this->configuration->ldapLoginFilterAttributes; |
|
| 994 | + if(is_array($attrsToFilter) && count($attrsToFilter) > 0) { |
|
| 995 | + $filterAttributes = '(|'; |
|
| 996 | + foreach($attrsToFilter as $attribute) { |
|
| 997 | + $filterAttributes .= '(' . $attribute . $loginpart . ')'; |
|
| 998 | + } |
|
| 999 | + $filterAttributes .= ')'; |
|
| 1000 | + $parts++; |
|
| 1001 | + } |
|
| 1002 | + |
|
| 1003 | + $filterLogin = ''; |
|
| 1004 | + if($parts > 1) { |
|
| 1005 | + $filterLogin = '(|'; |
|
| 1006 | + } |
|
| 1007 | + $filterLogin .= $filterUsername; |
|
| 1008 | + $filterLogin .= $filterEmail; |
|
| 1009 | + $filterLogin .= $filterAttributes; |
|
| 1010 | + if($parts > 1) { |
|
| 1011 | + $filterLogin .= ')'; |
|
| 1012 | + } |
|
| 1013 | + |
|
| 1014 | + $filter = '(&'.$ulf.$filterLogin.')'; |
|
| 1015 | + break; |
|
| 1016 | + } |
|
| 1017 | + |
|
| 1018 | + \OCP\Util::writeLog('user_ldap', 'Wiz: Final filter '.$filter, \OCP\Util::DEBUG); |
|
| 1019 | + |
|
| 1020 | + return $filter; |
|
| 1021 | + } |
|
| 1022 | + |
|
| 1023 | + /** |
|
| 1024 | + * Connects and Binds to an LDAP Server |
|
| 1025 | + * |
|
| 1026 | + * @param int $port the port to connect with |
|
| 1027 | + * @param bool $tls whether startTLS is to be used |
|
| 1028 | + * @return bool |
|
| 1029 | + * @throws \Exception |
|
| 1030 | + */ |
|
| 1031 | + private function connectAndBind($port, $tls) { |
|
| 1032 | + //connect, does not really trigger any server communication |
|
| 1033 | + $host = $this->configuration->ldapHost; |
|
| 1034 | + $hostInfo = parse_url($host); |
|
| 1035 | + if(!$hostInfo) { |
|
| 1036 | + throw new \Exception(self::$l->t('Invalid Host')); |
|
| 1037 | + } |
|
| 1038 | + \OCP\Util::writeLog('user_ldap', 'Wiz: Attempting to connect ', \OCP\Util::DEBUG); |
|
| 1039 | + $cr = $this->ldap->connect($host, $port); |
|
| 1040 | + if(!is_resource($cr)) { |
|
| 1041 | + throw new \Exception(self::$l->t('Invalid Host')); |
|
| 1042 | + } |
|
| 1043 | + |
|
| 1044 | + //set LDAP options |
|
| 1045 | + $this->ldap->setOption($cr, LDAP_OPT_PROTOCOL_VERSION, 3); |
|
| 1046 | + $this->ldap->setOption($cr, LDAP_OPT_REFERRALS, 0); |
|
| 1047 | + $this->ldap->setOption($cr, LDAP_OPT_NETWORK_TIMEOUT, self::LDAP_NW_TIMEOUT); |
|
| 1048 | + |
|
| 1049 | + try { |
|
| 1050 | + if($tls) { |
|
| 1051 | + $isTlsWorking = @$this->ldap->startTls($cr); |
|
| 1052 | + if(!$isTlsWorking) { |
|
| 1053 | + return false; |
|
| 1054 | + } |
|
| 1055 | + } |
|
| 1056 | + |
|
| 1057 | + \OCP\Util::writeLog('user_ldap', 'Wiz: Attemping to Bind ', \OCP\Util::DEBUG); |
|
| 1058 | + //interesting part: do the bind! |
|
| 1059 | + $login = $this->ldap->bind($cr, |
|
| 1060 | + $this->configuration->ldapAgentName, |
|
| 1061 | + $this->configuration->ldapAgentPassword |
|
| 1062 | + ); |
|
| 1063 | + $errNo = $this->ldap->errno($cr); |
|
| 1064 | + $error = ldap_error($cr); |
|
| 1065 | + $this->ldap->unbind($cr); |
|
| 1066 | + } catch(ServerNotAvailableException $e) { |
|
| 1067 | + return false; |
|
| 1068 | + } |
|
| 1069 | + |
|
| 1070 | + if($login === true) { |
|
| 1071 | + $this->ldap->unbind($cr); |
|
| 1072 | + \OCP\Util::writeLog('user_ldap', 'Wiz: Bind successful to Port '. $port . ' TLS ' . (int)$tls, \OCP\Util::DEBUG); |
|
| 1073 | + return true; |
|
| 1074 | + } |
|
| 1075 | + |
|
| 1076 | + if($errNo === -1) { |
|
| 1077 | + //host, port or TLS wrong |
|
| 1078 | + return false; |
|
| 1079 | + } |
|
| 1080 | + throw new \Exception($error, $errNo); |
|
| 1081 | + } |
|
| 1082 | + |
|
| 1083 | + /** |
|
| 1084 | + * checks whether a valid combination of agent and password has been |
|
| 1085 | + * provided (either two values or nothing for anonymous connect) |
|
| 1086 | + * @return bool, true if everything is fine, false otherwise |
|
| 1087 | + */ |
|
| 1088 | + private function checkAgentRequirements() { |
|
| 1089 | + $agent = $this->configuration->ldapAgentName; |
|
| 1090 | + $pwd = $this->configuration->ldapAgentPassword; |
|
| 1091 | + |
|
| 1092 | + return |
|
| 1093 | + ($agent !== '' && $pwd !== '') |
|
| 1094 | + || ($agent === '' && $pwd === '') |
|
| 1095 | + ; |
|
| 1096 | + } |
|
| 1097 | + |
|
| 1098 | + /** |
|
| 1099 | + * @param array $reqs |
|
| 1100 | + * @return bool |
|
| 1101 | + */ |
|
| 1102 | + private function checkRequirements($reqs) { |
|
| 1103 | + $this->checkAgentRequirements(); |
|
| 1104 | + foreach($reqs as $option) { |
|
| 1105 | + $value = $this->configuration->$option; |
|
| 1106 | + if(empty($value)) { |
|
| 1107 | + return false; |
|
| 1108 | + } |
|
| 1109 | + } |
|
| 1110 | + return true; |
|
| 1111 | + } |
|
| 1112 | + |
|
| 1113 | + /** |
|
| 1114 | + * does a cumulativeSearch on LDAP to get different values of a |
|
| 1115 | + * specified attribute |
|
| 1116 | + * @param string[] $filters array, the filters that shall be used in the search |
|
| 1117 | + * @param string $attr the attribute of which a list of values shall be returned |
|
| 1118 | + * @param int $dnReadLimit the amount of how many DNs should be analyzed. |
|
| 1119 | + * The lower, the faster |
|
| 1120 | + * @param string $maxF string. if not null, this variable will have the filter that |
|
| 1121 | + * yields most result entries |
|
| 1122 | + * @return array|false an array with the values on success, false otherwise |
|
| 1123 | + */ |
|
| 1124 | + public function cumulativeSearchOnAttribute($filters, $attr, $dnReadLimit = 3, &$maxF = null) { |
|
| 1125 | + $dnRead = array(); |
|
| 1126 | + $foundItems = array(); |
|
| 1127 | + $maxEntries = 0; |
|
| 1128 | + if(!is_array($this->configuration->ldapBase) |
|
| 1129 | + || !isset($this->configuration->ldapBase[0])) { |
|
| 1130 | + return false; |
|
| 1131 | + } |
|
| 1132 | + $base = $this->configuration->ldapBase[0]; |
|
| 1133 | + $cr = $this->getConnection(); |
|
| 1134 | + if(!$this->ldap->isResource($cr)) { |
|
| 1135 | + return false; |
|
| 1136 | + } |
|
| 1137 | + $lastFilter = null; |
|
| 1138 | + if(isset($filters[count($filters)-1])) { |
|
| 1139 | + $lastFilter = $filters[count($filters)-1]; |
|
| 1140 | + } |
|
| 1141 | + foreach($filters as $filter) { |
|
| 1142 | + if($lastFilter === $filter && count($foundItems) > 0) { |
|
| 1143 | + //skip when the filter is a wildcard and results were found |
|
| 1144 | + continue; |
|
| 1145 | + } |
|
| 1146 | + // 20k limit for performance and reason |
|
| 1147 | + $rr = $this->ldap->search($cr, $base, $filter, array($attr), 0, 20000); |
|
| 1148 | + if(!$this->ldap->isResource($rr)) { |
|
| 1149 | + continue; |
|
| 1150 | + } |
|
| 1151 | + $entries = $this->ldap->countEntries($cr, $rr); |
|
| 1152 | + $getEntryFunc = 'firstEntry'; |
|
| 1153 | + if(($entries !== false) && ($entries > 0)) { |
|
| 1154 | + if(!is_null($maxF) && $entries > $maxEntries) { |
|
| 1155 | + $maxEntries = $entries; |
|
| 1156 | + $maxF = $filter; |
|
| 1157 | + } |
|
| 1158 | + $dnReadCount = 0; |
|
| 1159 | + do { |
|
| 1160 | + $entry = $this->ldap->$getEntryFunc($cr, $rr); |
|
| 1161 | + $getEntryFunc = 'nextEntry'; |
|
| 1162 | + if(!$this->ldap->isResource($entry)) { |
|
| 1163 | + continue 2; |
|
| 1164 | + } |
|
| 1165 | + $rr = $entry; //will be expected by nextEntry next round |
|
| 1166 | + $attributes = $this->ldap->getAttributes($cr, $entry); |
|
| 1167 | + $dn = $this->ldap->getDN($cr, $entry); |
|
| 1168 | + if($dn === false || in_array($dn, $dnRead)) { |
|
| 1169 | + continue; |
|
| 1170 | + } |
|
| 1171 | + $newItems = array(); |
|
| 1172 | + $state = $this->getAttributeValuesFromEntry($attributes, |
|
| 1173 | + $attr, |
|
| 1174 | + $newItems); |
|
| 1175 | + $dnReadCount++; |
|
| 1176 | + $foundItems = array_merge($foundItems, $newItems); |
|
| 1177 | + $this->resultCache[$dn][$attr] = $newItems; |
|
| 1178 | + $dnRead[] = $dn; |
|
| 1179 | + } while(($state === self::LRESULT_PROCESSED_SKIP |
|
| 1180 | + || $this->ldap->isResource($entry)) |
|
| 1181 | + && ($dnReadLimit === 0 || $dnReadCount < $dnReadLimit)); |
|
| 1182 | + } |
|
| 1183 | + } |
|
| 1184 | + |
|
| 1185 | + return array_unique($foundItems); |
|
| 1186 | + } |
|
| 1187 | + |
|
| 1188 | + /** |
|
| 1189 | + * determines if and which $attr are available on the LDAP server |
|
| 1190 | + * @param string[] $objectclasses the objectclasses to use as search filter |
|
| 1191 | + * @param string $attr the attribute to look for |
|
| 1192 | + * @param string $dbkey the dbkey of the setting the feature is connected to |
|
| 1193 | + * @param string $confkey the confkey counterpart for the $dbkey as used in the |
|
| 1194 | + * Configuration class |
|
| 1195 | + * @param bool $po whether the objectClass with most result entries |
|
| 1196 | + * shall be pre-selected via the result |
|
| 1197 | + * @return array|false list of found items. |
|
| 1198 | + * @throws \Exception |
|
| 1199 | + */ |
|
| 1200 | + private function determineFeature($objectclasses, $attr, $dbkey, $confkey, $po = false) { |
|
| 1201 | + $cr = $this->getConnection(); |
|
| 1202 | + if(!$cr) { |
|
| 1203 | + throw new \Exception('Could not connect to LDAP'); |
|
| 1204 | + } |
|
| 1205 | + $p = 'objectclass='; |
|
| 1206 | + foreach($objectclasses as $key => $value) { |
|
| 1207 | + $objectclasses[$key] = $p.$value; |
|
| 1208 | + } |
|
| 1209 | + $maxEntryObjC = ''; |
|
| 1210 | + |
|
| 1211 | + //how deep to dig? |
|
| 1212 | + //When looking for objectclasses, testing few entries is sufficient, |
|
| 1213 | + $dig = 3; |
|
| 1214 | + |
|
| 1215 | + $availableFeatures = |
|
| 1216 | + $this->cumulativeSearchOnAttribute($objectclasses, $attr, |
|
| 1217 | + $dig, $maxEntryObjC); |
|
| 1218 | + if(is_array($availableFeatures) |
|
| 1219 | + && count($availableFeatures) > 0) { |
|
| 1220 | + natcasesort($availableFeatures); |
|
| 1221 | + //natcasesort keeps indices, but we must get rid of them for proper |
|
| 1222 | + //sorting in the web UI. Therefore: array_values |
|
| 1223 | + $this->result->addOptions($dbkey, array_values($availableFeatures)); |
|
| 1224 | + } else { |
|
| 1225 | + throw new \Exception(self::$l->t('Could not find the desired feature')); |
|
| 1226 | + } |
|
| 1227 | + |
|
| 1228 | + $setFeatures = $this->configuration->$confkey; |
|
| 1229 | + if(is_array($setFeatures) && !empty($setFeatures)) { |
|
| 1230 | + //something is already configured? pre-select it. |
|
| 1231 | + $this->result->addChange($dbkey, $setFeatures); |
|
| 1232 | + } else if ($po && $maxEntryObjC !== '') { |
|
| 1233 | + //pre-select objectclass with most result entries |
|
| 1234 | + $maxEntryObjC = str_replace($p, '', $maxEntryObjC); |
|
| 1235 | + $this->applyFind($dbkey, $maxEntryObjC); |
|
| 1236 | + $this->result->addChange($dbkey, $maxEntryObjC); |
|
| 1237 | + } |
|
| 1238 | + |
|
| 1239 | + return $availableFeatures; |
|
| 1240 | + } |
|
| 1241 | + |
|
| 1242 | + /** |
|
| 1243 | + * appends a list of values fr |
|
| 1244 | + * @param resource $result the return value from ldap_get_attributes |
|
| 1245 | + * @param string $attribute the attribute values to look for |
|
| 1246 | + * @param array &$known new values will be appended here |
|
| 1247 | + * @return int, state on of the class constants LRESULT_PROCESSED_OK, |
|
| 1248 | + * LRESULT_PROCESSED_INVALID or LRESULT_PROCESSED_SKIP |
|
| 1249 | + */ |
|
| 1250 | + private function getAttributeValuesFromEntry($result, $attribute, &$known) { |
|
| 1251 | + if(!is_array($result) |
|
| 1252 | + || !isset($result['count']) |
|
| 1253 | + || !$result['count'] > 0) { |
|
| 1254 | + return self::LRESULT_PROCESSED_INVALID; |
|
| 1255 | + } |
|
| 1256 | + |
|
| 1257 | + // strtolower on all keys for proper comparison |
|
| 1258 | + $result = \OCP\Util::mb_array_change_key_case($result); |
|
| 1259 | + $attribute = strtolower($attribute); |
|
| 1260 | + if(isset($result[$attribute])) { |
|
| 1261 | + foreach($result[$attribute] as $key => $val) { |
|
| 1262 | + if($key === 'count') { |
|
| 1263 | + continue; |
|
| 1264 | + } |
|
| 1265 | + if(!in_array($val, $known)) { |
|
| 1266 | + $known[] = $val; |
|
| 1267 | + } |
|
| 1268 | + } |
|
| 1269 | + return self::LRESULT_PROCESSED_OK; |
|
| 1270 | + } else { |
|
| 1271 | + return self::LRESULT_PROCESSED_SKIP; |
|
| 1272 | + } |
|
| 1273 | + } |
|
| 1274 | + |
|
| 1275 | + /** |
|
| 1276 | + * @return bool|mixed |
|
| 1277 | + */ |
|
| 1278 | + private function getConnection() { |
|
| 1279 | + if(!is_null($this->cr)) { |
|
| 1280 | + return $this->cr; |
|
| 1281 | + } |
|
| 1282 | + |
|
| 1283 | + $cr = $this->ldap->connect( |
|
| 1284 | + $this->configuration->ldapHost, |
|
| 1285 | + $this->configuration->ldapPort |
|
| 1286 | + ); |
|
| 1287 | + |
|
| 1288 | + $this->ldap->setOption($cr, LDAP_OPT_PROTOCOL_VERSION, 3); |
|
| 1289 | + $this->ldap->setOption($cr, LDAP_OPT_REFERRALS, 0); |
|
| 1290 | + $this->ldap->setOption($cr, LDAP_OPT_NETWORK_TIMEOUT, self::LDAP_NW_TIMEOUT); |
|
| 1291 | + if($this->configuration->ldapTLS === 1) { |
|
| 1292 | + $this->ldap->startTls($cr); |
|
| 1293 | + } |
|
| 1294 | + |
|
| 1295 | + $lo = @$this->ldap->bind($cr, |
|
| 1296 | + $this->configuration->ldapAgentName, |
|
| 1297 | + $this->configuration->ldapAgentPassword); |
|
| 1298 | + if($lo === true) { |
|
| 1299 | + $this->$cr = $cr; |
|
| 1300 | + return $cr; |
|
| 1301 | + } |
|
| 1302 | + |
|
| 1303 | + return false; |
|
| 1304 | + } |
|
| 1305 | + |
|
| 1306 | + /** |
|
| 1307 | + * @return array |
|
| 1308 | + */ |
|
| 1309 | + private function getDefaultLdapPortSettings() { |
|
| 1310 | + static $settings = array( |
|
| 1311 | + array('port' => 7636, 'tls' => false), |
|
| 1312 | + array('port' => 636, 'tls' => false), |
|
| 1313 | + array('port' => 7389, 'tls' => true), |
|
| 1314 | + array('port' => 389, 'tls' => true), |
|
| 1315 | + array('port' => 7389, 'tls' => false), |
|
| 1316 | + array('port' => 389, 'tls' => false), |
|
| 1317 | + ); |
|
| 1318 | + return $settings; |
|
| 1319 | + } |
|
| 1320 | + |
|
| 1321 | + /** |
|
| 1322 | + * @return array |
|
| 1323 | + */ |
|
| 1324 | + private function getPortSettingsToTry() { |
|
| 1325 | + //389 ← LDAP / Unencrypted or StartTLS |
|
| 1326 | + //636 ← LDAPS / SSL |
|
| 1327 | + //7xxx ← UCS. need to be checked first, because both ports may be open |
|
| 1328 | + $host = $this->configuration->ldapHost; |
|
| 1329 | + $port = (int)$this->configuration->ldapPort; |
|
| 1330 | + $portSettings = array(); |
|
| 1331 | + |
|
| 1332 | + //In case the port is already provided, we will check this first |
|
| 1333 | + if($port > 0) { |
|
| 1334 | + $hostInfo = parse_url($host); |
|
| 1335 | + if(!(is_array($hostInfo) |
|
| 1336 | + && isset($hostInfo['scheme']) |
|
| 1337 | + && stripos($hostInfo['scheme'], 'ldaps') !== false)) { |
|
| 1338 | + $portSettings[] = array('port' => $port, 'tls' => true); |
|
| 1339 | + } |
|
| 1340 | + $portSettings[] =array('port' => $port, 'tls' => false); |
|
| 1341 | + } |
|
| 1342 | + |
|
| 1343 | + //default ports |
|
| 1344 | + $portSettings = array_merge($portSettings, |
|
| 1345 | + $this->getDefaultLdapPortSettings()); |
|
| 1346 | + |
|
| 1347 | + return $portSettings; |
|
| 1348 | + } |
|
| 1349 | 1349 | |
| 1350 | 1350 | |
| 1351 | 1351 | } |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | public function __construct(Configuration $configuration, ILDAPWrapper $ldap, Access $access) { |
| 72 | 72 | parent::__construct($ldap); |
| 73 | 73 | $this->configuration = $configuration; |
| 74 | - if(is_null(Wizard::$l)) { |
|
| 74 | + if (is_null(Wizard::$l)) { |
|
| 75 | 75 | Wizard::$l = \OC::$server->getL10N('user_ldap'); |
| 76 | 76 | } |
| 77 | 77 | $this->access = $access; |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | public function __destruct() { |
| 82 | - if($this->result->hasChanges()) { |
|
| 82 | + if ($this->result->hasChanges()) { |
|
| 83 | 83 | $this->configuration->saveConfiguration(); |
| 84 | 84 | } |
| 85 | 85 | } |
@@ -94,18 +94,18 @@ discard block |
||
| 94 | 94 | */ |
| 95 | 95 | public function countEntries($filter, $type) { |
| 96 | 96 | $reqs = array('ldapHost', 'ldapPort', 'ldapBase'); |
| 97 | - if($type === 'users') { |
|
| 97 | + if ($type === 'users') { |
|
| 98 | 98 | $reqs[] = 'ldapUserFilter'; |
| 99 | 99 | } |
| 100 | - if(!$this->checkRequirements($reqs)) { |
|
| 100 | + if (!$this->checkRequirements($reqs)) { |
|
| 101 | 101 | throw new \Exception('Requirements not met', 400); |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | $attr = array('dn'); // default |
| 105 | 105 | $limit = 1001; |
| 106 | - if($type === 'groups') { |
|
| 107 | - $result = $this->access->countGroups($filter, $attr, $limit); |
|
| 108 | - } else if($type === 'users') { |
|
| 106 | + if ($type === 'groups') { |
|
| 107 | + $result = $this->access->countGroups($filter, $attr, $limit); |
|
| 108 | + } else if ($type === 'users') { |
|
| 109 | 109 | $result = $this->access->countUsers($filter, $attr, $limit); |
| 110 | 110 | } else if ($type === 'objects') { |
| 111 | 111 | $result = $this->access->countObjects($limit); |
@@ -125,7 +125,7 @@ discard block |
||
| 125 | 125 | */ |
| 126 | 126 | private function formatCountResult($count) { |
| 127 | 127 | $formatted = ($count !== false) ? $count : 0; |
| 128 | - if($formatted > 1000) { |
|
| 128 | + if ($formatted > 1000) { |
|
| 129 | 129 | $formatted = '> 1000'; |
| 130 | 130 | } |
| 131 | 131 | return $formatted; |
@@ -134,7 +134,7 @@ discard block |
||
| 134 | 134 | public function countGroups() { |
| 135 | 135 | $filter = $this->configuration->ldapGroupFilter; |
| 136 | 136 | |
| 137 | - if(empty($filter)) { |
|
| 137 | + if (empty($filter)) { |
|
| 138 | 138 | $output = self::$l->n('%s group found', '%s groups found', 0, array(0)); |
| 139 | 139 | $this->result->addChange('ldap_group_count', $output); |
| 140 | 140 | return $this->result; |
@@ -144,7 +144,7 @@ discard block |
||
| 144 | 144 | $groupsTotal = $this->formatCountResult($this->countEntries($filter, 'groups')); |
| 145 | 145 | } catch (\Exception $e) { |
| 146 | 146 | //400 can be ignored, 500 is forwarded |
| 147 | - if($e->getCode() === 500) { |
|
| 147 | + if ($e->getCode() === 500) { |
|
| 148 | 148 | throw $e; |
| 149 | 149 | } |
| 150 | 150 | return false; |
@@ -176,7 +176,7 @@ discard block |
||
| 176 | 176 | public function countInBaseDN() { |
| 177 | 177 | // we don't need to provide a filter in this case |
| 178 | 178 | $total = $this->countEntries(null, 'objects'); |
| 179 | - if($total === false) { |
|
| 179 | + if ($total === false) { |
|
| 180 | 180 | throw new \Exception('invalid results received'); |
| 181 | 181 | } |
| 182 | 182 | $this->result->addChange('ldap_test_base', $total); |
@@ -190,7 +190,7 @@ discard block |
||
| 190 | 190 | * @return int|bool |
| 191 | 191 | */ |
| 192 | 192 | public function countUsersWithAttribute($attr, $existsCheck = false) { |
| 193 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 193 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 194 | 194 | 'ldapPort', |
| 195 | 195 | 'ldapBase', |
| 196 | 196 | 'ldapUserFilter', |
@@ -200,7 +200,7 @@ discard block |
||
| 200 | 200 | |
| 201 | 201 | $filter = $this->access->combineFilterWithAnd(array( |
| 202 | 202 | $this->configuration->ldapUserFilter, |
| 203 | - $attr . '=*' |
|
| 203 | + $attr.'=*' |
|
| 204 | 204 | )); |
| 205 | 205 | |
| 206 | 206 | $limit = ($existsCheck === false) ? null : 1; |
@@ -215,7 +215,7 @@ discard block |
||
| 215 | 215 | * @throws \Exception |
| 216 | 216 | */ |
| 217 | 217 | public function detectUserDisplayNameAttribute() { |
| 218 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 218 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 219 | 219 | 'ldapPort', |
| 220 | 220 | 'ldapBase', |
| 221 | 221 | 'ldapUserFilter', |
@@ -227,8 +227,8 @@ discard block |
||
| 227 | 227 | if ($attr !== '' && $attr !== 'displayName') { |
| 228 | 228 | // most likely not the default value with upper case N, |
| 229 | 229 | // verify it still produces a result |
| 230 | - $count = (int)$this->countUsersWithAttribute($attr, true); |
|
| 231 | - if($count > 0) { |
|
| 230 | + $count = (int) $this->countUsersWithAttribute($attr, true); |
|
| 231 | + if ($count > 0) { |
|
| 232 | 232 | //no change, but we sent it back to make sure the user interface |
| 233 | 233 | //is still correct, even if the ajax call was cancelled meanwhile |
| 234 | 234 | $this->result->addChange('ldap_display_name', $attr); |
@@ -239,9 +239,9 @@ discard block |
||
| 239 | 239 | // first attribute that has at least one result wins |
| 240 | 240 | $displayNameAttrs = array('displayname', 'cn'); |
| 241 | 241 | foreach ($displayNameAttrs as $attr) { |
| 242 | - $count = (int)$this->countUsersWithAttribute($attr, true); |
|
| 242 | + $count = (int) $this->countUsersWithAttribute($attr, true); |
|
| 243 | 243 | |
| 244 | - if($count > 0) { |
|
| 244 | + if ($count > 0) { |
|
| 245 | 245 | $this->applyFind('ldap_display_name', $attr); |
| 246 | 246 | return $this->result; |
| 247 | 247 | } |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | * @return WizardResult|bool |
| 258 | 258 | */ |
| 259 | 259 | public function detectEmailAttribute() { |
| 260 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 260 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 261 | 261 | 'ldapPort', |
| 262 | 262 | 'ldapBase', |
| 263 | 263 | 'ldapUserFilter', |
@@ -267,8 +267,8 @@ discard block |
||
| 267 | 267 | |
| 268 | 268 | $attr = $this->configuration->ldapEmailAttribute; |
| 269 | 269 | if ($attr !== '') { |
| 270 | - $count = (int)$this->countUsersWithAttribute($attr, true); |
|
| 271 | - if($count > 0) { |
|
| 270 | + $count = (int) $this->countUsersWithAttribute($attr, true); |
|
| 271 | + if ($count > 0) { |
|
| 272 | 272 | return false; |
| 273 | 273 | } |
| 274 | 274 | $writeLog = true; |
@@ -279,19 +279,19 @@ discard block |
||
| 279 | 279 | $emailAttributes = array('mail', 'mailPrimaryAddress'); |
| 280 | 280 | $winner = ''; |
| 281 | 281 | $maxUsers = 0; |
| 282 | - foreach($emailAttributes as $attr) { |
|
| 282 | + foreach ($emailAttributes as $attr) { |
|
| 283 | 283 | $count = $this->countUsersWithAttribute($attr); |
| 284 | - if($count > $maxUsers) { |
|
| 284 | + if ($count > $maxUsers) { |
|
| 285 | 285 | $maxUsers = $count; |
| 286 | 286 | $winner = $attr; |
| 287 | 287 | } |
| 288 | 288 | } |
| 289 | 289 | |
| 290 | - if($winner !== '') { |
|
| 290 | + if ($winner !== '') { |
|
| 291 | 291 | $this->applyFind('ldap_email_attr', $winner); |
| 292 | - if($writeLog) { |
|
| 293 | - \OCP\Util::writeLog('user_ldap', 'The mail attribute has ' . |
|
| 294 | - 'automatically been reset, because the original value ' . |
|
| 292 | + if ($writeLog) { |
|
| 293 | + \OCP\Util::writeLog('user_ldap', 'The mail attribute has '. |
|
| 294 | + 'automatically been reset, because the original value '. |
|
| 295 | 295 | 'did not return any results.', \OCP\Util::INFO); |
| 296 | 296 | } |
| 297 | 297 | } |
@@ -304,7 +304,7 @@ discard block |
||
| 304 | 304 | * @throws \Exception |
| 305 | 305 | */ |
| 306 | 306 | public function determineAttributes() { |
| 307 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 307 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 308 | 308 | 'ldapPort', |
| 309 | 309 | 'ldapBase', |
| 310 | 310 | 'ldapUserFilter', |
@@ -320,7 +320,7 @@ discard block |
||
| 320 | 320 | $this->result->addOptions('ldap_loginfilter_attributes', $attributes); |
| 321 | 321 | |
| 322 | 322 | $selected = $this->configuration->ldapLoginFilterAttributes; |
| 323 | - if(is_array($selected) && !empty($selected)) { |
|
| 323 | + if (is_array($selected) && !empty($selected)) { |
|
| 324 | 324 | $this->result->addChange('ldap_loginfilter_attributes', $selected); |
| 325 | 325 | } |
| 326 | 326 | |
@@ -333,7 +333,7 @@ discard block |
||
| 333 | 333 | * @throws \Exception |
| 334 | 334 | */ |
| 335 | 335 | private function getUserAttributes() { |
| 336 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 336 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 337 | 337 | 'ldapPort', |
| 338 | 338 | 'ldapBase', |
| 339 | 339 | 'ldapUserFilter', |
@@ -341,20 +341,20 @@ discard block |
||
| 341 | 341 | return false; |
| 342 | 342 | } |
| 343 | 343 | $cr = $this->getConnection(); |
| 344 | - if(!$cr) { |
|
| 344 | + if (!$cr) { |
|
| 345 | 345 | throw new \Exception('Could not connect to LDAP'); |
| 346 | 346 | } |
| 347 | 347 | |
| 348 | 348 | $base = $this->configuration->ldapBase[0]; |
| 349 | 349 | $filter = $this->configuration->ldapUserFilter; |
| 350 | 350 | $rr = $this->ldap->search($cr, $base, $filter, array(), 1, 1); |
| 351 | - if(!$this->ldap->isResource($rr)) { |
|
| 351 | + if (!$this->ldap->isResource($rr)) { |
|
| 352 | 352 | return false; |
| 353 | 353 | } |
| 354 | 354 | $er = $this->ldap->firstEntry($cr, $rr); |
| 355 | 355 | $attributes = $this->ldap->getAttributes($cr, $er); |
| 356 | 356 | $pureAttributes = array(); |
| 357 | - for($i = 0; $i < $attributes['count']; $i++) { |
|
| 357 | + for ($i = 0; $i < $attributes['count']; $i++) { |
|
| 358 | 358 | $pureAttributes[] = $attributes[$i]; |
| 359 | 359 | } |
| 360 | 360 | |
@@ -389,23 +389,23 @@ discard block |
||
| 389 | 389 | * @throws \Exception |
| 390 | 390 | */ |
| 391 | 391 | private function determineGroups($dbKey, $confKey, $testMemberOf = true) { |
| 392 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 392 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 393 | 393 | 'ldapPort', |
| 394 | 394 | 'ldapBase', |
| 395 | 395 | ))) { |
| 396 | 396 | return false; |
| 397 | 397 | } |
| 398 | 398 | $cr = $this->getConnection(); |
| 399 | - if(!$cr) { |
|
| 399 | + if (!$cr) { |
|
| 400 | 400 | throw new \Exception('Could not connect to LDAP'); |
| 401 | 401 | } |
| 402 | 402 | |
| 403 | 403 | $this->fetchGroups($dbKey, $confKey); |
| 404 | 404 | |
| 405 | - if($testMemberOf) { |
|
| 405 | + if ($testMemberOf) { |
|
| 406 | 406 | $this->configuration->hasMemberOfFilterSupport = $this->testMemberOf(); |
| 407 | 407 | $this->result->markChange(); |
| 408 | - if(!$this->configuration->hasMemberOfFilterSupport) { |
|
| 408 | + if (!$this->configuration->hasMemberOfFilterSupport) { |
|
| 409 | 409 | throw new \Exception('memberOf is not supported by the server'); |
| 410 | 410 | } |
| 411 | 411 | } |
@@ -425,7 +425,7 @@ discard block |
||
| 425 | 425 | $obclasses = array('posixGroup', 'group', 'zimbraDistributionList', 'groupOfNames', 'groupOfUniqueNames'); |
| 426 | 426 | |
| 427 | 427 | $filterParts = array(); |
| 428 | - foreach($obclasses as $obclass) { |
|
| 428 | + foreach ($obclasses as $obclass) { |
|
| 429 | 429 | $filterParts[] = 'objectclass='.$obclass; |
| 430 | 430 | } |
| 431 | 431 | //we filter for everything |
@@ -442,8 +442,8 @@ discard block |
||
| 442 | 442 | // we need to request dn additionally here, otherwise memberOf |
| 443 | 443 | // detection will fail later |
| 444 | 444 | $result = $this->access->searchGroups($filter, array('cn', 'dn'), $limit, $offset); |
| 445 | - foreach($result as $item) { |
|
| 446 | - if(!isset($item['cn']) && !is_array($item['cn']) && !isset($item['cn'][0])) { |
|
| 445 | + foreach ($result as $item) { |
|
| 446 | + if (!isset($item['cn']) && !is_array($item['cn']) && !isset($item['cn'][0])) { |
|
| 447 | 447 | // just in case - no issue known |
| 448 | 448 | continue; |
| 449 | 449 | } |
@@ -453,7 +453,7 @@ discard block |
||
| 453 | 453 | $offset += $limit; |
| 454 | 454 | } while ($this->access->hasMoreResults()); |
| 455 | 455 | |
| 456 | - if(count($groupNames) > 0) { |
|
| 456 | + if (count($groupNames) > 0) { |
|
| 457 | 457 | natsort($groupNames); |
| 458 | 458 | $this->result->addOptions($dbKey, array_values($groupNames)); |
| 459 | 459 | } else { |
@@ -461,7 +461,7 @@ discard block |
||
| 461 | 461 | } |
| 462 | 462 | |
| 463 | 463 | $setFeatures = $this->configuration->$confKey; |
| 464 | - if(is_array($setFeatures) && !empty($setFeatures)) { |
|
| 464 | + if (is_array($setFeatures) && !empty($setFeatures)) { |
|
| 465 | 465 | //something is already configured? pre-select it. |
| 466 | 466 | $this->result->addChange($dbKey, $setFeatures); |
| 467 | 467 | } |
@@ -469,14 +469,14 @@ discard block |
||
| 469 | 469 | } |
| 470 | 470 | |
| 471 | 471 | public function determineGroupMemberAssoc() { |
| 472 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 472 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 473 | 473 | 'ldapPort', |
| 474 | 474 | 'ldapGroupFilter', |
| 475 | 475 | ))) { |
| 476 | 476 | return false; |
| 477 | 477 | } |
| 478 | 478 | $attribute = $this->detectGroupMemberAssoc(); |
| 479 | - if($attribute === false) { |
|
| 479 | + if ($attribute === false) { |
|
| 480 | 480 | return false; |
| 481 | 481 | } |
| 482 | 482 | $this->configuration->setConfiguration(array('ldapGroupMemberAssocAttr' => $attribute)); |
@@ -491,14 +491,14 @@ discard block |
||
| 491 | 491 | * @throws \Exception |
| 492 | 492 | */ |
| 493 | 493 | public function determineGroupObjectClasses() { |
| 494 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 494 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 495 | 495 | 'ldapPort', |
| 496 | 496 | 'ldapBase', |
| 497 | 497 | ))) { |
| 498 | 498 | return false; |
| 499 | 499 | } |
| 500 | 500 | $cr = $this->getConnection(); |
| 501 | - if(!$cr) { |
|
| 501 | + if (!$cr) { |
|
| 502 | 502 | throw new \Exception('Could not connect to LDAP'); |
| 503 | 503 | } |
| 504 | 504 | |
@@ -518,14 +518,14 @@ discard block |
||
| 518 | 518 | * @throws \Exception |
| 519 | 519 | */ |
| 520 | 520 | public function determineUserObjectClasses() { |
| 521 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 521 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 522 | 522 | 'ldapPort', |
| 523 | 523 | 'ldapBase', |
| 524 | 524 | ))) { |
| 525 | 525 | return false; |
| 526 | 526 | } |
| 527 | 527 | $cr = $this->getConnection(); |
| 528 | - if(!$cr) { |
|
| 528 | + if (!$cr) { |
|
| 529 | 529 | throw new \Exception('Could not connect to LDAP'); |
| 530 | 530 | } |
| 531 | 531 | |
@@ -548,7 +548,7 @@ discard block |
||
| 548 | 548 | * @throws \Exception |
| 549 | 549 | */ |
| 550 | 550 | public function getGroupFilter() { |
| 551 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 551 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 552 | 552 | 'ldapPort', |
| 553 | 553 | 'ldapBase', |
| 554 | 554 | ))) { |
@@ -572,7 +572,7 @@ discard block |
||
| 572 | 572 | * @throws \Exception |
| 573 | 573 | */ |
| 574 | 574 | public function getUserListFilter() { |
| 575 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 575 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 576 | 576 | 'ldapPort', |
| 577 | 577 | 'ldapBase', |
| 578 | 578 | ))) { |
@@ -585,7 +585,7 @@ discard block |
||
| 585 | 585 | $this->applyFind('ldap_display_name', $d['ldap_display_name']); |
| 586 | 586 | } |
| 587 | 587 | $filter = $this->composeLdapFilter(self::LFILTER_USER_LIST); |
| 588 | - if(!$filter) { |
|
| 588 | + if (!$filter) { |
|
| 589 | 589 | throw new \Exception('Cannot create filter'); |
| 590 | 590 | } |
| 591 | 591 | |
@@ -598,7 +598,7 @@ discard block |
||
| 598 | 598 | * @throws \Exception |
| 599 | 599 | */ |
| 600 | 600 | public function getUserLoginFilter() { |
| 601 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 601 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 602 | 602 | 'ldapPort', |
| 603 | 603 | 'ldapBase', |
| 604 | 604 | 'ldapUserFilter', |
@@ -607,7 +607,7 @@ discard block |
||
| 607 | 607 | } |
| 608 | 608 | |
| 609 | 609 | $filter = $this->composeLdapFilter(self::LFILTER_LOGIN); |
| 610 | - if(!$filter) { |
|
| 610 | + if (!$filter) { |
|
| 611 | 611 | throw new \Exception('Cannot create filter'); |
| 612 | 612 | } |
| 613 | 613 | |
@@ -621,7 +621,7 @@ discard block |
||
| 621 | 621 | * @throws \Exception |
| 622 | 622 | */ |
| 623 | 623 | public function testLoginName($loginName) { |
| 624 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 624 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 625 | 625 | 'ldapPort', |
| 626 | 626 | 'ldapBase', |
| 627 | 627 | 'ldapLoginFilter', |
@@ -630,17 +630,17 @@ discard block |
||
| 630 | 630 | } |
| 631 | 631 | |
| 632 | 632 | $cr = $this->access->connection->getConnectionResource(); |
| 633 | - if(!$this->ldap->isResource($cr)) { |
|
| 633 | + if (!$this->ldap->isResource($cr)) { |
|
| 634 | 634 | throw new \Exception('connection error'); |
| 635 | 635 | } |
| 636 | 636 | |
| 637 | - if(mb_strpos($this->access->connection->ldapLoginFilter, '%uid', 0, 'UTF-8') |
|
| 637 | + if (mb_strpos($this->access->connection->ldapLoginFilter, '%uid', 0, 'UTF-8') |
|
| 638 | 638 | === false) { |
| 639 | 639 | throw new \Exception('missing placeholder'); |
| 640 | 640 | } |
| 641 | 641 | |
| 642 | 642 | $users = $this->access->countUsersByLoginName($loginName); |
| 643 | - if($this->ldap->errno($cr) !== 0) { |
|
| 643 | + if ($this->ldap->errno($cr) !== 0) { |
|
| 644 | 644 | throw new \Exception($this->ldap->error($cr)); |
| 645 | 645 | } |
| 646 | 646 | $filter = str_replace('%uid', $loginName, $this->access->connection->ldapLoginFilter); |
@@ -655,22 +655,22 @@ discard block |
||
| 655 | 655 | * @throws \Exception |
| 656 | 656 | */ |
| 657 | 657 | public function guessPortAndTLS() { |
| 658 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 658 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 659 | 659 | ))) { |
| 660 | 660 | return false; |
| 661 | 661 | } |
| 662 | 662 | $this->checkHost(); |
| 663 | 663 | $portSettings = $this->getPortSettingsToTry(); |
| 664 | 664 | |
| 665 | - if(!is_array($portSettings)) { |
|
| 665 | + if (!is_array($portSettings)) { |
|
| 666 | 666 | throw new \Exception(print_r($portSettings, true)); |
| 667 | 667 | } |
| 668 | 668 | |
| 669 | 669 | //proceed from the best configuration and return on first success |
| 670 | - foreach($portSettings as $setting) { |
|
| 670 | + foreach ($portSettings as $setting) { |
|
| 671 | 671 | $p = $setting['port']; |
| 672 | 672 | $t = $setting['tls']; |
| 673 | - \OCP\Util::writeLog('user_ldap', 'Wiz: trying port '. $p . ', TLS '. $t, \OCP\Util::DEBUG); |
|
| 673 | + \OCP\Util::writeLog('user_ldap', 'Wiz: trying port '.$p.', TLS '.$t, \OCP\Util::DEBUG); |
|
| 674 | 674 | //connectAndBind may throw Exception, it needs to be catched by the |
| 675 | 675 | //callee of this method |
| 676 | 676 | |
@@ -680,7 +680,7 @@ discard block |
||
| 680 | 680 | // any reply other than -1 (= cannot connect) is already okay, |
| 681 | 681 | // because then we found the server |
| 682 | 682 | // unavailable startTLS returns -11 |
| 683 | - if($e->getCode() > 0) { |
|
| 683 | + if ($e->getCode() > 0) { |
|
| 684 | 684 | $settingsFound = true; |
| 685 | 685 | } else { |
| 686 | 686 | throw $e; |
@@ -690,10 +690,10 @@ discard block |
||
| 690 | 690 | if ($settingsFound === true) { |
| 691 | 691 | $config = array( |
| 692 | 692 | 'ldapPort' => $p, |
| 693 | - 'ldapTLS' => (int)$t |
|
| 693 | + 'ldapTLS' => (int) $t |
|
| 694 | 694 | ); |
| 695 | 695 | $this->configuration->setConfiguration($config); |
| 696 | - \OCP\Util::writeLog('user_ldap', 'Wiz: detected Port ' . $p, \OCP\Util::DEBUG); |
|
| 696 | + \OCP\Util::writeLog('user_ldap', 'Wiz: detected Port '.$p, \OCP\Util::DEBUG); |
|
| 697 | 697 | $this->result->addChange('ldap_port', $p); |
| 698 | 698 | return $this->result; |
| 699 | 699 | } |
@@ -708,7 +708,7 @@ discard block |
||
| 708 | 708 | * @return WizardResult|false WizardResult on success, false otherwise |
| 709 | 709 | */ |
| 710 | 710 | public function guessBaseDN() { |
| 711 | - if(!$this->checkRequirements(array('ldapHost', |
|
| 711 | + if (!$this->checkRequirements(array('ldapHost', |
|
| 712 | 712 | 'ldapPort', |
| 713 | 713 | ))) { |
| 714 | 714 | return false; |
@@ -717,9 +717,9 @@ discard block |
||
| 717 | 717 | //check whether a DN is given in the agent name (99.9% of all cases) |
| 718 | 718 | $base = null; |
| 719 | 719 | $i = stripos($this->configuration->ldapAgentName, 'dc='); |
| 720 | - if($i !== false) { |
|
| 720 | + if ($i !== false) { |
|
| 721 | 721 | $base = substr($this->configuration->ldapAgentName, $i); |
| 722 | - if($this->testBaseDN($base)) { |
|
| 722 | + if ($this->testBaseDN($base)) { |
|
| 723 | 723 | $this->applyFind('ldap_base', $base); |
| 724 | 724 | return $this->result; |
| 725 | 725 | } |
@@ -730,13 +730,13 @@ discard block |
||
| 730 | 730 | //a base DN |
| 731 | 731 | $helper = new Helper(\OC::$server->getConfig()); |
| 732 | 732 | $domain = $helper->getDomainFromURL($this->configuration->ldapHost); |
| 733 | - if(!$domain) { |
|
| 733 | + if (!$domain) { |
|
| 734 | 734 | return false; |
| 735 | 735 | } |
| 736 | 736 | |
| 737 | 737 | $dparts = explode('.', $domain); |
| 738 | - while(count($dparts) > 0) { |
|
| 739 | - $base2 = 'dc=' . implode(',dc=', $dparts); |
|
| 738 | + while (count($dparts) > 0) { |
|
| 739 | + $base2 = 'dc='.implode(',dc=', $dparts); |
|
| 740 | 740 | if ($base !== $base2 && $this->testBaseDN($base2)) { |
| 741 | 741 | $this->applyFind('ldap_base', $base2); |
| 742 | 742 | return $this->result; |
@@ -769,7 +769,7 @@ discard block |
||
| 769 | 769 | $hostInfo = parse_url($host); |
| 770 | 770 | |
| 771 | 771 | //removes Port from Host |
| 772 | - if(is_array($hostInfo) && isset($hostInfo['port'])) { |
|
| 772 | + if (is_array($hostInfo) && isset($hostInfo['port'])) { |
|
| 773 | 773 | $port = $hostInfo['port']; |
| 774 | 774 | $host = str_replace(':'.$port, '', $host); |
| 775 | 775 | $this->applyFind('ldap_host', $host); |
@@ -786,30 +786,30 @@ discard block |
||
| 786 | 786 | private function detectGroupMemberAssoc() { |
| 787 | 787 | $possibleAttrs = array('uniqueMember', 'memberUid', 'member', 'gidNumber'); |
| 788 | 788 | $filter = $this->configuration->ldapGroupFilter; |
| 789 | - if(empty($filter)) { |
|
| 789 | + if (empty($filter)) { |
|
| 790 | 790 | return false; |
| 791 | 791 | } |
| 792 | 792 | $cr = $this->getConnection(); |
| 793 | - if(!$cr) { |
|
| 793 | + if (!$cr) { |
|
| 794 | 794 | throw new \Exception('Could not connect to LDAP'); |
| 795 | 795 | } |
| 796 | 796 | $base = $this->configuration->ldapBase[0]; |
| 797 | 797 | $rr = $this->ldap->search($cr, $base, $filter, $possibleAttrs, 0, 1000); |
| 798 | - if(!$this->ldap->isResource($rr)) { |
|
| 798 | + if (!$this->ldap->isResource($rr)) { |
|
| 799 | 799 | return false; |
| 800 | 800 | } |
| 801 | 801 | $er = $this->ldap->firstEntry($cr, $rr); |
| 802 | - while(is_resource($er)) { |
|
| 802 | + while (is_resource($er)) { |
|
| 803 | 803 | $this->ldap->getDN($cr, $er); |
| 804 | 804 | $attrs = $this->ldap->getAttributes($cr, $er); |
| 805 | 805 | $result = array(); |
| 806 | 806 | $possibleAttrsCount = count($possibleAttrs); |
| 807 | - for($i = 0; $i < $possibleAttrsCount; $i++) { |
|
| 808 | - if(isset($attrs[$possibleAttrs[$i]])) { |
|
| 807 | + for ($i = 0; $i < $possibleAttrsCount; $i++) { |
|
| 808 | + if (isset($attrs[$possibleAttrs[$i]])) { |
|
| 809 | 809 | $result[$possibleAttrs[$i]] = $attrs[$possibleAttrs[$i]]['count']; |
| 810 | 810 | } |
| 811 | 811 | } |
| 812 | - if(!empty($result)) { |
|
| 812 | + if (!empty($result)) { |
|
| 813 | 813 | natsort($result); |
| 814 | 814 | return key($result); |
| 815 | 815 | } |
@@ -828,14 +828,14 @@ discard block |
||
| 828 | 828 | */ |
| 829 | 829 | private function testBaseDN($base) { |
| 830 | 830 | $cr = $this->getConnection(); |
| 831 | - if(!$cr) { |
|
| 831 | + if (!$cr) { |
|
| 832 | 832 | throw new \Exception('Could not connect to LDAP'); |
| 833 | 833 | } |
| 834 | 834 | |
| 835 | 835 | //base is there, let's validate it. If we search for anything, we should |
| 836 | 836 | //get a result set > 0 on a proper base |
| 837 | 837 | $rr = $this->ldap->search($cr, $base, 'objectClass=*', array('dn'), 0, 1); |
| 838 | - if(!$this->ldap->isResource($rr)) { |
|
| 838 | + if (!$this->ldap->isResource($rr)) { |
|
| 839 | 839 | $errorNo = $this->ldap->errno($cr); |
| 840 | 840 | $errorMsg = $this->ldap->error($cr); |
| 841 | 841 | \OCP\Util::writeLog('user_ldap', 'Wiz: Could not search base '.$base. |
@@ -857,11 +857,11 @@ discard block |
||
| 857 | 857 | */ |
| 858 | 858 | private function testMemberOf() { |
| 859 | 859 | $cr = $this->getConnection(); |
| 860 | - if(!$cr) { |
|
| 860 | + if (!$cr) { |
|
| 861 | 861 | throw new \Exception('Could not connect to LDAP'); |
| 862 | 862 | } |
| 863 | 863 | $result = $this->access->countUsers('memberOf=*', array('memberOf'), 1); |
| 864 | - if(is_int($result) && $result > 0) { |
|
| 864 | + if (is_int($result) && $result > 0) { |
|
| 865 | 865 | return true; |
| 866 | 866 | } |
| 867 | 867 | return false; |
@@ -882,27 +882,27 @@ discard block |
||
| 882 | 882 | case self::LFILTER_USER_LIST: |
| 883 | 883 | $objcs = $this->configuration->ldapUserFilterObjectclass; |
| 884 | 884 | //glue objectclasses |
| 885 | - if(is_array($objcs) && count($objcs) > 0) { |
|
| 885 | + if (is_array($objcs) && count($objcs) > 0) { |
|
| 886 | 886 | $filter .= '(|'; |
| 887 | - foreach($objcs as $objc) { |
|
| 888 | - $filter .= '(objectclass=' . $objc . ')'; |
|
| 887 | + foreach ($objcs as $objc) { |
|
| 888 | + $filter .= '(objectclass='.$objc.')'; |
|
| 889 | 889 | } |
| 890 | 890 | $filter .= ')'; |
| 891 | 891 | $parts++; |
| 892 | 892 | } |
| 893 | 893 | //glue group memberships |
| 894 | - if($this->configuration->hasMemberOfFilterSupport) { |
|
| 894 | + if ($this->configuration->hasMemberOfFilterSupport) { |
|
| 895 | 895 | $cns = $this->configuration->ldapUserFilterGroups; |
| 896 | - if(is_array($cns) && count($cns) > 0) { |
|
| 896 | + if (is_array($cns) && count($cns) > 0) { |
|
| 897 | 897 | $filter .= '(|'; |
| 898 | 898 | $cr = $this->getConnection(); |
| 899 | - if(!$cr) { |
|
| 899 | + if (!$cr) { |
|
| 900 | 900 | throw new \Exception('Could not connect to LDAP'); |
| 901 | 901 | } |
| 902 | 902 | $base = $this->configuration->ldapBase[0]; |
| 903 | - foreach($cns as $cn) { |
|
| 904 | - $rr = $this->ldap->search($cr, $base, 'cn=' . $cn, array('dn', 'primaryGroupToken')); |
|
| 905 | - if(!$this->ldap->isResource($rr)) { |
|
| 903 | + foreach ($cns as $cn) { |
|
| 904 | + $rr = $this->ldap->search($cr, $base, 'cn='.$cn, array('dn', 'primaryGroupToken')); |
|
| 905 | + if (!$this->ldap->isResource($rr)) { |
|
| 906 | 906 | continue; |
| 907 | 907 | } |
| 908 | 908 | $er = $this->ldap->firstEntry($cr, $rr); |
@@ -911,11 +911,11 @@ discard block |
||
| 911 | 911 | if ($dn === false || $dn === '') { |
| 912 | 912 | continue; |
| 913 | 913 | } |
| 914 | - $filterPart = '(memberof=' . $dn . ')'; |
|
| 915 | - if(isset($attrs['primaryGroupToken'])) { |
|
| 914 | + $filterPart = '(memberof='.$dn.')'; |
|
| 915 | + if (isset($attrs['primaryGroupToken'])) { |
|
| 916 | 916 | $pgt = $attrs['primaryGroupToken'][0]; |
| 917 | - $primaryFilterPart = '(primaryGroupID=' . $pgt .')'; |
|
| 918 | - $filterPart = '(|' . $filterPart . $primaryFilterPart . ')'; |
|
| 917 | + $primaryFilterPart = '(primaryGroupID='.$pgt.')'; |
|
| 918 | + $filterPart = '(|'.$filterPart.$primaryFilterPart.')'; |
|
| 919 | 919 | } |
| 920 | 920 | $filter .= $filterPart; |
| 921 | 921 | } |
@@ -924,8 +924,8 @@ discard block |
||
| 924 | 924 | $parts++; |
| 925 | 925 | } |
| 926 | 926 | //wrap parts in AND condition |
| 927 | - if($parts > 1) { |
|
| 928 | - $filter = '(&' . $filter . ')'; |
|
| 927 | + if ($parts > 1) { |
|
| 928 | + $filter = '(&'.$filter.')'; |
|
| 929 | 929 | } |
| 930 | 930 | if ($filter === '') { |
| 931 | 931 | $filter = '(objectclass=*)'; |
@@ -935,27 +935,27 @@ discard block |
||
| 935 | 935 | case self::LFILTER_GROUP_LIST: |
| 936 | 936 | $objcs = $this->configuration->ldapGroupFilterObjectclass; |
| 937 | 937 | //glue objectclasses |
| 938 | - if(is_array($objcs) && count($objcs) > 0) { |
|
| 938 | + if (is_array($objcs) && count($objcs) > 0) { |
|
| 939 | 939 | $filter .= '(|'; |
| 940 | - foreach($objcs as $objc) { |
|
| 941 | - $filter .= '(objectclass=' . $objc . ')'; |
|
| 940 | + foreach ($objcs as $objc) { |
|
| 941 | + $filter .= '(objectclass='.$objc.')'; |
|
| 942 | 942 | } |
| 943 | 943 | $filter .= ')'; |
| 944 | 944 | $parts++; |
| 945 | 945 | } |
| 946 | 946 | //glue group memberships |
| 947 | 947 | $cns = $this->configuration->ldapGroupFilterGroups; |
| 948 | - if(is_array($cns) && count($cns) > 0) { |
|
| 948 | + if (is_array($cns) && count($cns) > 0) { |
|
| 949 | 949 | $filter .= '(|'; |
| 950 | - foreach($cns as $cn) { |
|
| 951 | - $filter .= '(cn=' . $cn . ')'; |
|
| 950 | + foreach ($cns as $cn) { |
|
| 951 | + $filter .= '(cn='.$cn.')'; |
|
| 952 | 952 | } |
| 953 | 953 | $filter .= ')'; |
| 954 | 954 | } |
| 955 | 955 | $parts++; |
| 956 | 956 | //wrap parts in AND condition |
| 957 | - if($parts > 1) { |
|
| 958 | - $filter = '(&' . $filter . ')'; |
|
| 957 | + if ($parts > 1) { |
|
| 958 | + $filter = '(&'.$filter.')'; |
|
| 959 | 959 | } |
| 960 | 960 | break; |
| 961 | 961 | |
@@ -967,47 +967,47 @@ discard block |
||
| 967 | 967 | $userAttributes = array_change_key_case(array_flip($userAttributes)); |
| 968 | 968 | $parts = 0; |
| 969 | 969 | |
| 970 | - if($this->configuration->ldapLoginFilterUsername === '1') { |
|
| 970 | + if ($this->configuration->ldapLoginFilterUsername === '1') { |
|
| 971 | 971 | $attr = ''; |
| 972 | - if(isset($userAttributes['uid'])) { |
|
| 972 | + if (isset($userAttributes['uid'])) { |
|
| 973 | 973 | $attr = 'uid'; |
| 974 | - } else if(isset($userAttributes['samaccountname'])) { |
|
| 974 | + } else if (isset($userAttributes['samaccountname'])) { |
|
| 975 | 975 | $attr = 'samaccountname'; |
| 976 | - } else if(isset($userAttributes['cn'])) { |
|
| 976 | + } else if (isset($userAttributes['cn'])) { |
|
| 977 | 977 | //fallback |
| 978 | 978 | $attr = 'cn'; |
| 979 | 979 | } |
| 980 | 980 | if ($attr !== '') { |
| 981 | - $filterUsername = '(' . $attr . $loginpart . ')'; |
|
| 981 | + $filterUsername = '('.$attr.$loginpart.')'; |
|
| 982 | 982 | $parts++; |
| 983 | 983 | } |
| 984 | 984 | } |
| 985 | 985 | |
| 986 | 986 | $filterEmail = ''; |
| 987 | - if($this->configuration->ldapLoginFilterEmail === '1') { |
|
| 987 | + if ($this->configuration->ldapLoginFilterEmail === '1') { |
|
| 988 | 988 | $filterEmail = '(|(mailPrimaryAddress=%uid)(mail=%uid))'; |
| 989 | 989 | $parts++; |
| 990 | 990 | } |
| 991 | 991 | |
| 992 | 992 | $filterAttributes = ''; |
| 993 | 993 | $attrsToFilter = $this->configuration->ldapLoginFilterAttributes; |
| 994 | - if(is_array($attrsToFilter) && count($attrsToFilter) > 0) { |
|
| 994 | + if (is_array($attrsToFilter) && count($attrsToFilter) > 0) { |
|
| 995 | 995 | $filterAttributes = '(|'; |
| 996 | - foreach($attrsToFilter as $attribute) { |
|
| 997 | - $filterAttributes .= '(' . $attribute . $loginpart . ')'; |
|
| 996 | + foreach ($attrsToFilter as $attribute) { |
|
| 997 | + $filterAttributes .= '('.$attribute.$loginpart.')'; |
|
| 998 | 998 | } |
| 999 | 999 | $filterAttributes .= ')'; |
| 1000 | 1000 | $parts++; |
| 1001 | 1001 | } |
| 1002 | 1002 | |
| 1003 | 1003 | $filterLogin = ''; |
| 1004 | - if($parts > 1) { |
|
| 1004 | + if ($parts > 1) { |
|
| 1005 | 1005 | $filterLogin = '(|'; |
| 1006 | 1006 | } |
| 1007 | 1007 | $filterLogin .= $filterUsername; |
| 1008 | 1008 | $filterLogin .= $filterEmail; |
| 1009 | 1009 | $filterLogin .= $filterAttributes; |
| 1010 | - if($parts > 1) { |
|
| 1010 | + if ($parts > 1) { |
|
| 1011 | 1011 | $filterLogin .= ')'; |
| 1012 | 1012 | } |
| 1013 | 1013 | |
@@ -1032,12 +1032,12 @@ discard block |
||
| 1032 | 1032 | //connect, does not really trigger any server communication |
| 1033 | 1033 | $host = $this->configuration->ldapHost; |
| 1034 | 1034 | $hostInfo = parse_url($host); |
| 1035 | - if(!$hostInfo) { |
|
| 1035 | + if (!$hostInfo) { |
|
| 1036 | 1036 | throw new \Exception(self::$l->t('Invalid Host')); |
| 1037 | 1037 | } |
| 1038 | 1038 | \OCP\Util::writeLog('user_ldap', 'Wiz: Attempting to connect ', \OCP\Util::DEBUG); |
| 1039 | 1039 | $cr = $this->ldap->connect($host, $port); |
| 1040 | - if(!is_resource($cr)) { |
|
| 1040 | + if (!is_resource($cr)) { |
|
| 1041 | 1041 | throw new \Exception(self::$l->t('Invalid Host')); |
| 1042 | 1042 | } |
| 1043 | 1043 | |
@@ -1047,9 +1047,9 @@ discard block |
||
| 1047 | 1047 | $this->ldap->setOption($cr, LDAP_OPT_NETWORK_TIMEOUT, self::LDAP_NW_TIMEOUT); |
| 1048 | 1048 | |
| 1049 | 1049 | try { |
| 1050 | - if($tls) { |
|
| 1050 | + if ($tls) { |
|
| 1051 | 1051 | $isTlsWorking = @$this->ldap->startTls($cr); |
| 1052 | - if(!$isTlsWorking) { |
|
| 1052 | + if (!$isTlsWorking) { |
|
| 1053 | 1053 | return false; |
| 1054 | 1054 | } |
| 1055 | 1055 | } |
@@ -1063,17 +1063,17 @@ discard block |
||
| 1063 | 1063 | $errNo = $this->ldap->errno($cr); |
| 1064 | 1064 | $error = ldap_error($cr); |
| 1065 | 1065 | $this->ldap->unbind($cr); |
| 1066 | - } catch(ServerNotAvailableException $e) { |
|
| 1066 | + } catch (ServerNotAvailableException $e) { |
|
| 1067 | 1067 | return false; |
| 1068 | 1068 | } |
| 1069 | 1069 | |
| 1070 | - if($login === true) { |
|
| 1070 | + if ($login === true) { |
|
| 1071 | 1071 | $this->ldap->unbind($cr); |
| 1072 | - \OCP\Util::writeLog('user_ldap', 'Wiz: Bind successful to Port '. $port . ' TLS ' . (int)$tls, \OCP\Util::DEBUG); |
|
| 1072 | + \OCP\Util::writeLog('user_ldap', 'Wiz: Bind successful to Port '.$port.' TLS '.(int) $tls, \OCP\Util::DEBUG); |
|
| 1073 | 1073 | return true; |
| 1074 | 1074 | } |
| 1075 | 1075 | |
| 1076 | - if($errNo === -1) { |
|
| 1076 | + if ($errNo === -1) { |
|
| 1077 | 1077 | //host, port or TLS wrong |
| 1078 | 1078 | return false; |
| 1079 | 1079 | } |
@@ -1101,9 +1101,9 @@ discard block |
||
| 1101 | 1101 | */ |
| 1102 | 1102 | private function checkRequirements($reqs) { |
| 1103 | 1103 | $this->checkAgentRequirements(); |
| 1104 | - foreach($reqs as $option) { |
|
| 1104 | + foreach ($reqs as $option) { |
|
| 1105 | 1105 | $value = $this->configuration->$option; |
| 1106 | - if(empty($value)) { |
|
| 1106 | + if (empty($value)) { |
|
| 1107 | 1107 | return false; |
| 1108 | 1108 | } |
| 1109 | 1109 | } |
@@ -1125,33 +1125,33 @@ discard block |
||
| 1125 | 1125 | $dnRead = array(); |
| 1126 | 1126 | $foundItems = array(); |
| 1127 | 1127 | $maxEntries = 0; |
| 1128 | - if(!is_array($this->configuration->ldapBase) |
|
| 1128 | + if (!is_array($this->configuration->ldapBase) |
|
| 1129 | 1129 | || !isset($this->configuration->ldapBase[0])) { |
| 1130 | 1130 | return false; |
| 1131 | 1131 | } |
| 1132 | 1132 | $base = $this->configuration->ldapBase[0]; |
| 1133 | 1133 | $cr = $this->getConnection(); |
| 1134 | - if(!$this->ldap->isResource($cr)) { |
|
| 1134 | + if (!$this->ldap->isResource($cr)) { |
|
| 1135 | 1135 | return false; |
| 1136 | 1136 | } |
| 1137 | 1137 | $lastFilter = null; |
| 1138 | - if(isset($filters[count($filters)-1])) { |
|
| 1139 | - $lastFilter = $filters[count($filters)-1]; |
|
| 1138 | + if (isset($filters[count($filters) - 1])) { |
|
| 1139 | + $lastFilter = $filters[count($filters) - 1]; |
|
| 1140 | 1140 | } |
| 1141 | - foreach($filters as $filter) { |
|
| 1142 | - if($lastFilter === $filter && count($foundItems) > 0) { |
|
| 1141 | + foreach ($filters as $filter) { |
|
| 1142 | + if ($lastFilter === $filter && count($foundItems) > 0) { |
|
| 1143 | 1143 | //skip when the filter is a wildcard and results were found |
| 1144 | 1144 | continue; |
| 1145 | 1145 | } |
| 1146 | 1146 | // 20k limit for performance and reason |
| 1147 | 1147 | $rr = $this->ldap->search($cr, $base, $filter, array($attr), 0, 20000); |
| 1148 | - if(!$this->ldap->isResource($rr)) { |
|
| 1148 | + if (!$this->ldap->isResource($rr)) { |
|
| 1149 | 1149 | continue; |
| 1150 | 1150 | } |
| 1151 | 1151 | $entries = $this->ldap->countEntries($cr, $rr); |
| 1152 | 1152 | $getEntryFunc = 'firstEntry'; |
| 1153 | - if(($entries !== false) && ($entries > 0)) { |
|
| 1154 | - if(!is_null($maxF) && $entries > $maxEntries) { |
|
| 1153 | + if (($entries !== false) && ($entries > 0)) { |
|
| 1154 | + if (!is_null($maxF) && $entries > $maxEntries) { |
|
| 1155 | 1155 | $maxEntries = $entries; |
| 1156 | 1156 | $maxF = $filter; |
| 1157 | 1157 | } |
@@ -1159,13 +1159,13 @@ discard block |
||
| 1159 | 1159 | do { |
| 1160 | 1160 | $entry = $this->ldap->$getEntryFunc($cr, $rr); |
| 1161 | 1161 | $getEntryFunc = 'nextEntry'; |
| 1162 | - if(!$this->ldap->isResource($entry)) { |
|
| 1162 | + if (!$this->ldap->isResource($entry)) { |
|
| 1163 | 1163 | continue 2; |
| 1164 | 1164 | } |
| 1165 | 1165 | $rr = $entry; //will be expected by nextEntry next round |
| 1166 | 1166 | $attributes = $this->ldap->getAttributes($cr, $entry); |
| 1167 | 1167 | $dn = $this->ldap->getDN($cr, $entry); |
| 1168 | - if($dn === false || in_array($dn, $dnRead)) { |
|
| 1168 | + if ($dn === false || in_array($dn, $dnRead)) { |
|
| 1169 | 1169 | continue; |
| 1170 | 1170 | } |
| 1171 | 1171 | $newItems = array(); |
@@ -1176,7 +1176,7 @@ discard block |
||
| 1176 | 1176 | $foundItems = array_merge($foundItems, $newItems); |
| 1177 | 1177 | $this->resultCache[$dn][$attr] = $newItems; |
| 1178 | 1178 | $dnRead[] = $dn; |
| 1179 | - } while(($state === self::LRESULT_PROCESSED_SKIP |
|
| 1179 | + } while (($state === self::LRESULT_PROCESSED_SKIP |
|
| 1180 | 1180 | || $this->ldap->isResource($entry)) |
| 1181 | 1181 | && ($dnReadLimit === 0 || $dnReadCount < $dnReadLimit)); |
| 1182 | 1182 | } |
@@ -1199,11 +1199,11 @@ discard block |
||
| 1199 | 1199 | */ |
| 1200 | 1200 | private function determineFeature($objectclasses, $attr, $dbkey, $confkey, $po = false) { |
| 1201 | 1201 | $cr = $this->getConnection(); |
| 1202 | - if(!$cr) { |
|
| 1202 | + if (!$cr) { |
|
| 1203 | 1203 | throw new \Exception('Could not connect to LDAP'); |
| 1204 | 1204 | } |
| 1205 | 1205 | $p = 'objectclass='; |
| 1206 | - foreach($objectclasses as $key => $value) { |
|
| 1206 | + foreach ($objectclasses as $key => $value) { |
|
| 1207 | 1207 | $objectclasses[$key] = $p.$value; |
| 1208 | 1208 | } |
| 1209 | 1209 | $maxEntryObjC = ''; |
@@ -1215,7 +1215,7 @@ discard block |
||
| 1215 | 1215 | $availableFeatures = |
| 1216 | 1216 | $this->cumulativeSearchOnAttribute($objectclasses, $attr, |
| 1217 | 1217 | $dig, $maxEntryObjC); |
| 1218 | - if(is_array($availableFeatures) |
|
| 1218 | + if (is_array($availableFeatures) |
|
| 1219 | 1219 | && count($availableFeatures) > 0) { |
| 1220 | 1220 | natcasesort($availableFeatures); |
| 1221 | 1221 | //natcasesort keeps indices, but we must get rid of them for proper |
@@ -1226,7 +1226,7 @@ discard block |
||
| 1226 | 1226 | } |
| 1227 | 1227 | |
| 1228 | 1228 | $setFeatures = $this->configuration->$confkey; |
| 1229 | - if(is_array($setFeatures) && !empty($setFeatures)) { |
|
| 1229 | + if (is_array($setFeatures) && !empty($setFeatures)) { |
|
| 1230 | 1230 | //something is already configured? pre-select it. |
| 1231 | 1231 | $this->result->addChange($dbkey, $setFeatures); |
| 1232 | 1232 | } else if ($po && $maxEntryObjC !== '') { |
@@ -1248,7 +1248,7 @@ discard block |
||
| 1248 | 1248 | * LRESULT_PROCESSED_INVALID or LRESULT_PROCESSED_SKIP |
| 1249 | 1249 | */ |
| 1250 | 1250 | private function getAttributeValuesFromEntry($result, $attribute, &$known) { |
| 1251 | - if(!is_array($result) |
|
| 1251 | + if (!is_array($result) |
|
| 1252 | 1252 | || !isset($result['count']) |
| 1253 | 1253 | || !$result['count'] > 0) { |
| 1254 | 1254 | return self::LRESULT_PROCESSED_INVALID; |
@@ -1257,12 +1257,12 @@ discard block |
||
| 1257 | 1257 | // strtolower on all keys for proper comparison |
| 1258 | 1258 | $result = \OCP\Util::mb_array_change_key_case($result); |
| 1259 | 1259 | $attribute = strtolower($attribute); |
| 1260 | - if(isset($result[$attribute])) { |
|
| 1261 | - foreach($result[$attribute] as $key => $val) { |
|
| 1262 | - if($key === 'count') { |
|
| 1260 | + if (isset($result[$attribute])) { |
|
| 1261 | + foreach ($result[$attribute] as $key => $val) { |
|
| 1262 | + if ($key === 'count') { |
|
| 1263 | 1263 | continue; |
| 1264 | 1264 | } |
| 1265 | - if(!in_array($val, $known)) { |
|
| 1265 | + if (!in_array($val, $known)) { |
|
| 1266 | 1266 | $known[] = $val; |
| 1267 | 1267 | } |
| 1268 | 1268 | } |
@@ -1276,7 +1276,7 @@ discard block |
||
| 1276 | 1276 | * @return bool|mixed |
| 1277 | 1277 | */ |
| 1278 | 1278 | private function getConnection() { |
| 1279 | - if(!is_null($this->cr)) { |
|
| 1279 | + if (!is_null($this->cr)) { |
|
| 1280 | 1280 | return $this->cr; |
| 1281 | 1281 | } |
| 1282 | 1282 | |
@@ -1288,14 +1288,14 @@ discard block |
||
| 1288 | 1288 | $this->ldap->setOption($cr, LDAP_OPT_PROTOCOL_VERSION, 3); |
| 1289 | 1289 | $this->ldap->setOption($cr, LDAP_OPT_REFERRALS, 0); |
| 1290 | 1290 | $this->ldap->setOption($cr, LDAP_OPT_NETWORK_TIMEOUT, self::LDAP_NW_TIMEOUT); |
| 1291 | - if($this->configuration->ldapTLS === 1) { |
|
| 1291 | + if ($this->configuration->ldapTLS === 1) { |
|
| 1292 | 1292 | $this->ldap->startTls($cr); |
| 1293 | 1293 | } |
| 1294 | 1294 | |
| 1295 | 1295 | $lo = @$this->ldap->bind($cr, |
| 1296 | 1296 | $this->configuration->ldapAgentName, |
| 1297 | 1297 | $this->configuration->ldapAgentPassword); |
| 1298 | - if($lo === true) { |
|
| 1298 | + if ($lo === true) { |
|
| 1299 | 1299 | $this->$cr = $cr; |
| 1300 | 1300 | return $cr; |
| 1301 | 1301 | } |
@@ -1326,18 +1326,18 @@ discard block |
||
| 1326 | 1326 | //636 ← LDAPS / SSL |
| 1327 | 1327 | //7xxx ← UCS. need to be checked first, because both ports may be open |
| 1328 | 1328 | $host = $this->configuration->ldapHost; |
| 1329 | - $port = (int)$this->configuration->ldapPort; |
|
| 1329 | + $port = (int) $this->configuration->ldapPort; |
|
| 1330 | 1330 | $portSettings = array(); |
| 1331 | 1331 | |
| 1332 | 1332 | //In case the port is already provided, we will check this first |
| 1333 | - if($port > 0) { |
|
| 1333 | + if ($port > 0) { |
|
| 1334 | 1334 | $hostInfo = parse_url($host); |
| 1335 | - if(!(is_array($hostInfo) |
|
| 1335 | + if (!(is_array($hostInfo) |
|
| 1336 | 1336 | && isset($hostInfo['scheme']) |
| 1337 | 1337 | && stripos($hostInfo['scheme'], 'ldaps') !== false)) { |
| 1338 | 1338 | $portSettings[] = array('port' => $port, 'tls' => true); |
| 1339 | 1339 | } |
| 1340 | - $portSettings[] =array('port' => $port, 'tls' => false); |
|
| 1340 | + $portSettings[] = array('port' => $port, 'tls' => false); |
|
| 1341 | 1341 | } |
| 1342 | 1342 | |
| 1343 | 1343 | //default ports |
@@ -51,576 +51,576 @@ |
||
| 51 | 51 | use OCP\Util; |
| 52 | 52 | |
| 53 | 53 | class User_LDAP extends BackendUtility implements \OCP\IUserBackend, \OCP\UserInterface, IUserLDAP { |
| 54 | - /** @var \OCP\IConfig */ |
|
| 55 | - protected $ocConfig; |
|
| 56 | - |
|
| 57 | - /** @var INotificationManager */ |
|
| 58 | - protected $notificationManager; |
|
| 59 | - |
|
| 60 | - /** @var string */ |
|
| 61 | - protected $currentUserInDeletionProcess; |
|
| 62 | - |
|
| 63 | - /** @var UserPluginManager */ |
|
| 64 | - protected $userPluginManager; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @param Access $access |
|
| 68 | - * @param \OCP\IConfig $ocConfig |
|
| 69 | - * @param \OCP\Notification\IManager $notificationManager |
|
| 70 | - * @param IUserSession $userSession |
|
| 71 | - */ |
|
| 72 | - public function __construct(Access $access, IConfig $ocConfig, INotificationManager $notificationManager, IUserSession $userSession, UserPluginManager $userPluginManager) { |
|
| 73 | - parent::__construct($access); |
|
| 74 | - $this->ocConfig = $ocConfig; |
|
| 75 | - $this->notificationManager = $notificationManager; |
|
| 76 | - $this->userPluginManager = $userPluginManager; |
|
| 77 | - $this->registerHooks($userSession); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - protected function registerHooks(IUserSession $userSession) { |
|
| 81 | - $userSession->listen('\OC\User', 'preDelete', [$this, 'preDeleteUser']); |
|
| 82 | - $userSession->listen('\OC\User', 'postDelete', [$this, 'postDeleteUser']); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - public function preDeleteUser(IUser $user) { |
|
| 86 | - $this->currentUserInDeletionProcess = $user->getUID(); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - public function postDeleteUser() { |
|
| 90 | - $this->currentUserInDeletionProcess = null; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * checks whether the user is allowed to change his avatar in Nextcloud |
|
| 95 | - * @param string $uid the Nextcloud user name |
|
| 96 | - * @return boolean either the user can or cannot |
|
| 97 | - */ |
|
| 98 | - public function canChangeAvatar($uid) { |
|
| 99 | - if ($this->userPluginManager->implementsActions(Backend::PROVIDE_AVATAR)) { |
|
| 100 | - return $this->userPluginManager->canChangeAvatar($uid); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - $user = $this->access->userManager->get($uid); |
|
| 104 | - if(!$user instanceof User) { |
|
| 105 | - return false; |
|
| 106 | - } |
|
| 107 | - if($user->getAvatarImage() === false) { |
|
| 108 | - return true; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - return false; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * returns the username for the given login name, if available |
|
| 116 | - * |
|
| 117 | - * @param string $loginName |
|
| 118 | - * @return string|false |
|
| 119 | - */ |
|
| 120 | - public function loginName2UserName($loginName) { |
|
| 121 | - $cacheKey = 'loginName2UserName-'.$loginName; |
|
| 122 | - $username = $this->access->connection->getFromCache($cacheKey); |
|
| 123 | - if(!is_null($username)) { |
|
| 124 | - return $username; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - try { |
|
| 128 | - $ldapRecord = $this->getLDAPUserByLoginName($loginName); |
|
| 129 | - $user = $this->access->userManager->get($ldapRecord['dn'][0]); |
|
| 130 | - if($user instanceof OfflineUser) { |
|
| 131 | - // this path is not really possible, however get() is documented |
|
| 132 | - // to return User or OfflineUser so we are very defensive here. |
|
| 133 | - $this->access->connection->writeToCache($cacheKey, false); |
|
| 134 | - return false; |
|
| 135 | - } |
|
| 136 | - $username = $user->getUsername(); |
|
| 137 | - $this->access->connection->writeToCache($cacheKey, $username); |
|
| 138 | - return $username; |
|
| 139 | - } catch (NotOnLDAP $e) { |
|
| 140 | - $this->access->connection->writeToCache($cacheKey, false); |
|
| 141 | - return false; |
|
| 142 | - } |
|
| 143 | - } |
|
| 54 | + /** @var \OCP\IConfig */ |
|
| 55 | + protected $ocConfig; |
|
| 56 | + |
|
| 57 | + /** @var INotificationManager */ |
|
| 58 | + protected $notificationManager; |
|
| 59 | + |
|
| 60 | + /** @var string */ |
|
| 61 | + protected $currentUserInDeletionProcess; |
|
| 62 | + |
|
| 63 | + /** @var UserPluginManager */ |
|
| 64 | + protected $userPluginManager; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @param Access $access |
|
| 68 | + * @param \OCP\IConfig $ocConfig |
|
| 69 | + * @param \OCP\Notification\IManager $notificationManager |
|
| 70 | + * @param IUserSession $userSession |
|
| 71 | + */ |
|
| 72 | + public function __construct(Access $access, IConfig $ocConfig, INotificationManager $notificationManager, IUserSession $userSession, UserPluginManager $userPluginManager) { |
|
| 73 | + parent::__construct($access); |
|
| 74 | + $this->ocConfig = $ocConfig; |
|
| 75 | + $this->notificationManager = $notificationManager; |
|
| 76 | + $this->userPluginManager = $userPluginManager; |
|
| 77 | + $this->registerHooks($userSession); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + protected function registerHooks(IUserSession $userSession) { |
|
| 81 | + $userSession->listen('\OC\User', 'preDelete', [$this, 'preDeleteUser']); |
|
| 82 | + $userSession->listen('\OC\User', 'postDelete', [$this, 'postDeleteUser']); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + public function preDeleteUser(IUser $user) { |
|
| 86 | + $this->currentUserInDeletionProcess = $user->getUID(); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + public function postDeleteUser() { |
|
| 90 | + $this->currentUserInDeletionProcess = null; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * checks whether the user is allowed to change his avatar in Nextcloud |
|
| 95 | + * @param string $uid the Nextcloud user name |
|
| 96 | + * @return boolean either the user can or cannot |
|
| 97 | + */ |
|
| 98 | + public function canChangeAvatar($uid) { |
|
| 99 | + if ($this->userPluginManager->implementsActions(Backend::PROVIDE_AVATAR)) { |
|
| 100 | + return $this->userPluginManager->canChangeAvatar($uid); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + $user = $this->access->userManager->get($uid); |
|
| 104 | + if(!$user instanceof User) { |
|
| 105 | + return false; |
|
| 106 | + } |
|
| 107 | + if($user->getAvatarImage() === false) { |
|
| 108 | + return true; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + return false; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * returns the username for the given login name, if available |
|
| 116 | + * |
|
| 117 | + * @param string $loginName |
|
| 118 | + * @return string|false |
|
| 119 | + */ |
|
| 120 | + public function loginName2UserName($loginName) { |
|
| 121 | + $cacheKey = 'loginName2UserName-'.$loginName; |
|
| 122 | + $username = $this->access->connection->getFromCache($cacheKey); |
|
| 123 | + if(!is_null($username)) { |
|
| 124 | + return $username; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + try { |
|
| 128 | + $ldapRecord = $this->getLDAPUserByLoginName($loginName); |
|
| 129 | + $user = $this->access->userManager->get($ldapRecord['dn'][0]); |
|
| 130 | + if($user instanceof OfflineUser) { |
|
| 131 | + // this path is not really possible, however get() is documented |
|
| 132 | + // to return User or OfflineUser so we are very defensive here. |
|
| 133 | + $this->access->connection->writeToCache($cacheKey, false); |
|
| 134 | + return false; |
|
| 135 | + } |
|
| 136 | + $username = $user->getUsername(); |
|
| 137 | + $this->access->connection->writeToCache($cacheKey, $username); |
|
| 138 | + return $username; |
|
| 139 | + } catch (NotOnLDAP $e) { |
|
| 140 | + $this->access->connection->writeToCache($cacheKey, false); |
|
| 141 | + return false; |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - /** |
|
| 146 | - * returns the username for the given LDAP DN, if available |
|
| 147 | - * |
|
| 148 | - * @param string $dn |
|
| 149 | - * @return string|false with the username |
|
| 150 | - */ |
|
| 151 | - public function dn2UserName($dn) { |
|
| 152 | - return $this->access->dn2username($dn); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * returns an LDAP record based on a given login name |
|
| 157 | - * |
|
| 158 | - * @param string $loginName |
|
| 159 | - * @return array |
|
| 160 | - * @throws NotOnLDAP |
|
| 161 | - */ |
|
| 162 | - public function getLDAPUserByLoginName($loginName) { |
|
| 163 | - //find out dn of the user name |
|
| 164 | - $attrs = $this->access->userManager->getAttributes(); |
|
| 165 | - $users = $this->access->fetchUsersByLoginName($loginName, $attrs); |
|
| 166 | - if(count($users) < 1) { |
|
| 167 | - throw new NotOnLDAP('No user available for the given login name on ' . |
|
| 168 | - $this->access->connection->ldapHost . ':' . $this->access->connection->ldapPort); |
|
| 169 | - } |
|
| 170 | - return $users[0]; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * Check if the password is correct without logging in the user |
|
| 175 | - * |
|
| 176 | - * @param string $uid The username |
|
| 177 | - * @param string $password The password |
|
| 178 | - * @return false|string |
|
| 179 | - */ |
|
| 180 | - public function checkPassword($uid, $password) { |
|
| 181 | - try { |
|
| 182 | - $ldapRecord = $this->getLDAPUserByLoginName($uid); |
|
| 183 | - } catch(NotOnLDAP $e) { |
|
| 184 | - if($this->ocConfig->getSystemValue('loglevel', Util::WARN) === Util::DEBUG) { |
|
| 185 | - \OC::$server->getLogger()->logException($e, ['app' => 'user_ldap']); |
|
| 186 | - } |
|
| 187 | - return false; |
|
| 188 | - } |
|
| 189 | - $dn = $ldapRecord['dn'][0]; |
|
| 190 | - $user = $this->access->userManager->get($dn); |
|
| 191 | - |
|
| 192 | - if(!$user instanceof User) { |
|
| 193 | - Util::writeLog('user_ldap', |
|
| 194 | - 'LDAP Login: Could not get user object for DN ' . $dn . |
|
| 195 | - '. Maybe the LDAP entry has no set display name attribute?', |
|
| 196 | - Util::WARN); |
|
| 197 | - return false; |
|
| 198 | - } |
|
| 199 | - if($user->getUsername() !== false) { |
|
| 200 | - //are the credentials OK? |
|
| 201 | - if(!$this->access->areCredentialsValid($dn, $password)) { |
|
| 202 | - return false; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - $this->access->cacheUserExists($user->getUsername()); |
|
| 206 | - $user->processAttributes($ldapRecord); |
|
| 207 | - $user->markLogin(); |
|
| 208 | - |
|
| 209 | - return $user->getUsername(); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - return false; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - /** |
|
| 216 | - * Set password |
|
| 217 | - * @param string $uid The username |
|
| 218 | - * @param string $password The new password |
|
| 219 | - * @return bool |
|
| 220 | - */ |
|
| 221 | - public function setPassword($uid, $password) { |
|
| 222 | - if ($this->userPluginManager->implementsActions(Backend::SET_PASSWORD)) { |
|
| 223 | - return $this->userPluginManager->setPassword($uid, $password); |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - $user = $this->access->userManager->get($uid); |
|
| 227 | - |
|
| 228 | - if(!$user instanceof User) { |
|
| 229 | - throw new \Exception('LDAP setPassword: Could not get user object for uid ' . $uid . |
|
| 230 | - '. Maybe the LDAP entry has no set display name attribute?'); |
|
| 231 | - } |
|
| 232 | - if($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) { |
|
| 233 | - $ldapDefaultPPolicyDN = $this->access->connection->ldapDefaultPPolicyDN; |
|
| 234 | - $turnOnPasswordChange = $this->access->connection->turnOnPasswordChange; |
|
| 235 | - if (!empty($ldapDefaultPPolicyDN) && ((int)$turnOnPasswordChange === 1)) { |
|
| 236 | - //remove last password expiry warning if any |
|
| 237 | - $notification = $this->notificationManager->createNotification(); |
|
| 238 | - $notification->setApp('user_ldap') |
|
| 239 | - ->setUser($uid) |
|
| 240 | - ->setObject('pwd_exp_warn', $uid) |
|
| 241 | - ; |
|
| 242 | - $this->notificationManager->markProcessed($notification); |
|
| 243 | - } |
|
| 244 | - return true; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - return false; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * Get a list of all users |
|
| 252 | - * |
|
| 253 | - * @param string $search |
|
| 254 | - * @param integer $limit |
|
| 255 | - * @param integer $offset |
|
| 256 | - * @return string[] an array of all uids |
|
| 257 | - */ |
|
| 258 | - public function getUsers($search = '', $limit = 10, $offset = 0) { |
|
| 259 | - $search = $this->access->escapeFilterPart($search, true); |
|
| 260 | - $cachekey = 'getUsers-'.$search.'-'.$limit.'-'.$offset; |
|
| 261 | - |
|
| 262 | - //check if users are cached, if so return |
|
| 263 | - $ldap_users = $this->access->connection->getFromCache($cachekey); |
|
| 264 | - if(!is_null($ldap_users)) { |
|
| 265 | - return $ldap_users; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
|
| 269 | - // error. With a limit of 0, we get 0 results. So we pass null. |
|
| 270 | - if($limit <= 0) { |
|
| 271 | - $limit = null; |
|
| 272 | - } |
|
| 273 | - $filter = $this->access->combineFilterWithAnd(array( |
|
| 274 | - $this->access->connection->ldapUserFilter, |
|
| 275 | - $this->access->connection->ldapUserDisplayName . '=*', |
|
| 276 | - $this->access->getFilterPartForUserSearch($search) |
|
| 277 | - )); |
|
| 278 | - |
|
| 279 | - Util::writeLog('user_ldap', |
|
| 280 | - 'getUsers: Options: search '.$search.' limit '.$limit.' offset '.$offset.' Filter: '.$filter, |
|
| 281 | - Util::DEBUG); |
|
| 282 | - //do the search and translate results to Nextcloud names |
|
| 283 | - $ldap_users = $this->access->fetchListOfUsers( |
|
| 284 | - $filter, |
|
| 285 | - $this->access->userManager->getAttributes(true), |
|
| 286 | - $limit, $offset); |
|
| 287 | - $ldap_users = $this->access->nextcloudUserNames($ldap_users); |
|
| 288 | - Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', Util::DEBUG); |
|
| 289 | - |
|
| 290 | - $this->access->connection->writeToCache($cachekey, $ldap_users); |
|
| 291 | - return $ldap_users; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * checks whether a user is still available on LDAP |
|
| 296 | - * |
|
| 297 | - * @param string|\OCA\User_LDAP\User\User $user either the Nextcloud user |
|
| 298 | - * name or an instance of that user |
|
| 299 | - * @return bool |
|
| 300 | - * @throws \Exception |
|
| 301 | - * @throws \OC\ServerNotAvailableException |
|
| 302 | - */ |
|
| 303 | - public function userExistsOnLDAP($user) { |
|
| 304 | - if(is_string($user)) { |
|
| 305 | - $user = $this->access->userManager->get($user); |
|
| 306 | - } |
|
| 307 | - if(is_null($user)) { |
|
| 308 | - return false; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - $dn = $user->getDN(); |
|
| 312 | - //check if user really still exists by reading its entry |
|
| 313 | - if(!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) { |
|
| 314 | - $lcr = $this->access->connection->getConnectionResource(); |
|
| 315 | - if(is_null($lcr)) { |
|
| 316 | - throw new \Exception('No LDAP Connection to server ' . $this->access->connection->ldapHost); |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - try { |
|
| 320 | - $uuid = $this->access->getUserMapper()->getUUIDByDN($dn); |
|
| 321 | - if (!$uuid) { |
|
| 322 | - return false; |
|
| 323 | - } |
|
| 324 | - $newDn = $this->access->getUserDnByUuid($uuid); |
|
| 325 | - //check if renamed user is still valid by reapplying the ldap filter |
|
| 326 | - if (!is_array($this->access->readAttribute($newDn, '', $this->access->connection->ldapUserFilter))) { |
|
| 327 | - return false; |
|
| 328 | - } |
|
| 329 | - $this->access->getUserMapper()->setDNbyUUID($newDn, $uuid); |
|
| 330 | - return true; |
|
| 331 | - } catch (ServerNotAvailableException $e) { |
|
| 332 | - throw $e; |
|
| 333 | - } catch (\Exception $e) { |
|
| 334 | - return false; |
|
| 335 | - } |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - if($user instanceof OfflineUser) { |
|
| 339 | - $user->unmark(); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - return true; |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * check if a user exists |
|
| 347 | - * @param string $uid the username |
|
| 348 | - * @return boolean |
|
| 349 | - * @throws \Exception when connection could not be established |
|
| 350 | - */ |
|
| 351 | - public function userExists($uid) { |
|
| 352 | - $userExists = $this->access->connection->getFromCache('userExists'.$uid); |
|
| 353 | - if(!is_null($userExists)) { |
|
| 354 | - return (bool)$userExists; |
|
| 355 | - } |
|
| 356 | - //getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking. |
|
| 357 | - $user = $this->access->userManager->get($uid); |
|
| 358 | - |
|
| 359 | - if(is_null($user)) { |
|
| 360 | - Util::writeLog('user_ldap', 'No DN found for '.$uid.' on '. |
|
| 361 | - $this->access->connection->ldapHost, Util::DEBUG); |
|
| 362 | - $this->access->connection->writeToCache('userExists'.$uid, false); |
|
| 363 | - return false; |
|
| 364 | - } else if($user instanceof OfflineUser) { |
|
| 365 | - //express check for users marked as deleted. Returning true is |
|
| 366 | - //necessary for cleanup |
|
| 367 | - return true; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - $result = $this->userExistsOnLDAP($user); |
|
| 371 | - $this->access->connection->writeToCache('userExists'.$uid, $result); |
|
| 372 | - if($result === true) { |
|
| 373 | - $user->update(); |
|
| 374 | - } |
|
| 375 | - return $result; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - /** |
|
| 379 | - * returns whether a user was deleted in LDAP |
|
| 380 | - * |
|
| 381 | - * @param string $uid The username of the user to delete |
|
| 382 | - * @return bool |
|
| 383 | - */ |
|
| 384 | - public function deleteUser($uid) { |
|
| 385 | - if ($this->userPluginManager->canDeleteUser()) { |
|
| 386 | - return $this->userPluginManager->deleteUser($uid); |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - $marked = $this->ocConfig->getUserValue($uid, 'user_ldap', 'isDeleted', 0); |
|
| 390 | - if((int)$marked === 0) { |
|
| 391 | - \OC::$server->getLogger()->notice( |
|
| 392 | - 'User '.$uid . ' is not marked as deleted, not cleaning up.', |
|
| 393 | - array('app' => 'user_ldap')); |
|
| 394 | - return false; |
|
| 395 | - } |
|
| 396 | - \OC::$server->getLogger()->info('Cleaning up after user ' . $uid, |
|
| 397 | - array('app' => 'user_ldap')); |
|
| 398 | - |
|
| 399 | - $this->access->getUserMapper()->unmap($uid); |
|
| 400 | - $this->access->userManager->invalidate($uid); |
|
| 401 | - return true; |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - /** |
|
| 405 | - * get the user's home directory |
|
| 406 | - * |
|
| 407 | - * @param string $uid the username |
|
| 408 | - * @return bool|string |
|
| 409 | - * @throws NoUserException |
|
| 410 | - * @throws \Exception |
|
| 411 | - */ |
|
| 412 | - public function getHome($uid) { |
|
| 413 | - // user Exists check required as it is not done in user proxy! |
|
| 414 | - if(!$this->userExists($uid)) { |
|
| 415 | - return false; |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - if ($this->userPluginManager->implementsActions(Backend::GET_HOME)) { |
|
| 419 | - return $this->userPluginManager->getHome($uid); |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - $cacheKey = 'getHome'.$uid; |
|
| 423 | - $path = $this->access->connection->getFromCache($cacheKey); |
|
| 424 | - if(!is_null($path)) { |
|
| 425 | - return $path; |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - // early return path if it is a deleted user |
|
| 429 | - $user = $this->access->userManager->get($uid); |
|
| 430 | - if($user instanceof OfflineUser) { |
|
| 431 | - if($this->currentUserInDeletionProcess !== null |
|
| 432 | - && $this->currentUserInDeletionProcess === $user->getOCName() |
|
| 433 | - ) { |
|
| 434 | - return $user->getHomePath(); |
|
| 435 | - } else { |
|
| 436 | - throw new NoUserException($uid . ' is not a valid user anymore'); |
|
| 437 | - } |
|
| 438 | - } else if ($user === null) { |
|
| 439 | - throw new NoUserException($uid . ' is not a valid user anymore'); |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - $path = $user->getHomePath(); |
|
| 443 | - $this->access->cacheUserHome($uid, $path); |
|
| 444 | - |
|
| 445 | - return $path; |
|
| 446 | - } |
|
| 447 | - |
|
| 448 | - /** |
|
| 449 | - * get display name of the user |
|
| 450 | - * @param string $uid user ID of the user |
|
| 451 | - * @return string|false display name |
|
| 452 | - */ |
|
| 453 | - public function getDisplayName($uid) { |
|
| 454 | - if ($this->userPluginManager->implementsActions(Backend::GET_DISPLAYNAME)) { |
|
| 455 | - return $this->userPluginManager->getDisplayName($uid); |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - if(!$this->userExists($uid)) { |
|
| 459 | - return false; |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - $cacheKey = 'getDisplayName'.$uid; |
|
| 463 | - if(!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { |
|
| 464 | - return $displayName; |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - //Check whether the display name is configured to have a 2nd feature |
|
| 468 | - $additionalAttribute = $this->access->connection->ldapUserDisplayName2; |
|
| 469 | - $displayName2 = ''; |
|
| 470 | - if ($additionalAttribute !== '') { |
|
| 471 | - $displayName2 = $this->access->readAttribute( |
|
| 472 | - $this->access->username2dn($uid), |
|
| 473 | - $additionalAttribute); |
|
| 474 | - } |
|
| 475 | - |
|
| 476 | - $displayName = $this->access->readAttribute( |
|
| 477 | - $this->access->username2dn($uid), |
|
| 478 | - $this->access->connection->ldapUserDisplayName); |
|
| 479 | - |
|
| 480 | - if($displayName && (count($displayName) > 0)) { |
|
| 481 | - $displayName = $displayName[0]; |
|
| 482 | - |
|
| 483 | - if (is_array($displayName2)){ |
|
| 484 | - $displayName2 = count($displayName2) > 0 ? $displayName2[0] : ''; |
|
| 485 | - } |
|
| 486 | - |
|
| 487 | - $user = $this->access->userManager->get($uid); |
|
| 488 | - if ($user instanceof User) { |
|
| 489 | - $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2); |
|
| 490 | - $this->access->connection->writeToCache($cacheKey, $displayName); |
|
| 491 | - } |
|
| 492 | - if ($user instanceof OfflineUser) { |
|
| 493 | - /** @var OfflineUser $user*/ |
|
| 494 | - $displayName = $user->getDisplayName(); |
|
| 495 | - } |
|
| 496 | - return $displayName; |
|
| 497 | - } |
|
| 498 | - |
|
| 499 | - return null; |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - /** |
|
| 503 | - * set display name of the user |
|
| 504 | - * @param string $uid user ID of the user |
|
| 505 | - * @param string $displayName new display name of the user |
|
| 506 | - * @return string|false display name |
|
| 507 | - */ |
|
| 508 | - public function setDisplayName($uid, $displayName) { |
|
| 509 | - if ($this->userPluginManager->implementsActions(Backend::SET_DISPLAYNAME)) { |
|
| 510 | - return $this->userPluginManager->setDisplayName($uid, $displayName); |
|
| 511 | - } |
|
| 512 | - return false; |
|
| 513 | - } |
|
| 514 | - |
|
| 515 | - /** |
|
| 516 | - * Get a list of all display names |
|
| 517 | - * |
|
| 518 | - * @param string $search |
|
| 519 | - * @param string|null $limit |
|
| 520 | - * @param string|null $offset |
|
| 521 | - * @return array an array of all displayNames (value) and the corresponding uids (key) |
|
| 522 | - */ |
|
| 523 | - public function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
| 524 | - $cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset; |
|
| 525 | - if(!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) { |
|
| 526 | - return $displayNames; |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - $displayNames = array(); |
|
| 530 | - $users = $this->getUsers($search, $limit, $offset); |
|
| 531 | - foreach ($users as $user) { |
|
| 532 | - $displayNames[$user] = $this->getDisplayName($user); |
|
| 533 | - } |
|
| 534 | - $this->access->connection->writeToCache($cacheKey, $displayNames); |
|
| 535 | - return $displayNames; |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - /** |
|
| 539 | - * Check if backend implements actions |
|
| 540 | - * @param int $actions bitwise-or'ed actions |
|
| 541 | - * @return boolean |
|
| 542 | - * |
|
| 543 | - * Returns the supported actions as int to be |
|
| 544 | - * compared with \OC\User\Backend::CREATE_USER etc. |
|
| 545 | - */ |
|
| 546 | - public function implementsActions($actions) { |
|
| 547 | - return (bool)((Backend::CHECK_PASSWORD |
|
| 548 | - | Backend::GET_HOME |
|
| 549 | - | Backend::GET_DISPLAYNAME |
|
| 550 | - | Backend::PROVIDE_AVATAR |
|
| 551 | - | Backend::COUNT_USERS |
|
| 552 | - | (((int)$this->access->connection->turnOnPasswordChange === 1)?(Backend::SET_PASSWORD):0) |
|
| 553 | - | $this->userPluginManager->getImplementedActions()) |
|
| 554 | - & $actions); |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - /** |
|
| 558 | - * @return bool |
|
| 559 | - */ |
|
| 560 | - public function hasUserListings() { |
|
| 561 | - return true; |
|
| 562 | - } |
|
| 563 | - |
|
| 564 | - /** |
|
| 565 | - * counts the users in LDAP |
|
| 566 | - * |
|
| 567 | - * @return int|bool |
|
| 568 | - */ |
|
| 569 | - public function countUsers() { |
|
| 570 | - if ($this->userPluginManager->implementsActions(Backend::COUNT_USERS)) { |
|
| 571 | - return $this->userPluginManager->countUsers(); |
|
| 572 | - } |
|
| 573 | - |
|
| 574 | - $filter = $this->access->getFilterForUserCount(); |
|
| 575 | - $cacheKey = 'countUsers-'.$filter; |
|
| 576 | - if(!is_null($entries = $this->access->connection->getFromCache($cacheKey))) { |
|
| 577 | - return $entries; |
|
| 578 | - } |
|
| 579 | - $entries = $this->access->countUsers($filter); |
|
| 580 | - $this->access->connection->writeToCache($cacheKey, $entries); |
|
| 581 | - return $entries; |
|
| 582 | - } |
|
| 583 | - |
|
| 584 | - /** |
|
| 585 | - * Backend name to be shown in user management |
|
| 586 | - * @return string the name of the backend to be shown |
|
| 587 | - */ |
|
| 588 | - public function getBackendName(){ |
|
| 589 | - return 'LDAP'; |
|
| 590 | - } |
|
| 145 | + /** |
|
| 146 | + * returns the username for the given LDAP DN, if available |
|
| 147 | + * |
|
| 148 | + * @param string $dn |
|
| 149 | + * @return string|false with the username |
|
| 150 | + */ |
|
| 151 | + public function dn2UserName($dn) { |
|
| 152 | + return $this->access->dn2username($dn); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * returns an LDAP record based on a given login name |
|
| 157 | + * |
|
| 158 | + * @param string $loginName |
|
| 159 | + * @return array |
|
| 160 | + * @throws NotOnLDAP |
|
| 161 | + */ |
|
| 162 | + public function getLDAPUserByLoginName($loginName) { |
|
| 163 | + //find out dn of the user name |
|
| 164 | + $attrs = $this->access->userManager->getAttributes(); |
|
| 165 | + $users = $this->access->fetchUsersByLoginName($loginName, $attrs); |
|
| 166 | + if(count($users) < 1) { |
|
| 167 | + throw new NotOnLDAP('No user available for the given login name on ' . |
|
| 168 | + $this->access->connection->ldapHost . ':' . $this->access->connection->ldapPort); |
|
| 169 | + } |
|
| 170 | + return $users[0]; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * Check if the password is correct without logging in the user |
|
| 175 | + * |
|
| 176 | + * @param string $uid The username |
|
| 177 | + * @param string $password The password |
|
| 178 | + * @return false|string |
|
| 179 | + */ |
|
| 180 | + public function checkPassword($uid, $password) { |
|
| 181 | + try { |
|
| 182 | + $ldapRecord = $this->getLDAPUserByLoginName($uid); |
|
| 183 | + } catch(NotOnLDAP $e) { |
|
| 184 | + if($this->ocConfig->getSystemValue('loglevel', Util::WARN) === Util::DEBUG) { |
|
| 185 | + \OC::$server->getLogger()->logException($e, ['app' => 'user_ldap']); |
|
| 186 | + } |
|
| 187 | + return false; |
|
| 188 | + } |
|
| 189 | + $dn = $ldapRecord['dn'][0]; |
|
| 190 | + $user = $this->access->userManager->get($dn); |
|
| 191 | + |
|
| 192 | + if(!$user instanceof User) { |
|
| 193 | + Util::writeLog('user_ldap', |
|
| 194 | + 'LDAP Login: Could not get user object for DN ' . $dn . |
|
| 195 | + '. Maybe the LDAP entry has no set display name attribute?', |
|
| 196 | + Util::WARN); |
|
| 197 | + return false; |
|
| 198 | + } |
|
| 199 | + if($user->getUsername() !== false) { |
|
| 200 | + //are the credentials OK? |
|
| 201 | + if(!$this->access->areCredentialsValid($dn, $password)) { |
|
| 202 | + return false; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + $this->access->cacheUserExists($user->getUsername()); |
|
| 206 | + $user->processAttributes($ldapRecord); |
|
| 207 | + $user->markLogin(); |
|
| 208 | + |
|
| 209 | + return $user->getUsername(); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + return false; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + /** |
|
| 216 | + * Set password |
|
| 217 | + * @param string $uid The username |
|
| 218 | + * @param string $password The new password |
|
| 219 | + * @return bool |
|
| 220 | + */ |
|
| 221 | + public function setPassword($uid, $password) { |
|
| 222 | + if ($this->userPluginManager->implementsActions(Backend::SET_PASSWORD)) { |
|
| 223 | + return $this->userPluginManager->setPassword($uid, $password); |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + $user = $this->access->userManager->get($uid); |
|
| 227 | + |
|
| 228 | + if(!$user instanceof User) { |
|
| 229 | + throw new \Exception('LDAP setPassword: Could not get user object for uid ' . $uid . |
|
| 230 | + '. Maybe the LDAP entry has no set display name attribute?'); |
|
| 231 | + } |
|
| 232 | + if($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) { |
|
| 233 | + $ldapDefaultPPolicyDN = $this->access->connection->ldapDefaultPPolicyDN; |
|
| 234 | + $turnOnPasswordChange = $this->access->connection->turnOnPasswordChange; |
|
| 235 | + if (!empty($ldapDefaultPPolicyDN) && ((int)$turnOnPasswordChange === 1)) { |
|
| 236 | + //remove last password expiry warning if any |
|
| 237 | + $notification = $this->notificationManager->createNotification(); |
|
| 238 | + $notification->setApp('user_ldap') |
|
| 239 | + ->setUser($uid) |
|
| 240 | + ->setObject('pwd_exp_warn', $uid) |
|
| 241 | + ; |
|
| 242 | + $this->notificationManager->markProcessed($notification); |
|
| 243 | + } |
|
| 244 | + return true; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + return false; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * Get a list of all users |
|
| 252 | + * |
|
| 253 | + * @param string $search |
|
| 254 | + * @param integer $limit |
|
| 255 | + * @param integer $offset |
|
| 256 | + * @return string[] an array of all uids |
|
| 257 | + */ |
|
| 258 | + public function getUsers($search = '', $limit = 10, $offset = 0) { |
|
| 259 | + $search = $this->access->escapeFilterPart($search, true); |
|
| 260 | + $cachekey = 'getUsers-'.$search.'-'.$limit.'-'.$offset; |
|
| 261 | + |
|
| 262 | + //check if users are cached, if so return |
|
| 263 | + $ldap_users = $this->access->connection->getFromCache($cachekey); |
|
| 264 | + if(!is_null($ldap_users)) { |
|
| 265 | + return $ldap_users; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
|
| 269 | + // error. With a limit of 0, we get 0 results. So we pass null. |
|
| 270 | + if($limit <= 0) { |
|
| 271 | + $limit = null; |
|
| 272 | + } |
|
| 273 | + $filter = $this->access->combineFilterWithAnd(array( |
|
| 274 | + $this->access->connection->ldapUserFilter, |
|
| 275 | + $this->access->connection->ldapUserDisplayName . '=*', |
|
| 276 | + $this->access->getFilterPartForUserSearch($search) |
|
| 277 | + )); |
|
| 278 | + |
|
| 279 | + Util::writeLog('user_ldap', |
|
| 280 | + 'getUsers: Options: search '.$search.' limit '.$limit.' offset '.$offset.' Filter: '.$filter, |
|
| 281 | + Util::DEBUG); |
|
| 282 | + //do the search and translate results to Nextcloud names |
|
| 283 | + $ldap_users = $this->access->fetchListOfUsers( |
|
| 284 | + $filter, |
|
| 285 | + $this->access->userManager->getAttributes(true), |
|
| 286 | + $limit, $offset); |
|
| 287 | + $ldap_users = $this->access->nextcloudUserNames($ldap_users); |
|
| 288 | + Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', Util::DEBUG); |
|
| 289 | + |
|
| 290 | + $this->access->connection->writeToCache($cachekey, $ldap_users); |
|
| 291 | + return $ldap_users; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * checks whether a user is still available on LDAP |
|
| 296 | + * |
|
| 297 | + * @param string|\OCA\User_LDAP\User\User $user either the Nextcloud user |
|
| 298 | + * name or an instance of that user |
|
| 299 | + * @return bool |
|
| 300 | + * @throws \Exception |
|
| 301 | + * @throws \OC\ServerNotAvailableException |
|
| 302 | + */ |
|
| 303 | + public function userExistsOnLDAP($user) { |
|
| 304 | + if(is_string($user)) { |
|
| 305 | + $user = $this->access->userManager->get($user); |
|
| 306 | + } |
|
| 307 | + if(is_null($user)) { |
|
| 308 | + return false; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + $dn = $user->getDN(); |
|
| 312 | + //check if user really still exists by reading its entry |
|
| 313 | + if(!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) { |
|
| 314 | + $lcr = $this->access->connection->getConnectionResource(); |
|
| 315 | + if(is_null($lcr)) { |
|
| 316 | + throw new \Exception('No LDAP Connection to server ' . $this->access->connection->ldapHost); |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + try { |
|
| 320 | + $uuid = $this->access->getUserMapper()->getUUIDByDN($dn); |
|
| 321 | + if (!$uuid) { |
|
| 322 | + return false; |
|
| 323 | + } |
|
| 324 | + $newDn = $this->access->getUserDnByUuid($uuid); |
|
| 325 | + //check if renamed user is still valid by reapplying the ldap filter |
|
| 326 | + if (!is_array($this->access->readAttribute($newDn, '', $this->access->connection->ldapUserFilter))) { |
|
| 327 | + return false; |
|
| 328 | + } |
|
| 329 | + $this->access->getUserMapper()->setDNbyUUID($newDn, $uuid); |
|
| 330 | + return true; |
|
| 331 | + } catch (ServerNotAvailableException $e) { |
|
| 332 | + throw $e; |
|
| 333 | + } catch (\Exception $e) { |
|
| 334 | + return false; |
|
| 335 | + } |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + if($user instanceof OfflineUser) { |
|
| 339 | + $user->unmark(); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + return true; |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * check if a user exists |
|
| 347 | + * @param string $uid the username |
|
| 348 | + * @return boolean |
|
| 349 | + * @throws \Exception when connection could not be established |
|
| 350 | + */ |
|
| 351 | + public function userExists($uid) { |
|
| 352 | + $userExists = $this->access->connection->getFromCache('userExists'.$uid); |
|
| 353 | + if(!is_null($userExists)) { |
|
| 354 | + return (bool)$userExists; |
|
| 355 | + } |
|
| 356 | + //getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking. |
|
| 357 | + $user = $this->access->userManager->get($uid); |
|
| 358 | + |
|
| 359 | + if(is_null($user)) { |
|
| 360 | + Util::writeLog('user_ldap', 'No DN found for '.$uid.' on '. |
|
| 361 | + $this->access->connection->ldapHost, Util::DEBUG); |
|
| 362 | + $this->access->connection->writeToCache('userExists'.$uid, false); |
|
| 363 | + return false; |
|
| 364 | + } else if($user instanceof OfflineUser) { |
|
| 365 | + //express check for users marked as deleted. Returning true is |
|
| 366 | + //necessary for cleanup |
|
| 367 | + return true; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + $result = $this->userExistsOnLDAP($user); |
|
| 371 | + $this->access->connection->writeToCache('userExists'.$uid, $result); |
|
| 372 | + if($result === true) { |
|
| 373 | + $user->update(); |
|
| 374 | + } |
|
| 375 | + return $result; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + /** |
|
| 379 | + * returns whether a user was deleted in LDAP |
|
| 380 | + * |
|
| 381 | + * @param string $uid The username of the user to delete |
|
| 382 | + * @return bool |
|
| 383 | + */ |
|
| 384 | + public function deleteUser($uid) { |
|
| 385 | + if ($this->userPluginManager->canDeleteUser()) { |
|
| 386 | + return $this->userPluginManager->deleteUser($uid); |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + $marked = $this->ocConfig->getUserValue($uid, 'user_ldap', 'isDeleted', 0); |
|
| 390 | + if((int)$marked === 0) { |
|
| 391 | + \OC::$server->getLogger()->notice( |
|
| 392 | + 'User '.$uid . ' is not marked as deleted, not cleaning up.', |
|
| 393 | + array('app' => 'user_ldap')); |
|
| 394 | + return false; |
|
| 395 | + } |
|
| 396 | + \OC::$server->getLogger()->info('Cleaning up after user ' . $uid, |
|
| 397 | + array('app' => 'user_ldap')); |
|
| 398 | + |
|
| 399 | + $this->access->getUserMapper()->unmap($uid); |
|
| 400 | + $this->access->userManager->invalidate($uid); |
|
| 401 | + return true; |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + /** |
|
| 405 | + * get the user's home directory |
|
| 406 | + * |
|
| 407 | + * @param string $uid the username |
|
| 408 | + * @return bool|string |
|
| 409 | + * @throws NoUserException |
|
| 410 | + * @throws \Exception |
|
| 411 | + */ |
|
| 412 | + public function getHome($uid) { |
|
| 413 | + // user Exists check required as it is not done in user proxy! |
|
| 414 | + if(!$this->userExists($uid)) { |
|
| 415 | + return false; |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + if ($this->userPluginManager->implementsActions(Backend::GET_HOME)) { |
|
| 419 | + return $this->userPluginManager->getHome($uid); |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + $cacheKey = 'getHome'.$uid; |
|
| 423 | + $path = $this->access->connection->getFromCache($cacheKey); |
|
| 424 | + if(!is_null($path)) { |
|
| 425 | + return $path; |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + // early return path if it is a deleted user |
|
| 429 | + $user = $this->access->userManager->get($uid); |
|
| 430 | + if($user instanceof OfflineUser) { |
|
| 431 | + if($this->currentUserInDeletionProcess !== null |
|
| 432 | + && $this->currentUserInDeletionProcess === $user->getOCName() |
|
| 433 | + ) { |
|
| 434 | + return $user->getHomePath(); |
|
| 435 | + } else { |
|
| 436 | + throw new NoUserException($uid . ' is not a valid user anymore'); |
|
| 437 | + } |
|
| 438 | + } else if ($user === null) { |
|
| 439 | + throw new NoUserException($uid . ' is not a valid user anymore'); |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + $path = $user->getHomePath(); |
|
| 443 | + $this->access->cacheUserHome($uid, $path); |
|
| 444 | + |
|
| 445 | + return $path; |
|
| 446 | + } |
|
| 447 | + |
|
| 448 | + /** |
|
| 449 | + * get display name of the user |
|
| 450 | + * @param string $uid user ID of the user |
|
| 451 | + * @return string|false display name |
|
| 452 | + */ |
|
| 453 | + public function getDisplayName($uid) { |
|
| 454 | + if ($this->userPluginManager->implementsActions(Backend::GET_DISPLAYNAME)) { |
|
| 455 | + return $this->userPluginManager->getDisplayName($uid); |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + if(!$this->userExists($uid)) { |
|
| 459 | + return false; |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + $cacheKey = 'getDisplayName'.$uid; |
|
| 463 | + if(!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { |
|
| 464 | + return $displayName; |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + //Check whether the display name is configured to have a 2nd feature |
|
| 468 | + $additionalAttribute = $this->access->connection->ldapUserDisplayName2; |
|
| 469 | + $displayName2 = ''; |
|
| 470 | + if ($additionalAttribute !== '') { |
|
| 471 | + $displayName2 = $this->access->readAttribute( |
|
| 472 | + $this->access->username2dn($uid), |
|
| 473 | + $additionalAttribute); |
|
| 474 | + } |
|
| 475 | + |
|
| 476 | + $displayName = $this->access->readAttribute( |
|
| 477 | + $this->access->username2dn($uid), |
|
| 478 | + $this->access->connection->ldapUserDisplayName); |
|
| 479 | + |
|
| 480 | + if($displayName && (count($displayName) > 0)) { |
|
| 481 | + $displayName = $displayName[0]; |
|
| 482 | + |
|
| 483 | + if (is_array($displayName2)){ |
|
| 484 | + $displayName2 = count($displayName2) > 0 ? $displayName2[0] : ''; |
|
| 485 | + } |
|
| 486 | + |
|
| 487 | + $user = $this->access->userManager->get($uid); |
|
| 488 | + if ($user instanceof User) { |
|
| 489 | + $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2); |
|
| 490 | + $this->access->connection->writeToCache($cacheKey, $displayName); |
|
| 491 | + } |
|
| 492 | + if ($user instanceof OfflineUser) { |
|
| 493 | + /** @var OfflineUser $user*/ |
|
| 494 | + $displayName = $user->getDisplayName(); |
|
| 495 | + } |
|
| 496 | + return $displayName; |
|
| 497 | + } |
|
| 498 | + |
|
| 499 | + return null; |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + /** |
|
| 503 | + * set display name of the user |
|
| 504 | + * @param string $uid user ID of the user |
|
| 505 | + * @param string $displayName new display name of the user |
|
| 506 | + * @return string|false display name |
|
| 507 | + */ |
|
| 508 | + public function setDisplayName($uid, $displayName) { |
|
| 509 | + if ($this->userPluginManager->implementsActions(Backend::SET_DISPLAYNAME)) { |
|
| 510 | + return $this->userPluginManager->setDisplayName($uid, $displayName); |
|
| 511 | + } |
|
| 512 | + return false; |
|
| 513 | + } |
|
| 514 | + |
|
| 515 | + /** |
|
| 516 | + * Get a list of all display names |
|
| 517 | + * |
|
| 518 | + * @param string $search |
|
| 519 | + * @param string|null $limit |
|
| 520 | + * @param string|null $offset |
|
| 521 | + * @return array an array of all displayNames (value) and the corresponding uids (key) |
|
| 522 | + */ |
|
| 523 | + public function getDisplayNames($search = '', $limit = null, $offset = null) { |
|
| 524 | + $cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset; |
|
| 525 | + if(!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) { |
|
| 526 | + return $displayNames; |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + $displayNames = array(); |
|
| 530 | + $users = $this->getUsers($search, $limit, $offset); |
|
| 531 | + foreach ($users as $user) { |
|
| 532 | + $displayNames[$user] = $this->getDisplayName($user); |
|
| 533 | + } |
|
| 534 | + $this->access->connection->writeToCache($cacheKey, $displayNames); |
|
| 535 | + return $displayNames; |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + /** |
|
| 539 | + * Check if backend implements actions |
|
| 540 | + * @param int $actions bitwise-or'ed actions |
|
| 541 | + * @return boolean |
|
| 542 | + * |
|
| 543 | + * Returns the supported actions as int to be |
|
| 544 | + * compared with \OC\User\Backend::CREATE_USER etc. |
|
| 545 | + */ |
|
| 546 | + public function implementsActions($actions) { |
|
| 547 | + return (bool)((Backend::CHECK_PASSWORD |
|
| 548 | + | Backend::GET_HOME |
|
| 549 | + | Backend::GET_DISPLAYNAME |
|
| 550 | + | Backend::PROVIDE_AVATAR |
|
| 551 | + | Backend::COUNT_USERS |
|
| 552 | + | (((int)$this->access->connection->turnOnPasswordChange === 1)?(Backend::SET_PASSWORD):0) |
|
| 553 | + | $this->userPluginManager->getImplementedActions()) |
|
| 554 | + & $actions); |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + /** |
|
| 558 | + * @return bool |
|
| 559 | + */ |
|
| 560 | + public function hasUserListings() { |
|
| 561 | + return true; |
|
| 562 | + } |
|
| 563 | + |
|
| 564 | + /** |
|
| 565 | + * counts the users in LDAP |
|
| 566 | + * |
|
| 567 | + * @return int|bool |
|
| 568 | + */ |
|
| 569 | + public function countUsers() { |
|
| 570 | + if ($this->userPluginManager->implementsActions(Backend::COUNT_USERS)) { |
|
| 571 | + return $this->userPluginManager->countUsers(); |
|
| 572 | + } |
|
| 573 | + |
|
| 574 | + $filter = $this->access->getFilterForUserCount(); |
|
| 575 | + $cacheKey = 'countUsers-'.$filter; |
|
| 576 | + if(!is_null($entries = $this->access->connection->getFromCache($cacheKey))) { |
|
| 577 | + return $entries; |
|
| 578 | + } |
|
| 579 | + $entries = $this->access->countUsers($filter); |
|
| 580 | + $this->access->connection->writeToCache($cacheKey, $entries); |
|
| 581 | + return $entries; |
|
| 582 | + } |
|
| 583 | + |
|
| 584 | + /** |
|
| 585 | + * Backend name to be shown in user management |
|
| 586 | + * @return string the name of the backend to be shown |
|
| 587 | + */ |
|
| 588 | + public function getBackendName(){ |
|
| 589 | + return 'LDAP'; |
|
| 590 | + } |
|
| 591 | 591 | |
| 592 | - /** |
|
| 593 | - * Return access for LDAP interaction. |
|
| 594 | - * @param string $uid |
|
| 595 | - * @return Access instance of Access for LDAP interaction |
|
| 596 | - */ |
|
| 597 | - public function getLDAPAccess($uid) { |
|
| 598 | - return $this->access; |
|
| 599 | - } |
|
| 592 | + /** |
|
| 593 | + * Return access for LDAP interaction. |
|
| 594 | + * @param string $uid |
|
| 595 | + * @return Access instance of Access for LDAP interaction |
|
| 596 | + */ |
|
| 597 | + public function getLDAPAccess($uid) { |
|
| 598 | + return $this->access; |
|
| 599 | + } |
|
| 600 | 600 | |
| 601 | - /** |
|
| 602 | - * Return LDAP connection resource from a cloned connection. |
|
| 603 | - * The cloned connection needs to be closed manually. |
|
| 604 | - * of the current access. |
|
| 605 | - * @param string $uid |
|
| 606 | - * @return resource of the LDAP connection |
|
| 607 | - */ |
|
| 608 | - public function getNewLDAPConnection($uid) { |
|
| 609 | - $connection = clone $this->access->getConnection(); |
|
| 610 | - return $connection->getConnectionResource(); |
|
| 611 | - } |
|
| 612 | - |
|
| 613 | - /** |
|
| 614 | - * create new user |
|
| 615 | - * @param string $username username of the new user |
|
| 616 | - * @param string $password password of the new user |
|
| 617 | - * @return bool was the user created? |
|
| 618 | - */ |
|
| 619 | - public function createUser($username, $password) { |
|
| 620 | - if ($this->userPluginManager->implementsActions(Backend::CREATE_USER)) { |
|
| 621 | - return $this->userPluginManager->createUser($username, $password); |
|
| 622 | - } |
|
| 623 | - return false; |
|
| 624 | - } |
|
| 601 | + /** |
|
| 602 | + * Return LDAP connection resource from a cloned connection. |
|
| 603 | + * The cloned connection needs to be closed manually. |
|
| 604 | + * of the current access. |
|
| 605 | + * @param string $uid |
|
| 606 | + * @return resource of the LDAP connection |
|
| 607 | + */ |
|
| 608 | + public function getNewLDAPConnection($uid) { |
|
| 609 | + $connection = clone $this->access->getConnection(); |
|
| 610 | + return $connection->getConnectionResource(); |
|
| 611 | + } |
|
| 612 | + |
|
| 613 | + /** |
|
| 614 | + * create new user |
|
| 615 | + * @param string $username username of the new user |
|
| 616 | + * @param string $password password of the new user |
|
| 617 | + * @return bool was the user created? |
|
| 618 | + */ |
|
| 619 | + public function createUser($username, $password) { |
|
| 620 | + if ($this->userPluginManager->implementsActions(Backend::CREATE_USER)) { |
|
| 621 | + return $this->userPluginManager->createUser($username, $password); |
|
| 622 | + } |
|
| 623 | + return false; |
|
| 624 | + } |
|
| 625 | 625 | |
| 626 | 626 | } |
@@ -101,10 +101,10 @@ discard block |
||
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | $user = $this->access->userManager->get($uid); |
| 104 | - if(!$user instanceof User) { |
|
| 104 | + if (!$user instanceof User) { |
|
| 105 | 105 | return false; |
| 106 | 106 | } |
| 107 | - if($user->getAvatarImage() === false) { |
|
| 107 | + if ($user->getAvatarImage() === false) { |
|
| 108 | 108 | return true; |
| 109 | 109 | } |
| 110 | 110 | |
@@ -120,14 +120,14 @@ discard block |
||
| 120 | 120 | public function loginName2UserName($loginName) { |
| 121 | 121 | $cacheKey = 'loginName2UserName-'.$loginName; |
| 122 | 122 | $username = $this->access->connection->getFromCache($cacheKey); |
| 123 | - if(!is_null($username)) { |
|
| 123 | + if (!is_null($username)) { |
|
| 124 | 124 | return $username; |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | try { |
| 128 | 128 | $ldapRecord = $this->getLDAPUserByLoginName($loginName); |
| 129 | 129 | $user = $this->access->userManager->get($ldapRecord['dn'][0]); |
| 130 | - if($user instanceof OfflineUser) { |
|
| 130 | + if ($user instanceof OfflineUser) { |
|
| 131 | 131 | // this path is not really possible, however get() is documented |
| 132 | 132 | // to return User or OfflineUser so we are very defensive here. |
| 133 | 133 | $this->access->connection->writeToCache($cacheKey, false); |
@@ -163,9 +163,9 @@ discard block |
||
| 163 | 163 | //find out dn of the user name |
| 164 | 164 | $attrs = $this->access->userManager->getAttributes(); |
| 165 | 165 | $users = $this->access->fetchUsersByLoginName($loginName, $attrs); |
| 166 | - if(count($users) < 1) { |
|
| 167 | - throw new NotOnLDAP('No user available for the given login name on ' . |
|
| 168 | - $this->access->connection->ldapHost . ':' . $this->access->connection->ldapPort); |
|
| 166 | + if (count($users) < 1) { |
|
| 167 | + throw new NotOnLDAP('No user available for the given login name on '. |
|
| 168 | + $this->access->connection->ldapHost.':'.$this->access->connection->ldapPort); |
|
| 169 | 169 | } |
| 170 | 170 | return $users[0]; |
| 171 | 171 | } |
@@ -180,8 +180,8 @@ discard block |
||
| 180 | 180 | public function checkPassword($uid, $password) { |
| 181 | 181 | try { |
| 182 | 182 | $ldapRecord = $this->getLDAPUserByLoginName($uid); |
| 183 | - } catch(NotOnLDAP $e) { |
|
| 184 | - if($this->ocConfig->getSystemValue('loglevel', Util::WARN) === Util::DEBUG) { |
|
| 183 | + } catch (NotOnLDAP $e) { |
|
| 184 | + if ($this->ocConfig->getSystemValue('loglevel', Util::WARN) === Util::DEBUG) { |
|
| 185 | 185 | \OC::$server->getLogger()->logException($e, ['app' => 'user_ldap']); |
| 186 | 186 | } |
| 187 | 187 | return false; |
@@ -189,16 +189,16 @@ discard block |
||
| 189 | 189 | $dn = $ldapRecord['dn'][0]; |
| 190 | 190 | $user = $this->access->userManager->get($dn); |
| 191 | 191 | |
| 192 | - if(!$user instanceof User) { |
|
| 192 | + if (!$user instanceof User) { |
|
| 193 | 193 | Util::writeLog('user_ldap', |
| 194 | - 'LDAP Login: Could not get user object for DN ' . $dn . |
|
| 194 | + 'LDAP Login: Could not get user object for DN '.$dn. |
|
| 195 | 195 | '. Maybe the LDAP entry has no set display name attribute?', |
| 196 | 196 | Util::WARN); |
| 197 | 197 | return false; |
| 198 | 198 | } |
| 199 | - if($user->getUsername() !== false) { |
|
| 199 | + if ($user->getUsername() !== false) { |
|
| 200 | 200 | //are the credentials OK? |
| 201 | - if(!$this->access->areCredentialsValid($dn, $password)) { |
|
| 201 | + if (!$this->access->areCredentialsValid($dn, $password)) { |
|
| 202 | 202 | return false; |
| 203 | 203 | } |
| 204 | 204 | |
@@ -225,14 +225,14 @@ discard block |
||
| 225 | 225 | |
| 226 | 226 | $user = $this->access->userManager->get($uid); |
| 227 | 227 | |
| 228 | - if(!$user instanceof User) { |
|
| 229 | - throw new \Exception('LDAP setPassword: Could not get user object for uid ' . $uid . |
|
| 228 | + if (!$user instanceof User) { |
|
| 229 | + throw new \Exception('LDAP setPassword: Could not get user object for uid '.$uid. |
|
| 230 | 230 | '. Maybe the LDAP entry has no set display name attribute?'); |
| 231 | 231 | } |
| 232 | - if($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) { |
|
| 232 | + if ($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) { |
|
| 233 | 233 | $ldapDefaultPPolicyDN = $this->access->connection->ldapDefaultPPolicyDN; |
| 234 | 234 | $turnOnPasswordChange = $this->access->connection->turnOnPasswordChange; |
| 235 | - if (!empty($ldapDefaultPPolicyDN) && ((int)$turnOnPasswordChange === 1)) { |
|
| 235 | + if (!empty($ldapDefaultPPolicyDN) && ((int) $turnOnPasswordChange === 1)) { |
|
| 236 | 236 | //remove last password expiry warning if any |
| 237 | 237 | $notification = $this->notificationManager->createNotification(); |
| 238 | 238 | $notification->setApp('user_ldap') |
@@ -261,18 +261,18 @@ discard block |
||
| 261 | 261 | |
| 262 | 262 | //check if users are cached, if so return |
| 263 | 263 | $ldap_users = $this->access->connection->getFromCache($cachekey); |
| 264 | - if(!is_null($ldap_users)) { |
|
| 264 | + if (!is_null($ldap_users)) { |
|
| 265 | 265 | return $ldap_users; |
| 266 | 266 | } |
| 267 | 267 | |
| 268 | 268 | // if we'd pass -1 to LDAP search, we'd end up in a Protocol |
| 269 | 269 | // error. With a limit of 0, we get 0 results. So we pass null. |
| 270 | - if($limit <= 0) { |
|
| 270 | + if ($limit <= 0) { |
|
| 271 | 271 | $limit = null; |
| 272 | 272 | } |
| 273 | 273 | $filter = $this->access->combineFilterWithAnd(array( |
| 274 | 274 | $this->access->connection->ldapUserFilter, |
| 275 | - $this->access->connection->ldapUserDisplayName . '=*', |
|
| 275 | + $this->access->connection->ldapUserDisplayName.'=*', |
|
| 276 | 276 | $this->access->getFilterPartForUserSearch($search) |
| 277 | 277 | )); |
| 278 | 278 | |
@@ -285,7 +285,7 @@ discard block |
||
| 285 | 285 | $this->access->userManager->getAttributes(true), |
| 286 | 286 | $limit, $offset); |
| 287 | 287 | $ldap_users = $this->access->nextcloudUserNames($ldap_users); |
| 288 | - Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', Util::DEBUG); |
|
| 288 | + Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users).' Users found', Util::DEBUG); |
|
| 289 | 289 | |
| 290 | 290 | $this->access->connection->writeToCache($cachekey, $ldap_users); |
| 291 | 291 | return $ldap_users; |
@@ -301,19 +301,19 @@ discard block |
||
| 301 | 301 | * @throws \OC\ServerNotAvailableException |
| 302 | 302 | */ |
| 303 | 303 | public function userExistsOnLDAP($user) { |
| 304 | - if(is_string($user)) { |
|
| 304 | + if (is_string($user)) { |
|
| 305 | 305 | $user = $this->access->userManager->get($user); |
| 306 | 306 | } |
| 307 | - if(is_null($user)) { |
|
| 307 | + if (is_null($user)) { |
|
| 308 | 308 | return false; |
| 309 | 309 | } |
| 310 | 310 | |
| 311 | 311 | $dn = $user->getDN(); |
| 312 | 312 | //check if user really still exists by reading its entry |
| 313 | - if(!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) { |
|
| 313 | + if (!is_array($this->access->readAttribute($dn, '', $this->access->connection->ldapUserFilter))) { |
|
| 314 | 314 | $lcr = $this->access->connection->getConnectionResource(); |
| 315 | - if(is_null($lcr)) { |
|
| 316 | - throw new \Exception('No LDAP Connection to server ' . $this->access->connection->ldapHost); |
|
| 315 | + if (is_null($lcr)) { |
|
| 316 | + throw new \Exception('No LDAP Connection to server '.$this->access->connection->ldapHost); |
|
| 317 | 317 | } |
| 318 | 318 | |
| 319 | 319 | try { |
@@ -335,7 +335,7 @@ discard block |
||
| 335 | 335 | } |
| 336 | 336 | } |
| 337 | 337 | |
| 338 | - if($user instanceof OfflineUser) { |
|
| 338 | + if ($user instanceof OfflineUser) { |
|
| 339 | 339 | $user->unmark(); |
| 340 | 340 | } |
| 341 | 341 | |
@@ -350,18 +350,18 @@ discard block |
||
| 350 | 350 | */ |
| 351 | 351 | public function userExists($uid) { |
| 352 | 352 | $userExists = $this->access->connection->getFromCache('userExists'.$uid); |
| 353 | - if(!is_null($userExists)) { |
|
| 354 | - return (bool)$userExists; |
|
| 353 | + if (!is_null($userExists)) { |
|
| 354 | + return (bool) $userExists; |
|
| 355 | 355 | } |
| 356 | 356 | //getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking. |
| 357 | 357 | $user = $this->access->userManager->get($uid); |
| 358 | 358 | |
| 359 | - if(is_null($user)) { |
|
| 359 | + if (is_null($user)) { |
|
| 360 | 360 | Util::writeLog('user_ldap', 'No DN found for '.$uid.' on '. |
| 361 | 361 | $this->access->connection->ldapHost, Util::DEBUG); |
| 362 | 362 | $this->access->connection->writeToCache('userExists'.$uid, false); |
| 363 | 363 | return false; |
| 364 | - } else if($user instanceof OfflineUser) { |
|
| 364 | + } else if ($user instanceof OfflineUser) { |
|
| 365 | 365 | //express check for users marked as deleted. Returning true is |
| 366 | 366 | //necessary for cleanup |
| 367 | 367 | return true; |
@@ -369,7 +369,7 @@ discard block |
||
| 369 | 369 | |
| 370 | 370 | $result = $this->userExistsOnLDAP($user); |
| 371 | 371 | $this->access->connection->writeToCache('userExists'.$uid, $result); |
| 372 | - if($result === true) { |
|
| 372 | + if ($result === true) { |
|
| 373 | 373 | $user->update(); |
| 374 | 374 | } |
| 375 | 375 | return $result; |
@@ -387,13 +387,13 @@ discard block |
||
| 387 | 387 | } |
| 388 | 388 | |
| 389 | 389 | $marked = $this->ocConfig->getUserValue($uid, 'user_ldap', 'isDeleted', 0); |
| 390 | - if((int)$marked === 0) { |
|
| 390 | + if ((int) $marked === 0) { |
|
| 391 | 391 | \OC::$server->getLogger()->notice( |
| 392 | - 'User '.$uid . ' is not marked as deleted, not cleaning up.', |
|
| 392 | + 'User '.$uid.' is not marked as deleted, not cleaning up.', |
|
| 393 | 393 | array('app' => 'user_ldap')); |
| 394 | 394 | return false; |
| 395 | 395 | } |
| 396 | - \OC::$server->getLogger()->info('Cleaning up after user ' . $uid, |
|
| 396 | + \OC::$server->getLogger()->info('Cleaning up after user '.$uid, |
|
| 397 | 397 | array('app' => 'user_ldap')); |
| 398 | 398 | |
| 399 | 399 | $this->access->getUserMapper()->unmap($uid); |
@@ -411,7 +411,7 @@ discard block |
||
| 411 | 411 | */ |
| 412 | 412 | public function getHome($uid) { |
| 413 | 413 | // user Exists check required as it is not done in user proxy! |
| 414 | - if(!$this->userExists($uid)) { |
|
| 414 | + if (!$this->userExists($uid)) { |
|
| 415 | 415 | return false; |
| 416 | 416 | } |
| 417 | 417 | |
@@ -421,22 +421,22 @@ discard block |
||
| 421 | 421 | |
| 422 | 422 | $cacheKey = 'getHome'.$uid; |
| 423 | 423 | $path = $this->access->connection->getFromCache($cacheKey); |
| 424 | - if(!is_null($path)) { |
|
| 424 | + if (!is_null($path)) { |
|
| 425 | 425 | return $path; |
| 426 | 426 | } |
| 427 | 427 | |
| 428 | 428 | // early return path if it is a deleted user |
| 429 | 429 | $user = $this->access->userManager->get($uid); |
| 430 | - if($user instanceof OfflineUser) { |
|
| 431 | - if($this->currentUserInDeletionProcess !== null |
|
| 430 | + if ($user instanceof OfflineUser) { |
|
| 431 | + if ($this->currentUserInDeletionProcess !== null |
|
| 432 | 432 | && $this->currentUserInDeletionProcess === $user->getOCName() |
| 433 | 433 | ) { |
| 434 | 434 | return $user->getHomePath(); |
| 435 | 435 | } else { |
| 436 | - throw new NoUserException($uid . ' is not a valid user anymore'); |
|
| 436 | + throw new NoUserException($uid.' is not a valid user anymore'); |
|
| 437 | 437 | } |
| 438 | 438 | } else if ($user === null) { |
| 439 | - throw new NoUserException($uid . ' is not a valid user anymore'); |
|
| 439 | + throw new NoUserException($uid.' is not a valid user anymore'); |
|
| 440 | 440 | } |
| 441 | 441 | |
| 442 | 442 | $path = $user->getHomePath(); |
@@ -455,12 +455,12 @@ discard block |
||
| 455 | 455 | return $this->userPluginManager->getDisplayName($uid); |
| 456 | 456 | } |
| 457 | 457 | |
| 458 | - if(!$this->userExists($uid)) { |
|
| 458 | + if (!$this->userExists($uid)) { |
|
| 459 | 459 | return false; |
| 460 | 460 | } |
| 461 | 461 | |
| 462 | 462 | $cacheKey = 'getDisplayName'.$uid; |
| 463 | - if(!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { |
|
| 463 | + if (!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { |
|
| 464 | 464 | return $displayName; |
| 465 | 465 | } |
| 466 | 466 | |
@@ -477,10 +477,10 @@ discard block |
||
| 477 | 477 | $this->access->username2dn($uid), |
| 478 | 478 | $this->access->connection->ldapUserDisplayName); |
| 479 | 479 | |
| 480 | - if($displayName && (count($displayName) > 0)) { |
|
| 480 | + if ($displayName && (count($displayName) > 0)) { |
|
| 481 | 481 | $displayName = $displayName[0]; |
| 482 | 482 | |
| 483 | - if (is_array($displayName2)){ |
|
| 483 | + if (is_array($displayName2)) { |
|
| 484 | 484 | $displayName2 = count($displayName2) > 0 ? $displayName2[0] : ''; |
| 485 | 485 | } |
| 486 | 486 | |
@@ -522,7 +522,7 @@ discard block |
||
| 522 | 522 | */ |
| 523 | 523 | public function getDisplayNames($search = '', $limit = null, $offset = null) { |
| 524 | 524 | $cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset; |
| 525 | - if(!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) { |
|
| 525 | + if (!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) { |
|
| 526 | 526 | return $displayNames; |
| 527 | 527 | } |
| 528 | 528 | |
@@ -544,12 +544,12 @@ discard block |
||
| 544 | 544 | * compared with \OC\User\Backend::CREATE_USER etc. |
| 545 | 545 | */ |
| 546 | 546 | public function implementsActions($actions) { |
| 547 | - return (bool)((Backend::CHECK_PASSWORD |
|
| 547 | + return (bool) ((Backend::CHECK_PASSWORD |
|
| 548 | 548 | | Backend::GET_HOME |
| 549 | 549 | | Backend::GET_DISPLAYNAME |
| 550 | 550 | | Backend::PROVIDE_AVATAR |
| 551 | 551 | | Backend::COUNT_USERS |
| 552 | - | (((int)$this->access->connection->turnOnPasswordChange === 1)?(Backend::SET_PASSWORD):0) |
|
| 552 | + | (((int) $this->access->connection->turnOnPasswordChange === 1) ? (Backend::SET_PASSWORD) : 0) |
|
| 553 | 553 | | $this->userPluginManager->getImplementedActions()) |
| 554 | 554 | & $actions); |
| 555 | 555 | } |
@@ -573,7 +573,7 @@ discard block |
||
| 573 | 573 | |
| 574 | 574 | $filter = $this->access->getFilterForUserCount(); |
| 575 | 575 | $cacheKey = 'countUsers-'.$filter; |
| 576 | - if(!is_null($entries = $this->access->connection->getFromCache($cacheKey))) { |
|
| 576 | + if (!is_null($entries = $this->access->connection->getFromCache($cacheKey))) { |
|
| 577 | 577 | return $entries; |
| 578 | 578 | } |
| 579 | 579 | $entries = $this->access->countUsers($filter); |
@@ -585,7 +585,7 @@ discard block |
||
| 585 | 585 | * Backend name to be shown in user management |
| 586 | 586 | * @return string the name of the backend to be shown |
| 587 | 587 | */ |
| 588 | - public function getBackendName(){ |
|
| 588 | + public function getBackendName() { |
|
| 589 | 589 | return 'LDAP'; |
| 590 | 590 | } |
| 591 | 591 | |