Skip to content

Commit

Permalink
feat: filament 3
Browse files Browse the repository at this point in the history
  • Loading branch information
phpsa committed Nov 30, 2023
1 parent fe7facf commit cb54d84
Show file tree
Hide file tree
Showing 19 changed files with 304 additions and 136 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
"minimum-stability": "dev",
"require": {
"php": "^8.0",
"filament/filament": "^2.16",
"filament/filament": "^3.0",
"lab404/laravel-impersonate": "^1.7",
"spatie/laravel-package-tools": "^1.13",
"spatie/laravel-permission": "^5.5"
"spatie/laravel-permission": "^5.5|^6.0"
},
"require-dev": {
"laravel/pint": "^1.2"
Expand Down
10 changes: 5 additions & 5 deletions src/FilamentAuthenticationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

use Filament\Facades\Filament;
use Filament\Navigation\UserMenuItem;
use Filament\PluginServiceProvider;
use Filament\Tables\Columns\TextColumn;
use Illuminate\Support\Facades\Config;
use Phpsa\FilamentAuthentication\Http\Middleware\ImpersonatingMiddleware;
use Phpsa\FilamentAuthentication\Widgets\LatestUsersWidget;
use Filament\Tables\Columns\TextColumn;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Phpsa\FilamentAuthentication\Widgets\LatestUsersWidget;
use Phpsa\FilamentAuthentication\Http\Middleware\ImpersonatingMiddleware;

class FilamentAuthenticationProvider extends PluginServiceProvider
class FilamentAuthenticationProvider extends PackageServiceProvider
{
public static string $name = 'filament-authentication';

Expand Down
10 changes: 5 additions & 5 deletions src/Pages/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Profile extends Page
*/
public array $formData;

protected static function shouldRegisterNavigation(): bool
public static function shouldRegisterNavigation(): bool
{
return false;
}
Expand All @@ -52,7 +52,7 @@ public function mount(): void
{
$this->form->fill([
// @phpstan-ignore-next-line
'name' => $this->getFormModel()->name,
'name' => $this->getFormModel()->name,
// @phpstan-ignore-next-line
'email' => $this->getFormModel()->email,
]);
Expand All @@ -63,8 +63,8 @@ public function submit(): void
$data = $this->form->getState();

$state = array_filter([
'name' => $data['name'],
'email' => $data['email'],
'name' => $data['name'],
'email' => $data['email'],
'password' => $data['new_password'] ? Hash::make($data['new_password']) : null,
]);

Expand All @@ -83,7 +83,7 @@ public function getCancelButtonUrlProperty(): string
return static::getUrl();
}

protected function getBreadcrumbs(): array
public function getBreadcrumbs(): array
{
return [
url()->current() => 'Profile',
Expand Down
38 changes: 25 additions & 13 deletions src/Resources/PermissionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@

namespace Phpsa\FilamentAuthentication\Resources;

use Filament\Forms\Components\BelongsToManyMultiSelect;
use Filament\Forms\Components\Card;
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Form;
use Filament\Forms\Form;
use Filament\Tables\Table;
use Filament\Resources\Resource;
use Filament\Resources\Table;
use Filament\Forms\Components\Grid;
use Filament\Tables\Actions\EditAction;
use Filament\Tables\Actions\ViewAction;
use Filament\Tables\Columns\TextColumn;
use Phpsa\FilamentAuthentication\Resources\PermissionResource\Pages\CreatePermission;
use Filament\Forms\Components\TextInput;
use Spatie\Permission\Models\Permission;
use Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Actions\DeleteBulkAction;
use Filament\Forms\Components\Section as Card;
use Filament\Forms\Components\BelongsToManyMultiSelect;
use Phpsa\FilamentAuthentication\Resources\PermissionResource\Pages\EditPermission;
use Phpsa\FilamentAuthentication\Resources\PermissionResource\Pages\ListPermissions;
use Phpsa\FilamentAuthentication\Resources\PermissionResource\Pages\ViewPermission;
use Phpsa\FilamentAuthentication\Resources\PermissionResource\Pages\ListPermissions;
use Phpsa\FilamentAuthentication\Resources\PermissionResource\Pages\CreatePermission;
use Phpsa\FilamentAuthentication\Resources\PermissionResource\RelationManager\RoleRelationManager;
use Spatie\Permission\Models\Permission;

class PermissionResource extends Resource
{
Expand All @@ -33,7 +37,7 @@ public static function getLabel(): string
return strval(__('filament-authentication::filament-authentication.section.permission'));
}

protected static function getNavigationGroup(): ?string
public static function getNavigationGroup(): ?string
{
return strval(__(config('filament-authentication.section.group') ?? 'filament-authentication::filament-authentication.section.group'));
}
Expand Down Expand Up @@ -82,6 +86,14 @@ public static function table(Table $table): Table
])
->filters([
//
])
->actions([
ViewAction::make(),
EditAction::make(),
DeleteAction::make(),
])
->bulkActions([
DeleteBulkAction::make(),
]);
}

Expand All @@ -95,10 +107,10 @@ public static function getRelations(): array
public static function getPages(): array
{
return [
'index' => ListPermissions::route('/'),
'index' => ListPermissions::route('/'),
'create' => CreatePermission::route('/create'),
'edit' => EditPermission::route('/{record}/edit'),
'view' => ViewPermission::route('/{record}'),
'edit' => EditPermission::route('/{record}/edit'),
'view' => ViewPermission::route('/{record}'),
];
}
}
14 changes: 12 additions & 2 deletions src/Resources/PermissionResource/Pages/EditPermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Phpsa\FilamentAuthentication\Resources\PermissionResource\Pages;

