mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 17:24:03 +00:00
* Add commented UrlBinding + FortifyRouteTenancy bootstrappers to the config * Improve FortifyRoute bootstrapper docblock * Rename bootstrappers * Complete renaming * Pass defaults of the original URL generator to the new one * Fix URL generator-related test (query string id test WIP) * Fix code style (php-cs-fixer) * Make Fortify bootstrapper not depend on the UrlGenerator bootstrapper, update comments * Fix testing UrlGenerator bootstrapper * Update TenancyUrlGenerator annotations * Pass tenant parameter manually in Fortify bootstrapper * Properly test TenancyUrlGenerator functionality * Get rid of query string in Fortify bootstrapper * Fix code style (php-cs-fixer) * Delete outdated comment * Improve comment * Improve before/afterEach * Encourage passing parameters using TenancyUrlGenerator instead of URL::defaults() * Delete rest of defaulting logic * Fix code style (php-cs-fixer) * Delete test group * Update ForgetTenantParameter docblock * Update passTenantParameterToRoutes annotation * Complete todo in test * Improve test * Update comment * Improve comment * Add keepQueryParameters bool to Fortify bootstrapper * Test keepQueryParameters * minor docblock update * minor docblock changes * Delete extra import * Update src/Overrides/TenancyUrlGenerator.php Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com> * Improve comment * Rename test * Update bypass parameter-related test comments * Fix merge * Rename $keepQueryParameters * Add docblock * Add comment * Refactor Fortify bootstrapper * Fix code style (php-cs-fixer) * Fix comment * Skip Fortify bootstrapper test * minor code improvements * Improve fortify bootstrapper test * Add Fortify bootstrapper annotation, improve code * Fix code style (php-cs-fixer) * Add commenet * Complete resource syncing todo (cleanup not needed) * Delete incorrect namespace * Complete route context trait name todo * Fix code style (php-cs-fixer) --------- Co-authored-by: PHP CS Fixer <phpcsfixer@example.com> Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
105 lines
3.7 KiB
PHP
105 lines
3.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Stancl\Tenancy\Bootstrappers\Integrations;
|
|
|
|
use Illuminate\Config\Repository;
|
|
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
|
use Stancl\Tenancy\Contracts\Tenant;
|
|
use Stancl\Tenancy\Enums\Context;
|
|
use Stancl\Tenancy\Resolvers\PathTenantResolver;
|
|
|
|
/**
|
|
* Allows customizing Fortify action redirects
|
|
* so that they can also redirect to tenant routes instead of just the central routes.
|
|
*
|
|
* Works with path and query string identification.
|
|
*/
|
|
class FortifyRouteTenancyBootstrapper implements TenancyBootstrapper
|
|
{
|
|
/**
|
|
* Make Fortify actions redirect to custom routes.
|
|
*
|
|
* For each route redirect, specify the intended route context (central or tenant).
|
|
* Based on the provided context, we pass the tenant parameter to the route (or not).
|
|
* The tenant parameter is only passed to the route when you specify its context as tenant.
|
|
*
|
|
* The route redirects should be in the following format:
|
|
*
|
|
* 'fortify_action' => [
|
|
* 'route_name' => 'tenant.route',
|
|
* 'context' => Context::TENANT,
|
|
* ]
|
|
*
|
|
* For example:
|
|
*
|
|
* FortifyRouteTenancyBootstrapper::$fortifyRedirectMap = [
|
|
* // On logout, redirect the user to the "bye" route in the central app
|
|
* 'logout' => [
|
|
* 'route_name' => 'bye',
|
|
* 'context' => Context::CENTRAL,
|
|
* ],
|
|
*
|
|
* // On login, redirect the user to the "welcome" route in the tenant app
|
|
* 'login' => [
|
|
* 'route_name' => 'welcome',
|
|
* 'context' => Context::TENANT,
|
|
* ],
|
|
*];
|
|
*/
|
|
public static array $fortifyRedirectMap = [];
|
|
|
|
/**
|
|
* Tenant route that serves as Fortify's home (e.g. a tenant dashboard route).
|
|
* This route will always receive the tenant parameter.
|
|
*/
|
|
public static string $fortifyHome = 'tenant.dashboard';
|
|
|
|
protected array $originalFortifyConfig = [];
|
|
|
|
public function __construct(
|
|
protected Repository $config,
|
|
) {
|
|
}
|
|
|
|
public function bootstrap(Tenant $tenant): void
|
|
{
|
|
$this->originalFortifyConfig = $this->config->get('fortify') ?? [];
|
|
|
|
$this->useTenantRoutesInFortify($tenant);
|
|
}
|
|
|
|
public function revert(): void
|
|
{
|
|
$this->config->set('fortify', $this->originalFortifyConfig);
|
|
}
|
|
|
|
protected function useTenantRoutesInFortify(Tenant $tenant): void
|
|
{
|
|
$tenantKey = $tenant->getTenantKey();
|
|
$tenantParameterName = PathTenantResolver::tenantParameterName();
|
|
|
|
$generateLink = function (array $redirect) use ($tenantKey, $tenantParameterName) {
|
|
// Specifying the context is only required with query string identification
|
|
// because with path identification, the tenant parameter should always present
|
|
$passTenantParameter = $redirect['context'] === Context::TENANT;
|
|
|
|
// Only pass the tenant parameter when the user should be redirected to a tenant route
|
|
return route($redirect['route_name'], $passTenantParameter ? [$tenantParameterName => $tenantKey] : []);
|
|
};
|
|
|
|
// Get redirect URLs for the configured redirect routes
|
|
$redirects = array_merge(
|
|
$this->originalFortifyConfig['redirects'] ?? [], // Fortify config redirects
|
|
array_map(fn (array $redirect) => $generateLink($redirect), static::$fortifyRedirectMap), // Mapped redirects
|
|
);
|
|
|
|
if (static::$fortifyHome) {
|
|
// Generate the home route URL with the tenant parameter and make it the Fortify home route
|
|
$this->config->set('fortify.home', route(static::$fortifyHome, [$tenantParameterName => $tenantKey]));
|
|
}
|
|
|
|
$this->config->set('fortify.redirects', $redirects);
|
|
}
|
|
}
|