Skip to content

Commit

Permalink
Merge pull request #16 from canyongbs/feature/roles-and-permissions-r…
Browse files Browse the repository at this point in the history
…esources

[Feature] Roles and Permissions Resources
  • Loading branch information
Orrison authored Jul 18, 2023
2 parents a391acf + 3420033 commit 5555897
Show file tree
Hide file tree
Showing 19 changed files with 566 additions and 25 deletions.
75 changes: 75 additions & 0 deletions app/Filament/Resources/PermissionResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace App\Filament\Resources;

use Filament\Forms;
use Filament\Tables;
use Filament\Forms\Form;
use App\Models\Permission;
use Filament\Tables\Table;
use Filament\Resources\Resource;
use App\Filament\Resources\PermissionResource\Pages;
use App\Filament\Resources\PermissionResource\RelationManagers\RolesRelationManager;

class PermissionResource extends Resource
{
protected static ?string $model = Permission::class;

protected static ?string $navigationIcon = 'heroicon-o-key';

protected static ?string $navigationGroup = 'Administration';

protected static ?int $navigationSort = 2;

public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(125),
Forms\Components\TextInput::make('guard_name')
->required()
->maxLength(125),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->searchable(),
Tables\Columns\TextColumn::make('guard_name')
->searchable(),
Tables\Columns\TextColumn::make('created_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])
->filters([
])
->actions([
Tables\Actions\ViewAction::make(),
]);
}

public static function getRelations(): array
{
return [
RolesRelationManager::class,
];
}

public static function getPages(): array
{
return [
'index' => Pages\ListPermissions::route('/'),
'view' => Pages\ViewPermission::route('/{record}'),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Filament\Resources\PermissionResource\Pages;

use Filament\Resources\Pages\ListRecords;
use Illuminate\Database\Eloquent\Builder;
use Filament\Resources\Pages\ListRecords\Tab;
use App\Filament\Resources\PermissionResource;

class ListPermissions extends ListRecords
{
protected static string $resource = PermissionResource::class;

public function getTabs(): array
{
return [
'web' => Tab::make('Web')
->modifyQueryUsing(fn (Builder $query) => $query->web()),
'api' => Tab::make('Api')
->modifyQueryUsing(fn (Builder $query) => $query->api()),
];
}

protected function getHeaderActions(): array
{
return [];
}
}
17 changes: 17 additions & 0 deletions app/Filament/Resources/PermissionResource/Pages/ViewPermission.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Filament\Resources\PermissionResource\Pages;

use Filament\Resources\Pages\ViewRecord;
use App\Filament\Resources\PermissionResource;

class ViewPermission extends ViewRecord
{
protected static string $resource = PermissionResource::class;

protected function getHeaderActions(): array
{
return [
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Filament\Resources\PermissionResource\RelationManagers;

use Filament\Forms;
use Filament\Tables;
use Filament\Forms\Form;
use Filament\Tables\Table;
use Filament\Resources\RelationManagers\RelationManager;

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

protected static ?string $recordTitleAttribute = 'name';

public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
]);
}

public function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name'),
])
->filters([
])
->headerActions([
Tables\Actions\CreateAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
}
84 changes: 84 additions & 0 deletions app/Filament/Resources/RoleResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace App\Filament\Resources;

use Filament\Forms;
use App\Models\Role;
use Filament\Tables;
use Filament\Forms\Form;
use Filament\Tables\Table;
use Filament\Resources\Resource;
use Illuminate\Database\Eloquent\Builder;
use App\Filament\Resources\RoleResource\Pages;
use App\Filament\Resources\RoleResource\RelationManagers\PermissionsRelationManager;

class RoleResource extends Resource
{
protected static ?string $model = Role::class;

protected static ?string $navigationIcon = 'heroicon-o-shield-check';

protected static ?string $navigationGroup = 'Administration';

protected static ?int $navigationSort = 1;

public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(125),
Forms\Components\TextInput::make('guard_name')
->required()
->maxLength(125),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->searchable(),
Tables\Columns\TextColumn::make('guard_name')
->searchable(),
Tables\Columns\TextColumn::make('created_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
Tables\Columns\TextColumn::make('updated_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])
->actions([
Tables\Actions\ViewAction::make(),
])
->bulkActions([
]);
}

public static function getRelations(): array
{
return [
PermissionsRelationManager::class,
];
}

public static function getPages(): array
{
return [
'index' => Pages\ListRoles::route('/'),
'create' => Pages\CreateRole::route('/create'),
'view' => Pages\ViewRole::route('/{record}'),
'edit' => Pages\EditRole::route('/{record}/edit'),
];
}

public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()
->withoutGlobalScopes([]);
}
}
11 changes: 11 additions & 0 deletions app/Filament/Resources/RoleResource/Pages/CreateRole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Filament\Resources\RoleResource\Pages;

use App\Filament\Resources\RoleResource;
use Filament\Resources\Pages\CreateRecord;

class CreateRole extends CreateRecord
{
protected static string $resource = RoleResource::class;
}
19 changes: 19 additions & 0 deletions app/Filament/Resources/RoleResource/Pages/EditRole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Filament\Resources\RoleResource\Pages;

use Filament\Actions;
use App\Filament\Resources\RoleResource;
use Filament\Resources\Pages\EditRecord;

class EditRole extends EditRecord
{
protected static string $resource = RoleResource::class;

protected function getHeaderActions(): array
{
return [
Actions\ViewAction::make(),
];
}
}
31 changes: 31 additions & 0 deletions app/Filament/Resources/RoleResource/Pages/ListRoles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Filament\Resources\RoleResource\Pages;

use Filament\Actions;
use App\Filament\Resources\RoleResource;
use Filament\Resources\Pages\ListRecords;
use Filament\Resources\Pages\ListRecords\Tab;
use Illuminate\Contracts\Database\Eloquent\Builder;

class ListRoles extends ListRecords
{
protected static string $resource = RoleResource::class;

public function getTabs(): array
{
return [
'web' => Tab::make('Web')
->modifyQueryUsing(fn (Builder $query) => $query->web()),
'api' => Tab::make('Api')
->modifyQueryUsing(fn (Builder $query) => $query->api()),
];
}

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
17 changes: 17 additions & 0 deletions app/Filament/Resources/RoleResource/Pages/ViewRole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Filament\Resources\RoleResource\Pages;

use App\Filament\Resources\RoleResource;
use Filament\Resources\Pages\ViewRecord;

class ViewRole extends ViewRecord
{
protected static string $resource = RoleResource::class;

protected function getHeaderActions(): array
{
return [
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Filament\Resources\RoleResource\RelationManagers;

use Filament\Forms;
use Filament\Tables;
use Filament\Forms\Form;
use Filament\Tables\Table;
use Filament\Resources\RelationManagers\RelationManager;

class PermissionsRelationManager extends RelationManager
{
protected static string $relationship = 'permissions';

protected static ?string $recordTitleAttribute = 'name';

public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
]);
}

public function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name'),
])
->filters([
])
->headerActions([
Tables\Actions\CreateAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
}
Loading

0 comments on commit 5555897

Please sign in to comment.