refactor(urlgenerator): Move getLogoutUrl from OC_User to IUrlGenerator

And some minor cleanup of both IApacheBackend and UrlGenerator to
properly type non-empty-string

Signed-off-by: Carl Schwan <carlschwan@kde.org>
pull/62081/head
Carl Schwan 1 month ago
parent 0e649b9eeb
commit 3f7c3216fe
No known key found for this signature in database
GPG Key ID: 02325448204E452A
  1. 6
      core/AppInfo/Application.php
  2. 105
      lib/private/URLGenerator.php
  3. 15
      lib/private/User/Manager.php
  4. 24
      lib/private/legacy/OC_User.php
  5. 10
      lib/public/Authentication/IApacheBackend.php
  6. 22
      lib/public/IURLGenerator.php

@ -40,7 +40,6 @@ use OCP\Interaction\RestrictInteractionEvent;
use OCP\IURLGenerator;
use OCP\IUserSession;
use OCP\L10N\IFactory;
use OCP\Server;
use OCP\User\Events\BeforeUserDeletedEvent;
use OCP\User\Events\PasswordUpdatedEvent;
use OCP\User\Events\UserDeletedEvent;
@ -116,15 +115,16 @@ class Application extends App implements IBootstrap {
INavigationManager $navigationManager,
IUserSession $userSession,
IURLGenerator $urlGenerator,
IFactory $factory,
): void {
if (!$userSession->isLoggedIn()) {
return;
}
$l = Server::get(IFactory::class)->get('core');
$l = $factory->get('core');
// Register the logout button in the user settings
$logoutUrl = \OC_User::getLogoutUrl($urlGenerator);
$logoutUrl = $urlGenerator->getLogoutUrl();
$navigationManager->add([
'type' => 'settings',
'id' => 'logout',

@ -10,30 +10,36 @@ declare(strict_types=1);
namespace OC;
use OC\Route\Router;
use OC\Security\CSRF\CsrfTokenManager;
use OCA\Theming\ThemingDefaults;
use OCP\App\AppPathNotFoundException;
use OCP\App\IAppManager;
use OCP\Authentication\IApacheBackend;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\INavigationManager;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Server;
use OCP\User\Backend\ICustomLogout;
use Override;
use RuntimeException;
class URLGenerator implements IURLGenerator {
/** @var non-empty-string|null $baseUrl */
private ?string $baseUrl = null;
private ?IAppManager $appManager = null;
private ?INavigationManager $navigationManager = null;
public function __construct(
private IConfig $config,
private readonly IConfig $config,
public IUserSession $userSession,
private ICacheFactory $cacheFactory,
private IRequest $request,
private Router $router,
private readonly ICacheFactory $cacheFactory,
private readonly IRequest $request,
private readonly Router $router,
) {
}
@ -53,28 +59,11 @@ class URLGenerator implements IURLGenerator {
return $this->navigationManager;
}
/**
* Creates an url using a defined route
*
* @param string $routeName
* @param array $arguments args with param=>value, will be appended to the returned url
* @return string the url
*
* Returns a url to the given route.
*/
#[\Override]
public function linkToRoute(string $routeName, array $arguments = []): string {
return $this->router->generate($routeName, $arguments);
}
/**
* Creates an absolute url using a defined route
* @param string $routeName
* @param array $arguments args with param=>value, will be appended to the returned url
* @return string the url
*
* Returns an absolute url to the given route.
*/
#[\Override]
public function linkToRouteAbsolute(string $routeName, array $arguments = []): string {
return $this->getAbsoluteURL($this->linkToRoute($routeName, $arguments));
@ -104,17 +93,6 @@ class URLGenerator implements IURLGenerator {
return $this->getAbsoluteURL($route);
}
/**
* Creates an url
*
* @param string $appName app
* @param string $file file
* @param array $args array with param=>value, will be appended to the returned url
* The value of $args will be urlencoded
* @return string the url
*
* Returns a url to the given app and file.
*/
#[\Override]
public function linkTo(string $appName, string $file, array $args = []): string {
$frontControllerActive = ($this->config->getSystemValueBool('htaccess.IgnoreFrontController', false) || getenv('front_controller_active') === 'true');
@ -154,16 +132,6 @@ class URLGenerator implements IURLGenerator {
return $urlLinkTo;
}
/**
* Creates path to an image
*
* @param string $appName app
* @param string $file image name
* @throws \RuntimeException If the image does not exist
* @return string the url
*
* Returns the path to the image.
*/
#[\Override]
public function imagePath(string $appName, string $file): string {
$cache = $this->cacheFactory->createDistributed('imagePath-' . md5($this->getBaseUrl()) . '-');
@ -242,11 +210,6 @@ class URLGenerator implements IURLGenerator {
throw new RuntimeException('image not found: image:' . $file . ' webroot:' . \OC::$WEBROOT . ' serverroot:' . \OC::$SERVERROOT);
}
/**
* Makes an URL absolute
* @param string $url the url in the Nextcloud host
* @return string the absolute version of the url
*/
#[\Override]
public function getAbsoluteURL(string $url): string {
$separator = str_starts_with($url, '/') ? '' : '/';
@ -262,21 +225,12 @@ class URLGenerator implements IURLGenerator {
return $this->getBaseUrl() . $separator . $url;
}
/**
* @param string $key
* @return string url to the online documentation
*/
#[\Override]
public function linkToDocs(string $key): string {
$theme = Server::get('ThemingDefaults');
return $theme->buildDocLinkToKey($key);
}
/**
* Returns the URL of the default page based on the system configuration
* and the apps visible for the current user
* @return string
*/
#[\Override]
public function linkToDefaultPageUrl(): string {
// Deny the redirect if the URL contains a @
@ -308,9 +262,6 @@ class URLGenerator implements IURLGenerator {
return $this->getAbsoluteURL($href);
}
/**
* @return string base url of the current request
*/
#[\Override]
public function getBaseUrl(): string {
// BaseUrl can be equal to 'http(s)://' during the first steps of the initial setup.
@ -320,9 +271,6 @@ class URLGenerator implements IURLGenerator {
return $this->baseUrl;
}
/**
* @return string webroot part of the base url
*/
#[\Override]
public function getWebroot(): string {
return \OC::$WEBROOT;
@ -335,4 +283,37 @@ class URLGenerator implements IURLGenerator {
$remoteBase . (($service[strlen($service) - 1] !== '/') ? '/' : '')
);
}
#[Override]
public function getLogoutUrl(): string {
$apacheBackend = null;
foreach (Server::get(IUserManager::class)->getBackends() as $backend) {
if ($backend instanceof IApacheBackend) {
if ($backend->isSessionActive()) {
$apacheBackend = $backend;
break;
}
}
}
if ($apacheBackend) {
return $apacheBackend->getLogoutUrl();
}
$user = $this->userSession->getUser();
if ($user instanceof IUser) {
$backend = $user->getBackend();
if ($backend instanceof ICustomLogout) {
$logoutUrl = $backend->getLogoutUrl();
if ($logoutUrl !== '') {
return $logoutUrl;
}
}
}
$logoutUrl = $this->linkToRoute('core.login.logout');
$logoutUrl .= '?requesttoken=' . urlencode(Server::get(CsrfTokenManager::class)->getToken()->getEncryptedValue());
return $logoutUrl;
}
}

@ -40,7 +40,6 @@ use OCP\User\Exceptions\UserNotFoundException;
use OCP\UserInterface;
use OCP\Util;
use Psr\Log\LoggerInterface;
use RuntimeException;
/**
* Class Manager
@ -871,21 +870,11 @@ class Manager extends PublicEmitter implements IUserManager {
#[\Override]
public function getAvatarUrlLight(string $userId, int $size): string {
$url = ($this->urlGenerator ??= Server::get(IURLGenerator::class))->linkToRouteAbsolute('core.avatar.getAvatar', ['userId' => $userId, 'size' => $size]);
if ($url === '') {
throw new RuntimeException('The URL is empty.');
}
return $url;
return ($this->urlGenerator ??= Server::get(IURLGenerator::class))->linkToRouteAbsolute('core.avatar.getAvatar', ['userId' => $userId, 'size' => $size]);
}
#[\Override]
public function getAvatarUrlDark(string $userId, int $size): string {
$url = ($this->urlGenerator ??= Server::get(IURLGenerator::class))->linkToRouteAbsolute('core.avatar.getAvatarDark', ['userId' => $userId, 'size' => $size]);
if ($url === '') {
throw new RuntimeException('The URL is empty.');
}
return $url;
return ($this->urlGenerator ??= Server::get(IURLGenerator::class))->linkToRouteAbsolute('core.avatar.getAvatarDark', ['userId' => $userId, 'size' => $size]);
}
}

@ -6,7 +6,6 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
use OC\Authentication\Token\IProvider;
use OC\Security\CSRF\CsrfTokenManager;
use OC\SystemConfig;
use OC\User\Database;
use OC\User\DisabledUserException;
@ -22,12 +21,10 @@ use OCP\IGroupManager;
use OCP\IRequest;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Server;
use OCP\Session\Exceptions\SessionNotAvailableException;
use OCP\User\Backend\ICustomLogout;
use OCP\User\Events\BeforeUserLoggedInEvent;
use OCP\User\Events\UserLoggedInEvent;
use OCP\UserInterface;
@ -279,26 +276,7 @@ class OC_User {
* @return non-empty-string
*/
public static function getLogoutUrl(IURLGenerator $urlGenerator): string {
$backend = self::findFirstActiveUsedBackend();
if ($backend) {
return $backend->getLogoutUrl();
}
$user = Server::get(IUserSession::class)->getUser();
if ($user instanceof IUser) {
$backend = $user->getBackend();
if ($backend instanceof ICustomLogout) {
$logoutUrl = $backend->getLogoutUrl();
if ($logoutUrl !== '') {
return $logoutUrl;
}
}
}
$logoutUrl = $urlGenerator->linkToRoute('core.login.logout');
$logoutUrl .= '?requesttoken=' . urlencode(Server::get(CsrfTokenManager::class)->getToken()->getEncryptedValue());
return $logoutUrl;
return $urlGenerator->getLogoutUrl();
}
/**

@ -19,23 +19,23 @@ interface IApacheBackend {
/**
* In case the user has been authenticated by a module true is returned.
*
* @return boolean whether the module reports a user as currently logged in.
* @return bool whether the module reports a user as currently logged in.
* @since 6.0.0
*/
public function isSessionActive();
public function isSessionActive(): bool;
/**
* Gets the current logout URL
*
* @return string
* @return non-empty-string
* @since 12.0.3
*/
public function getLogoutUrl();
public function getLogoutUrl(): string;
/**
* Return the id of the current user
* @return string
* @since 6.0.0
*/
public function getCurrentUserId();
public function getCurrentUserId(): string;
}

@ -40,7 +40,7 @@ interface IURLGenerator {
/**
* Returns the URL for a route
* @param string $routeName the name of the route
* @param non-empty-string $routeName the name of the route
* @param array $arguments an array with arguments which will be filled into the url
* @return string the url
* @since 6.0.0
@ -49,9 +49,9 @@ interface IURLGenerator {
/**
* Returns the absolute URL for a route
* @param string $routeName the name of the route
* @param non-empty-string $routeName the name of the route
* @param array $arguments an array with arguments which will be filled into the url
* @return string the absolute url
* @return non-empty-string the absolute url
* @since 8.0.0
*/
public function linkToRouteAbsolute(string $routeName, array $arguments = []): string;
@ -65,7 +65,7 @@ interface IURLGenerator {
public function linkToOCSRouteAbsolute(string $routeName, array $arguments = []): string;
/**
* Returns an URL for an image or file
* Returns a URL for an image or file
* @param string $appName the name of the app
* @param string $file the name of the file
* @param array $args array with param=>value, will be appended to the returned url
@ -86,9 +86,9 @@ interface IURLGenerator {
public function imagePath(string $appName, string $file): string;
/**
* Makes an URL absolute
* Makes a URL absolute
* @param string $url the url in the ownCloud host
* @return string the absolute version of the url
* @return non-empty-string the absolute version of the url
* @since 6.0.0
*/
public function getAbsoluteURL(string $url): string;
@ -109,7 +109,7 @@ interface IURLGenerator {
public function linkToDefaultPageUrl(): string;
/**
* @return string base url of the current request
* @return non-empty-string base url of the current request
* @since 13.0.0
*/
public function getBaseUrl(): string;
@ -126,4 +126,12 @@ interface IURLGenerator {
* @since 34.0.0
*/
public function linkToRemote(string $service): string;
/**
* Return the url to the logout action.
*
* @return non-empty-string
* @since 35.0.0
*/
public function getLogoutUrl(): string;
}

Loading…
Cancel
Save