Skip to content

Commit

Permalink
move consts from resouce into enum & model
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy132 committed Sep 19, 2024
1 parent 16dbb53 commit 6ff4083
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 42 deletions.
16 changes: 16 additions & 0 deletions app/Enums/RolePermissionModels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Enums;

enum RolePermissionModels: string
{
case ApiKey = 'apikey';
case DatabaseHost = 'databasehost';
case Database = 'database';
case Egg = 'egg';
case Mount = 'mount';
case Node = 'node';
case Role = 'role';
case Server = 'server';
case User = 'user';
}
12 changes: 12 additions & 0 deletions app/Enums/RolePermissionPrefixes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Enums;

enum RolePermissionPrefixes: string
{
case ViewAny = 'viewList';
case View = 'view';
case Create = 'create';
case Update = 'update';
case Delete = 'delete';
}
52 changes: 10 additions & 42 deletions app/Filament/Resources/RoleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace App\Filament\Resources;

use App\Enums\RolePermissionModels;
use App\Enums\RolePermissionPrefixes;
use App\Filament\Resources\RoleResource\Pages;
use App\Models\Role;
use Filament\Facades\Filament;
Expand Down Expand Up @@ -30,61 +32,27 @@ public static function getNavigationBadge(): ?string
return static::getModel()::count() ?: null;
}

private const PERMISSION_MODELS = [
'ApiKey',
'DatabaseHost',
'Database',
'Egg',
'Mount',
'Node',
'Role',
'Server',
'User',
];

private const PERMISSION_PREFIXES = [
'viewList',
'view',
'create',
'update',
'delete',
];

private const MODEL_SPECIFIC_PERMISSIONS = [
'Egg' => [
'import',
'export',
],
];

private const SPECIAL_PERMISSIONS = [
'Settings' => [
'view',
'update',
],
];

public static function form(Form $form): Form
{
$permissions = [];

foreach (self::PERMISSION_MODELS as $model) {
foreach (RolePermissionModels::cases() as $model) {
$options = [];

foreach (self::PERMISSION_PREFIXES as $prefix) {
$options[$prefix . ' ' . strtolower($model)] = Str::headline($prefix);
foreach (RolePermissionPrefixes::cases() as $prefix) {
$options[$prefix->value . ' ' . strtolower($model->value)] = Str::headline($prefix);
}

if (array_key_exists($model, self::MODEL_SPECIFIC_PERMISSIONS)) {
foreach (self::MODEL_SPECIFIC_PERMISSIONS[$model] as $permission) {
$options[$permission . ' ' . strtolower($model)] = Str::headline($permission);
if (array_key_exists($model, Role::MODEL_SPECIFIC_PERMISSIONS)) {
foreach (Role::MODEL_SPECIFIC_PERMISSIONS[$model] as $permission) {
$options[$permission . ' ' . strtolower($model->value)] = Str::headline($permission);
}
}

$permissions[] = self::makeSection($model, $options);
$permissions[] = self::makeSection($model->value, $options);
}

foreach (self::SPECIAL_PERMISSIONS as $model => $prefixes) {
foreach (Role::SPECIAL_PERMISSIONS as $model => $prefixes) {
$options = [];

foreach ($prefixes as $prefix) {
Expand Down
14 changes: 14 additions & 0 deletions app/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ class Role extends BaseRole

public const ROOT_ADMIN = 'Root Admin';

public const MODEL_SPECIFIC_PERMISSIONS = [
'egg' => [
'import',
'export',
],
];

public const SPECIAL_PERMISSIONS = [
'settings' => [
'view',
'update',
],
];

public function isRootAdmin(): bool
{
return $this->name === self::ROOT_ADMIN;
Expand Down

0 comments on commit 6ff4083

Please sign in to comment.