|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Filament\Admin\Resources; |
| 4 | + |
| 5 | +use Filament\Forms; |
| 6 | +use App\Models\User; |
| 7 | +use Filament\Tables; |
| 8 | +use Filament\Forms\Form; |
| 9 | +use Filament\Tables\Table; |
| 10 | +use Illuminate\Support\Str; |
| 11 | +use App\Mail\PasswordResetMail; |
| 12 | +use Filament\Tables\Actions\EditAction; |
| 13 | +use Filament\Resources\Resource; |
| 14 | +use Filament\Tables\Actions\Action; |
| 15 | +use Illuminate\Support\Facades\Hash; |
| 16 | +use Illuminate\Support\Facades\Mail; |
| 17 | +use Filament\Tables\Actions\ViewAction; |
| 18 | +use Filament\Notifications\Notification; |
| 19 | +use Filament\Tables\Actions\ActionGroup; |
| 20 | +use Filament\Tables\Actions\DeleteAction; |
| 21 | +use Illuminate\Database\Eloquent\Builder; |
| 22 | +use Illuminate\Database\Eloquent\SoftDeletingScope; |
| 23 | +use App\Filament\Admin\Resources\UserResource\Pages; |
| 24 | +use App\Filament\Admin\Resources\UserResource\RelationManagers; |
| 25 | +use App\Filament\Admin\Resources\UserResource\RelationManagers\UseraddressesRelationManager; |
| 26 | + |
| 27 | + |
| 28 | +class UserResource extends Resource |
| 29 | +{ |
| 30 | + protected static ?string $model = User::class; |
| 31 | + |
| 32 | + protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack'; |
| 33 | + |
| 34 | + public static function form(Form $form): Form |
| 35 | + { |
| 36 | + return $form |
| 37 | + ->schema([ |
| 38 | + Forms\Components\TextInput::make('name') |
| 39 | + ->required() |
| 40 | + ->maxLength(255), |
| 41 | + Forms\Components\TextInput::make('email') |
| 42 | + ->email() |
| 43 | + ->required() |
| 44 | + ->maxLength(255), |
| 45 | + Forms\Components\TextInput::make('telephone') |
| 46 | + ->tel() |
| 47 | + ->required() |
| 48 | + ->maxLength(255), |
| 49 | + Forms\Components\TextInput::make('document_number') |
| 50 | + ->required() |
| 51 | + ->maxLength(255), |
| 52 | + Forms\Components\TextInput::make('avatar_url') |
| 53 | + ->maxLength(255), |
| 54 | + Forms\Components\TextInput::make('settings'), |
| 55 | + Forms\Components\Toggle::make('is_admin') |
| 56 | + ->required(), |
| 57 | + Forms\Components\DateTimePicker::make('email_verified_at'), |
| 58 | + Forms\Components\TextInput::make('password') |
| 59 | + ->password() |
| 60 | + ->required() |
| 61 | + ->maxLength(255), |
| 62 | + ]); |
| 63 | + } |
| 64 | + |
| 65 | + public static function table(Table $table): Table |
| 66 | + { |
| 67 | + return $table |
| 68 | + ->columns([ |
| 69 | + Tables\Columns\TextColumn::make('name') |
| 70 | + ->searchable(), |
| 71 | + Tables\Columns\TextColumn::make('email') |
| 72 | + ->searchable(), |
| 73 | + Tables\Columns\TextColumn::make('telephone') |
| 74 | + ->searchable(), |
| 75 | + Tables\Columns\TextColumn::make('document_number') |
| 76 | + ->searchable(), |
| 77 | + Tables\Columns\TextColumn::make('avatar_url') |
| 78 | + ->searchable(), |
| 79 | + Tables\Columns\IconColumn::make('is_admin') |
| 80 | + ->boolean(), |
| 81 | + Tables\Columns\TextColumn::make('email_verified_at') |
| 82 | + ->dateTime() |
| 83 | + ->sortable(), |
| 84 | + Tables\Columns\TextColumn::make('created_at') |
| 85 | + ->dateTime() |
| 86 | + ->sortable() |
| 87 | + ->toggleable(isToggledHiddenByDefault: true), |
| 88 | + Tables\Columns\TextColumn::make('updated_at') |
| 89 | + ->dateTime() |
| 90 | + ->sortable() |
| 91 | + ->toggleable(isToggledHiddenByDefault: true), |
| 92 | + ]) |
| 93 | + ->filters([ |
| 94 | + // |
| 95 | + ]) |
| 96 | + ->actions([ |
| 97 | + |
| 98 | + ActionGroup::make([ |
| 99 | + ViewAction::make(), |
| 100 | + EditAction::make(), |
| 101 | + DeleteAction::make(), |
| 102 | + Action::make('Resetar Senha') |
| 103 | + ->requiresConfirmation() |
| 104 | + ->action(function (User $user) { |
| 105 | + $newPassword = Str::random(8); |
| 106 | + // Define a nova senha criptografada |
| 107 | + $user->password = Hash::make($newPassword); |
| 108 | + $user->save(); |
| 109 | + // Envia o e-mail com a nova senha |
| 110 | + Mail::to($user->email)->queue(new PasswordResetMail($newPassword, $user->name)); |
| 111 | + |
| 112 | + Notification::make() |
| 113 | + ->title('Senha Alterada com Sucesso') |
| 114 | + ->body('Um Email foi enviado para o usuário com a nova senha') |
| 115 | + ->success() |
| 116 | + ->send(); |
| 117 | + }) |
| 118 | + ->color('warning') // Defina a cor, como amarelo para chamar atenção |
| 119 | + ->icon('heroicon-o-key'), // Ícone da chave |
| 120 | + ]), |
| 121 | + |
| 122 | + |
| 123 | + ]) |
| 124 | + ->bulkActions([ |
| 125 | + Tables\Actions\BulkActionGroup::make([ |
| 126 | + Tables\Actions\DeleteBulkAction::make(), |
| 127 | + ]), |
| 128 | + ]); |
| 129 | + } |
| 130 | + |
| 131 | + public static function getRelations(): array |
| 132 | + { |
| 133 | + return [ |
| 134 | + UseraddressesRelationManager::class, |
| 135 | + // |
| 136 | + ]; |
| 137 | + } |
| 138 | + |
| 139 | + public static function getPages(): array |
| 140 | + { |
| 141 | + return [ |
| 142 | + 'index' => Pages\ListUsers::route('/'), |
| 143 | + 'create' => Pages\CreateUser::route('/create'), |
| 144 | + 'view' => Pages\ViewUser::route('/{record}'), |
| 145 | + 'edit' => Pages\EditUser::route('/{record}/edit'), |
| 146 | + ]; |
| 147 | + } |
| 148 | +} |
0 commit comments