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

Fix disabled user list for subadmins #48766

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions apps/provisioning_api/lib/Controller/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,21 +235,21 @@ public function getDisabledUsersDetails(string $search = '', ?int $limit = null,
/* We have to handle offset ourselve for correctness */
$tempLimit = ($limit === null ? null : $limit + $offset);
foreach ($subAdminOfGroups as $group) {
$users = array_merge(
$users = array_unique(array_merge(
$users,
array_map(
fn (IUser $user): string => $user->getUID(),
array_filter(
$group->searchUsers($search, ($tempLimit === null ? null : $tempLimit - count($users))),
$group->searchUsers($search),
fn (IUser $user): bool => !$user->isEnabled()
)
)
);
));
if (($tempLimit !== null) && (count($users) >= $tempLimit)) {
break;
}
}
$users = array_slice($users, $offset);
$users = array_slice($users, $offset, $limit);
}

$usersDetails = [];
Expand Down
125 changes: 125 additions & 0 deletions apps/provisioning_api/tests/Controller/UsersControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,131 @@ public function testGetUsersAsSubAdmin(): void {
$this->assertEquals($expected, $this->api->getUsers('MyCustomSearch')->getData());
}

private function createUserMock(string $uid, bool $enabled): MockObject&IUser {
$mockUser = $this->getMockBuilder(IUser::class)
->disableOriginalConstructor()
->getMock();
$mockUser
->method('getUID')
->willReturn($uid);
$mockUser
->method('isEnabled')
->willReturn($enabled);
return $mockUser;
}

public function testGetDisabledUsersAsAdmin(): void {
$loggedInUser = $this->getMockBuilder(IUser::class)
->disableOriginalConstructor()
->getMock();
$loggedInUser
->expects($this->once())
->method('getUID')
->willReturn('admin');
$this->userSession
->expects($this->atLeastOnce())
->method('getUser')
->willReturn($loggedInUser);
$this->groupManager
->expects($this->once())
->method('isAdmin')
->willReturn(true);
$this->userManager
->expects($this->once())
->method('getDisabledUsers')
->with(3, 0, 'MyCustomSearch')
->willReturn([
$this->createUserMock('admin', false),
$this->createUserMock('foo', false),
$this->createUserMock('bar', false),
]);

$expected = [
'users' => [
'admin' => ['id' => 'admin'],
'foo' => ['id' => 'foo'],
'bar' => ['id' => 'bar'],
],
];
$this->assertEquals($expected, $this->api->getDisabledUsersDetails('MyCustomSearch', 3)->getData());
}

public function testGetDisabledUsersAsSubAdmin(): void {
$loggedInUser = $this->getMockBuilder(IUser::class)
->disableOriginalConstructor()
->getMock();
$loggedInUser
->expects($this->once())
->method('getUID')
->willReturn('subadmin');
$this->userSession
->expects($this->atLeastOnce())
->method('getUser')
->willReturn($loggedInUser);
$this->groupManager
->expects($this->once())
->method('isAdmin')
->willReturn(false);
$firstGroup = $this->getMockBuilder('OCP\IGroup')
->disableOriginalConstructor()
->getMock();
$secondGroup = $this->getMockBuilder('OCP\IGroup')
->disableOriginalConstructor()
->getMock();
$subAdminManager = $this->getMockBuilder('OC\SubAdmin')
->disableOriginalConstructor()->getMock();
$subAdminManager
->expects($this->once())
->method('isSubAdmin')
->with($loggedInUser)
->willReturn(true);
$subAdminManager
->expects($this->once())
->method('getSubAdminsGroups')
->with($loggedInUser)
->willReturn([$firstGroup, $secondGroup]);
$this->groupManager
->expects($this->once())
->method('getSubAdmin')
->willReturn($subAdminManager);
$this->groupManager
->expects($this->never())
->method('displayNamesInGroup');

$firstGroup
->expects($this->once())
->method('searchUsers')
->with('MyCustomSearch')
->willReturn([
$this->createUserMock('user1', false),
$this->createUserMock('bob', true),
$this->createUserMock('user2', false),
$this->createUserMock('alice', true),
]);

$secondGroup
->expects($this->once())
->method('searchUsers')
->with('MyCustomSearch')
->willReturn([
$this->createUserMock('user2', false),
$this->createUserMock('joe', true),
$this->createUserMock('user3', false),
$this->createUserMock('jim', true),
$this->createUserMock('john', true),
]);


$expected = [
'users' => [
'user1' => ['id' => 'user1'],
'user2' => ['id' => 'user2'],
'user3' => ['id' => 'user3'],
],
];
$this->assertEquals($expected, $this->api->getDisabledUsersDetails('MyCustomSearch', 3)->getData());
}


public function testAddUserAlreadyExisting(): void {
$this->expectException(OCSException::class);
Expand Down
Loading