Skip to content

Commit db3adeb

Browse files
committed
added user invitation feature logic
Signed-off-by: Marjose Darang <[email protected]>
1 parent 3270498 commit db3adeb

File tree

4 files changed

+199
-0
lines changed

4 files changed

+199
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace App\Notifications\User;
4+
5+
use App\Models\User;
6+
use Filament\Exceptions\NoDefaultPanelSetException;
7+
use Illuminate\Bus\Queueable;
8+
use Illuminate\Contracts\Queue\ShouldQueue;
9+
use Illuminate\Notifications\Messages\MailMessage;
10+
use Illuminate\Notifications\Notification;
11+
12+
class WelcomeSystemUserNotification extends Notification
13+
{
14+
use Queueable;
15+
16+
protected ?User $user;
17+
protected ?string $login_link;
18+
/**
19+
* Create a new notification instance.
20+
* @throws NoDefaultPanelSetException
21+
*/
22+
public function __construct(?User $user)
23+
{
24+
$this->user = $user;
25+
$this->login_link = filament()->getDefaultPanel()->getLoginUrl();
26+
}
27+
28+
/**
29+
* Get the notification's delivery channels.
30+
*
31+
* @return array<int, string>
32+
*/
33+
public function via(object $notifiable): array
34+
{
35+
return ['mail'];
36+
}
37+
38+
/**
39+
* Get the mail representation of the notification.
40+
*/
41+
public function toMail(object $notifiable): MailMessage
42+
{
43+
return (new MailMessage)
44+
->subject('Welcome to Our System - Verification & Registration Completed')
45+
->greeting("Dear {$this->user->name},")
46+
->line('We are pleased to inform you that your registration has been successfully completed, and your account is now fully active in our system.')
47+
->line('You can now log in with your registered email address and the password you created during the registration process. If you ever forget your password, you can use the "Forgot Password" feature on the login page to reset it.')
48+
->action('Login Now!', $this->login_link)
49+
->line('If you have any questions or need assistance as you explore the system, please feel free to contact our support team.')
50+
->line('Thank you for choosing our system, and we look forward to seeing the positive impact of your contributions.')
51+
;
52+
}
53+
54+
/**
55+
* Get the array representation of the notification.
56+
*
57+
* @return array<string, mixed>
58+
*/
59+
public function toArray(object $notifiable): array
60+
{
61+
return [
62+
//
63+
];
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<x-filament-panels::page.simple>
2+
<x-filament-panels::form wire:submit="create">
3+
{{ $this->form }}
4+
5+
<x-filament-panels::form.actions
6+
:actions="$this->getCachedFormActions()"
7+
:full-width="$this->hasFullWidthFormActions()"
8+
/>
9+
</x-filament-panels::form>
10+
11+
</x-filament-panels::page.simple>

routes/web.php

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
// Candidate Portal Invitation
2121
Route::get('portal/invite/{id}', \App\Livewire\Portal\Invitation\CreateCandidateUser::class)->name('portal.invite');
22+
Route::get('/invite/{id}', \App\Livewire\User\Invitation\CreateSystemUserForm::class)->name('system-user.invite');
2223

2324
//Route::get('/invite', function () {
2425

0 commit comments

Comments
 (0)