diff --git a/web/Modules/Customer/App/Filament/Resources/GitSshKeyResource.php b/web/Modules/Customer/App/Filament/Resources/GitSshKeyResource.php index 489608e..481353d 100644 --- a/web/Modules/Customer/App/Filament/Resources/GitSshKeyResource.php +++ b/web/Modules/Customer/App/Filament/Resources/GitSshKeyResource.php @@ -4,6 +4,7 @@ namespace Modules\Customer\App\Filament\Resources; use App\Models\Domain; use App\Models\GitSshKey; +use App\Models\Scopes\CustomerScope; use Modules\Customer\App\Filament\Resources\GitSshKeyResource\Pages; use Modules\Customer\App\Filament\Resources\GitSshKeyResource\RelationManagers; use Filament\Forms; diff --git a/web/app/Models/GitRepository.php b/web/app/Models/GitRepository.php index 33e28e8..9a333d8 100644 --- a/web/app/Models/GitRepository.php +++ b/web/app/Models/GitRepository.php @@ -3,6 +3,8 @@ namespace App\Models; use App\GitClient; +use App\Models\Scopes\CustomerDomainScope; +use App\Models\Scopes\CustomerHostingSubscriptionScope; use App\ShellApi; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -37,6 +39,15 @@ class GitRepository extends Model 'domain_id', 'git_ssh_key_id', ]; + + /** + * The "booted" method of the model. + */ + protected static function booted(): void + { + static::addGlobalScope(new CustomerDomainScope()); + } + public static function boot() { parent::boot(); diff --git a/web/app/Models/GitSshKey.php b/web/app/Models/GitSshKey.php index 7dc3b83..e691beb 100644 --- a/web/app/Models/GitSshKey.php +++ b/web/app/Models/GitSshKey.php @@ -2,6 +2,9 @@ namespace App\Models; +use App\Models\Scopes\CustomerScope; +use App\Models\Scopes\CustomerHostingSubscriptionScope; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; @@ -16,6 +19,14 @@ class GitSshKey extends Model 'public_key', ]; + /** + * The "booted" method of the model. + */ + protected static function booted(): void + { + static::addGlobalScope(new CustomerHostingSubscriptionScope()); + } + public function hostingSubscription() { return $this->belongsTo(HostingSubscription::class); diff --git a/web/app/Models/Scopes/CustomerDomainScope.php b/web/app/Models/Scopes/CustomerDomainScope.php new file mode 100644 index 0000000..16948fb --- /dev/null +++ b/web/app/Models/Scopes/CustomerDomainScope.php @@ -0,0 +1,27 @@ +check() && auth()->guard()->name == 'web_customer') { + + $findHostingSubscriptionIds = HostingSubscription::where('customer_id', auth()->user()->id)->pluck('id'); + $findDomainIds = Domain::whereIn('hosting_subscription_id', $findHostingSubscriptionIds)->pluck('id'); + + $builder->whereIn('domain_id', $findDomainIds); + } + } + +} diff --git a/web/app/Models/Scopes/CustomerHostingSubscriptionScope.php b/web/app/Models/Scopes/CustomerHostingSubscriptionScope.php new file mode 100644 index 0000000..d3103c7 --- /dev/null +++ b/web/app/Models/Scopes/CustomerHostingSubscriptionScope.php @@ -0,0 +1,23 @@ +check() && auth()->guard()->name == 'web_customer') { + $builder->whereHas('hostingSubscription', function ($query) { + $query->where('customer_id', auth()->user()->id); + }); + } + } + +} diff --git a/web/app/Models/Scopes/CustomerScope.php b/web/app/Models/Scopes/CustomerScope.php new file mode 100644 index 0000000..5768021 --- /dev/null +++ b/web/app/Models/Scopes/CustomerScope.php @@ -0,0 +1,20 @@ +check() && auth()->guard()->name == 'web_customer') { + $builder->where('customer_id', auth()->user()->id); + } + } +}