I’m trying to only Sync Certain Users created, perhaps the users that only have a global_id or an owner flag.
however, any user that I create into the TenantDB users table tries to sync back to the CentralDB
I have a multi Tenancy Application,
The central User, is copied to the Tenant DB and sync’d, but if a Tenant Creates a Secondary User, I don’t want that sent back to the CentralDB, however it is trying to do that which causes issues as Secondary Users don’t have all the data that a Primary User has.
The Flow of the app is
Client Buys access, creates subdomain and their user account,
The Tenancy is created subdomain activated, database provisioned, the Primary User is copied to the Tenant DB.
The user logs into their space using the account saved in the Tenant DB, then the user tries to create a secondary user and it creates a Global_id for that user and then tries to sync back, I don’t want that user to have a global_id and I don’t want it to sync back.
I’ve implemented this from Stancl Documentation and the sync is working, perhaps all a little too well.
use StanclTenancyDatabaseModelsTenant as BaseTenant;
use StanclTenancyDatabaseModelsTenantPivot;
class Tenant extends BaseTenant implements TenantWithDatabase
{
use HasDatabase, HasDomains;
public function users()
{
return $this->belongsToMany(CentralUser::class, 'tenant_users', 'tenant_id', 'global_user_id', 'id', 'global_id')
->using(TenantPivot::class);
}
}
class CentralUser extends Model implements SyncMaster
{
// Note that we force the central connection on this model
use ResourceSyncing, CentralConnection;
protected $guarded = [];
public $timestamps = false;
public $table = 'users';
public function tenants(): BelongsToMany
{
return $this->belongsToMany(Tenant::class, 'tenant_users', 'global_user_id', 'tenant_id', 'global_id')
->using(TenantPivot::class);
}
public function getTenantModelName(): string
{
return User::class;
}
public function getGlobalIdentifierKey()
{
return $this->getAttribute($this->getGlobalIdentifierKeyName());
}
public function getGlobalIdentifierKeyName(): string
{
return 'global_id';
}
public function getCentralModelName(): string
{
return static::class;
}
public function getSyncedAttributeNames(): array
{
return [
'name',
'password',
'email',
];
}
}
class User extends Model implements Syncable
{
use ResourceSyncing;
protected $guarded = [];
public $timestamps = false;
public function getGlobalIdentifierKey()
{
return $this->getAttribute($this->getGlobalIdentifierKeyName());
}
public function getGlobalIdentifierKeyName(): string
{
return 'global_id';
}
public function getCentralModelName(): string
{
return CentralUser::class;
}
public function getSyncedAttributeNames(): array
{
return [
'name',
'password',
'email',
];
}
}
// Pivot table migration in the central database
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
class CreateTenantUsersTable extends Migration
{
public function up()
{
Schema::create('tenant_users', function (Blueprint $table) {
$table->increments('id');
$table->string('tenant_id');
$table->string('global_user_id');
$table->unique(['tenant_id', 'global_user_id']);
$table->foreign('tenant_id')->references('id')->on('tenants')->onUpdate('cascade')->onDelete('cascade');
$table->foreign('global_user_id')->references('global_id')->on('users')->onUpdate('cascade')->onDelete('cascade');
});
}
public function down()
{
Schema::dropIfExists('tenant_users');
}
}