Skip to content

Commit 8882d32

Browse files
committed
check user state when fetching to avoid dealing with offline objects
fixes #9502 Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
1 parent c61e957 commit 8882d32

2 files changed

Lines changed: 56 additions & 8 deletions

File tree

apps/user_ldap/lib/Access.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252

5353
use OC\ServerNotAvailableException;
5454
use OCP\IConfig;
55-
use OCP\Util;
5655

5756
/**
5857
* Class Access
@@ -651,6 +650,7 @@ public function nextcloudGroupNames($ldapGroups) {
651650
* @param array $ldapObjects as returned by fetchList()
652651
* @param bool $isUsers
653652
* @return array
653+
* @throws \Exception
654654
*/
655655
private function ldap2NextcloudNames($ldapObjects, $isUsers) {
656656
if($isUsers) {
@@ -659,7 +659,7 @@ private function ldap2NextcloudNames($ldapObjects, $isUsers) {
659659
} else {
660660
$nameAttribute = $this->connection->ldapGroupDisplayName;
661661
}
662-
$nextcloudNames = array();
662+
$nextcloudNames = [];
663663

664664
foreach($ldapObjects as $ldapObject) {
665665
$nameByLDAP = null;
@@ -675,6 +675,7 @@ private function ldap2NextcloudNames($ldapObjects, $isUsers) {
675675
if($ncName) {
676676
$nextcloudNames[] = $ncName;
677677
if($isUsers) {
678+
$this->updateUserState($ncName);
678679
//cache the user names so it does not need to be retrieved
679680
//again later (e.g. sharing dialogue).
680681
if(is_null($nameByLDAP)) {
@@ -689,6 +690,19 @@ private function ldap2NextcloudNames($ldapObjects, $isUsers) {
689690
return $nextcloudNames;
690691
}
691692

693+
/**
694+
* removes the deleted-flag of a user if it was set
695+
*
696+
* @param string $ncname
697+
* @throws \Exception
698+
*/
699+
public function updateUserState($ncname) {
700+
$user = $this->userManager->get($ncname);
701+
if($user instanceof OfflineUser) {
702+
$user->unmark();
703+
}
704+
}
705+
692706
/**
693707
* caches the user display name
694708
* @param string $ocName the internal Nextcloud username
@@ -862,7 +876,9 @@ public function fetchListOfUsers($filter, $attr, $limit = null, $offset = null,
862876
* provided with an array of LDAP user records the method will fetch the
863877
* user object and requests it to process the freshly fetched attributes and
864878
* and their values
879+
*
865880
* @param array $ldapRecords
881+
* @throws \Exception
866882
*/
867883
public function batchApplyUserAttributes(array $ldapRecords){
868884
$displayNameAttribute = strtolower($this->connection->ldapUserDisplayName);
@@ -875,11 +891,8 @@ public function batchApplyUserAttributes(array $ldapRecords){
875891
if($ocName === false) {
876892
continue;
877893
}
894+
$this->updateUserState($ocName);
878895
$user = $this->userManager->get($ocName);
879-
if($user instanceof OfflineUser) {
880-
$user->unmark();
881-
$user = $this->userManager->get($ocName);
882-
}
883896
if ($user !== null) {
884897
$user->processAttributes($userRecord);
885898
} else {

apps/user_ldap/tests/AccessTest.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
use OCA\User_LDAP\LogWrapper;
4444
use OCA\User_LDAP\Mapping\UserMapping;
4545
use OCA\User_LDAP\User\Manager;
46+
use OCA\User_LDAP\User\OfflineUser;
4647
use OCA\User_LDAP\User\User;
4748
use OCP\IAvatarManager;
4849
use OCP\IConfig;
@@ -312,7 +313,7 @@ public function testBatchApplyUserAttributes() {
312313
$userMock->expects($this->exactly(count($data)))
313314
->method('processAttributes');
314315

315-
$this->userManager->expects($this->exactly(count($data)))
316+
$this->userManager->expects($this->exactly(count($data) * 2))
316317
->method('get')
317318
->will($this->returnValue($userMock));
318319

@@ -394,7 +395,7 @@ public function testBatchApplyUserAttributesDontSkip() {
394395
$userMock->expects($this->exactly(count($data)))
395396
->method('processAttributes');
396397

397-
$this->userManager->expects($this->exactly(count($data)))
398+
$this->userManager->expects($this->exactly(count($data) * 2))
398399
->method('get')
399400
->will($this->returnValue($userMock));
400401

@@ -662,6 +663,40 @@ public function testSanitizeUsername($name, $expected) {
662663
$this->assertSame($expected, $sanitizedName);
663664
}
664665

666+
public function testUserStateUpdate() {
667+
$this->connection->expects($this->any())
668+
->method('__get')
669+
->willReturnMap([
670+
[ 'ldapUserDisplayName', 'displayName' ],
671+
[ 'ldapUserDisplayName2', null],
672+
]);
673+
674+
$offlineUserMock = $this->createMock(OfflineUser::class);
675+
$offlineUserMock->expects($this->once())
676+
->method('unmark');
677+
678+
$regularUserMock = $this->createMock(User::class);
679+
680+
$this->userManager->expects($this->atLeastOnce())
681+
->method('get')
682+
->with('detta')
683+
->willReturnOnConsecutiveCalls($offlineUserMock, $regularUserMock);
684+
685+
/** @var UserMapping|\PHPUnit_Framework_MockObject_MockObject $mapperMock */
686+
$mapperMock = $this->createMock(UserMapping::class);
687+
$mapperMock->expects($this->any())
688+
->method('getNameByDN')
689+
->with('uid=detta,ou=users,dc=hex,dc=ample')
690+
->willReturn('detta');
691+
$this->access->setUserMapper($mapperMock);
665692

693+
$records = [
694+
[
695+
'dn' => ['uid=detta,ou=users,dc=hex,dc=ample'],
696+
'displayName' => ['Detta Detkova'],
697+
]
698+
];
699+
$this->access->nextcloudUserNames($records);
700+
}
666701

667702
}

0 commit comments

Comments
 (0)