feat: multi-tenant SaaS implementation - admin interface, tenant isolation, and UI updates
This commit is contained in:
38
app/Traits/BelongsToTenant.php
Normal file
38
app/Traits/BelongsToTenant.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
trait BelongsToTenant
|
||||
{
|
||||
protected static function bootBelongsToTenant()
|
||||
{
|
||||
static::addGlobalScope('tenant', function (Builder $builder) {
|
||||
if (Auth::check()) {
|
||||
$user = Auth::user();
|
||||
|
||||
if ($user->role === 'super_admin') {
|
||||
// Super admins see everything
|
||||
return;
|
||||
}
|
||||
|
||||
if ($user->tenant_id) {
|
||||
$builder->where('tenant_id', $user->tenant_id);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
static::creating(function ($model) {
|
||||
if (Auth::check() && Auth::user()->tenant_id && Auth::user()->role !== 'super_admin') {
|
||||
$model->tenant_id = Auth::user()->tenant_id;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function tenant()
|
||||
{
|
||||
return $this->belongsTo(\App\Models\Tenant::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user