1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 22:14: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

@ -3,14 +3,32 @@
namespace Stancl\Tenancy\Database\Models\Concerns;
use Stancl\Tenancy\Contracts\Syncable;
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;
use Stancl\Tenancy\Events\SyncedResourceSaved;
trait ResourceSyncing
{
public static function bootResourceSyncing()
{
static::saving(function (Syncable $model) {
event(new SyncedResourceSaved($model, tenant()));
static::saved(function (Syncable $model) {
/** @var ResourceSyncing $model */
$model->triggerSyncEvent();
});
static::creating(function (self $model) {
if (! $model->getAttribute($model->getGlobalIdentifierKeyName()) && app()->bound(UniqueIdentifierGenerator::class)) {
$model->setAttribute(
$model->getGlobalIdentifierKeyName(),
app(UniqueIdentifierGenerator::class)->generate($model)
);
}
});
}
public function triggerSyncEvent()
{
/** @var Syncable $this */
event(new SyncedResourceSaved($this, tenant()));
}
}

View file

@ -0,0 +1,15 @@
<?php
namespace Stancl\Tenancy\Database\Models;
use Illuminate\Database\Eloquent\Relations\Pivot;
class TenantPivot extends Pivot
{
public static function booted()
{
static::saved(function (self $pivot) {
$pivot->pivotParent->triggerSyncEvent();
});
}
}