Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions app/Console/Commands/UpdateUserRole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace App\Console\Commands;

use App\Models\User;
use Illuminate\Console\Command;

use function Laravel\Prompts\confirm;
use function Laravel\Prompts\info;
use function Laravel\Prompts\select;
use function Laravel\Prompts\text;

class UpdateUserRole extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:update-user-role';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Change the role for a given user.';

/**
* Execute the console command.
*/
public function handle()
{
$email = text(
label: 'What is the email address?',
required: true,
validate: fn (string $value) => match (true) {
! User::firstWhere('email', $value) => 'User not found.',
default => null
}
);

$role = select(
label: 'What role should the user have?',
options: [
'admin' => 'Admin',
'guest' => 'Guest',
'user' => 'User',
],
default: 'guest'
);

$confirmed = confirm(
label: 'Are you sure?',
required: true
);

if ($confirmed) {
User::firstWhere('email', $email)
->update([
'role' => $role,
]);

info('User role updated.');
}
}
}
8 changes: 2 additions & 6 deletions app/Filament/Pages/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@ class Dashboard extends BasePage

protected static string $view = 'filament.pages.dashboard';

protected function getPollingInterval(): ?string
{
return null;
}

protected function getHeaderActions(): array
{
return [
Action::make('speedtest')
->label('Queue Speedtest')
->action('queueSpeedtest'),
->action('queueSpeedtest')
->hidden(fn (): bool => ! auth()->user()->is_admin && ! auth()->user()->is_user),
];
}

Expand Down
10 changes: 10 additions & 0 deletions app/Filament/Pages/DeleteData.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ class DeleteData extends Page

protected ?string $maxContentWidth = '3xl';

public function mount(): void
{
abort_unless(auth()->user()->is_admin, 403);
}

public static function shouldRegisterNavigation(): bool
{
return auth()->user()->is_admin;
}

