Skip to content

Commit 6c37d6f

Browse files
authored
Feat/userhandling - password confirmation and team resource (#292)
* feat: add password confirmation to user create/edit and add handling for teams * fix: phpmd and cleanup
1 parent 74c83b4 commit 6c37d6f

File tree

24 files changed

+359
-126
lines changed

24 files changed

+359
-126
lines changed

app/Actions/Fortify/UpdateUserProfileInformation.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@
66
use Illuminate\Contracts\Auth\MustVerifyEmail;
77
use Illuminate\Support\Facades\Validator;
88
use Illuminate\Validation\Rule;
9+
use Illuminate\Validation\ValidationException;
910
use Laravel\Fortify\Contracts\UpdatesUserProfileInformation;
1011

1112
class UpdateUserProfileInformation implements UpdatesUserProfileInformation
1213
{
1314
/**
1415
* Validate and update the given user's profile information.
1516
*
16-
* @param array<string, mixed> $input
17+
* @param array<string, mixed> $input
18+
* @SuppressWarnings(PHPMD)
1719
*
18-
* @SuppressWarnings(PHPMD.ElseExpression)
20+
* @throws ValidationException
1921
*/
2022
public function update(User $user, array $input): void
2123
{

app/Filament/Resources/Shield/RoleResource.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Filament\Forms;
1212
use Filament\Forms\Components\Component;
1313
use Filament\Forms\Form;
14+
use Filament\Resources\Pages\PageRegistration;
1415
use Filament\Resources\Resource;
1516
use Filament\Tables;
1617
use Filament\Tables\Table;
@@ -122,8 +123,8 @@ public static function getRelations(): array
122123
}
123124

124125
/**
125-
* @SuppressWarnings(PHPMD.StaticAccess)
126-
* @return array|\Filament\Resources\Pages\PageRegistration[]
126+
* @return array|PageRegistration[]
127+
* @SuppressWarnings(PHPMD)
127128
*/
128129
public static function getPages(): array
129130
{
@@ -255,8 +256,8 @@ public static function getResourcePermissionOptions(array $entity): array
255256
* @param string $operation
256257
* @param array $permissions
257258
* @param Model|null $record
259+
* @SuppressWarnings(PHPMD)
258260
* @return void
259-
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
260261
*/
261262
public static function setPermissionStateForRecordPermissions(Component $component, string $operation, array $permissions, ?Model $record): void
262263
{
@@ -391,8 +392,8 @@ public static function getTabFormComponentForSimpleResourcePermissionsView(): Co
391392
* @param string $name
392393
* @param array $options
393394
* @param bool $searchable
395+
* @SuppressWarnings(PHPMD)
394396
* @return Component
395-
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
396397
*/
397398
public static function getCheckboxListFormComponent(string $name, array $options, bool $searchable = true): Component
398399
{

app/Filament/Resources/Shield/RoleResource/Pages/CreateRole.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class CreateRole extends CreateRecord
1616

1717
/**
1818
* @param array $data
19+
* @SuppressWarnings(PHPMD)
1920
* @return array|mixed[]
20-
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
2121
*/
2222
protected function mutateFormDataBeforeCreate(array $data): array
2323
{

app/Filament/Resources/Shield/RoleResource/Pages/EditRole.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ protected function getActions(): array
2424

2525
/**
2626
* @param array $data
27+
* @SuppressWarnings(PHPMD)
2728
* @return array|mixed[]
28-
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
2929
*/
3030
protected function mutateFormDataBeforeSave(array $data): array
3131
{
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace App\Filament\Resources;
4+
5+
use App\Filament\Resources\TeamResource\Pages;
6+
use App\Filament\Resources\TeamResource\RelationManagers;
7+
use App\Models\Team;
8+
use App\Models\User;
9+
use Filament\Actions\EditAction;
10+
use Filament\Forms;
11+
use Filament\Forms\Form;
12+
use Filament\Resources\Resource;
13+
use Filament\Tables;
14+
use Filament\Tables\Table;
15+
use Illuminate\Database\Eloquent\Builder;
16+
use Illuminate\Database\Eloquent\SoftDeletingScope;
17+
use Livewire\Component;
18+
19+
class TeamResource extends Resource
20+
{
21+
protected static ?string $model = Team::class;
22+
23+
protected static ?string $navigationLabel = 'Teams';
24+
protected static ?string $navigationIcon = 'heroicon-o-users';
25+
protected static ?int $navigationSort = 3;
26+
27+
protected static ?string $slug = 'teams';
28+
protected static ?string $navigationGroup = 'Management';
29+
30+
31+
public static function form(Form $form): Form
32+
{
33+
return $form
34+
->schema([
35+
Forms\Components\TextInput::make('name')
36+
->required()
37+
->maxLength(255),
38+
// Benutzer, der das Team erstellt, als "Besitzer" des Teams setzen
39+
Forms\Components\TextInput::make('personal_team')
40+
->default(fn () => false)
41+
->hidden(),
42+
Forms\Components\Select::make('users')
43+
->label('Mitglieder')
44+
->multiple()
45+
->relationship('users', 'name')
46+
->preload(),
47+
]);
48+
}
49+
50+
public static function table(Table $table): Table
51+
{
52+
return $table
53+
->columns([
54+
Tables\Columns\TextColumn::make('name')
55+
->searchable()
56+
->sortable(),
57+
Tables\Columns\TextColumn::make('users.name')
58+
->label('Mitglieder')
59+
->limit(3), // Zeige maximal 3 User-Namen
60+
Tables\Columns\TextColumn::make('created_at')
61+
->label('Created')
62+
->sortable()
63+
->dateTime(),
64+
Tables\Columns\TextColumn::make('updated_at')
65+
->label('Updated')
66+
->sortable()
67+
->dateTime(),
68+
])
69+
->filters([
70+
])
71+
->actions([
72+
Tables\Actions\EditAction::make(),
73+
Tables\Actions\Action::make('delete')
74+
->action(function (Team $record) {
75+
$record->delete();
76+
})
77+
->requiresConfirmation(),
78+
])
79+
->bulkActions([
80+
Tables\Actions\BulkAction::make('delete')
81+
->action(function (array $records) {
82+
Team::destroy($records);
83+
})
84+
->requiresConfirmation(),
85+
]);
86+
}
87+
88+
public static function getRelations(): array
89+
{
90+
return [
91+
RelationManagers\UsersRelationManager::class,
92+
];
93+
}
94+
95+
public static function getPages(): array
96+
{
97+
return [
98+
'index' => Pages\ListTeams::route('/'),
99+
'create' => Pages\CreateTeam::route('/create'),
100+
'edit' => Pages\EditTeam::route('/{record}/edit'),
101+
];
102+
}
103+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace App\Filament\Resources\TeamResource\Pages;
4+
5+
use App\Filament\Resources\TeamResource;
6+
use Filament\Actions;
7+
use Filament\Resources\Pages\CreateRecord;
8+
9+
class CreateTeam extends CreateRecord
10+
{
11+
protected static string $resource = TeamResource::class;
12+
13+
14+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Filament\Resources\TeamResource\Pages;
4+
5+
use App\Filament\Resources\TeamResource;
6+
use Filament\Actions;
7+
use Filament\Resources\Pages\EditRecord;
8+
use Livewire\Attributes\On;
9+
use function Laravel\Prompts\table;
10+
11+
class EditTeam extends EditRecord
12+
{
13+
protected static string $resource = TeamResource::class;
14+
15+
protected function getHeaderActions(): array
16+
{
17+
return [
18+
Actions\DeleteAction::make(),
19+
];
20+
}
21+
22+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\Filament\Resources\TeamResource\Pages;
4+
5+
use App\Filament\Resources\TeamResource;
6+
use Filament\Actions;
7+
use Filament\Resources\Pages\ListRecords;
8+
9+
class ListTeams extends ListRecords
10+
{
11+
protected static string $resource = TeamResource::class;
12+
13+
protected function getHeaderActions(): array
14+
{
15+
return [
16+
Actions\CreateAction::make(),
17+
];
18+
}
19+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace App\Filament\Resources\TeamResource\RelationManagers;
4+
5+
use Filament\Forms;
6+
use Filament\Tables;
7+
use Filament\Resources\RelationManagers\RelationManager;
8+
use Livewire\Attributes\On;
9+
use Livewire\Component;
10+
11+
class UsersRelationManager extends RelationManager
12+
{
13+
14+
protected static string $relationship = 'users';
15+
16+
17+
18+
public function form(Forms\Form $form): Forms\Form
19+
{
20+
return $form
21+
->schema([
22+
Forms\Components\Select::make('user_id')
23+
->relationship('users', 'name')
24+
->required(),
25+
]);
26+
}
27+
28+
29+
public function table(Tables\Table $table): Tables\Table
30+
{
31+
return $table
32+
->columns([
33+
Tables\Columns\TextColumn::make('name')
34+
->label('Name'),
35+
Tables\Columns\TextColumn::make('username')
36+
->label('Username'),
37+
Tables\Columns\TextColumn::make('email')
38+
->label('Email'),
39+
])
40+
->filters([])
41+
->headerActions([
42+
])
43+
->actions([
44+
Tables\Actions\DeleteAction::make(),
45+
])
46+
->bulkActions([]);
47+
}
48+
49+
50+
}
51+

app/Filament/Resources/UserResource.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
use App\Models\User;
77
use Filament\Forms;
88
use Filament\Forms\Form;
9+
use Filament\Forms\Get;
10+
use Filament\Resources\Pages\PageRegistration;
911
use Filament\Resources\Resource;
1012
use Filament\Tables;
1113
use Filament\Tables\Table;
14+
use Illuminate\Support\Facades\Hash;
1215

1316
class UserResource extends Resource
1417
{
@@ -27,22 +30,20 @@ public static function form(Form $form): Form
2730
->email()
2831
->required()
2932
->maxLength(255),
30-
Forms\Components\DateTimePicker::make('email_verified_at'),
3133
Forms\Components\TextInput::make('password')
34+
->password()
35+
->revealable(filament()->arePasswordsRevealable())
36+
->required(fn($operation) => $operation === 'create')
37+
->autocomplete('new-password')
38+
->dehydrated(fn ($state): bool => filled($state))
39+
->dehydrateStateUsing(fn ($state): string => Hash::make($state))
40+
->live(debounce: 500)
41+
->same('passwordConfirmation'),
42+
Forms\Components\TextInput::make('passwordConfirmation')
3243
->password()
3344
->required()
34-
->maxLength(255),
35-
Forms\Components\Textarea::make('two_factor_secret')
36-
->columnSpanFull(),
37-
Forms\Components\Textarea::make('two_factor_recovery_codes')
38-
->columnSpanFull(),
39-
Forms\Components\DateTimePicker::make('two_factor_confirmed_at'),
40-
Forms\Components\TextInput::make('current_team_id')
41-
->numeric()
42-
->default(null),
43-
Forms\Components\TextInput::make('profile_photo_path')
44-
->maxLength(2048)
45-
->default(null),
45+
->visible(fn (Get $get): bool => filled($get('password')))
46+
->dehydrated(false),
4647
Forms\Components\TextInput::make('username')
4748
->required()
4849
->maxLength(255),
@@ -95,8 +96,8 @@ public static function table(Table $table): Table
9596
}
9697

9798
/**
98-
* @return array|\Filament\Resources\Pages\PageRegistration[]
99-
* @SuppressWarnings(PHPMD.StaticAccess)
99+
* @SuppressWarnings(PHPMD)
100+
* @return array|PageRegistration[]
100101
*/
101102
public static function getPages(): array
102103
{

0 commit comments

Comments
 (0)