1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-13 01:44:04 +00:00

[4.x] URL generation, request data identification improvements (#1357)

* UrlGenerator: set defaults based on config; request data: move config to config file+resolver

* Claude code adjustments

* improve request data tests, simplify complex test in UrlGeneratorBootstrapperTest

* url generator test: test changing tenant parameter name

* request data identification: add tenant_model_column configuration

* defaultParameterNames -> passQueryParameter

* move comment

* minor refactor in PathIdentificationTest, expand CLAUDE.md to include early identification section

* Fix COLOR_FLAG

* improve test name

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* TenancyUrlGenerator: add a check for queryParameterName being null

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Fix code style (php-cs-fixer)

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This commit is contained in:
Samuel Štancl 2025-06-02 03:43:47 +02:00 committed by GitHub
parent f4cc99b317
commit 5f7fd38e5a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 440 additions and 126 deletions

View file

@ -18,9 +18,6 @@ class InitializeTenancyByRequestData extends IdentificationMiddleware
{
use UsableWithEarlyIdentification;
public static string $header = 'X-Tenant';
public static string $cookie = 'tenant';
public static string $queryParameter = 'tenant';
public static ?Closure $onFail = null;
public static bool $requireCookieEncryption = false;
@ -54,18 +51,19 @@ class InitializeTenancyByRequestData extends IdentificationMiddleware
protected function getPayload(Request $request): string|null
{
if (static::$header && $request->hasHeader(static::$header)) {
$payload = $request->header(static::$header);
} elseif (
static::$queryParameter &&
$request->has(static::$queryParameter)
) {
$payload = $request->get(static::$queryParameter);
} elseif (static::$cookie && $request->hasCookie(static::$cookie)) {
$payload = $request->cookie(static::$cookie);
$headerName = RequestDataTenantResolver::headerName();
$queryParameterName = RequestDataTenantResolver::queryParameterName();
$cookieName = RequestDataTenantResolver::cookieName();
if ($headerName && $request->hasHeader($headerName)) {
$payload = $request->header($headerName);
} elseif ($queryParameterName && $request->has($queryParameterName)) {
$payload = $request->get($queryParameterName);
} elseif ($cookieName && $request->hasCookie($cookieName)) {
$payload = $request->cookie($cookieName);
if ($payload && is_string($payload)) {
$payload = $this->getTenantFromCookie($payload);
$payload = $this->getTenantFromCookie($cookieName, $payload);
}
} else {
$payload = null;
@ -86,12 +84,12 @@ class InitializeTenancyByRequestData extends IdentificationMiddleware
return (bool) $this->getPayload($request);
}
protected function getTenantFromCookie(string $cookie): string|null
protected function getTenantFromCookie(string $cookieName, string $cookieValue): string|null
{
// If the cookie looks like it's encrypted, we try decrypting it
if (str_starts_with($cookie, 'eyJpdiI')) {
if (str_starts_with($cookieValue, 'eyJpdiI')) {
try {
$json = base64_decode($cookie);
$json = base64_decode($cookieValue);
$data = json_decode($json, true);
if (
@ -100,9 +98,9 @@ class InitializeTenancyByRequestData extends IdentificationMiddleware
) {
// We can confidently assert that the cookie is encrypted. If this call were to fail, this method would just
// return null and the cookie payload would get skipped.
$cookie = CookieValuePrefix::validate(
static::$cookie,
Crypt::decryptString($cookie),
$cookieValue = CookieValuePrefix::validate(
$cookieName,
Crypt::decryptString($cookieValue),
Crypt::getAllKeys()
);
}
@ -113,6 +111,6 @@ class InitializeTenancyByRequestData extends IdentificationMiddleware
return null;
}
return $cookie;
return $cookieValue;
}
}