Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User management #306

Merged
merged 2 commits into from
Jan 11, 2023
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
111 changes: 111 additions & 0 deletions app/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace App\Filament\Resources;

use App\Filament\Resources\UserResource\Pages;
use App\Filament\Resources\UserResource\Pages\CreateUser;
use App\Filament\Resources\UserResource\Pages\EditUser;
use App\Models\User;
use Filament\Forms\Components\Card;
use Filament\Forms\Components\Group;
use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Form;
use Filament\Resources\Resource;
use Filament\Resources\Table;
use Filament\Tables\Actions\DeleteBulkAction;
use Filament\Tables\Actions\EditAction;
use Filament\Tables\Columns\TextColumn;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\HtmlString;
use Illuminate\Validation\Rules\Password;

class UserResource extends Resource
{
protected static ?string $model = User::class;

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

protected static ?string $navigationGroup = 'System';

public static function form(Form $form): Form
{
return $form
->schema([
'left' => Card::make([
'name' => TextInput::make('name')
->required(),
'email' => TextInput::make('email')
->required()
->unique(ignoreRecord: true),
'password' => TextInput::make('password')
->required()
->password()
->dehydrateStateUsing(fn ($state) => Hash::make($state))
->visible(fn ($livewire) => $livewire instanceof CreateUser)
->rule(Password::default()),
'new_password_group' => Group::make([
'new_password' => TextInput::make('new_password')
->password()
->label('New Password')
->nullable()
->rule(Password::default())
->visible(fn ($livewire) => $livewire instanceof EditUser)
->dehydrated(false),
'new_password_confirmation' => TextInput::make('new_password_confirmation')
->password()
->label('Confirm New Password')
->rule('required', fn ($get) => (bool) $get('new_password'))
->same('new_password')
->visible(fn ($livewire) => $livewire instanceof EditUser)
->dehydrated(false),
]),
])->columnSpan(8),
'right' => Card::make([
'created_at' => Placeholder::make('created_at')
->content(fn ($record) => $record?->created_at?->diffForHumans() ?? new HtmlString('&mdash;')),
])->columnSpan(4),
])
->columns(12);
}

public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('name'),
TextColumn::make('email'),
TextColumn::make('email_verified_at')
->dateTime(),
TextColumn::make('created_at')
->dateTime(),
TextColumn::make('updated_at')
->dateTime(),
])
->filters([
//
])
->actions([
EditAction::make(),
])
->bulkActions([
DeleteBulkAction::make(),
]);
}

public static function getRelations(): array
{
return [
//
];
}

public static function getPages(): array
{
return [
'index' => Pages\ListUsers::route('/'),
'create' => Pages\CreateUser::route('/create'),
'edit' => Pages\EditUser::route('/{record}/edit'),
];
}
}
11 changes: 11 additions & 0 deletions app/Filament/Resources/UserResource/Pages/CreateUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Filament\Resources\UserResource\Pages;

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

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

namespace App\Filament\Resources\UserResource\Pages;

use App\Filament\Resources\UserResource;
use Filament\Pages\Actions;
use Filament\Resources\Pages\EditRecord;
use Illuminate\Support\Facades\Hash;

class EditUser extends EditRecord
{
protected static string $resource = UserResource::class;

protected function getActions(): array
{
return [
Actions\DeleteAction::make(),
];
}

public function beforeSave()
{
if (! array_key_exists('new_password', $this->data) || ! filled($this->data['new_password'])) {
return;
}

$this->record->password = Hash::make($this->data['new_password']);
}
}
19 changes: 19 additions & 0 deletions app/Filament/Resources/UserResource/Pages/ListUsers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Filament\Resources\UserResource\Pages;

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

class ListUsers extends ListRecords
{
protected static string $resource = UserResource::class;

protected function getActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}