Skip to content

Commit

Permalink
Merge pull request #42706 from nextcloud/fix/42480/user-admin-not-admin
Browse files Browse the repository at this point in the history
  • Loading branch information
skjnldsv authored Feb 24, 2024
2 parents 1d09dec + b080113 commit 0582e88
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 22 deletions.
4 changes: 0 additions & 4 deletions apps/settings/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,6 @@ public function register(IRegistrationContext $context): void {
/**
* Core class wrappers
*/
/** FIXME: Remove once OC_User is non-static and mockable */
$context->registerService('isAdmin', function () {
return \OC_User::isAdminUser(\OC_User::getUser());
});
/** FIXME: Remove once OC_SubAdmin is non-static and mockable */
$context->registerService('isSubAdmin', function () {
$userObject = \OC::$server->getUserSession()->getUser();
Expand Down
8 changes: 4 additions & 4 deletions apps/settings/lib/Controller/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public function __construct(
private IGroupManager $groupManager,
private IUserSession $userSession,
private IConfig $config,
private bool $isAdmin,
private IL10N $l10n,
private IMailer $mailer,
private IFactory $l10nFactory,
Expand Down Expand Up @@ -119,6 +118,7 @@ public function usersListByGroup(): TemplateResponse {
public function usersList(): TemplateResponse {
$user = $this->userSession->getUser();
$uid = $user->getUID();
$isAdmin = $this->groupManager->isAdmin($uid);

\OC::$server->getNavigationManager()->setActiveEntry('core_users');

Expand All @@ -143,7 +143,7 @@ public function usersList(): TemplateResponse {
/* GROUPS */
$groupsInfo = new \OC\Group\MetaData(
$uid,
$this->isAdmin,
$isAdmin,
$this->groupManager,
$this->userSession
);
Expand All @@ -161,7 +161,7 @@ public function usersList(): TemplateResponse {
$userCount = 0;

if (!$isLDAPUsed) {
if ($this->isAdmin) {
if ($isAdmin) {
$disabledUsers = $this->userManager->countDisabledUsers();
$userCount = array_reduce($this->userManager->countUsers(), function ($v, $w) {
return $v + (int)$w;
Expand Down Expand Up @@ -217,7 +217,7 @@ public function usersList(): TemplateResponse {
// groups
$serverData['groups'] = array_merge_recursive($adminGroup, [$disabledUsersGroup], $groups);
// Various data
$serverData['isAdmin'] = $this->isAdmin;
$serverData['isAdmin'] = $isAdmin;
$serverData['sortGroups'] = $sortGroupsBy;
$serverData['quotaPreset'] = $quotaPreset;
$serverData['allowUnlimitedQuota'] = $allowUnlimitedQuota;
Expand Down
6 changes: 4 additions & 2 deletions apps/settings/tests/Controller/UsersControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ protected function setUp(): void {
* @return UsersController | \PHPUnit\Framework\MockObject\MockObject
*/
protected function getController($isAdmin = false, $mockedMethods = []) {
$this->groupManager->expects($this->any())
->method('isAdmin')
->willReturn($isAdmin);

if (empty($mockedMethods)) {
return new UsersController(
'settings',
Expand All @@ -141,7 +145,6 @@ protected function getController($isAdmin = false, $mockedMethods = []) {
$this->groupManager,
$this->userSession,
$this->config,
$isAdmin,
$this->l,
$this->mailer,
$this->l10nFactory,
Expand All @@ -164,7 +167,6 @@ protected function getController($isAdmin = false, $mockedMethods = []) {
$this->groupManager,
$this->userSession,
$this->config,
$isAdmin,
$this->l,
$this->mailer,
$this->l10nFactory,
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Group/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
use OCP\IGroupManager;
use OCP\IUser;
use Psr\Log\LoggerInterface;
use function is_string;

/**
* Class Manager
Expand Down Expand Up @@ -356,7 +357,7 @@ public function getUserIdGroups(string $uid): array {
*/
public function isAdmin($userId) {
foreach ($this->backends as $backend) {
if ($backend->implementsActions(Backend::IS_ADMIN) && $backend->isAdmin($userId)) {
if (is_string($userId) && $backend->implementsActions(Backend::IS_ADMIN) && $backend->isAdmin($userId)) {
return true;
}
}
Expand Down
22 changes: 11 additions & 11 deletions lib/private/legacy/OC_User.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@

use OC\User\LoginException;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserManager;
use OCP\Server;
use OCP\User\Events\BeforeUserLoggedInEvent;
use OCP\User\Events\UserLoggedInEvent;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -93,7 +96,7 @@ public static function useBackend($backend = 'database') {
case 'database':
case 'mysql':
case 'sqlite':
\OCP\Server::get(LoggerInterface::class)->debug('Adding user backend ' . $backend . '.', ['app' => 'core']);
Server::get(LoggerInterface::class)->debug('Adding user backend ' . $backend . '.', ['app' => 'core']);
self::$_usedBackends[$backend] = new \OC\User\Database();
\OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
break;
Expand All @@ -102,7 +105,7 @@ public static function useBackend($backend = 'database') {
\OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
break;
default:
\OCP\Server::get(LoggerInterface::class)->debug('Adding default user backend ' . $backend . '.', ['app' => 'core']);
Server::get(LoggerInterface::class)->debug('Adding default user backend ' . $backend . '.', ['app' => 'core']);
$className = 'OC_USER_' . strtoupper($backend);
self::$_usedBackends[$backend] = new $className();
\OC::$server->getUserManager()->registerBackend(self::$_usedBackends[$backend]);
Expand Down Expand Up @@ -147,10 +150,10 @@ public static function setupBackends() {
self::useBackend($backend);
self::$_setupedBackends[] = $i;
} else {
\OCP\Server::get(LoggerInterface::class)->debug('User backend ' . $class . ' already initialized.', ['app' => 'core']);
Server::get(LoggerInterface::class)->debug('User backend ' . $class . ' already initialized.', ['app' => 'core']);
}
} else {
\OCP\Server::get(LoggerInterface::class)->error('User backend ' . $class . ' not found.', ['app' => 'core']);
Server::get(LoggerInterface::class)->error('User backend ' . $class . ' not found.', ['app' => 'core']);
}
}
}
Expand Down Expand Up @@ -303,7 +306,7 @@ public static function getLogoutUrl(\OCP\IURLGenerator $urlGenerator) {
}

$user = \OC::$server->getUserSession()->getUser();
if ($user instanceof \OCP\IUser) {
if ($user instanceof IUser) {
$backend = $user->getBackend();
if ($backend instanceof \OCP\User\Backend\ICustomLogout) {
return $backend->getLogoutUrl();
Expand All @@ -323,12 +326,9 @@ public static function getLogoutUrl(\OCP\IURLGenerator $urlGenerator) {
* @return bool
*/
public static function isAdminUser($uid) {
$group = \OC::$server->getGroupManager()->get('admin');
$user = \OC::$server->getUserManager()->get($uid);
if ($group && $user && $group->inGroup($user) && self::$incognitoMode === false) {
return true;
}
return false;
$user = Server::get(IUserManager::class)->get($uid);
$isAdmin = $user && Server::get(IGroupManager::class)->isAdmin($user->getUID());
return $isAdmin && self::$incognitoMode === false;
}


Expand Down

0 comments on commit 0582e88

Please sign in to comment.