1010namespace OC ;
1111
1212use OC \Route \Router ;
13+ use OC \Security \CSRF \CsrfTokenManager ;
1314use OCA \Theming \ThemingDefaults ;
1415use OCP \App \AppPathNotFoundException ;
1516use OCP \App \IAppManager ;
17+ use OCP \Authentication \IApacheBackend ;
1618use OCP \ICacheFactory ;
1719use OCP \IConfig ;
1820use OCP \INavigationManager ;
1921use OCP \IRequest ;
2022use OCP \IURLGenerator ;
23+ use OCP \IUser ;
24+ use OCP \IUserManager ;
2125use OCP \IUserSession ;
2226use OCP \Server ;
27+ use OCP \User \Backend \ICustomLogout ;
2328use Override ;
2429use RuntimeException ;
2530
2631class 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}
0 commit comments