Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
bobimicroweber committed Apr 26, 2024
1 parent 29b1ba8 commit aab1b81
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 3 deletions.
90 changes: 88 additions & 2 deletions web/Modules/Customer/App/Filament/Resources/DatabaseResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Modules\Customer\App\Filament\Resources;

use App\Models\Database;
use App\Models\HostingSubscription;
use App\Models\RemoteDatabaseServer;
use Modules\Customer\App\Filament\Resources\DatabaseResource\Pages;
use Modules\Customer\App\Filament\Resources\DatabaseResource\RelationManagers;
use Filament\Forms;
Expand All @@ -21,10 +23,94 @@ class DatabaseResource extends Resource

public static function form(Form $form): Form
{
$systemUsername = '';
if ($form->getRecord()) {
$systemUsername = $form->getRecord()->hostingSubscription->system_username;
}

return $form
->schema([
//
]);

Forms\Components\Select::make('hosting_subscription_id')
->label('Hosting Subscription')
->options(
\App\Models\HostingSubscription::all()->pluck('domain', 'id')
)
->live()
->afterStateUpdated(function (Forms\Components\Select $component) use($systemUsername) {
$findHostingSubscription = HostingSubscription::select(['system_username'])
->where('id', $component->getState())
->first();
if ($findHostingSubscription) {
$systemUsername = $findHostingSubscription->system_username;
}
})
->disabled(function ($record) {
return $record;
})
->required(),

Forms\Components\ToggleButtons::make('is_remote_database_server')
->default(0)
->disabled(function ($record) {
return $record;
})
->live()
->options([
0 => 'Internal',
1 => 'Remote Database Server',
])->inline(),

Forms\Components\Select::make('remote_database_server_id')
->label('Remote Database Server')
->hidden(fn(Forms\Get $get): bool => '1' !== $get('is_remote_database_server'))
->options(
RemoteDatabaseServer::all()->pluck('name', 'id')
),

Forms\Components\TextInput::make('database_name')
->prefix(function ($record) use($systemUsername) {
if ($record) {
return $record->database_prefix;
}
if (!$systemUsername) {
return false;
}
return $systemUsername.'_';
})
->disabled(function ($record) {
return $record;
})
->label('Database Name')
->required(),

Forms\Components\Repeater::make('databaseUsers')
->relationship('databaseUsers')
->schema([
Forms\Components\TextInput::make('username')
->disabled(function ($record) {
return $record;
})
->prefix(function ($record) use($systemUsername) {
if ($record) {
return $record->username_prefix;
}
if (!$systemUsername) {
return false;
}
return $systemUsername.'_';
})
->required(),
Forms\Components\TextInput::make('password')
->disabled(function ($record) {
return $record;
})
//->password()
->required(),
])
->columns(2)

])->columns(1);
}

public static function table(Table $table): Table
Expand Down
2 changes: 1 addition & 1 deletion web/app/Models/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Database extends Model
protected static function booted(): void
{
static::addGlobalScope('customer', function (Builder $query) {
if (auth()->check()) {
if (auth()->check() && auth()->guard()->name == 'web_customer') {
$query->whereHas('hostingSubscription', function ($query) {
$query->where('customer_id', auth()->user()->id);
});
Expand Down
11 changes: 11 additions & 0 deletions web/app/Models/HostingSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Actions\CreateLinuxWebUser;
use App\Actions\GetLinuxUser;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
Expand All @@ -26,6 +27,16 @@ class HostingSubscription extends Model
'renewal_date',
];

protected static function booted(): void
{
static::addGlobalScope('customer', function (Builder $query) {
if (auth()->check() && auth()->guard()->name == 'web_customer') {
$query->where('customer_id', auth()->user()->id);
}
});
}


public static function boot()
{
parent::boot();
Expand Down

0 comments on commit aab1b81

Please sign in to comment.