Skip to content

Commit 9487cb9

Browse files
committed
LDAP: do not attempt to process user records without display name, fixes fatal error
1 parent df4bd32 commit 9487cb9

File tree

5 files changed

+141
-1
lines changed

5 files changed

+141
-1
lines changed

apps/user_ldap/lib/access.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,8 +706,16 @@ public function fetchListOfUsers($filter, $attr, $limit = null, $offset = null)
706706
* @param array $ldapRecords
707707
*/
708708
public function batchApplyUserAttributes(array $ldapRecords){
709+
$displayNameAttribute = strtolower($this->connection->ldapUserDisplayName);
709710
foreach($ldapRecords as $userRecord) {
711+
if(!isset($userRecord[$displayNameAttribute])) {
712+
// displayName is obligatory
713+
continue;
714+
}
710715
$ocName = $this->dn2ocname($userRecord['dn'][0]);
716+
if($ocName === false) {
717+
continue;
718+
}
711719
$this->cacheUserExists($ocName);
712720
$user = $this->userManager->get($ocName);
713721
if($user instanceof OfflineUser) {
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* @author Arthur Schiwon <blizzz@owncloud.com>
4+
*
5+
* @copyright Copyright (c) 2015, ownCloud, Inc.
6+
* @license AGPL-3.0
7+
*
8+
* This code is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License, version 3,
10+
* as published by the Free Software Foundation.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License, version 3,
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>
19+
*
20+
*/
21+
22+
namespace OCA\user_ldap\tests\integration\lib;
23+
24+
use OCA\User_LDAP\Mapping\UserMapping;
25+
use OCA\user_ldap\tests\integration\AbstractIntegrationTest;
26+
27+
require_once __DIR__ . '/../../../../../lib/base.php';
28+
29+
class IntegrationTestBatchApplyUserAttributes extends AbstractIntegrationTest {
30+
/**
31+
* prepares the LDAP environment and sets up a test configuration for
32+
* the LDAP backend.
33+
*/
34+
public function init() {
35+
require(__DIR__ . '/../setup-scripts/createExplicitUsers.php');
36+
require(__DIR__ . '/../setup-scripts/createUsersWithoutDisplayName.php');
37+
parent::init();
38+
39+
$this->mapping = new UserMapping(\OC::$server->getDatabaseConnection());
40+
$this->mapping->clear();
41+
$this->access->setUserMapper($this->mapping);
42+
}
43+
44+
/**
45+
* sets up the LDAP configuration to be used for the test
46+
*/
47+
protected function initConnection() {
48+
parent::initConnection();
49+
$this->connection->setConfiguration([
50+
'ldapUserDisplayName' => 'displayname',
51+
]);
52+
}
53+
54+
/**
55+
* indirectly tests whether batchApplyUserAttributes does it job properly,
56+
* when a user without display name is included in the result set from LDAP.
57+
*
58+
* @return bool
59+
*/
60+
protected function case1() {
61+
$result = $this->access->fetchListOfUsers('objectclass=person', 'dn');
62+
// on the original issue, PHP would emit a fatal error
63+
// – cannot catch it here, but will render the test as unsuccessful
64+
return is_array($result) && !empty($result);
65+
}
66+
67+
}
68+
69+
require_once(__DIR__ . '/../setup-scripts/config.php');
70+
$test = new IntegrationTestBatchApplyUserAttributes($host, $port, $adn, $apwd, $bdn);
71+
$test->init();
72+
$test->run();
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* @author Arthur Schiwon <blizzz@owncloud.com>
4+
*
5+
* @copyright Copyright (c) 2015, ownCloud, Inc.
6+
* @license AGPL-3.0
7+
*
8+
* This code is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License, version 3,
10+
* as published by the Free Software Foundation.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License, version 3,
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>
19+
*
20+
*/
21+
if(php_sapi_name() !== 'cli') {
22+
print('Only via CLI, please.');
23+
exit(1);
24+
}
25+
26+
include __DIR__ . '/config.php';
27+
28+
$cr = ldap_connect($host, $port);
29+
ldap_set_option($cr, LDAP_OPT_PROTOCOL_VERSION, 3);
30+
$ok = ldap_bind($cr, $adn, $apwd);
31+
32+
if (!$ok) {
33+
die(ldap_error($cr));
34+
}
35+
36+
$ouName = 'Users';
37+
$ouDN = 'ou=' . $ouName . ',' . $bdn;
38+
39+
$users = ['robot'];
40+
41+
foreach ($users as $uid) {
42+
$newDN = 'uid=' . $uid . ',' . $ouDN;
43+
$fn = ucfirst($uid);
44+
$sn = ucfirst(str_shuffle($uid)); // not so explicit but it's OK.
45+
46+
$entry = [];
47+
$entry['cn'] = ucfirst($uid);
48+
$entry['objectclass'][] = 'inetOrgPerson';
49+
$entry['objectclass'][] = 'person';
50+
$entry['sn'] = $sn;
51+
$entry['userPassword'] = $uid;
52+
53+
$ok = ldap_add($cr, $newDN, $entry);
54+
if ($ok) {
55+
echo('created user ' . ': ' . $entry['cn'] . PHP_EOL);
56+
} else {
57+
die(ldap_error($cr));
58+
}
59+
}

apps/user_ldap/tests/user_ldap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ private function prepareAccessForGetUsers(&$access) {
294294
$access->expects($this->any())
295295
->method('combineFilterWithAnd')
296296
->will($this->returnCallback(function($param) {
297-
return $param[1];
297+
return $param[2];
298298
}));
299299

300300
$access->expects($this->any())

apps/user_ldap/user_ldap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ public function getUsers($search = '', $limit = 10, $offset = 0) {
170170
}
171171
$filter = $this->access->combineFilterWithAnd(array(
172172
$this->access->connection->ldapUserFilter,
173+
$this->access->connection->ldapUserDisplayName . '=*',
173174
$this->access->getFilterPartForUserSearch($search)
174175
));
175176

0 commit comments

Comments
 (0)