Skip to content

Commit

Permalink
Dont use isSyncMaintained
Browse files Browse the repository at this point in the history
  • Loading branch information
mrow4a committed Mar 12, 2018
1 parent b24aae4 commit b691b7f
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 40 deletions.
4 changes: 0 additions & 4 deletions lib/private/Group/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,4 @@ public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
public function isVisibleForScope($scope) {
return true;
}

public function isSyncMaintained() {
return false;
}
}
7 changes: 0 additions & 7 deletions lib/private/Group/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,4 @@ public function groupExists($groupName) {
$stmt->closeCursor();
return isset($data['exists']);
}

/**
* Groups and memberships of this backend are maintained by the users
*/
public function isSyncMaintained() {
return false;
}
}
47 changes: 40 additions & 7 deletions lib/private/Group/SyncService.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class SyncService {
private $logger;
/** @var string[] backendclass -> gid array map */
private $prefetchedGroupIds;
/** @var string[] backendclass -> BackendGroup array map */
private $membershipsByBackend;
/** @var string uid - uid of last user for which sync was run */
private $lastUser;

/**
* SyncService constructor.
Expand All @@ -69,6 +73,8 @@ public function __construct(GroupMapper $groupMapper,
$this->membershipManager = $membershipManager;
$this->logger = $logger;
$this->prefetchedGroupIds = [];
$this->membershipsByBackend = [];
$this->lastUser = null;
}

/**
Expand Down Expand Up @@ -221,13 +227,7 @@ public function syncUserMemberships(GroupInterface $backend, $uid) {
$remoteGids = $backend->getUserGroups($uid);

/* @var BackendGroup[] $localSyncGidGroupMap */
$localSyncGidGroupMap = [];
$localSyncGroups = $this->membershipManager->getMemberBackendGroupsByType($uid,
MembershipManager::MEMBERSHIP_TYPE_GROUP_USER,
MembershipManager::MAINTENANCE_TYPE_SYNC);
foreach($localSyncGroups as $localSyncGroup) {
$localSyncGidGroupMap[$localSyncGroup->getGroupId()] = $localSyncGroup;
}
$localSyncGidGroupMap = $this->getLocalBackendMemberships($backend, $uid);

// Check which memberships need to removed or added
$membershipsToRemove = array_diff(array_keys($localSyncGidGroupMap), $remoteGids);
Expand Down Expand Up @@ -278,6 +278,39 @@ public function syncUserMemberships(GroupInterface $backend, $uid) {
);
}

/**
* Return gid->BackendGroup map of user groups for this backend
*
* @param GroupInterface $backend
* @param $uid
*/
private function getLocalBackendMemberships(GroupInterface $backend, $uid) {
if ($this->lastUser !== $uid) {
// Fetch all memberships for this user and segregate by backend class
$this->lastUser = $uid;
$this->membershipsByBackend = [];

/* @var BackendGroup[] $localSyncGroups */
$localSyncGroups = $this->membershipManager->getMemberBackendGroupsByType($uid,
MembershipManager::MEMBERSHIP_TYPE_GROUP_USER,
MembershipManager::MAINTENANCE_TYPE_SYNC);
foreach($localSyncGroups as $localSyncGroup) {
if (!isset($this->membershipsByBackend[$localSyncGroup->getBackend()])) {
$this->membershipsByBackend[$localSyncGroup->getBackend()] = [];
}
$this->membershipsByBackend[$localSyncGroup->getBackend()][$localSyncGroup->getGroupId()] = $localSyncGroup;
}
}

// Return and invalidate the cache for selected backend
if (isset($this->membershipsByBackend[get_class($backend)])) {
$memberships = $this->membershipsByBackend[get_class($backend)];
unset($this->membershipsByBackend[get_class($backend)]);
return $memberships;
}
return [];
}

/**
* Fetch backend service groups and
* use callback function for each of the groups, if callback has been specified.
Expand Down
4 changes: 1 addition & 3 deletions lib/private/User/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,7 @@ public function tryBasicAuthLogin(IRequest $request) {
private function updateUserMemberships(IUser $user) {
// Update only the backends which are sync maintained
foreach($this->groupManager->getBackends() as $groupBackend) {
if ($groupBackend->isSyncMaintained()) {
$this->groupSyncService->syncUserMemberships($groupBackend, $user->getUID());
}
$this->groupSyncService->syncUserMemberships($groupBackend, $user->getUID());
};
}

Expand Down
8 changes: 0 additions & 8 deletions lib/public/GroupInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,4 @@ public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0);
*/
public function isVisibleForScope($scope);

/**
* Returns whether the groups and memberships are to be maintained by core
* and sync mechanism to be used.
*
* @since 10.0.0
*/
public function isSyncMaintained();

}
1 change: 0 additions & 1 deletion tests/Core/Command/Group/SyncBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ protected function setUp() {
'addToGroup',
'removeFromGroup',
'isVisibleForScope',
'isSyncMaintained',
])
->getMock();
$this->userBackend1 = $this->getMockBuilder(UserInterface::class)
Expand Down
3 changes: 1 addition & 2 deletions tests/lib/Group/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ private function getTestBackend($implementedActions = null, $visibleForScopes =
'deleteGroup',
'addToGroup',
'removeFromGroup',
'isVisibleForScope',
'isSyncMaintained'
'isVisibleForScope'
])
->getMock();
$backend->expects($this->any())
Expand Down
1 change: 0 additions & 1 deletion tests/lib/Group/SyncServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ public function setUp() {
'addToGroup',
'removeFromGroup',
'isVisibleForScope',
'isSyncMaintained',
])
->getMock();

Expand Down
11 changes: 4 additions & 7 deletions tests/lib/User/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ protected function setUp() {
$this->groupSyncService = $this->createMock(\OC\Group\SyncService::class);

$groupBackend = $this->createMock(GroupInterface::class);
$groupBackend->expects($this->any())
->method('isSyncMaintained')
->willReturn(true);
$this->groupManager->expects($this->any())
->method('getBackends')
->willReturn([$groupBackend]);
Expand Down Expand Up @@ -1052,17 +1049,17 @@ public function testLogout() {
->method('set')
->with('user_id', 'foo');

/** @var Manager $manager */
$manager = $this->createMock(Manager::class);
/** @var Manager $userManager */
$userManager = $this->createMock(Manager::class);

/** @var IUser | \PHPUnit_Framework_MockObject_MockObject $user */
$user = $this->createMock(IUser::class);
$user->expects($this->once())
->method('getUID')
->will($this->returnValue('foo'));

$userSession = new Session($manager, $session, $this->timeFactory,
$this->tokenProvider, $this->config, $this->serviceLoader, $this->userSyncService);
$userSession = new Session($userManager, $this->groupManager, $session, $this->timeFactory,
$this->tokenProvider, $this->config, $this->serviceLoader, $this->userSyncService, $this->groupSyncService);
$userSession->setUser($user);

$this->assertTrue($userSession->logout());
Expand Down

0 comments on commit b691b7f

Please sign in to comment.