filament user
This commit is contained in:
parent
96d7d4048e
commit
3ea8245f85
196
app/Filament/Resources/UserResource.php
Normal file
196
app/Filament/Resources/UserResource.php
Normal file
@ -0,0 +1,196 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Resources;
|
||||||
|
|
||||||
|
use App\Filament\Resources\UserResource\Pages;
|
||||||
|
use App\Models\Department;
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Services\ACL\RoleService;
|
||||||
|
use App\Services\TimezoneList;
|
||||||
|
use Filament\Forms;
|
||||||
|
use Filament\Forms\Components\DateTimePicker;
|
||||||
|
use Filament\Forms\Components\FileUpload;
|
||||||
|
use Filament\Forms\Components\Select;
|
||||||
|
use Filament\Forms\Components\Textarea;
|
||||||
|
use Filament\Forms\Components\TextInput;
|
||||||
|
use Filament\Forms\Components\Toggle;
|
||||||
|
use Filament\Forms\Form;
|
||||||
|
use Filament\Forms\Get;
|
||||||
|
use Filament\Resources\Resource;
|
||||||
|
use Filament\Tables;
|
||||||
|
use Filament\Tables\Actions\BulkActionGroup;
|
||||||
|
use Filament\Tables\Actions\EditAction;
|
||||||
|
use Filament\Tables\Actions\ViewAction;
|
||||||
|
use Filament\Tables\Columns\ImageColumn;
|
||||||
|
use Filament\Tables\Columns\TextColumn;
|
||||||
|
use Filament\Tables\Table;
|
||||||
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
|
||||||
|
use Ramsey\Uuid\Uuid;
|
||||||
|
|
||||||
|
class UserResource extends Resource
|
||||||
|
{
|
||||||
|
protected static ?string $model = User::class;
|
||||||
|
|
||||||
|
protected static ?string $navigationIcon = 'heroicon-o-users';
|
||||||
|
|
||||||
|
public static function form(Form $form): Form
|
||||||
|
{
|
||||||
|
$isCreate = $form->getOperation() === 'create';
|
||||||
|
|
||||||
|
return $form
|
||||||
|
->schema([
|
||||||
|
Forms\Components\TextInput::make('guid')
|
||||||
|
->label('Unique ID')
|
||||||
|
->default(sprintf('USR-%s', Str::of(Uuid::uuid4())->lower()))
|
||||||
|
->disabled()
|
||||||
|
->dehydrated()
|
||||||
|
->required()
|
||||||
|
->maxLength(40)
|
||||||
|
->unique(ignoreRecord: true),
|
||||||
|
Toggle::make('is_active')
|
||||||
|
->required(),
|
||||||
|
TextInput::make('prefix')
|
||||||
|
->maxLength(80),
|
||||||
|
TextInput::make('first_name')
|
||||||
|
->required()
|
||||||
|
->maxLength(120),
|
||||||
|
TextInput::make('last_name')
|
||||||
|
->maxLength(120),
|
||||||
|
TextInput::make('display_name')
|
||||||
|
->required()
|
||||||
|
->maxLength(160),
|
||||||
|
TextInput::make('username')
|
||||||
|
->unique(ignoreRecord: true)
|
||||||
|
->required()
|
||||||
|
->maxLength(24),
|
||||||
|
TextInput::make('password')
|
||||||
|
->password()
|
||||||
|
->revealable()
|
||||||
|
->dehydrated(fn ($state) => filled($state))
|
||||||
|
->dehydrateStateUsing(fn ($state) => Hash::make($state))
|
||||||
|
->required(fn (string $context): bool => $context === 'create')
|
||||||
|
->maxLength(32),
|
||||||
|
TextInput::make('phone')
|
||||||
|
->unique(ignoreRecord: true)
|
||||||
|
->tel()
|
||||||
|
->telRegex('/^\+?[1-9]\d{8,14}$/')
|
||||||
|
->maxLength(80),
|
||||||
|
TextInput::make('email')
|
||||||
|
->unique(ignoreRecord: true)
|
||||||
|
->email()
|
||||||
|
->maxLength(80),
|
||||||
|
DateTimePicker::make('email_verified_at'),
|
||||||
|
TextInput::make('profile_photo_path')
|
||||||
|
->maxLength(255),
|
||||||
|
FileUpload::make('signature_image_path')
|
||||||
|
->disk('public')
|
||||||
|
->visibility('public')
|
||||||
|
->directory('signatures')
|
||||||
|
->preserveFilenames()
|
||||||
|
->getUploadedFileNameForStorageUsing(
|
||||||
|
function (TemporaryUploadedFile $file) {
|
||||||
|
return Str::of(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME))
|
||||||
|
->slug()
|
||||||
|
->prepend(now()->timestamp . '_')
|
||||||
|
->append('.' . $file->getClientOriginalExtension());
|
||||||
|
}
|
||||||
|
)
|
||||||
|
->image(),
|
||||||
|
Textarea::make('signature_text')
|
||||||
|
->columnSpanFull(),
|
||||||
|
Select::make('organization_id')
|
||||||
|
->label('Organization')
|
||||||
|
->live()
|
||||||
|
->relationship('organization', 'name'),
|
||||||
|
Select::make('department_id')
|
||||||
|
->label('Department')
|
||||||
|
->options(function (Get $get) {
|
||||||
|
$organization_id = $get('organization_id');
|
||||||
|
$result = [];
|
||||||
|
if ($organization_id != null) {
|
||||||
|
$result = Department::active()->organization($organization_id)->pluck('name', 'id')->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}),
|
||||||
|
Select::make('roles')
|
||||||
|
->relationship('roles', 'name')
|
||||||
|
->multiple()
|
||||||
|
->required(fn (string $context): bool => $context === 'create')
|
||||||
|
->searchable()
|
||||||
|
// ->options(RoleService::select())
|
||||||
|
->afterStateUpdated(function ($state, User $user) {
|
||||||
|
$user->assignRole($state);
|
||||||
|
}),
|
||||||
|
Select::make('timezone')
|
||||||
|
->options(
|
||||||
|
(new TimezoneList)->splitGroup(true)->toArray(false)
|
||||||
|
)
|
||||||
|
->required()
|
||||||
|
->default('Asia/Dhaka'),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function table(Table $table): Table
|
||||||
|
{
|
||||||
|
return $table
|
||||||
|
->columns([
|
||||||
|
Tables\Columns\IconColumn::make('is_active')
|
||||||
|
->label('Active?')
|
||||||
|
->boolean(),
|
||||||
|
TextColumn::make('display_name')
|
||||||
|
->label('Name')
|
||||||
|
->searchable(),
|
||||||
|
TextColumn::make('username')
|
||||||
|
->searchable(),
|
||||||
|
TextColumn::make('email')
|
||||||
|
->searchable(),
|
||||||
|
// ImageColumn::make('signature_image_path'),
|
||||||
|
TextColumn::make('organization.name')
|
||||||
|
->label('Org')
|
||||||
|
->badge()
|
||||||
|
->sortable(),
|
||||||
|
TextColumn::make('department.name')
|
||||||
|
->label('Dept')
|
||||||
|
->sortable(),
|
||||||
|
TextColumn::make('created_at')
|
||||||
|
->dateTime()
|
||||||
|
->sortable()
|
||||||
|
->toggleable(isToggledHiddenByDefault: true),
|
||||||
|
TextColumn::make('guid')
|
||||||
|
->limit(16)
|
||||||
|
->searchable(),
|
||||||
|
])
|
||||||
|
->filters([
|
||||||
|
//
|
||||||
|
])
|
||||||
|
->actions([
|
||||||
|
ViewAction::make(),
|
||||||
|
EditAction::make(),
|
||||||
|
])
|
||||||
|
->bulkActions([
|
||||||
|
BulkActionGroup::make([
|
||||||
|
Tables\Actions\DeleteBulkAction::make(),
|
||||||
|
]),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getRelations(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
//
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getPages(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'index' => Pages\ListUsers::route('/'),
|
||||||
|
'create' => Pages\CreateUser::route('/create'),
|
||||||
|
'view' => Pages\ViewUser::route('/{record}'),
|
||||||
|
'edit' => Pages\EditUser::route('/{record}/edit'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
11
app/Filament/Resources/UserResource/Pages/CreateUser.php
Normal file
11
app/Filament/Resources/UserResource/Pages/CreateUser.php
Normal file
@ -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;
|
||||||
|
}
|
20
app/Filament/Resources/UserResource/Pages/EditUser.php
Normal file
20
app/Filament/Resources/UserResource/Pages/EditUser.php
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Resources\UserResource\Pages;
|
||||||
|
|
||||||
|
use App\Filament\Resources\UserResource;
|
||||||
|
use Filament\Actions;
|
||||||
|
use Filament\Resources\Pages\EditRecord;
|
||||||
|
|
||||||
|
class EditUser extends EditRecord
|
||||||
|
{
|
||||||
|
protected static string $resource = UserResource::class;
|
||||||
|
|
||||||
|
protected function getHeaderActions(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
Actions\ViewAction::make(),
|
||||||
|
Actions\DeleteAction::make(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
19
app/Filament/Resources/UserResource/Pages/ListUsers.php
Normal file
19
app/Filament/Resources/UserResource/Pages/ListUsers.php
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Resources\UserResource\Pages;
|
||||||
|
|
||||||
|
use App\Filament\Resources\UserResource;
|
||||||
|
use Filament\Actions;
|
||||||
|
use Filament\Resources\Pages\ListRecords;
|
||||||
|
|
||||||
|
class ListUsers extends ListRecords
|
||||||
|
{
|
||||||
|
protected static string $resource = UserResource::class;
|
||||||
|
|
||||||
|
protected function getHeaderActions(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
Actions\CreateAction::make(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
19
app/Filament/Resources/UserResource/Pages/ViewUser.php
Normal file
19
app/Filament/Resources/UserResource/Pages/ViewUser.php
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Resources\UserResource\Pages;
|
||||||
|
|
||||||
|
use App\Filament\Resources\UserResource;
|
||||||
|
use Filament\Actions;
|
||||||
|
use Filament\Resources\Pages\ViewRecord;
|
||||||
|
|
||||||
|
class ViewUser extends ViewRecord
|
||||||
|
{
|
||||||
|
protected static string $resource = UserResource::class;
|
||||||
|
|
||||||
|
protected function getHeaderActions(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
Actions\EditAction::make(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
28
app/Services/ACL/RoleService.php
Normal file
28
app/Services/ACL/RoleService.php
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services\ACL;
|
||||||
|
|
||||||
|
use App\Domain\ACL\Role;
|
||||||
|
use Spatie\Permission\Models\Role as SpatieRole;
|
||||||
|
|
||||||
|
final class RoleService
|
||||||
|
{
|
||||||
|
private static array $roles = [];
|
||||||
|
|
||||||
|
private static function initCache(): void
|
||||||
|
{
|
||||||
|
if (empty(self::$roles)) {
|
||||||
|
self::$roles = SpatieRole::pluck('id', 'name')->toArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function select(): array
|
||||||
|
{
|
||||||
|
// self::initCache();
|
||||||
|
|
||||||
|
return collect(Role::cases())
|
||||||
|
// ->mapWithKeys(fn (Role $r) => [self::$roles[$r->value] => $r->name])
|
||||||
|
->mapWithKeys(fn (Role $r) => [$r->value => $r->name])
|
||||||
|
->toArray();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user