From 4e295c753fd193f1c579c8a9e61dd084e810f698 Mon Sep 17 00:00:00 2001 From: Sam Maosa Date: Thu, 28 Sep 2023 11:22:55 +0300 Subject: [PATCH] Added ability to exclude some models from the global multi-tenancy scope configuration - Added config for saas to include excluded models - Refactoring: default_team helper was using the wrong namespace for Team::class --- config/saas.php | 10 +++++++++- src/Concerns/Model/HasTeam.php | 21 +++++++++++++++++++++ src/Filament/Resources/TeamResource.php | 9 +++++++++ src/SaasServiceProvider.php | 1 + src/helpers.php | 2 +- 5 files changed, 41 insertions(+), 2 deletions(-) diff --git a/config/saas.php b/config/saas.php index dc8c6a0..e8e53e4 100644 --- a/config/saas.php +++ b/config/saas.php @@ -2,5 +2,13 @@ // config for Savannabits/Saas return [ - + 'shared_models' => [ + 'App\Models\User', + \Savannabits\Saas\Models\User::class, + \Savannabits\Saas\Models\Role::class, + \Savannabits\Saas\Models\Permission::class, + \Savannabits\Saas\Models\Country::class, + \Savannabits\Saas\Models\Currency::class, + \Savannabits\Saas\Models\Team::class, + ] ]; diff --git a/src/Concerns/Model/HasTeam.php b/src/Concerns/Model/HasTeam.php index caf04c6..41769ed 100644 --- a/src/Concerns/Model/HasTeam.php +++ b/src/Concerns/Model/HasTeam.php @@ -2,6 +2,8 @@ namespace Savannabits\Saas\Concerns\Model; +use Illuminate\Database\Eloquent\Builder; +use Illuminate\Support\Facades\Schema; use Savannabits\Saas\Models\Team; use Illuminate\Database\Eloquent\Model; use function Savannabits\Saas\default_team; @@ -29,6 +31,21 @@ public static function bootHasTeam() $model->{$col} = null; }*/ }); + + // Add scope + if (auth()->check()) { + static::addGlobalScope('team', function (Builder $query) { + if (in_array($query->getModel()->getMorphClass(), static::getSharedModels())) { + return; + } + if (Schema::hasColumn($query->getModel()->getTable(),'team_id')) { + $query->whereBelongsTo(auth()->user()->team)->orWhereNull('team_id') + ->orWhere('team_id','=', default_team()?->id); + } +// $query->where('team_id', auth()->user()->team_id); + // or with a `team` relationship defined: + }); + } } public function team() @@ -40,4 +57,8 @@ protected function initializeHasTeam() { // $this->casts['is_cross_team'] = 'bool'; } + + protected static function getSharedModels() { + return config('saas.shared_models',[]); + } } diff --git a/src/Filament/Resources/TeamResource.php b/src/Filament/Resources/TeamResource.php index 9e28566..ef78eb6 100644 --- a/src/Filament/Resources/TeamResource.php +++ b/src/Filament/Resources/TeamResource.php @@ -2,6 +2,7 @@ namespace Savannabits\Saas\Filament\Resources; +use Savannabits\Saas\SaasPlugin; use Savannabits\Sass\Filament\Resources\TeamResource\RelationManagers; use Filament\Forms; use Filament\Forms\Form; @@ -16,6 +17,14 @@ class TeamResource extends Resource protected static ?string $model = Team::class; protected static ?string $navigationIcon = 'heroicon-o-building-office-2'; + public static function getNavigationBadge(): ?string + { + return static::getModel()::count(); + } + public static function getNavigationGroup(): ?string + { + return SaasPlugin::getNavigationGroupLabel(); + } public static function form(Form $form): Form { diff --git a/src/SaasServiceProvider.php b/src/SaasServiceProvider.php index d2911c6..3b6c081 100644 --- a/src/SaasServiceProvider.php +++ b/src/SaasServiceProvider.php @@ -51,6 +51,7 @@ public function configurePackage(Package $package): void $package->hasConfigFile([ 'armor', + 'saas', 'vanadi', 'ldap', 'permission', diff --git a/src/helpers.php b/src/helpers.php index c3ba9b4..f80d709 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -2,7 +2,7 @@ namespace Savannabits\Saas; use App\Model\User; -use App\Models\Team; +use Savannabits\Saas\Models\Team; use Savannabits\Saas\Helpers\Access; use Savannabits\Saas\Models\Currency; use Savannabits\Saas\Settings\WebserviceSettings;