131 lines
4.6 KiB
PHP
Raw Normal View History

2024-12-10 15:28:14 -08:00
<?php
namespace App\Filament\Admin\Resources;
2025-01-16 17:37:32 -05:00
use App\Enums\IconEnum;
2024-12-10 15:28:14 -08:00
use App\Models\User;
2025-01-24 21:37:05 -05:00
use Filament\Forms\Components\Grid;
2025-01-04 16:41:24 -05:00
use Filament\Forms\Components\Section;
2025-01-21 21:28:41 -05:00
use Filament\Forms\Components\Select;
2025-01-04 16:41:24 -05:00
use Filament\Forms\Components\TextInput;
2025-01-24 21:37:05 -05:00
use Filament\Forms\Components\Toggle;
2024-12-10 15:28:14 -08:00
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
class UserResource extends Resource
{
protected static ?int $navigationSort = 10;
protected static ?string $model = User::class;
2025-01-16 17:37:32 -05:00
protected static ?string $navigationIcon = IconEnum::USER->value;
2024-12-10 15:28:14 -08:00
protected static ?string $navigationGroup = 'Settings';
public static function form(Form $form): Form
{
return $form
->schema([
2025-01-24 21:37:05 -05:00
Section::make('Login details')
->description(fn (string $operation) => $operation == 'edit' ? 'To leave the password unchanged, leave both fields empty,' : false)
2025-01-04 16:41:24 -05:00
->schema([
TextInput::make('username')
2025-01-24 21:37:05 -05:00
->autocomplete('new-username')
2025-01-21 21:28:41 -05:00
->unique()
2025-01-24 21:37:05 -05:00
->required()
->columnSpan(1),
Grid::make(2)
->schema([
TextInput::make('password')
->password()
->autocomplete('new-password')
->revealable()
->dehydrated(fn ($state) => ! empty($state))
->required(fn (string $operation): bool => $operation === 'create'),
TextInput::make('password_verify')
->label('Verify password')
->password()
->revealable()
->same('password')
->dehydrated(false)
->required(fn (string $operation) => $operation === 'create'),
])
->columnSpan(2),
]),
Section::make('Permissions')
->description('Administrators can access financial information and change settings.')
->schema([
Toggle::make('is_admin')
->label('User is an administrator')
2025-01-21 21:28:41 -05:00
->reactive()
->afterStateUpdated(fn ($state, callable $set) => $set('customer_id', null))
->disabled(fn (?User $record, $operation) => $operation !== 'create' && auth()->user()->id === $record->id),
2025-01-24 21:37:05 -05:00
])
->columns(2),
Section::make('Customer Login')
->description('If this account is for a customer, select them here.')
->schema([
2025-01-21 21:28:41 -05:00
Select::make('customer_id')
->relationship('customer', 'company_name')
->disabled(fn ($get) => $get('is_admin'))
->afterStateUpdated(fn ($state, callable $set) => $set('is_admin', false))
->nullable()
->searchable()
->preload(),
2025-01-04 16:41:24 -05:00
]),
2024-12-10 15:28:14 -08:00
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
2025-01-04 16:41:24 -05:00
TextColumn::make('username')
->extraHeaderAttributes(['class' => 'w-full']),
2025-01-21 21:28:41 -05:00
TextColumn::make('customer.company_name')
2025-01-24 21:37:05 -05:00
->sortable()
->placeholder('Internal'),
2024-12-10 15:28:14 -08:00
Tables\Columns\IconColumn::make('is_admin')
->label('Admin')
->boolean()
->alignRight(),
])
->filters([
//
])
->actions([
2025-01-24 21:37:05 -05:00
Tables\Actions\EditAction::make()->modal(),
2024-12-10 15:28:14 -08:00
])
2025-01-21 21:28:41 -05:00
->defaultSort('customer_id', 'asc');
2024-12-10 15:28:14 -08:00
}
2025-01-04 16:41:24 -05:00
public static function canAccess(): bool
{
return auth()->user()->is_admin ?? false;
2025-01-04 16:41:24 -05:00
}
2024-12-10 15:28:14 -08:00
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
2025-01-24 21:37:05 -05:00
'index' => \App\Filament\Admin\Resources\UserResource\Pages\ListUsers::route('/'),
2024-12-10 15:28:14 -08:00
];
}
2025-01-24 21:37:05 -05:00
private static function Grid() {}
2024-12-10 15:28:14 -08:00
}