public function getHeaderActions(): array
{
return [
Expand Down
10 changes: 10 additions & 0 deletions app/Filament/Pages/Settings/GeneralPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ class GeneralPage extends SettingsPage

protected static string $settings = GeneralSettings::class;

public function mount(): void
{
abort_unless(auth()->user()->is_admin, 403);
}

public static function shouldRegisterNavigation(): bool
{
return auth()->user()->is_admin;
}

public function form(Form $form): Form
{
return $form
Expand Down
10 changes: 10 additions & 0 deletions app/Filament/Pages/Settings/InfluxDbPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ class InfluxDbPage extends SettingsPage

protected static string $settings = InfluxDbSettings::class;

public function mount(): void
{
abort_unless(auth()->user()->is_admin, 403);
}

public static function shouldRegisterNavigation(): bool
{
return auth()->user()->is_admin;
}

public function form(Form $form): Form
{
return $form
Expand Down
10 changes: 10 additions & 0 deletions app/Filament/Pages/Settings/NotificationPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ class NotificationPage extends SettingsPage

protected static string $settings = NotificationSettings::class;

public function mount(): void
{
abort_unless(auth()->user()->is_admin, 403);
}

public static function shouldRegisterNavigation(): bool
{
return auth()->user()->is_admin;
}

public function form(Form $form): Form
{
return $form
Expand Down
10 changes: 10 additions & 0 deletions app/Filament/Pages/Settings/ThresholdsPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ class ThresholdsPage extends SettingsPage

protected static string $settings = ThresholdSettings::class;

public function mount(): void
{
abort_unless(auth()->user()->is_admin, 403);
}

public static function shouldRegisterNavigation(): bool
{
return auth()->user()->is_admin;
}

public function form(Form $form): Form
{
return $form
Expand Down
4 changes: 2 additions & 2 deletions app/Filament/Resources/ResultResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class ResultResource extends Resource

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

protected static ?string $navigationLabel = 'Results';

public static function form(Form $form): Form
{
$settings = new GeneralSettings();
Expand Down Expand Up @@ -168,6 +166,7 @@ public static function table(Table $table): Table
Tables\Actions\ViewAction::make(),
Tables\Actions\Action::make('updateComments')
->icon('heroicon-o-chat-bubble-bottom-center-text')
->hidden(fn (): bool => ! auth()->user()->is_admin && ! auth()->user()->is_user)
->mountUsing(fn (Forms\ComponentContainer $form, Result $record) => $form->fill([
'comments' => $record->comments,
]))
Expand All @@ -188,6 +187,7 @@ public static function table(Table $table): Table
Tables\Actions\BulkAction::make('export')
->label('Export selected')
->icon('heroicon-o-arrow-down-tray')
->hidden(fn (): bool => ! auth()->user()->is_admin)
->action(function (Collection $records) {
$export = new ResultsSelectedBulkExport($records->toArray());

Expand Down
83 changes: 57 additions & 26 deletions app/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ class UserResource extends Resource
{
protected static ?string $model = User::class;

protected static ?string $navigationGroup = 'System';

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

protected static ?int $navigationSort = 0;

protected static ?string $slug = 'system/users';
protected static ?string $navigationIcon = 'heroicon-o-users';

public static function form(Form $form): Form
{
Expand Down Expand Up @@ -65,20 +59,48 @@ public static function form(Form $form): Form
->visible(fn ($livewire) => $livewire instanceof EditUser)
->dehydrated(false),
])
->columns('full')
->columns(1)
->columnSpan([
'md' => 2,
]),

Forms\Components\Section::make()
Forms\Components\Grid::make([
'default' => 1,
])
->schema([
Forms\Components\Placeholder::make('created_at')
->content(fn ($record) => $record?->created_at?->diffForHumans() ?? new HtmlString('&mdash;')),
Forms\Components\Placeholder::make('updated_at')
->content(fn ($record) => $record?->updated_at?->diffForHumans() ?? new HtmlString('&mdash;')),
Forms\Components\Section::make()
->schema([
Forms\Components\Select::make('role')
->options([
'admin' => 'Admin',
'guest' => 'Guest',
'user' => 'User',
])
->default('guest')
->disabled(fn (): bool => ! auth()->user()->is_admin || auth()->user()->is_user)
->required(),
])
->columns(1)
->columnSpan([
'md' => 1,
]),

Forms\Components\Section::make()
->schema([
Forms\Components\Placeholder::make('created_at')
->content(fn ($record) => $record?->created_at?->diffForHumans() ?? new HtmlString('&mdash;')),
Forms\Components\Placeholder::make('updated_at')
->content(fn ($record) => $record?->updated_at?->diffForHumans() ?? new HtmlString('&mdash;')),
])
->columns(1)
->columnSpan([
'md' => 1,
]),
])
->columns('full')
->columnSpan(1),
->columns(1)
->columnSpan([
'md' => 1,
]),
]),
]);
}
Expand All @@ -87,27 +109,36 @@ public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('id')
->label('ID'),
Tables\Columns\TextColumn::make('name')
->searchable(),
Tables\Columns\TextColumn::make('email')
->searchable(),
Tables\Columns\TextColumn::make('email_verified_at')
->dateTime(),
Tables\Columns\TextColumn::make('created_at')
->dateTime(),
Tables\Columns\TextColumn::make('role')
->badge()
->color(fn (string $state): string => match ($state) {
'admin' => 'success',
'guest' => 'gray',
'user' => 'info',
}),
Tables\Columns\TextColumn::make('updated_at')
->label('Last updated')
->dateTime(),
])
->filters([
//
Tables\Filters\SelectFilter::make('role')
->options([
'admin' => 'Admin',
'guest' => 'Guest',
'user' => 'User',
]),
])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make()
->requiresConfirmation(),
Tables\Actions\ActionGroup::make([
Tables\Actions\ViewAction::make(),
Tables\Actions\EditAction::make(),
Tables\Actions\DeleteAction::make(),
]),
]);
}
Expand Down
32 changes: 32 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Filament\Models\Contracts\FilamentUser;
use Filament\Panel;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
Expand All @@ -25,6 +26,7 @@ class User extends Authenticatable implements FilamentUser
'email',
'email_verified_at',
'password',
'role',
];

/**
Expand Down Expand Up @@ -53,4 +55,34 @@ public function canAccessPanel(Panel $panel): bool
{
return true;
}

/**
* Determine if the user has an admin role.
*/
protected function isAdmin(): Attribute
{
return Attribute::make(
get: fn (mixed $value, array $attributes): bool => $attributes['role'] == 'admin',
);
}

/**
* Determine if the user has a guest role.
*/
protected function isGuest(): Attribute
{
return Attribute::make(
get: fn (mixed $value, array $attributes): bool => $attributes['role'] == 'guest' || blank($attributes['role']),
);
}

/**
* Determine if the user has a user role.
*/
protected function isUser(): Attribute
{
return Attribute::make(
get: fn (mixed $value, array $attributes): bool => $attributes['role'] == 'user',
);
}
}
Loading