Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check user state when fetching to avoid dealing with offline objects, fixes #9502 #9640

Merged
merged 1 commit into from
May 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions apps/user_ldap/lib/Access.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
use OCP\IConfig;
use OCP\ILogger;
use OCP\IUserManager;
use OCP\Util;

/**
* Class Access
Expand Down Expand Up @@ -664,6 +663,7 @@ public function nextcloudGroupNames($ldapGroups) {
* @param array $ldapObjects as returned by fetchList()
* @param bool $isUsers
* @return array
* @throws \Exception
*/
private function ldap2NextcloudNames($ldapObjects, $isUsers) {
if($isUsers) {
Expand All @@ -672,7 +672,7 @@ private function ldap2NextcloudNames($ldapObjects, $isUsers) {
} else {
$nameAttribute = $this->connection->ldapGroupDisplayName;
}
$nextcloudNames = array();
$nextcloudNames = [];

foreach($ldapObjects as $ldapObject) {
$nameByLDAP = null;
Expand All @@ -688,6 +688,7 @@ private function ldap2NextcloudNames($ldapObjects, $isUsers) {
if($ncName) {
$nextcloudNames[] = $ncName;
if($isUsers) {
$this->updateUserState($ncName);
//cache the user names so it does not need to be retrieved
//again later (e.g. sharing dialogue).
if(is_null($nameByLDAP)) {
Expand All @@ -702,6 +703,19 @@ private function ldap2NextcloudNames($ldapObjects, $isUsers) {
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
* @param string $ocName the internal Nextcloud username
Expand Down Expand Up @@ -873,7 +887,9 @@ public function fetchListOfUsers($filter, $attr, $limit = null, $offset = null,
* 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
* and their values
*
* @param array $ldapRecords
* @throws \Exception
*/
public function batchApplyUserAttributes(array $ldapRecords){
$displayNameAttribute = strtolower($this->connection->ldapUserDisplayName);
Expand All @@ -886,11 +902,8 @@ public function batchApplyUserAttributes(array $ldapRecords){
if($ocName === false) {
continue;
}
$this->updateUserState($ocName);
$user = $this->userManager->get($ocName);
if($user instanceof OfflineUser) {
$user->unmark();
$user = $this->userManager->get($ocName);
}
if ($user !== null) {
$user->processAttributes($userRecord);
} else {
Expand Down
39 changes: 37 additions & 2 deletions apps/user_ldap/tests/AccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
use OCA\User_LDAP\LogWrapper;
use OCA\User_LDAP\Mapping\UserMapping;
use OCA\User_LDAP\User\Manager;
use OCA\User_LDAP\User\OfflineUser;
use OCA\User_LDAP\User\User;
use OCP\IAvatarManager;
use OCP\IConfig;
Expand Down Expand Up @@ -316,7 +317,7 @@ public function testBatchApplyUserAttributes() {
$userMock->expects($this->exactly(count($data)))
->method('processAttributes');

$this->userManager->expects($this->exactly(count($data)))
$this->userManager->expects($this->exactly(count($data) * 2))
->method('get')
->will($this->returnValue($userMock));

Expand Down Expand Up @@ -398,7 +399,7 @@ public function testBatchApplyUserAttributesDontSkip() {
$userMock->expects($this->exactly(count($data)))
->method('processAttributes');

$this->userManager->expects($this->exactly(count($data)))
$this->userManager->expects($this->exactly(count($data) * 2))
->method('get')
->will($this->returnValue($userMock));

Expand Down Expand Up @@ -666,6 +667,40 @@ public function testSanitizeUsername($name, $expected) {
$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);
}

}