|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Livewire\User\Invitation; |
| 4 | + |
| 5 | +use App\Models\User; |
| 6 | +use App\Notifications\User\WelcomeSystemUserNotification; |
| 7 | +use Carbon\Carbon; |
| 8 | +use Filament\Actions\Action; |
| 9 | +use Filament\Actions\Concerns\InteractsWithActions; |
| 10 | +use Filament\Exceptions\NoDefaultPanelSetException; |
| 11 | +use Filament\Forms\Concerns\InteractsWithForms; |
| 12 | +use Filament\Forms; |
| 13 | +use Filament\Notifications\Notification; |
| 14 | +use Filament\Pages\Concerns\InteractsWithFormActions; |
| 15 | +use Filament\Pages\SimplePage; |
| 16 | +use Filament\Support\Colors\Color; |
| 17 | +use Illuminate\Http\Request; |
| 18 | +use Phpsa\FilamentPasswordReveal\Password; |
| 19 | + |
| 20 | +class CreateSystemUserForm extends SimplePage |
| 21 | +{ |
| 22 | + use InteractsWithActions; |
| 23 | + use InteractsWithFormActions; |
| 24 | + use InteractsWithForms; |
| 25 | + |
| 26 | + |
| 27 | + protected ?string $subheading = 'System User Invitation - Verify and create account.'; |
| 28 | + |
| 29 | + protected static ?string $title = 'Recruit System Invitation'; |
| 30 | + |
| 31 | + protected ?string $heading = ''; |
| 32 | + |
| 33 | + public ?array $data = []; |
| 34 | + |
| 35 | + public ?User $user; |
| 36 | + |
| 37 | + public static string $view = "livewire.user.invitation.create-system-user"; |
| 38 | + |
| 39 | + protected static string $layout = 'components.layouts.simple'; |
| 40 | + |
| 41 | + public function mount(Request $request, ?string $id): void |
| 42 | + { |
| 43 | + if (! $request->hasValidSignature()) { |
| 44 | + abort(403, 'Invalid Signature'); |
| 45 | + } |
| 46 | + $this->user = User::whereInvitationId($id)->first(); |
| 47 | + if ($this->user === null || $this->user->joined_at) { |
| 48 | + abort(410, 'Link has Expired'); |
| 49 | + } |
| 50 | + |
| 51 | + $this->data = [...$this->user->toArray()]; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @throws NoDefaultPanelSetException |
| 56 | + */ |
| 57 | + public function create() |
| 58 | + { |
| 59 | + $this->form->getState(); |
| 60 | + $this->user->forceFill([ |
| 61 | + 'password' => \Hash::make($this->data['password']), |
| 62 | + 'email_verified_at' => Carbon::now(), |
| 63 | + 'joined_at' => Carbon::now(), |
| 64 | + ])->save(); |
| 65 | + |
| 66 | + $this->user->notify(new WelcomeSystemUserNotification($this->user)); |
| 67 | + |
| 68 | + Notification::make('create_account_success') |
| 69 | + ->success() |
| 70 | + ->duration(10000) |
| 71 | + ->title('Your Account is Ready') |
| 72 | + ->body('You can now access the recruit system, by using the credential you\'ve provided.') |
| 73 | + ->send(); |
| 74 | + |
| 75 | + $this->redirect(filament()->getDefaultPanel()->getLoginUrl()); |
| 76 | + |
| 77 | + } |
| 78 | + |
| 79 | + public function form(Forms\Form $form): Forms\Form |
| 80 | + { |
| 81 | + return $form |
| 82 | + ->schema([ |
| 83 | + Forms\Components\TextInput::make('name') |
| 84 | + ->label('Name') |
| 85 | + ->disabled(), |
| 86 | + Forms\Components\TextInput::make('email') |
| 87 | + ->email() |
| 88 | + ->label('Email') |
| 89 | + ->disabled(), |
| 90 | + Password::make('password') |
| 91 | + ->minLength(8) |
| 92 | + ->confirmed() |
| 93 | + ->label('Password'), |
| 94 | + Password::make('password_confirmation') |
| 95 | + ->minLength(8) |
| 96 | + ->label('Confirm Password'), |
| 97 | + ]) |
| 98 | + ->statePath('data'); |
| 99 | + } |
| 100 | + |
| 101 | + protected function getFormActions(): array |
| 102 | + { |
| 103 | + return [ |
| 104 | + $this->getAuthenticateFormAction(), |
| 105 | + ]; |
| 106 | + } |
| 107 | + |
| 108 | + protected function getAuthenticateFormAction(): Action |
| 109 | + { |
| 110 | + return Action::make('authenticate') |
| 111 | + ->label('Verify & Create Account') |
| 112 | + ->color(Color::Gray) |
| 113 | + ->submit('create'); |
| 114 | + } |
| 115 | + |
| 116 | + protected function hasFullWidthFormActions(): bool |
| 117 | + { |
| 118 | + return true; |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | +} |
0 commit comments