Files
nexusphp/app/Filament/Resources/Oauth/ProviderResource.php

161 lines
5.5 KiB
PHP
Raw Normal View History

2025-04-24 15:30:07 +07:00
<?php
namespace App\Filament\Resources\Oauth;
use Filament\Schemas\Schema;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\IconColumn;
use Filament\Actions\EditAction;
use Filament\Actions\DeleteAction;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use App\Filament\Resources\Oauth\ProviderResource\Pages\ManageProviders;
2025-04-24 15:30:07 +07:00
use App\Filament\Resources\Oauth\ProviderResource\Pages;
use App\Filament\Resources\Oauth\ProviderResource\RelationManagers;
use App\Models\OauthProvider;
use App\Models\User;
2025-04-24 15:30:07 +07:00
use Filament\Forms;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
2025-05-02 11:53:56 +07:00
use Nexus\Database\NexusDB;
use Ramsey\Uuid;
2025-04-24 15:30:07 +07:00
class ProviderResource extends Resource
{
protected static ?string $model = OauthProvider::class;
protected static string | \BackedEnum | null $navigationIcon = 'heroicon-o-rectangle-stack';
2025-04-24 15:30:07 +07:00
protected static string | \UnitEnum | null $navigationGroup = 'Oauth';
2025-04-24 15:30:07 +07:00
protected static ?int $navigationSort = 5;
public static function getNavigationLabel(): string
{
return __('admin.sidebar.oauth_provider');
}
public static function getBreadcrumb(): string
{
return self::getNavigationLabel();
}
public static function form(Schema $schema): Schema
2025-04-24 15:30:07 +07:00
{
return $schema
->components([
TextInput::make('name')
2025-04-24 15:30:07 +07:00
->label(__('label.name'))
->required()
,
TextInput::make('client_id')
2025-04-24 15:30:07 +07:00
->label(__('oauth.client_id'))
->required()
,
TextInput::make('client_secret')
2025-04-24 15:30:07 +07:00
->label(__('oauth.secret'))
->required()
,
TextInput::make('authorization_endpoint_url')
2025-04-24 15:30:07 +07:00
->label(__('oauth.authorization_endpoint_url'))
->required()
,
TextInput::make('token_endpoint_url')
2025-04-24 15:30:07 +07:00
->label(__('oauth.token_endpoint_url'))
->required()
,
TextInput::make('user_info_endpoint_url')
2025-04-24 15:30:07 +07:00
->label(__('oauth.user_info_endpoint_url'))
->required()
,
TextInput::make('id_claim')
2025-04-24 15:30:07 +07:00
->label(__('oauth.id_claim'))
->required()
,
TextInput::make('email_claim')
2025-04-24 15:30:07 +07:00
->label(__('oauth.email_claim'))
2025-05-02 14:22:35 +07:00
->required()
2025-04-24 15:30:07 +07:00
,
TextInput::make('username_claim')
2025-05-02 14:22:35 +07:00
->label(__('oauth.username_claim'))
,
TextInput::make('level_claim')
->label(__('oauth.level_claim'))
,
TextInput::make('level_limit')
->numeric()
->label(__('oauth.level_limit'))
->helperText(__('oauth.level_limit_help'))
,
TextInput::make('priority')
2025-04-24 15:30:07 +07:00
->label(__('label.priority'))
->default(0)
->numeric()
->helperText(__('label.priority_help'))
,
Toggle::make('enabled')
2025-04-24 15:30:07 +07:00
->label(__('label.enabled'))
,
TextInput::make('redirect')
2025-05-02 11:53:56 +07:00
->default(fn ($record) => OauthProvider::getCallbackUrl($record->uuid ?? self::getNewUuid()))
->disabled()
->label(__('oauth.redirect'))
->columnSpanFull()
,
2025-04-24 15:30:07 +07:00
]);
}
2025-05-02 11:53:56 +07:00
private static function getNewUuid(): string
{
return NexusDB::remember("new_oauth_provider_uuid", 86400 * 365, function () {
return UUid\v4();
});
}
2025-04-24 15:30:07 +07:00
public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('id'),
TextColumn::make('name')->label(__('label.name')),
TextColumn::make('client_id')->label(__('oauth.client_id')),
TextColumn::make('client_secret')->label(__('oauth.secret')),
TextColumn::make('authorization_endpoint_url')->label(__('oauth.authorization_endpoint_url')),
TextColumn::make('uuid')
2025-04-24 15:30:07 +07:00
->label(__('oauth.redirect'))
->formatStateUsing(fn ($state) => url("/oauth/callback/$state"))
,
TextColumn::make('priority')->label(__('label.priority')),
TextColumn::make('updated_at')->label(__('label.updated_at')),
TextColumn::make('level_limit')->label(__('oauth.level_limit')),
IconColumn::make('enabled')->boolean()->label(__('label.enabled')),
2025-04-24 15:30:07 +07:00
])
->defaultSort('priority', 'desc')
->filters([
//
])
->recordActions([
EditAction::make(),
DeleteAction::make(),
2025-04-24 15:30:07 +07:00
])
->toolbarActions([
BulkActionGroup::make([
DeleteBulkAction::make(),
2025-04-24 15:30:07 +07:00
]),
]);
}
public static function getPages(): array
{
return [
'index' => ManageProviders::route('/'),
2025-04-24 15:30:07 +07:00
];
}
}