This commit is contained in:
Bozhidar 2024-09-12 11:11:28 +03:00
parent c4fa067577
commit ed8b0fb50d
6 changed files with 93 additions and 0 deletions

View file

@ -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;

View file

@ -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();

View file

@ -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);

View file

@ -0,0 +1,27 @@
<?php
namespace App\Models\Scopes;
use App\Models\Domain;
use App\Models\HostingSubscription;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
class CustomerDomainScope implements Scope
{
/**
* Apply the scope to a given Eloquent query builder.
*/
public function apply(Builder $builder, Model $model): void
{
if (auth()->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);
}
}
}

View file

@ -0,0 +1,23 @@
<?php
namespace App\Models\Scopes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
class CustomerHostingSubscriptionScope implements Scope
{
/**
* Apply the scope to a given Eloquent query builder.
*/
public function apply(Builder $builder, Model $model): void
{
if (auth()->check() && auth()->guard()->name == 'web_customer') {
$builder->whereHas('hostingSubscription', function ($query) {
$query->where('customer_id', auth()->user()->id);
});
}
}
}

View file

@ -0,0 +1,20 @@
<?php
namespace App\Models\Scopes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
class CustomerScope implements Scope
{
/**
* Apply the scope to a given Eloquent query builder.
*/
public function apply(Builder $builder, Model $model): void
{
if (auth()->check() && auth()->guard()->name == 'web_customer') {
$builder->where('customer_id', auth()->user()->id);
}
}
}