Merge pull request #9640 from nextcloud/fix/9502/missedstatecheck

check user state when fetching to avoid dealing with offline objects, fixes #9502
This commit is contained in:
blizzz 2018-05-29 16:55:14 +02:00 committed by GitHub
commit c0ed6f91a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 8 deletions

View File

@ -54,7 +54,6 @@ use OC\ServerNotAvailableException;
use OCP\IConfig; use OCP\IConfig;
use OCP\ILogger; use OCP\ILogger;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\Util;
/** /**
* Class Access * Class Access
@ -664,6 +663,7 @@ class Access extends LDAPUtility implements IUserTools {
* @param array $ldapObjects as returned by fetchList() * @param array $ldapObjects as returned by fetchList()
* @param bool $isUsers * @param bool $isUsers
* @return array * @return array
* @throws \Exception
*/ */
private function ldap2NextcloudNames($ldapObjects, $isUsers) { private function ldap2NextcloudNames($ldapObjects, $isUsers) {
if($isUsers) { if($isUsers) {
@ -672,7 +672,7 @@ class Access extends LDAPUtility implements IUserTools {
} else { } else {
$nameAttribute = $this->connection->ldapGroupDisplayName; $nameAttribute = $this->connection->ldapGroupDisplayName;
} }
$nextcloudNames = array(); $nextcloudNames = [];
foreach($ldapObjects as $ldapObject) { foreach($ldapObjects as $ldapObject) {
$nameByLDAP = null; $nameByLDAP = null;
@ -688,6 +688,7 @@ class Access extends LDAPUtility implements IUserTools {
if($ncName) { if($ncName) {
$nextcloudNames[] = $ncName; $nextcloudNames[] = $ncName;
if($isUsers) { if($isUsers) {
$this->updateUserState($ncName);
//cache the user names so it does not need to be retrieved //cache the user names so it does not need to be retrieved
//again later (e.g. sharing dialogue). //again later (e.g. sharing dialogue).
if(is_null($nameByLDAP)) { if(is_null($nameByLDAP)) {
@ -702,6 +703,19 @@ class Access extends LDAPUtility implements IUserTools {
return $nextcloudNames; return $nextcloudNames;
} }
/**
* removes the deleted-flag of a user if it was set
*
* @param string $ncname
* @throws \Exception
*/
public function updateUserState($ncname) {
$user = $this->userManager->get($ncname);
if($user instanceof OfflineUser) {
$user->unmark();
}
}
/** /**
* caches the user display name * caches the user display name
* @param string $ocName the internal Nextcloud username * @param string $ocName the internal Nextcloud username
@ -873,7 +887,9 @@ class Access extends LDAPUtility implements IUserTools {
* provided with an array of LDAP user records the method will fetch the * provided with an array of LDAP user records the method will fetch the
* user object and requests it to process the freshly fetched attributes and * user object and requests it to process the freshly fetched attributes and
* and their values * and their values
*
* @param array $ldapRecords * @param array $ldapRecords
* @throws \Exception
*/ */
public function batchApplyUserAttributes(array $ldapRecords){ public function batchApplyUserAttributes(array $ldapRecords){
$displayNameAttribute = strtolower($this->connection->ldapUserDisplayName); $displayNameAttribute = strtolower($this->connection->ldapUserDisplayName);
@ -886,11 +902,8 @@ class Access extends LDAPUtility implements IUserTools {
if($ocName === false) { if($ocName === false) {
continue; continue;
} }
$this->updateUserState($ocName);
$user = $this->userManager->get($ocName); $user = $this->userManager->get($ocName);
if($user instanceof OfflineUser) {
$user->unmark();
$user = $this->userManager->get($ocName);
}
if ($user !== null) { if ($user !== null) {
$user->processAttributes($userRecord); $user->processAttributes($userRecord);
} else { } else {

View File

@ -43,6 +43,7 @@ use OCA\User_LDAP\LDAP;
use OCA\User_LDAP\LogWrapper; use OCA\User_LDAP\LogWrapper;
use OCA\User_LDAP\Mapping\UserMapping; use OCA\User_LDAP\Mapping\UserMapping;
use OCA\User_LDAP\User\Manager; use OCA\User_LDAP\User\Manager;
use OCA\User_LDAP\User\OfflineUser;
use OCA\User_LDAP\User\User; use OCA\User_LDAP\User\User;
use OCP\IAvatarManager; use OCP\IAvatarManager;
use OCP\IConfig; use OCP\IConfig;
@ -316,7 +317,7 @@ class AccessTest extends TestCase {
$userMock->expects($this->exactly(count($data))) $userMock->expects($this->exactly(count($data)))
->method('processAttributes'); ->method('processAttributes');
$this->userManager->expects($this->exactly(count($data))) $this->userManager->expects($this->exactly(count($data) * 2))
->method('get') ->method('get')
->will($this->returnValue($userMock)); ->will($this->returnValue($userMock));
@ -398,7 +399,7 @@ class AccessTest extends TestCase {
$userMock->expects($this->exactly(count($data))) $userMock->expects($this->exactly(count($data)))
->method('processAttributes'); ->method('processAttributes');
$this->userManager->expects($this->exactly(count($data))) $this->userManager->expects($this->exactly(count($data) * 2))
->method('get') ->method('get')
->will($this->returnValue($userMock)); ->will($this->returnValue($userMock));
@ -666,6 +667,40 @@ class AccessTest extends TestCase {
$this->assertSame($expected, $sanitizedName); $this->assertSame($expected, $sanitizedName);
} }
public function testUserStateUpdate() {
$this->connection->expects($this->any())
->method('__get')
->willReturnMap([
[ 'ldapUserDisplayName', 'displayName' ],
[ 'ldapUserDisplayName2', null],
]);
$offlineUserMock = $this->createMock(OfflineUser::class);
$offlineUserMock->expects($this->once())
->method('unmark');
$regularUserMock = $this->createMock(User::class);
$this->userManager->expects($this->atLeastOnce())
->method('get')
->with('detta')
->willReturnOnConsecutiveCalls($offlineUserMock, $regularUserMock);
/** @var UserMapping|\PHPUnit_Framework_MockObject_MockObject $mapperMock */
$mapperMock = $this->createMock(UserMapping::class);
$mapperMock->expects($this->any())
->method('getNameByDN')
->with('uid=detta,ou=users,dc=hex,dc=ample')
->willReturn('detta');
$this->access->setUserMapper($mapperMock);
$records = [
[
'dn' => ['uid=detta,ou=users,dc=hex,dc=ample'],
'displayName' => ['Detta Detkova'],
]
];
$this->access->nextcloudUserNames($records);
}
} }