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: Return correct list of managers for a user #48538

Open
wants to merge 3 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
74 changes: 45 additions & 29 deletions apps/provisioning_api/lib/Controller/AUserData.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
use OCP\Files\NotFoundException;
use OCP\Group\ISubAdmin;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\L10N\IFactory;
Expand All @@ -45,35 +47,18 @@
public const USER_FIELD_MANAGER = 'manager';
public const USER_FIELD_NOTIFICATION_EMAIL = 'notify_email';

/** @var IUserManager */
protected $userManager;
/** @var IConfig */
protected $config;
/** @var Manager */
protected $groupManager;
/** @var IUserSession */
protected $userSession;
/** @var IAccountManager */
protected $accountManager;
/** @var IFactory */
protected $l10nFactory;

public function __construct(string $appName,
public function __construct(
string $appName,
IRequest $request,
IUserManager $userManager,
IConfig $config,
IGroupManager $groupManager,
IUserSession $userSession,
IAccountManager $accountManager,
IFactory $l10nFactory) {
protected IUserManager $userManager,
protected IConfig $config,
protected IGroupManager $groupManager,
protected IUserSession $userSession,
protected IAccountManager $accountManager,
protected ISubAdmin $subAdminManager,
protected IFactory $l10nFactory,
) {
parent::__construct($appName, $request);

$this->userManager = $userManager;
$this->config = $config;
$this->groupManager = $groupManager;
$this->userSession = $userSession;
$this->accountManager = $accountManager;
$this->l10nFactory = $l10nFactory;
}

/**
Expand Down Expand Up @@ -102,7 +87,7 @@
$isDelegatedAdmin = $this->groupManager->isDelegatedAdmin($currentLoggedInUser->getUID());
if ($isAdmin
|| $isDelegatedAdmin
|| $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) {

Check failure on line 90 in apps/provisioning_api/lib/Controller/AUserData.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedInterfaceMethod

apps/provisioning_api/lib/Controller/AUserData.php:90:28: UndefinedInterfaceMethod: Method OCP\IGroupManager::getSubAdmin does not exist (see https://psalm.dev/181)
$data['enabled'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true') === 'true';
} else {
// Check they are looking up themselves
Expand Down Expand Up @@ -136,8 +121,8 @@
$data['backend'] = $targetUserObject->getBackendClassName();
$data['subadmin'] = $this->getUserSubAdminGroupsData($targetUserObject->getUID());
$data[self::USER_FIELD_QUOTA] = $this->fillStorageInfo($targetUserObject->getUID());
$managerUids = $targetUserObject->getManagerUids();
$data[self::USER_FIELD_MANAGER] = empty($managerUids) ? '' : $managerUids[0];
$managers = $this->getManagers($targetUserObject);
$data[self::USER_FIELD_MANAGER] = empty($managers) ? '' : $managers[0];

try {
if ($includeScopes) {
Expand Down Expand Up @@ -206,6 +191,37 @@
return $data;
}

/**
* @return string[]
*/
protected function getManagers(IUser $user): array {
$currentLoggedInUser = $this->userSession->getUser();
$isAdmin = $this->groupManager->isAdmin($currentLoggedInUser->getUID());
$isDelegatedAdmin = $this->groupManager->isDelegatedAdmin($currentLoggedInUser->getUID());
$isSubAdmin = $this->subAdminManager->isSubAdmin($currentLoggedInUser);

$managerUids = $user->getManagerUids();
if ($isAdmin || $isDelegatedAdmin) {
return $managerUids;
}

if ($isSubAdmin) {
$accessibleManagerUids = array_values(array_filter(
$managerUids,
function (string $managerUid) use ($currentLoggedInUser) {
$manager = $this->userManager->get($managerUid);
if (!($manager instanceof IUser)) {
return false;
}
return $this->subAdminManager->isUserAccessible($currentLoggedInUser, $manager);
},
));
return $accessibleManagerUids;
}

return [];
}

/**
* Get the groups a user is a subadmin of
*
Expand All @@ -221,7 +237,7 @@
}

// Get the subadmin groups
$subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);

Check failure on line 240 in apps/provisioning_api/lib/Controller/AUserData.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedInterfaceMethod

apps/provisioning_api/lib/Controller/AUserData.php:240:42: UndefinedInterfaceMethod: Method OCP\IGroupManager::getSubAdmin does not exist (see https://psalm.dev/181)
$groups = [];
foreach ($subAdminGroups as $key => $group) {
$groups[] = $group->getGID();
Expand Down
3 changes: 3 additions & 0 deletions apps/provisioning_api/lib/Controller/GroupsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use OCP\AppFramework\OCS\OCSForbiddenException;
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
use OCP\Group\ISubAdmin;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IGroupManager;
Expand All @@ -47,6 +48,7 @@
IGroupManager $groupManager,
IUserSession $userSession,
IAccountManager $accountManager,
ISubAdmin $subAdminManager,
IFactory $l10nFactory,
LoggerInterface $logger) {
parent::__construct($appName,
Expand All @@ -56,6 +58,7 @@
$groupManager,
$userSession,
$accountManager,
$subAdminManager,
$l10nFactory
);

Expand Down Expand Up @@ -149,7 +152,7 @@
// Check the group exists
$group = $this->groupManager->get($groupId);
if ($group !== null) {
$isSubadminOfGroup = $this->groupManager->getSubAdmin()->isSubAdminOfGroup($user, $group);

Check failure on line 155 in apps/provisioning_api/lib/Controller/GroupsController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedInterfaceMethod

apps/provisioning_api/lib/Controller/GroupsController.php:155:46: UndefinedInterfaceMethod: Method OCP\IGroupManager::getSubAdmin does not exist (see https://psalm.dev/181)
} else {
throw new OCSNotFoundException('The requested group could not be found');
}
Expand Down Expand Up @@ -192,7 +195,7 @@
// Check the group exists
$group = $this->groupManager->get($groupId);
if ($group !== null) {
$isSubadminOfGroup = $this->groupManager->getSubAdmin()->isSubAdminOfGroup($currentUser, $group);

Check failure on line 198 in apps/provisioning_api/lib/Controller/GroupsController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedInterfaceMethod

apps/provisioning_api/lib/Controller/GroupsController.php:198:46: UndefinedInterfaceMethod: Method OCP\IGroupManager::getSubAdmin does not exist (see https://psalm.dev/181)
} else {
throw new OCSException('The requested group could not be found', OCSController::RESPOND_NOT_FOUND);
}
Expand Down Expand Up @@ -334,7 +337,7 @@
}

/** @var IUser[] $subadmins */
$subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup);