use Filament\Resources\Pages\EditRecord;
use Filament\Actions\ViewAction;
use Filament\Actions\DeleteAction;
use Illuminate\Support\Facades\Config;
use Spatie\Permission\Contracts\Permission;
use Filament\Resources\Pages\EditRecord;
use Spatie\Permission\PermissionRegistrar;
use Spatie\Permission\Contracts\Permission;

class EditPermission extends EditRecord
{
Expand All @@ -22,4 +24,12 @@ public function afterSave(): void

app(PermissionRegistrar::class)->forgetCachedPermissions();
}

protected function getHeaderActions(): array
{
return [
ViewAction::make(),
DeleteAction::make(),
];
}
}
46 changes: 27 additions & 19 deletions src/Resources/PermissionResource/Pages/ListPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Phpsa\FilamentAuthentication\Resources\PermissionResource\Pages;

use Filament\Actions\CreateAction;
use Filament\Forms\Components\Select;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Support\Facades\Config;
use Filament\Tables\Actions\BulkAction;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Config;

class ListPermissions extends ListRecords
{
Expand All @@ -15,25 +16,32 @@ public static function getResource(): string
return Config::get('filament-authentication.resources.PermissionResource');
}

protected function getTableBulkActions(): array
protected function getHeaderActions(): array
{
$roleClass = config('filament-authentication.models.Role');

return [
BulkAction::make('Attach Role')
->action(function (Collection $records, array $data): void {
// dd($data);
foreach ($records as $record) {
$record->roles()->sync($data['role']);
$record->save();
}
})
->form([
Select::make('role')
->label(strval(__('filament-authentication::filament-authentication.field.role')))
->options((new $roleClass)::query()->pluck('name', 'id'))
->required(),
])->deselectRecordsAfterCompletion(),
CreateAction::make(),
];
}

// protected function getTableBulkActions(): array
// {
// $roleClass = config('filament-authentication.models.Role');

// return [
// BulkAction::make('Attach Role')
// ->action(function (Collection $records, array $data): void {
// // dd($data);
// foreach ($records as $record) {
// $record->roles()->sync($data['role']);
// $record->save();
// }
// })
// ->form([
// Select::make('role')
// ->label(strval(__('filament-authentication::filament-authentication.field.role')))
// ->options((new $roleClass())::query()->pluck('name', 'id'))
// ->required(),
// ])->deselectRecordsAfterCompletion(),
// ];
// }
}
10 changes: 9 additions & 1 deletion src/Resources/PermissionResource/Pages/ViewPermission.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@

namespace Phpsa\FilamentAuthentication\Resources\PermissionResource\Pages;

use Filament\Resources\Pages\ViewRecord;
use Filament\Actions\EditAction;
use Illuminate\Support\Facades\Config;
use Filament\Resources\Pages\ViewRecord;

class ViewPermission extends ViewRecord
{
public static function getResource(): string
{
return Config::get('filament-authentication.resources.PermissionResource');
}

protected function getHeaderActions(): array
{
return[
EditAction::make(),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@

namespace Phpsa\FilamentAuthentication\Resources\PermissionResource\RelationManager;

use Filament\Forms\Components\TextInput;
use Filament\Resources\Form;
use Filament\Resources\RelationManagers\BelongsToManyRelationManager;
use Filament\Resources\Table;
use Filament\Forms\Form;
use Filament\Tables\Table;
use Filament\Tables\Columns\TextColumn;
use Filament\Forms\Components\TextInput;
use Filament\Tables\Actions\AttachAction;
use Filament\Tables\Actions\CreateAction;
use Filament\Tables\Actions\DetachAction;
use Spatie\Permission\PermissionRegistrar;
use Filament\Tables\Actions\DissociateBulkAction;
use Filament\Resources\RelationManagers\RelationManager;

class RoleRelationManager extends BelongsToManyRelationManager
class RoleRelationManager extends RelationManager
{
protected static string $relationship = 'roles';

protected static ?string $recordTitleAttribute = 'name';

public static function form(Form $form): Form
public function form(Form $form): Form
{
return $form
->schema([
Expand All @@ -28,7 +32,7 @@ public static function form(Form $form): Form
]);
}

public static function table(Table $table): Table
public function table(Table $table): Table
{
return $table
->columns([
Expand All @@ -38,8 +42,18 @@ public static function table(Table $table): Table
->label(strval(__('filament-authentication::filament-authentication.field.guard_name'))),

])
->filters([
//
->headerActions([
CreateAction::make(),
AttachAction::make(),
])
->actions([
DetachAction::make()
])

->bulkActions([

DissociateBulkAction::make(),

]);
}

Expand All @@ -52,4 +66,9 @@ public function afterDetach(): void
{
app(PermissionRegistrar::class)->forgetCachedPermissions();
}

public function isReadOnly(): bool
{
return false;
}
}
Loading

0 comments on commit cb54d84

Please sign in to comment.