Skip to content

Commit 3f7c321

Browse files
committed
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>
1 parent 0e649b9 commit 3f7c321

6 files changed

Lines changed: 69 additions & 113 deletions

File tree

core/AppInfo/Application.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
use OCP\IURLGenerator;
4141
use OCP\IUserSession;
4242
use OCP\L10N\IFactory;
43-
use OCP\Server;
4443
use OCP\User\Events\BeforeUserDeletedEvent;
4544
use OCP\User\Events\PasswordUpdatedEvent;
4645
use OCP\User\Events\UserDeletedEvent;
@@ -116,15 +115,16 @@ public function registerNavigationEntries(
116115
INavigationManager $navigationManager,
117116
IUserSession $userSession,
118117
IURLGenerator $urlGenerator,
118+
IFactory $factory,
119119
): void {
120120
if (!$userSession->isLoggedIn()) {
121121
return;
122122
}
123123

124-
$l = Server::get(IFactory::class)->get('core');
124+
$l = $factory->get('core');
125125

126126
// Register the logout button in the user settings
127-
$logoutUrl = \OC_User::getLogoutUrl($urlGenerator);
127+
$logoutUrl = $urlGenerator->getLogoutUrl();
128128
$navigationManager->add([
129129
'type' => 'settings',
130130
'id' => 'logout',

lib/private/URLGenerator.php

Lines changed: 43 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,36 @@
1010
namespace OC;
1111

1212
use OC\Route\Router;
13+
use OC\Security\CSRF\CsrfTokenManager;
1314
use OCA\Theming\ThemingDefaults;
1415
use OCP\App\AppPathNotFoundException;
1516
use OCP\App\IAppManager;
17+
use OCP\Authentication\IApacheBackend;
1618
use OCP\ICacheFactory;
1719
use OCP\IConfig;
1820
use OCP\INavigationManager;
1921
use OCP\IRequest;
2022
use OCP\IURLGenerator;
23+
use OCP\IUser;
24+
use OCP\IUserManager;
2125
use OCP\IUserSession;
2226
use OCP\Server;
27+
use OCP\User\Backend\ICustomLogout;
2328
use Override;
2429
use RuntimeException;
2530

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

3137
public function __construct(
32-
private IConfig $config,
38+
private readonly IConfig $config,
3339
public IUserSession $userSession,
34-
private ICacheFactory $cacheFactory,
35-
private IRequest $request,
36-
private Router $router,
40+
private readonly ICacheFactory $cacheFactory,
41+
private readonly IRequest $request,
42+
private readonly Router $router,
3743
) {
3844
}
3945

@@ -53,28 +59,11 @@ private function getNavigationManager(): INavigationManager {
5359
return $this->navigationManager;
5460
}
5561

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

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

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

157-
/**
158-
* Creates path to an image
159-
*
160-
* @param string $appName app
161-
* @param string $file image name
162-
* @throws \RuntimeException If the image does not exist
163-
* @return string the url
164-
*
165-
* Returns the path to the image.
166-
*/
167135
#[\Override]
168136
public function imagePath(string $appName, string $file): string {
169137
$cache = $this->cacheFactory->createDistributed('imagePath-' . md5($this->getBaseUrl()) . '-');
@@ -242,11 +210,6 @@ public function imagePath(string $appName, string $file): string {
242210
throw new RuntimeException('image not found: image:' . $file . ' webroot:' . \OC::$WEBROOT . ' serverroot:' . \OC::$SERVERROOT);
243211
}
244212

245-
/**
246-
* Makes an URL absolute
247-
* @param string $url the url in the Nextcloud host
248-
* @return string the absolute version of the url
249-
*/
250213
#[\Override]
251214
public function getAbsoluteURL(string $url): string {
252215
$separator = str_starts_with($url, '/') ? '' : '/';
@@ -262,21 +225,12 @@ public function getAbsoluteURL(string $url): string {
262225
return $this->getBaseUrl() . $separator . $url;
263226
}
264227

265-
/**
266-
* @param string $key
267-
* @return string url to the online documentation
268-
*/
269228
#[\Override]
270229
public function linkToDocs(string $key): string {
271230
$theme = Server::get('ThemingDefaults');
272231
return $theme->buildDocLinkToKey($key);
273232
}
274233

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-
*/
280234
#[\Override]
281235
public function linkToDefaultPageUrl(): string {
282236
// Deny the redirect if the URL contains a @
@@ -308,9 +262,6 @@ public function linkToDefaultPageUrl(): string {
308262
return $this->getAbsoluteURL($href);
309263
}
310264

311-
/**
312-
* @return string base url of the current request
313-
*/
314265
#[\Override]
315266
public function getBaseUrl(): string {
316267
// BaseUrl can be equal to 'http(s)://' during the first steps of the initial setup.
@@ -320,9 +271,6 @@ public function getBaseUrl(): string {
320271
return $this->baseUrl;
321272
}
322273

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

lib/private/User/Manager.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
use OCP\UserInterface;
4141
use OCP\Util;
4242
use Psr\Log\LoggerInterface;
43-
use RuntimeException;
4443

4544
/**
4645
* 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;
@@ -279,26 +276,7 @@ public static function isIncognitoMode(): bool {
279276
* @return non-empty-string
280277
*/
281278
public static function getLogoutUrl(IURLGenerator $urlGenerator): string {
282-
$backend = self::findFirstActiveUsedBackend();
283-
if ($backend) {
284-
return $backend->getLogoutUrl();
285-
}
286-
287-
$user = Server::get(IUserSession::class)->getUser();
288-
if ($user instanceof IUser) {
289-
$backend = $user->getBackend();
290-
if ($backend instanceof ICustomLogout) {
291-
$logoutUrl = $backend->getLogoutUrl();
292-
if ($logoutUrl !== '') {
293-
return $logoutUrl;
294-
}
295-
}
296-
}
297-
298-
$logoutUrl = $urlGenerator->linkToRoute('core.login.logout');
299-
$logoutUrl .= '?requesttoken=' . urlencode(Server::get(CsrfTokenManager::class)->getToken()->getEncryptedValue());
300-
301-
return $logoutUrl;
279+
return $urlGenerator->getLogoutUrl();
302280
}
303281

304282
/**

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
@@ -40,7 +40,7 @@ interface IURLGenerator {
4040

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

5050
/**
5151
* Returns the absolute URL for a route
52-
* @param string $routeName the name of the route
52+
* @param non-empty-string $routeName the name of the route
5353
* @param array $arguments an array with arguments which will be filled into the url
54-
* @return string the absolute url
54+
* @return non-empty-string the absolute url
5555
* @since 8.0.0
5656
*/
5757
public function linkToRouteAbsolute(string $routeName, array $arguments = []): string;
@@ -65,7 +65,7 @@ public function linkToRouteAbsolute(string $routeName, array $arguments = []): s
6565
public function linkToOCSRouteAbsolute(string $routeName, array $arguments = []): string;
6666

6767
/**
68-
* Returns an URL for an image or file
68+
* Returns a URL for an image or file
6969
* @param string $appName the name of the app
7070
* @param string $file the name of the file
7171
* @param array $args array with param=>value, will be appended to the returned url
@@ -86,9 +86,9 @@ public function linkTo(string $appName, string $file, array $args = []): string;
8686
public function imagePath(string $appName, string $file): string;
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)