Check failure on line 340 in apps/provisioning_api/lib/Controller/GroupsController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedInterfaceMethod

apps/provisioning_api/lib/Controller/GroupsController.php:340:37: UndefinedInterfaceMethod: Method OCP\IGroupManager::getSubAdmin does not exist (see https://psalm.dev/181)
// New class returns IUser[] so convert back
/** @var string[] $uids */
$uids = [];
Expand Down
5 changes: 4 additions & 1 deletion apps/provisioning_api/lib/Controller/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use OCP\AppFramework\OCS\OCSNotFoundException;
use OCP\AppFramework\OCSController;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Group\ISubAdmin;
use OCP\HintException;
use OCP\IConfig;
use OCP\IGroup;
Expand Down Expand Up @@ -63,6 +64,7 @@
IGroupManager $groupManager,
IUserSession $userSession,
IAccountManager $accountManager,
ISubAdmin $subAdminManager,
IFactory $l10nFactory,
private IURLGenerator $urlGenerator,
private LoggerInterface $logger,
Expand All @@ -81,6 +83,7 @@
$groupManager,
$userSession,
$accountManager,
$subAdminManager,
$l10nFactory
);

Expand All @@ -104,7 +107,7 @@

// Admin? Or SubAdmin?
$uid = $user->getUID();
$subAdminManager = $this->groupManager->getSubAdmin();

Check failure on line 110 in apps/provisioning_api/lib/Controller/UsersController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedInterfaceMethod

