]> BookStack Code Mirror - bookstack/commitdiff
Started refactor of URL system to better extend Laravel
authorDan Brown <redacted>
Sun, 21 Jul 2019 20:32:08 +0000 (21:32 +0100)
committerDan Brown <redacted>
Sun, 21 Jul 2019 20:32:08 +0000 (21:32 +0100)
app/Http/Controllers/Auth/LoginController.php
app/Providers/AppServiceProvider.php
app/UrlGenerator.php [new file with mode: 0644]
app/helpers.php

index 78a8d33c0aed8ae342a5a80d86b4c2015ef02158..21ded23552431a0feb3b770df64e037c0ba2c6a5 100644 (file)
@@ -106,9 +106,7 @@ class LoginController extends Controller
             $this->ldapService->syncGroups($user, $request->get($this->username()));
         }
 
-        $path = session()->pull('url.intended', '/');
-        $path = baseUrl($path, true);
-        return redirect($path);
+        return redirect()->intended('/');
     }
 
     /**
index e45a0be92c79b0107ffe70b23a02514236b2f283..6bc1f90471503098cb33d2a3638223f1e757664d 100644 (file)
@@ -8,6 +8,7 @@ use BookStack\Entities\Chapter;
 use BookStack\Entities\Page;
 use BookStack\Settings\Setting;
 use BookStack\Settings\SettingService;
+use BookStack\UrlGenerator;
 use Illuminate\Database\Eloquent\Relations\Relation;
 use Illuminate\Support\Facades\View;
 use Illuminate\Support\ServiceProvider;
@@ -72,5 +73,10 @@ class AppServiceProvider extends ServiceProvider
         $this->app->singleton(SettingService::class, function ($app) {
             return new SettingService($app->make(Setting::class), $app->make('Illuminate\Contracts\Cache\Repository'));
         });
+
+        $this->app->bind(
+            \Illuminate\Contracts\Routing\UrlGenerator::class,
+            UrlGenerator::class
+        );
     }
 }
diff --git a/app/UrlGenerator.php b/app/UrlGenerator.php
new file mode 100644 (file)
index 0000000..471792c
--- /dev/null
@@ -0,0 +1,58 @@
+<?php
+
+namespace BookStack;
+
+class UrlGenerator extends \Illuminate\Routing\UrlGenerator
+{
+
+    /**
+     * Generate an absolute URL to the given path.
+     *
+     * @param  string  $path
+     * @param  mixed  $extra
+     * @param  bool|null  $secure
+     * @return string
+     */
+    public function to($path, $extra = [], $secure = null)
+    {
+        $tail = implode('/', array_map(
+                'rawurlencode', (array) $this->formatParameters($extra))
+        );
+
+        $defaultRoot = $this->formatRoot($this->formatScheme($secure));
+
+        list($path, $query) = $this->extractQueryString($path);
+
+        return $this->formatWithBase(
+                $defaultRoot, trim($path.'/'.$tail, '/')
+            ).$query;
+    }
+
+    /**
+     * Format the given URL segments into a single URL.
+     *
+     * @param  string  $defaultRoot
+     * @param  string  $path
+     * @return string
+     */
+    public function formatWithBase($defaultRoot, $path)
+    {
+        $isFullPath = strpos($path, 'http') === 0;
+        $setBasePath = trim(config('app.url'), '/');
+
+        if ($isFullPath) {
+            return $path;
+        }
+
+        if (! empty($setBasePath)) {
+            $defaultRoot = $setBasePath;
+        }
+
+        // TODO - Add mechanism to set path correctly for intended() and back() redirects
+        // TODO - Update tests to align with new system
+        // TODO - Clean up helpers and refactor their usage.
+
+        return trim($defaultRoot. '/' .$path, '/');
+    }
+
+}
\ No newline at end of file
index 8cb3fa3f4a22be3725da7510cdf448eb6a8a7e76..7bc1165553e1011676aba527aac725650e148973 100644 (file)
@@ -112,6 +112,7 @@ function setting($key = null, $default = false)
  */
 function baseUrl($path, $forceAppDomain = false)
 {
+    return url($path);
     $isFullUrl = strpos($path, 'http') === 0;
     if ($isFullUrl && !$forceAppDomain) {
         return $path;