Skip to content

Commit

Permalink
Added ability to exclude some models from the global multi-tenancy sc…
Browse files Browse the repository at this point in the history
…ope configuration

- Added config for saas to include excluded models
- Refactoring: default_team helper was using the wrong namespace for Team::class
  • Loading branch information
coolsam726 committed Sep 28, 2023
1 parent 9d58df3 commit 4e295c7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
10 changes: 9 additions & 1 deletion config/saas.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
];
21 changes: 21 additions & 0 deletions src/Concerns/Model/HasTeam.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand All @@ -40,4 +57,8 @@ protected function initializeHasTeam()
{
// $this->casts['is_cross_team'] = 'bool';
}

protected static function getSharedModels() {
return config('saas.shared_models',[]);
}
}
9 changes: 9 additions & 0 deletions src/Filament/Resources/TeamResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down
1 change: 1 addition & 0 deletions src/SaasServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function configurePackage(Package $package): void

$package->hasConfigFile([
'armor',
'saas',
'vanadi',
'ldap',
'permission',
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4e295c7

Please sign in to comment.