apps/provisioning_api/lib/Controller/UsersController.php:110:43: UndefinedInterfaceMethod: Method OCP\IGroupManager::getSubAdmin does not exist (see https://psalm.dev/181)
$isAdmin = $this->groupManager->isAdmin($uid);
$isDelegatedAdmin = $this->groupManager->isDelegatedAdmin($uid);
if ($isAdmin || $isDelegatedAdmin) {
Expand Down Expand Up @@ -146,7 +149,7 @@

// Admin? Or SubAdmin?
$uid = $currentUser->getUID();
$subAdminManager = $this->groupManager->getSubAdmin();

Check failure on line 152 in apps/provisioning_api/lib/Controller/UsersController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedInterfaceMethod

apps/provisioning_api/lib/Controller/UsersController.php:152:43: UndefinedInterfaceMethod: Method OCP\IGroupManager::getSubAdmin does not exist (see https://psalm.dev/181)
$isAdmin = $this->groupManager->isAdmin($uid);
$isDelegatedAdmin = $this->groupManager->isDelegatedAdmin($uid);
if ($isAdmin || $isDelegatedAdmin) {
Expand Down Expand Up @@ -218,7 +221,7 @@

// Admin? Or SubAdmin?
$uid = $currentUser->getUID();
$subAdminManager = $this->groupManager->getSubAdmin();

Check failure on line 224 in apps/provisioning_api/lib/Controller/UsersController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedInterfaceMethod

apps/provisioning_api/lib/Controller/UsersController.php:224:43: UndefinedInterfaceMethod: Method OCP\IGroupManager::getSubAdmin does not exist (see https://psalm.dev/181)
$isAdmin = $this->groupManager->isAdmin($uid);
$isDelegatedAdmin = $this->groupManager->isDelegatedAdmin($uid);
if ($isAdmin || $isDelegatedAdmin) {
Expand Down Expand Up @@ -456,7 +459,7 @@
$user = $this->userSession->getUser();
$isAdmin = $this->groupManager->isAdmin($user->getUID());
$isDelegatedAdmin = $this->groupManager->isDelegatedAdmin($user->getUID());
$subAdminManager = $this->groupManager->getSubAdmin();

Check failure on line 462 in apps/provisioning_api/lib/Controller/UsersController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedInterfaceMethod

apps/provisioning_api/lib/Controller/UsersController.php:462:43: UndefinedInterfaceMethod: Method OCP\IGroupManager::getSubAdmin does not exist (see https://psalm.dev/181)

if (empty($userid) && $this->config->getAppValue('core', 'newUser.generateUserID', 'no') === 'yes') {
$userid = $this->createNewUserId();
Expand Down Expand Up @@ -726,7 +729,7 @@
throw new OCSException('', OCSController::RESPOND_NOT_FOUND);
}

$subAdminManager = $this->groupManager->getSubAdmin();

Check failure on line 732 in apps/provisioning_api/lib/Controller/UsersController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedInterfaceMethod

apps/provisioning_api/lib/Controller/UsersController.php:732:44: UndefinedInterfaceMethod: Method OCP\IGroupManager::getSubAdmin does not exist (see https://psalm.dev/181)
$isAdmin = $this->groupManager->isAdmin($currentLoggedInUser->getUID());
$isDelegatedAdmin = $this->groupManager->isDelegatedAdmin($currentLoggedInUser->getUID());
if (
Expand Down Expand Up @@ -946,7 +949,7 @@
$permittedFields[] = IAccountManager::PROPERTY_PROFILE_ENABLED;
$permittedFields[] = IAccountManager::PROPERTY_BIRTHDATE;
$permittedFields[] = IAccountManager::PROPERTY_PRONOUNS;

$permittedFields[] = IAccountManager::PROPERTY_PHONE . self::SCOPE_SUFFIX;
$permittedFields[] = IAccountManager::PROPERTY_ADDRESS . self::SCOPE_SUFFIX;
$permittedFields[] = IAccountManager::PROPERTY_WEBSITE . self::SCOPE_SUFFIX;
Expand Down
3 changes: 3 additions & 0 deletions lib/private/SubAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ public function isSubAdmin(IUser $user): bool {
* @return bool
*/
public function isUserAccessible(IUser $subadmin, IUser $user): bool {
if ($subadmin->getUID() === $user->getUID()) {
return true;
}
if (!$this->isSubAdmin($subadmin)) {
return false;
}
Expand Down
Loading