Skip to content

Commit 04e1f41

Browse files
CarlSchwanbackportbot[bot]
authored andcommitted
refactor(urlgenerator): Move getLogoutUrl from OC_User to IUrlGenerator
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> [skip ci]
1 parent fc25556 commit 04e1f41

5 files changed

Lines changed: 66 additions & 105 deletions

File tree

lib/private/URLGenerator.php

Lines changed: 43 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,36 @@
99
namespace OC;
1010

1111
use OC\Route\Router;
12+
use OC\Security\CSRF\CsrfTokenManager;
1213
use OCA\Theming\ThemingDefaults;
1314
use OCP\App\AppPathNotFoundException;
1415
use OCP\App\IAppManager;
16+
use OCP\Authentication\IApacheBackend;
1517
use OCP\ICacheFactory;
1618
use OCP\IConfig;
1719
use OCP\INavigationManager;
1820
use OCP\IRequest;
1921
use OCP\IURLGenerator;
22+
use OCP\IUser;
23+
use OCP\IUserManager;
2024
use OCP\IUserSession;
2125
use OCP\Server;
26+
use OCP\User\Backend\ICustomLogout;
2227
use Override;
2328
use RuntimeException;
2429

2530
class URLGenerator implements IURLGenerator {
31+
/** @var non-empty-string|null $baseUrl */
2632
private ?string $baseUrl = null;
2733
private ?IAppManager $appManager = null;
2834
private ?INavigationManager $navigationManager = null;
2935

3036
public function __construct(
31-
private IConfig $config,
37+
private readonly IConfig $config,
3238
public IUserSession $userSession,
33-
private ICacheFactory $cacheFactory,
34-
private IRequest $request,
35-
private Router $router,
39+
private readonly ICacheFactory $cacheFactory,
40+
private readonly IRequest $request,
41+
private readonly Router $router,
3642
) {
3743
}
3844

@@ -52,28 +58,11 @@ private function getNavigationManager(): INavigationManager {
5258
return $this->navigationManager;
5359
}
5460

55-
/**
56-
* Creates an url using a defined route
57-
*
58-
* @param string $routeName
59-
* @param array $arguments args with param=>value, will be appended to the returned url
60-
* @return string the url
61-
*
62-
* Returns a url to the given route.
63-
*/
6461
#[\Override]
6562
public function linkToRoute(string $routeName, array $arguments = []): string {
6663
return $this->router->generate($routeName, $arguments);
6764
}
6865

69-
/**
70-
* Creates an absolute url using a defined route
71-
* @param string $routeName
72-
* @param array $arguments args with param=>value, will be appended to the returned url
73-
* @return string the url
74-
*
75-
* Returns an absolute url to the given route.
76-
*/
7766
#[\Override]
7867
public function linkToRouteAbsolute(string $routeName, array $arguments = []): string {
7968
return $this->getAbsoluteURL($this->linkToRoute($routeName, $arguments));
@@ -103,17 +92,6 @@ public function linkToOCSRouteAbsolute(string $routeName, array $arguments = [])
10392
return $this->getAbsoluteURL($route);
10493
}
10594

106-
/**
107-
* Creates an url
108-
*
109-
* @param string $appName app
110-
* @param string $file file
111-
* @param array $args array with param=>value, will be appended to the returned url
112-
* The value of $args will be urlencoded
113-
* @return string the url
114-
*
115-
* Returns a url to the given app and file.
116-
*/
11795
#[\Override]
11896
public function linkTo(string $appName, string $file, array $args = []): string {
11997
$frontControllerActive = ($this->config->getSystemValueBool('htaccess.IgnoreFrontController', false) || getenv('front_controller_active') === 'true');
@@ -153,16 +131,6 @@ public function linkTo(string $appName, string $file, array $args = []): string
153131
return $urlLinkTo;
154132
}
155133

156-
/**
157-
* Creates path to an image
158-
*
159-
* @param string $appName app
160-
* @param string $file image name
161-
* @throws \RuntimeException If the image does not exist
162-
* @return string the url
163-
*
164-
* Returns the path to the image.
165-
*/
166134
#[\Override]
167135
public function imagePath(string $appName, string $file): string {
168136
$cache = $this->cacheFactory->createDistributed('imagePath-' . md5($this->getBaseUrl()) . '-');
@@ -262,21 +230,12 @@ public function getAbsoluteURL(string $url): string {
262230
return $this->getBaseUrl() . $separator . $url;
263231
}
264232

265-
/**
266-
* @param string $key
267-
* @return string url to the online documentation
268-
*/
269233
#[\Override]
270234
public function linkToDocs(string $key): string {
271235
$theme = Server::get('ThemingDefaults');
272236
return $theme->buildDocLinkToKey($key);
273237
}
274238

275-
/**
276-
* Returns the URL of the default page based on the system configuration
277-
* and the apps visible for the current user
278-
* @return string
279-
*/
280239
#[\Override]
281240
public function linkToDefaultPageUrl(): string {
282241
// Deny the redirect if the URL contains a @
@@ -308,9 +267,6 @@ public function linkToDefaultPageUrl(): string {
308267
return $this->getAbsoluteURL($href);
309268
}
310269

311-
/**
312-
* @return string base url of the current request
313-
*/
314270
#[\Override]
315271
public function getBaseUrl(): string {
316272
// BaseUrl can be equal to 'http(s)://' during the first steps of the initial setup.
@@ -320,9 +276,6 @@ public function getBaseUrl(): string {
320276
return $this->baseUrl;
321277
}
322278

323-
/**
324-
* @return string webroot part of the base url
325-
*/
326279
#[\Override]
327280
public function getWebroot(): string {
328281
return \OC::$WEBROOT;
@@ -335,4 +288,37 @@ public function linkToRemote(string $service): string {
335288
$remoteBase . (($service[strlen($service) - 1] !== '/') ? '/' : '')
336289
);
337290
}
291+
292+
#[Override]
293+
public function getLogoutUrl(): string {
294+
$apacheBackend = null;
295+
foreach (Server::get(IUserManager::class)->getBackends() as $backend) {
296+
if ($backend instanceof IApacheBackend) {
297+
if ($backend->isSessionActive()) {
298+
$apacheBackend = $backend;
299+
break;
300+
}
301+
}
302+
}
303+
304+
if ($apacheBackend) {
305+
return $apacheBackend->getLogoutUrl();
306+
}
307+
308+
$user = $this->userSession->getUser();
309+
if ($user instanceof IUser) {
310+
$backend = $user->getBackend();
311+
if ($backend instanceof ICustomLogout) {
312+
$logoutUrl = $backend->getLogoutUrl();
313+
if ($logoutUrl !== '') {
314+
return $logoutUrl;
315+
}
316+
}
317+
}
318+
319+
$logoutUrl = $this->linkToRoute('core.login.logout');
320+
$logoutUrl .= '?requesttoken=' . urlencode(Server::get(CsrfTokenManager::class)->getToken()->getEncryptedValue());
321+
322+
return $logoutUrl;
323+
}
338324
}

lib/private/User/Manager.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
use OCP\UserInterface;
3939
use OCP\Util;
4040
use Psr\Log\LoggerInterface;
41-
use RuntimeException;
4241

4342
/**
4443
* Class Manager
@@ -871,21 +870,11 @@ public function getExistingUser(string $userId, ?string $displayName = null): IU
871870

872871
#[\Override]
873872
public function getAvatarUrlLight(string $userId, int $size): string {
874-
$url = ($this->urlGenerator ??= Server::get(IURLGenerator::class))->linkToRouteAbsolute('core.avatar.getAvatar', ['userId' => $userId, 'size' => $size]);
875-
if ($url === '') {
876-
throw new RuntimeException('The URL is empty.');
877-
}
878-
879-
return $url;
873+
return ($this->urlGenerator ??= Server::get(IURLGenerator::class))->linkToRouteAbsolute('core.avatar.getAvatar', ['userId' => $userId, 'size' => $size]);
880874
}
881875

882876
#[\Override]
883877
public function getAvatarUrlDark(string $userId, int $size): string {
884-
$url = ($this->urlGenerator ??= Server::get(IURLGenerator::class))->linkToRouteAbsolute('core.avatar.getAvatarDark', ['userId' => $userId, 'size' => $size]);
885-
if ($url === '') {
886-
throw new RuntimeException('The URL is empty.');
887-
}
888-
889-
return $url;
878+
return ($this->urlGenerator ??= Server::get(IURLGenerator::class))->linkToRouteAbsolute('core.avatar.getAvatarDark', ['userId' => $userId, 'size' => $size]);
890879
}
891880
}

lib/private/legacy/OC_User.php

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* SPDX-License-Identifier: AGPL-3.0-only
77
*/
88
use OC\Authentication\Token\IProvider;
9-
use OC\Security\CSRF\CsrfTokenManager;
109
use OC\SystemConfig;
1110
use OC\User\Database;
1211
use OC\User\DisabledUserException;
@@ -22,12 +21,10 @@
2221
use OCP\IRequest;
2322
use OCP\ISession;
2423
use OCP\IURLGenerator;
25-
use OCP\IUser;
2624
use OCP\IUserManager;
2725
use OCP\IUserSession;
2826
use OCP\Server;
2927
use OCP\Session\Exceptions\SessionNotAvailableException;
30-
use OCP\User\Backend\ICustomLogout;
3128
use OCP\User\Events\BeforeUserLoggedInEvent;
3229
use OCP\User\Events\UserLoggedInEvent;
3330
use OCP\UserInterface;
@@ -280,26 +277,7 @@ public static function isIncognitoMode(): bool {
280277
* @return non-empty-string
281278
*/
282279
public static function getLogoutUrl(IURLGenerator $urlGenerator): string {
283-
$backend = self::findFirstActiveUsedBackend();
284-
if ($backend) {
285-
return $backend->getLogoutUrl();
286-
}
287-
288-
$user = Server::get(IUserSession::class)->getUser();
289-
if ($user instanceof IUser) {
290-
$backend = $user->getBackend();
291-
if ($backend instanceof ICustomLogout) {
292-
$logoutUrl = $backend->getLogoutUrl();
293-
if ($logoutUrl !== '') {
294-
return $logoutUrl;
295-
}
296-
}
297-
}
298-
299-
$logoutUrl = $urlGenerator->linkToRoute('core.login.logout');
300-
$logoutUrl .= '?requesttoken=' . urlencode(Server::get(CsrfTokenManager::class)->getToken()->getEncryptedValue());
301-
302-
return $logoutUrl;
280+
return $urlGenerator->getLogoutUrl();
303281
}
304282

305283
/**

lib/public/Authentication/IApacheBackend.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,23 @@ interface IApacheBackend {
1919
/**
2020
* In case the user has been authenticated by a module true is returned.
2121
*
22-
* @return boolean whether the module reports a user as currently logged in.
22+
* @return bool whether the module reports a user as currently logged in.
2323
* @since 6.0.0
2424
*/
25-
public function isSessionActive();
25+
public function isSessionActive(): bool;
2626

2727
/**
2828
* Gets the current logout URL
2929
*
30-
* @return string
30+
* @return non-empty-string
3131
* @since 12.0.3
3232
*/
33-
public function getLogoutUrl();
33+
public function getLogoutUrl(): string;
3434

3535
/**
3636
* Return the id of the current user
3737
* @return string
3838
* @since 6.0.0
3939
*/
40-
public function getCurrentUserId();
40+
public function getCurrentUserId(): string;
4141
}

lib/public/IURLGenerator.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ interface IURLGenerator {
3939

4040
/**
4141
* Returns the URL for a route
42-
* @param string $routeName the name of the route
42+
* @param non-empty-string $routeName the name of the route
4343
* @param array $arguments an array with arguments which will be filled into the url
4444
* @return string the url
4545
* @since 6.0.0
@@ -48,9 +48,9 @@ public function linkToRoute(string $routeName, array $arguments = []): string;
4848

4949
/**
5050
* Returns the absolute URL for a route
51-
* @param string $routeName the name of the route
51+
* @param non-empty-string $routeName the name of the route
5252
* @param array $arguments an array with arguments which will be filled into the url
53-
* @return string the absolute url
53+
* @return non-empty-string the absolute url
5454
* @since 8.0.0
5555
*/
5656
public function linkToRouteAbsolute(string $routeName, array $arguments = []): string;
@@ -64,7 +64,7 @@ public function linkToRouteAbsolute(string $routeName, array $arguments = []): s
6464
public function linkToOCSRouteAbsolute(string $routeName, array $arguments = []): string;
6565

6666
/**
67-
* Returns an URL for an image or file
67+
* Returns a URL for an image or file
6868
* @param string $appName the name of the app
6969
* @param string $file the name of the file
7070
* @param array $args array with param=>value, will be appended to the returned url
@@ -86,9 +86,9 @@ public function imagePath(string $appName, string $file): string;
8686

8787

8888
/**
89-
* Makes an URL absolute
89+
* Makes a URL absolute
9090
* @param string $url the url in the ownCloud host
91-
* @return string the absolute version of the url
91+
* @return non-empty-string the absolute version of the url
9292
* @since 6.0.0
9393
*/
9494
public function getAbsoluteURL(string $url): string;
@@ -109,7 +109,7 @@ public function linkToDocs(string $key): string;
109109
public function linkToDefaultPageUrl(): string;
110110

111111
/**
112-
* @return string base url of the current request
112+
* @return non-empty-string base url of the current request
113113
* @since 13.0.0
114114
*/
115115
public function getBaseUrl(): string;
@@ -126,4 +126,12 @@ public function getWebroot(): string;
126126
* @since 34.0.0
127127
*/
128128
public function linkToRemote(string $service): string;
129+
130+
/**
131+
* Return the url to the logout action.
132+
*
133+
* @return non-empty-string
134+
* @since 35.0.0
135+
*/
136+
public function getLogoutUrl(): string;
129137
}

0 commit comments

Comments
 (0)