Started refactor of URL system to better extend Laravel

This commit is contained in:
Dan Brown 2019-07-21 21:32:08 +01:00
parent 1e7df28238
commit 30da105812
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
4 changed files with 66 additions and 3 deletions

View 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('/');
}
/**

View 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
);
}
}

58
app/UrlGenerator.php Normal file
View File

@ -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, '/');
}
}

View 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;