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

Fix #998, properly replace ALL tenant_id literals

This commit is contained in:
Samuel Štancl 2022-11-10 16:44:52 +01:00
parent 8a00a105d0
commit 2a39b0526a
11 changed files with 35 additions and 22 deletions

View file

@ -6,6 +6,7 @@ namespace Stancl\Tenancy\Database\Concerns;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Database\TenantScope;
use Stancl\Tenancy\Tenancy;
/**
* @property-read Tenant $tenant
@ -14,12 +15,7 @@ trait BelongsToTenant
{
public function tenant()
{
return $this->belongsTo(config('tenancy.models.tenant'), static::tenantIdColumn());
}
public static function tenantIdColumn(): string
{
return config('tenancy.models.tenant_key_column');
return $this->belongsTo(config('tenancy.models.tenant'), Tenancy::tenantKeyColumn());
}
public static function bootBelongsToTenant(): void
@ -27,9 +23,9 @@ trait BelongsToTenant
static::addGlobalScope(new TenantScope);
static::creating(function ($model) {
if (! $model->getAttribute(static::tenantIdColumn()) && ! $model->relationLoaded('tenant')) {
if (! $model->getAttribute(Tenancy::tenantKeyColumn()) && ! $model->relationLoaded('tenant')) {
if (tenancy()->initialized) {
$model->setAttribute(static::tenantIdColumn(), tenant()->getTenantKey());
$model->setAttribute(Tenancy::tenantKeyColumn(), tenant()->getTenantKey());
$model->setRelation('tenant', tenant());
}
}

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Database\Concerns;
use Stancl\Tenancy\Contracts\Domain;
use Stancl\Tenancy\Tenancy;
/**
* @property-read Domain[]|\Illuminate\Database\Eloquent\Collection $domains
@ -15,7 +16,7 @@ trait HasDomains
{
public function domains()
{
return $this->hasMany(config('tenancy.models.domain'), 'tenant_id');
return $this->hasMany(config('tenancy.models.domain'), Tenancy::tenantKeyColumn());
}
public function createDomain($data): Domain

View file

@ -6,16 +6,17 @@ namespace Stancl\Tenancy\Database\Concerns;
use Illuminate\Validation\Rules\Exists;
use Illuminate\Validation\Rules\Unique;
use Stancl\Tenancy\Tenancy;
trait HasScopedValidationRules
{
public function unique($table, $column = 'NULL')
{
return (new Unique($table, $column))->where(BelongsToTenant::tenantIdColumn(), $this->getTenantKey());
return (new Unique($table, $column))->where(Tenancy::tenantKeyColumn(), $this->getTenantKey());
}
public function exists($table, $column = 'NULL')
{
return (new Exists($table, $column))->where(BelongsToTenant::tenantIdColumn(), $this->getTenantKey());
return (new Exists($table, $column))->where(Tenancy::tenantKeyColumn(), $this->getTenantKey());
}
}

View file

@ -7,7 +7,7 @@ namespace Stancl\Tenancy\Database;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
use Stancl\Tenancy\Database\Concerns\BelongsToTenant;
use Stancl\Tenancy\Tenancy;
class TenantScope implements Scope
{
@ -17,7 +17,7 @@ class TenantScope implements Scope
return;
}
$builder->where($model->qualifyColumn(BelongsToTenant::tenantIdColumn()), tenant()->getTenantKey());
$builder->where($model->qualifyColumn(Tenancy::tenantKeyColumn()), tenant()->getTenantKey());
}
public function extend(Builder $builder): void

View file

@ -20,7 +20,7 @@ class UserImpersonation implements Feature
{
$tenancy->macro('impersonate', function (Tenant $tenant, string $userId, string $redirectUrl, string $authGuard = null): ImpersonationToken {
return ImpersonationToken::create([
'tenant_id' => $tenant->getTenantKey(),
Tenancy::tenantKeyColumn() => $tenant->getTenantKey(),
'user_id' => $userId,
'redirect_url' => $redirectUrl,
'auth_guard' => $authGuard,
@ -39,7 +39,7 @@ class UserImpersonation implements Feature
abort_if($tokenExpired, 403);
$tokenTenantId = (string) $token->tenant_id;
$tokenTenantId = (string) $token->getAttribute(Tenancy::tenantKeyColumn());
$currentTenantId = (string) tenant()->getTenantKey();
abort_unless($tokenTenantId === $currentTenantId, 403);

View file

@ -14,6 +14,7 @@ use Stancl\Tenancy\Database\TenantCollection;
use Stancl\Tenancy\Events\SyncedResourceChangedInForeignDatabase;
use Stancl\Tenancy\Events\SyncedResourceSaved;
use Stancl\Tenancy\Exceptions\ModelNotSyncMasterException;
use Stancl\Tenancy\Tenancy;
// todo@v4 review all code related to resource syncing
@ -77,7 +78,7 @@ class UpdateSyncedResource extends QueueableListener
/** @var Tenant */
$tenant = $event->tenant;
return ((string) $model->pivot->tenant_id) === ((string) $tenant->getTenantKey());
return ((string) $model->pivot->getAttribute(Tenancy::tenantKeyColumn())) === ((string) $tenant->getTenantKey());
};
$mappingExists = $centralModel->tenants->contains($currentTenantMapping);

View file

@ -105,6 +105,12 @@ class Tenancy
return $model;
}
/** Name of the column used to relate models to tenants. */
public static function tenantKeyColumn(): string
{
return config('tenancy.models.tenant_key_column') ?? 'tenant_id';
}
/**
* Try to find a tenant using an ID.
*