Skip to content

Commit

Permalink
Merge pull request #422 from ECFMP/division-discord-webhooks
Browse files Browse the repository at this point in the history
Division discord webhooks admin
  • Loading branch information
AndyTWF authored Sep 12, 2023
2 parents 771a1ea + 840fc95 commit b6de69e
Show file tree
Hide file tree
Showing 10 changed files with 763 additions and 11 deletions.
77 changes: 77 additions & 0 deletions app/Filament/Resources/DivisionDiscordWebhookResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace App\Filament\Resources;

use App\Filament\Resources\DivisionDiscordWebhookResource\Pages;
use App\Filament\Resources\DivisionDiscordWebhookResource\RelationManagers\FlightinformationregionsRelationManager;
use App\Models\DivisionDiscordWebhook;
use App\Models\FlightInformationRegion;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Form;
use Filament\Resources\Resource;
use Filament\Resources\Table;
use Filament\Tables;
use Filament\Tables\Columns\TagsColumn;
use Filament\Tables\Columns\TextColumn;

class DivisionDiscordWebhookResource extends Resource
{
protected static ?string $model = DivisionDiscordWebhook::class;

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

public static function form(Form $form): Form
{
return $form
->schema([
TextInput::make('description')
->label(__('Description'))
->required()
->maxLength(255),
TextInput::make('url')
->url()
->required()
->maxLength(
500
)
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('id')
->label(__('id')),
TextColumn::make('description')
->label(__('description'))
->searchable(),
TextColumn::make('url')
->label(__('URL')),
TagsColumn::make('firs')
->getStateUsing(fn (DivisionDiscordWebhook $record) => $record->flightInformationRegions->map(fn (FlightInformationRegion $fir) => $fir->identifierName)->toArray())
->label(__('FIRs')),
TextColumn::make('created_at')
->label(__('Created At')),
])
->actions([
Tables\Actions\EditAction::make(),
]);
}

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

public static function getPages(): array
{
return [
'index' => Pages\ListDivisionDiscordWebhooks::route('/'),
'create' => Pages\CreateDivisionDiscordWebhook::route('/create'),
'edit' => Pages\EditDivisionDiscordWebhook::route('/{record}/edit'),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Filament\Resources\DivisionDiscordWebhookResource\Pages;

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

class CreateDivisionDiscordWebhook extends CreateRecord
{
protected static string $resource = DivisionDiscordWebhookResource::class;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Filament\Resources\DivisionDiscordWebhookResource\Pages;

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

class EditDivisionDiscordWebhook extends EditRecord
{
protected static string $resource = DivisionDiscordWebhookResource::class;

protected function getActions(): array
{
return [
Actions\DeleteAction::make(),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Filament\Resources\DivisionDiscordWebhookResource\Pages;

use App\Filament\Resources\DivisionDiscordWebhookResource;
use Filament\Pages\Actions;
use Filament\Resources\Pages\ListRecords;

class ListDivisionDiscordWebhooks extends ListRecords
{
protected static string $resource = DivisionDiscordWebhookResource::class;

protected function getActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Filament\Resources\DivisionDiscordWebhookResource\RelationManagers;

use App\Models\FlightInformationRegion;
use Filament\Forms\Components\TextInput;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Resources\Table;
use Filament\Tables;
use Filament\Tables\Actions\AttachAction;
use Filament\Tables\Actions\DetachAction;

class FlightinformationregionsRelationManager extends RelationManager
{
protected static string $relationship = 'flightInformationRegions';

protected static ?string $title = 'Flight Information Regions';

protected static ?string $recordTitleAttribute = 'identifier';

protected static ?string $inverseRelationship = 'divisionDiscordWebhooks';

public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('fir')
->label(__('FIR'))
->formatStateUsing(fn (FlightInformationRegion $record) => $record->identifierName),
Tables\Columns\TextColumn::make('tag')
->label(__('Mention Tag')),
])
->headerActions([
AttachAction::make('attach-fir')
->form(fn (AttachAction $action) => [
$action->getRecordSelect(),
TextInput::make('tag')
->label(__('Discord Tag'))
->helperText(__('A Discord tag for a person / group / role that should be mentioned in the post.'))
->maxLength(255)
]),
])
->actions([
DetachAction::make(),
]);
}
}
156 changes: 156 additions & 0 deletions app/Policies/DivisionDiscordWebhookPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

namespace App\Policies;

use App\Models\User;
use App\Enums\RoleKey;
use App\Models\DivisionDiscordWebhook;
use Illuminate\Auth\Access\HandlesAuthorization;

class DivisionDiscordWebhookPolicy
{
use HandlesAuthorization;

/**
* Determine whether the user can view any models.
*
* @param \App\Models\User $user
* @return \Illuminate\Auth\Access\Response|bool
*/
public function viewAny(User $user)
{
return in_array($user->role->key, [
RoleKey::SYSTEM,
RoleKey::NMT,
]);
}

/**
* Determine whether the user can view the model.
*
* @param \App\Models\User $user
* @param \App\Models\DivisionDiscordWebhook $divisionDiscordWebhook
* @return \Illuminate\Auth\Access\Response|bool
*/
public function view(User $user, DivisionDiscordWebhook $divisionDiscordWebhook)
{
return in_array($user->role->key, [
RoleKey::SYSTEM,
RoleKey::NMT,
]);
}

/**
* Determine whether the user can create models.
*
* @param \App\Models\User $user
* @return \Illuminate\Auth\Access\Response|bool
*/
public function create(User $user)
{
return in_array($user->role->key, [
RoleKey::SYSTEM,
RoleKey::NMT,
]);
}

/**
* Determine whether the user can update the model.
*
* @param \App\Models\User $user
* @param \App\Models\DivisionDiscordWebhook $divisionDiscordWebhook
* @return \Illuminate\Auth\Access\Response|bool
*/
public function update(User $user, DivisionDiscordWebhook $divisionDiscordWebhook)
{
return in_array($user->role->key, [
RoleKey::SYSTEM,
RoleKey::NMT,
]);
}

/**
* Determine whether the user can delete the model.
*
* @param \App\Models\User $user
* @param \App\Models\DivisionDiscordWebhook $divisionDiscordWebhook
* @return \Illuminate\Auth\Access\Response|bool
*/
public function delete(User $user, DivisionDiscordWebhook $divisionDiscordWebhook)
{
return in_array($user->role->key, [
RoleKey::SYSTEM,
RoleKey::NMT,
]);
}

/**
* Determine whether the user can restore the model.
*
* @param \App\Models\User $user
* @param \App\Models\DivisionDiscordWebhook $divisionDiscordWebhook
* @return \Illuminate\Auth\Access\Response|bool
*/
public function restore(User $user, DivisionDiscordWebhook $divisionDiscordWebhook)
{
return in_array($user->role->key, [
RoleKey::SYSTEM,
RoleKey::NMT,
]);
}

/**
* Determine whether the user can permanently delete the model.
*
* @param \App\Models\User $user
* @param \App\Models\DivisionDiscordWebhook $divisionDiscordWebhook
* @return \Illuminate\Auth\Access\Response|bool
*/
public function forceDelete(User $user, DivisionDiscordWebhook $divisionDiscordWebhook)
{
return in_array($user->role->key, [
RoleKey::SYSTEM,
RoleKey::NMT,
]);
}

public function deleteAny()
{
return false;
}

public function detachAny()
{
return false;
}

/**
* Determine whether the user can restore the model.
*
* @param \App\Models\User $user
* @param \App\Models\DivisionDiscordWebhook $divisionDiscordWebhook
* @return \Illuminate\Auth\Access\Response|bool
*/
public function attach(User $user, DivisionDiscordWebhook $divisionDiscordWebhook)
{
return in_array($user->role->key, [
RoleKey::SYSTEM,
RoleKey::NMT,
]);
}

/**
* Determine whether the user can restore the model.
*
* @param \App\Models\User $user
* @param \App\Models\DivisionDiscordWebhook $divisionDiscordWebhook
* @return \Illuminate\Auth\Access\Response|bool
*/
public function detach(User $user, DivisionDiscordWebhook $divisionDiscordWebhook)
{
return in_array($user->role->key, [
RoleKey::SYSTEM,
RoleKey::NMT,
]);
}
}
3 changes: 3 additions & 0 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace App\Providers;

use App\Models\DivisionDiscordWebhook;
use App\Policies\ActivityPolicy;
use App\Policies\DivisionDiscordWebhookPolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Spatie\Activitylog\Models\Activity;

Expand All @@ -15,6 +17,7 @@ class AuthServiceProvider extends ServiceProvider
*/
protected $policies = [
Activity::class => ActivityPolicy::class,
DivisionDiscordWebhook::class => DivisionDiscordWebhookPolicy::class,
];

/**
Expand Down
Loading

0 comments on commit b6de69e

Please sign in to comment.