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

Shared users - complete

This commit is contained in:
Samuel Štancl 2020-05-12 01:54:02 +02:00
parent daae67c0f7
commit b7c8f1fba7
12 changed files with 488 additions and 56 deletions

View file

@ -4,12 +4,13 @@ namespace Stancl\Tenancy\Events\Listeners;
use Illuminate\Contracts\Queue\ShouldQueue;
/**
* @method handle(object $event)
*/
abstract class QueueableListener implements ShouldQueue
{
public static $shouldQueue = false;
abstract public function handle();
public function shouldQueue($event)
{
if (static::$shouldQueue) {

View file

@ -3,12 +3,15 @@
namespace Stancl\Tenancy\Events\Listeners;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Stancl\Tenancy\Contracts\SyncMaster;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Events\SyncedResourceSaved;
use Stancl\Tenancy\Exceptions\ModelNotSyncMaster;
class UpdateSyncedResource
class UpdateSyncedResource extends QueueableListener
{
public static $shouldQueue = false;
public function handle(SyncedResourceSaved $event)
{
$syncedAttributes = $event->model->only($event->model->getSyncedAttributeNames());
@ -19,36 +22,76 @@ class UpdateSyncedResource
$centralModel = $event->model->getCentralModelName()
::where($event->model->getGlobalIdentifierKeyName(), $event->model->getGlobalIdentifierKey())
->first();
// We disable events for this call, to avoid triggering this event & listener again.
$centralModel->withoutEvents(function () use ($centralModel, $syncedAttributes) {
$centralModel->update($syncedAttributes);
$event->model->getCentralModelName()::withoutEvents(function () use (&$centralModel, $syncedAttributes, $event) {
if ($centralModel) {
$centralModel->update($syncedAttributes);
} else {
// If the resource doesn't exist at all in the central DB,we create
// the record with all attributes, not just the synced ones.
$centralModel = $event->model->getCentralModelName()::create($event->model->getAttributes());
}
});
// If the model was just created, the mapping of the tenant to the user likely doesn't exist, so we create it.
$mappingExists = $centralModel->tenants->contains(function ($model) use ($event) {
return $model->tenant_id === $event->tenant->getTenantKey();
});
if (! $mappingExists) {
// Here we should call TenantPivot, but we call general Pivot, so that this works
// even if people use their own pivot model that is not based on our TenantPivot
Pivot::withoutEvents(function () use ($centralModel, $event) {
$centralModel->tenants()->attach($event->tenant->getTenantKey());
});
}
$tenants = $centralModel->tenants->except($event->tenant->getTenantKey());
} else {
$centralModel = $event->model;
if (! $centralModel instanceof SyncMaster) {
// If we're trying to use a tenant User model instead of the central User model, for example.
throw new ModelNotSyncMaster(get_class($centralModel));
}
/** @var SyncMaster|Model $centralModel */
// Since this model is "dirty" (taken by reference from the event), it might have the tenants
// relationship already loaded and cached. For this reason, we refresh the relationship.
$centralModel->load('tenants');
$tenants = $centralModel->tenants;
}
foreach ($tenants as $tenant) {
// todo: performance optimization - $tenant->run() does tenancy()->end() after each call.
// we dont want that when we want to initialize for the next tenant afterwards rather than for the previous tenant
// so we should write a method like run() for running things on multiple tenants efficiently
tenancy()->runForMultiple($tenants, function () use ($event, $syncedAttributes) {
// Forget instance state and find the model,
// again in the current tenant's context.
$tenant->run(function () use ($event, $syncedAttributes) {
// Forget instance state and find the model,
// again in the current tenant's context.
$eventModel = $event->model;
/** @var Tenant|Model $model */
$localModel = $event->model::find($event->model->getKey());
if ($eventModel instanceof SyncMaster) {
// If event model comes from central DB, we get the tenant model name to run the query
$localModelClass = $eventModel->getTenantModelName();
} else {
$localModelClass = get_class($eventModel);
}
// Also: We're syncing attributes, not columns, which is
// why we're using Eloquent instead of direct DB queries.
/** @var Model|null */
$localModel = $localModelClass::firstWhere($event->model->getGlobalIdentifierKeyName(), $event->model->getGlobalIdentifierKey());
// We disable events for this call, to avoid triggering this event & listener again.
$localModel->update($syncedAttributes);
// Also: We're syncing attributes, not columns, which is
// why we're using Eloquent instead of direct DB queries.
// We disable events for this call, to avoid triggering this event & listener again.
$localModelClass::withoutEvents(function () use ($localModelClass, $localModel, $syncedAttributes, $eventModel) {
if ($localModel) {
$localModel->update($syncedAttributes);
} else {
// When creating, we use all columns, not just the synced ones.
$localModelClass::create($eventModel->getAttributes());
}
});
}
});
}
}