Skip to content

Commit 0a956fd

Browse files
committed
Fixed user create in filament
1 parent 09b168c commit 0a956fd

File tree

6 files changed

+74
-8
lines changed

6 files changed

+74
-8
lines changed

app/Console/Commands/Admin/UserCreateCommand.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,15 @@ public function handle(): int
5757
}
5858

5959
$user = null;
60-
DB::transaction(function () use (&$user, $name, $email, $password): void {
60+
DB::transaction(function () use (&$user, $name, $email, $password, $verifyEmail): void {
6161
$user = app(UserService::class)->createUser(
6262
$name,
6363
$email,
6464
$password,
6565
'UTC',
6666
Weekday::Monday,
6767
'EUR',
68+
$verifyEmail
6869
);
6970
});
7071
/** @var Organization|null $organization */
@@ -73,10 +74,6 @@ public function handle(): int
7374
throw new LogicException('User does not have an organization');
7475
}
7576

76-
if ($verifyEmail) {
77-
$user->markEmailAsVerified();
78-
}
79-
8077
$this->info('Created user "'.$name.'" ("'.$email.'")');
8178
$this->line('ID: '.$user->getKey());
8279
$this->line('Name: '.$name);

app/Filament/Resources/UserResource.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Illuminate\Database\Eloquent\Builder;
2626
use Illuminate\Support\Facades\Auth;
2727
use Illuminate\Support\Facades\Hash;
28+
use Korridor\LaravelModelValidationRules\Rules\UniqueEloquent;
2829
use STS\FilamentImpersonate\Tables\Actions\Impersonate;
2930

3031
class UserResource extends Resource
@@ -39,6 +40,8 @@ class UserResource extends Resource
3940

4041
public static function form(Form $form): Form
4142
{
43+
/** @var User|null $record */
44+
$record = $form->getRecord();
4245
return $form
4346
->columns(1)
4447
->schema([
@@ -55,14 +58,25 @@ public static function form(Form $form): Form
5558
Forms\Components\TextInput::make('email')
5659
->label('Email')
5760
->required()
61+
->rules($record?->is_placeholder ? [] : [
62+
UniqueEloquent::make(User::class, 'email')
63+
->ignore($record?->getKey()),
64+
])
65+
->rule([
66+
'email',
67+
])
5868
->maxLength(255),
5969
Forms\Components\Toggle::make('is_placeholder')
6070
->label('Is Placeholder?')
6171
->hiddenOn(['create'])
6272
->disabledOn(['edit']),
6373
Forms\Components\DateTimePicker::make('email_verified_at')
6474
->label('Email Verified At')
75+
->hiddenOn(['create'])
6576
->nullable(),
77+
Forms\Components\Toggle::make('is_email_verified')
78+
->label('Email Verified?')
79+
->visibleOn(['create']),
6680
Forms\Components\Select::make('timezone')
6781
->label('Timezone')
6882
->options(fn (): array => app(TimezoneService::class)->getSelectOptions())
@@ -74,8 +88,16 @@ public static function form(Form $form): Form
7488
->required(),
7589
TextInput::make('password')
7690
->password()
91+
->label('Password')
7792
->dehydrateStateUsing(fn ($state) => Hash::make($state))
7893
->dehydrated(fn ($state) => filled($state))
94+
->hiddenOn(['create'])
95+
->required(fn (string $context): bool => $context === 'create')
96+
->maxLength(255),
97+
TextInput::make('password_create')
98+
->password()
99+
->label('Password')
100+
->visibleOn(['create'])
79101
->required(fn (string $context): bool => $context === 'create')
80102
->maxLength(255),
81103
Forms\Components\Select::make('currency')

app/Filament/Resources/UserResource/Pages/CreateUser.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ protected function handleRecordCreation(array $data): User
2020
$user = $userService->createUser(
2121
$data['name'],
2222
$data['email'],
23-
$data['password'],
23+
$data['password_create'],
2424
$data['timezone'],
2525
Weekday::from($data['week_start']),
2626
$data['currency'],
27+
(bool) $data['is_email_verified']
2728
);
2829

2930
return $user;

app/Models/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
* @property string $id
3535
* @property string $name
3636
* @property string $email
37-
* @property string|null $email_verified_at
37+
* @property Carbon|null $email_verified_at
3838
* @property string|null $password
3939
* @property string|null $two_factor_secret
4040
* @property string $timezone

app/Service/UserService.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,22 @@
1212
use App\Models\ProjectMember;
1313
use App\Models\TimeEntry;
1414
use App\Models\User;
15+
use Illuminate\Support\Carbon;
1516
use Illuminate\Support\Facades\Hash;
1617

1718
class UserService
1819
{
19-
public function createUser(string $name, string $email, string $password, string $timezone, Weekday $weekStart, string $currency): User
20+
public function createUser(string $name, string $email, string $password, string $timezone, Weekday $weekStart, string $currency, bool $verifyEmail = false): User
2021
{
2122
$user = new User;
2223
$user->name = $name;
2324
$user->email = $email;
2425
$user->password = Hash::make($password);
2526
$user->timezone = $timezone;
2627
$user->week_start = $weekStart;
28+
if ($verifyEmail) {
29+
$user->email_verified_at = Carbon::now();
30+
}
2731
$user->save();
2832

2933
$organization = new Organization;

tests/Unit/Filament/Resources/UserResourceTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use App\Models\User;
1212
use App\Service\DeletionService;
1313
use Illuminate\Support\Facades\Config;
14+
use Illuminate\Support\Facades\Hash;
1415
use Livewire\Livewire;
1516
use Mockery\MockInterface;
1617
use PHPUnit\Framework\Attributes\UsesClass;
@@ -67,6 +68,47 @@ public function test_can_see_view_page_of_user(): void
6768
$response->assertSuccessful();
6869
}
6970

71+
public function test_can_see_create_page_of_user(): void
72+
{
73+
// Act
74+
$response = Livewire::test(UserResource\Pages\CreateUser::class);
75+
76+
// Assert
77+
$response->assertSuccessful();
78+
}
79+
80+
public function test_can_create_user(): void
81+
{
82+
// Arrange
83+
$userFake = User::factory()->make();
84+
85+
// Act
86+
$response = Livewire::test(UserResource\Pages\CreateUser::class)
87+
->fillForm([
88+
'name' => $userFake->name,
89+
'email' => $userFake->email,
90+
'password_create' => 'password',
91+
'timezone' => $userFake->timezone,
92+
'week_start' => $userFake->week_start->value,
93+
'currency' => 'EUR',
94+
])
95+
->call('create')
96+
->assertHasNoFormErrors();
97+
98+
// Assert
99+
$response->assertSuccessful();
100+
$user = User::where('email', $userFake->email)->first();
101+
$this->assertNotNull($user);
102+
$this->assertSame($userFake->name, $user->name);
103+
$this->assertSame($userFake->email, $user->email);
104+
$this->assertSame($userFake->timezone, $user->timezone);
105+
$this->assertSame($userFake->week_start->value, $user->week_start->value);
106+
$organization = $user->ownedTeams()->first();
107+
$this->assertNotNull($organization);
108+
$this->assertSame('EUR', $organization->currency);
109+
$this->assertTrue(Hash::check('password', $user->password));
110+
}
111+
70112
public function test_can_delete_a_user(): void
71113
{
72114
// Arrange

0 commit comments

Comments
 (0)