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(admin role): fix old and wrong way to determine whether user is admin #42706

Merged
merged 2 commits into from
Feb 24, 2024
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
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
Loading