99namespace OC ;
1010
1111use OC \Route \Router ;
12+ use OC \Security \CSRF \CsrfTokenManager ;
1213use OCA \Theming \ThemingDefaults ;
1314use OCP \App \AppPathNotFoundException ;
1415use OCP \App \IAppManager ;
16+ use OCP \Authentication \IApacheBackend ;
1517use OCP \ICacheFactory ;
1618use OCP \IConfig ;
1719use OCP \INavigationManager ;
1820use OCP \IRequest ;
1921use OCP \IURLGenerator ;
22+ use OCP \IUser ;
23+ use OCP \IUserManager ;
2024use OCP \IUserSession ;
2125use OCP \Server ;
26+ use OCP \User \Backend \ICustomLogout ;
2227use Override ;
2328use RuntimeException ;
2429
2530class 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}
0 commit comments