mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 03:14:03 +00:00
Correct asset helpers, make asset helpers work with path identification (#6)
* Make asset_helper_tenancy false by default * Make tenant_asset() respect ASSET_URL * Set asset helper tenancy to true in tests where needed * If the `asset_helper_tenancy` key is missing, default to false in filesystem bootstrapper * Make temporary clone action changes * Make tenancy asset route universal * Make the asset controller's asset method behave differently if path ID MW is the default * Test that asset helper works with path identification * Fix code style (php-cs-fixer) * Delete path traversal attack prevention * Fix code style (php-cs-fixer) * Skip cloning of stancl.tenancy.asset route in some tests * Fix code style (php-cs-fixer) * Clone asset route in TSP stub * Add cloning only the passed route * Clone asset route in tenant asset test beforeEach * Skip asset route cloning by default * Fix typo * Change public method back to protected * Remove cloning of specific routes, skip cloning routes flagged as tenant * Delete constructor from asset controiler, change asset method to invoke * Update asset route registration, add prefixed asest route for path identification * Use default middleware from config instead of `tenancy()->defaultMiddleware()` * Delete old code from TSP stub * Revert TSP stub change * Revert FilesystemTenancyBootstrapper changes * Suffix asset url in tenant_asset() * Simplify `tenant_asset()` * Ensure the base asset url is always suffixed with '/' * remove unnecessary ?? false --------- Co-authored-by: PHP CS Fixer <phpcsfixer@example.com> Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
This commit is contained in:
parent
f7d9f02fd4
commit
8d38f42cd0
8 changed files with 82 additions and 18 deletions
|
|
@ -37,7 +37,9 @@ use Stancl\Tenancy\PathIdentificationManager;
|
|||
class CloneRoutesAsTenant
|
||||
{
|
||||
protected array $cloneRouteUsing = [];
|
||||
protected array $skippedRoutes = [];
|
||||
protected array $skippedRoutes = [
|
||||
'stancl.tenancy.asset',
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
protected Router $router,
|
||||
|
|
@ -87,7 +89,11 @@ class CloneRoutesAsTenant
|
|||
* universal routes will be available in both contexts.
|
||||
*/
|
||||
return collect($this->router->getRoutes()->get())->filter(function (Route $route) use ($tenantParameterName) {
|
||||
if (in_array($tenantParameterName, $route->parameterNames(), true) || in_array($route->getName(), $this->skippedRoutes, true)) {
|
||||
if (
|
||||
tenancy()->routeHasMiddleware($route, 'tenant') ||
|
||||
in_array($route->getName(), $this->skippedRoutes, true) ||
|
||||
in_array($tenantParameterName, $route->parameterNames(), true)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper
|
|||
}
|
||||
|
||||
// asset()
|
||||
if ($this->app['config']['tenancy.filesystem.asset_helper_tenancy'] ?? true) {
|
||||
if ($this->app['config']['tenancy.filesystem.asset_helper_tenancy']) {
|
||||
if ($this->originalPaths['asset_url']) {
|
||||
$this->app['config']['app.asset_url'] = $this->originalPaths['asset_url'] . "/$suffix";
|
||||
$this->app['url']->setAssetRoot($this->app['config']['app.asset_url']);
|
||||
|
|
|
|||
|
|
@ -5,21 +5,15 @@ declare(strict_types=1);
|
|||
namespace Stancl\Tenancy\Controllers;
|
||||
|
||||
use Illuminate\Routing\Controller;
|
||||
use Stancl\Tenancy\Tenancy;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
use Throwable;
|
||||
|
||||
class TenantAssetController extends Controller // todo@docs this was renamed from TenantAssetsController
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware(Tenancy::defaultMiddleware());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
|
||||
*/
|
||||
public function asset(string $path = null): BinaryFileResponse
|
||||
public function __invoke(string $path = null): BinaryFileResponse
|
||||
{
|
||||
abort_if($path === null, 404);
|
||||
|
||||
|
|
|
|||
|
|
@ -35,9 +35,18 @@ if (! function_exists('tenant')) {
|
|||
|
||||
if (! function_exists('tenant_asset')) {
|
||||
// todo docblock
|
||||
// todo add an option to generate paths respecting the ASSET_URL
|
||||
function tenant_asset(string|null $asset): string
|
||||
{
|
||||
if ($assetUrl = config('app.asset_url')) {
|
||||
$assetUrl = str($assetUrl)->rtrim('/')->append('/');
|
||||
|
||||
if (tenant()) {
|
||||
$assetUrl .= config('tenancy.filesystem.suffix_base') . tenant()->getTenantKey();
|
||||
}
|
||||
|
||||
return $assetUrl . $asset;
|
||||
}
|
||||
|
||||
return route('stancl.tenancy.asset', ['path' => $asset]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue