44
55use App \Models \User ;
66use App \Notifications \UserCreatedNotification ;
7+ use Filament \Forms \Components \Checkbox ;
8+ use Filament \Forms \Components \CheckboxList ;
9+ use Filament \Forms \Components \Grid ;
10+ use Filament \Forms \Components \MultiSelect ;
11+ use Filament \Forms \Components \Placeholder ;
712use Filament \Forms \Components \Select ;
13+ use Filament \Forms \Components \TagsInput ;
814use Filament \Forms \Components \TextInput ;
915use Filament \Forms \Concerns \InteractsWithForms ;
1016use Filament \Forms \Contracts \HasForms ;
1117use Filament \Notifications \Actions \Action ;
1218use Filament \Notifications \Notification ;
19+ use Illuminate \Support \HtmlString ;
1320use Livewire \Component ;
1421use Ramsey \Uuid \Uuid ;
22+ use Spatie \Permission \Models \Permission ;
23+ use Closure ;
1524
1625class UsersDialog extends Component implements HasForms
1726{
@@ -22,11 +31,14 @@ class UsersDialog extends Component implements HasForms
2231
2332 protected $ listeners = ['doDeleteUser ' , 'cancelDeleteUser ' ];
2433
25- public function mount (): void {
34+ public array $ permissions ;
35+
36+ public function mount (): void
37+ {
2638 $ this ->form ->fill ([
2739 'name ' => $ this ->user ->name ,
2840 'email ' => $ this ->user ->email ,
29- 'role ' => $ this ->user ->role ,
41+ 'permissions ' => $ this ->user ->permissions -> pluck ( ' id ' )-> toArray () ,
3042 ]);
3143 }
3244
@@ -44,40 +56,91 @@ public function render()
4456 protected function getFormSchema (): array
4557 {
4658 return [
47- TextInput::make ('name ' )
48- ->label (__ ('Full name ' ))
49- ->maxLength (255 )
50- ->required (),
51-
52- TextInput::make ('email ' )
53- ->label (__ ('Email address ' ))
54- ->email ()
55- ->unique (table: User::class, column: 'email ' , ignorable: fn () => $ this ->user ->id ? $ this ->user : null )
56- ->required (),
57-
58- Select::make ('role ' )
59- ->label (__ ('Role ' ))
59+ Grid::make ()
60+ ->schema ([
61+ TextInput::make ('name ' )
62+ ->label (__ ('Full name ' ))
63+ ->maxLength (255 )
64+ ->required (),
65+
66+ TextInput::make ('email ' )
67+ ->label (__ ('Email address ' ))
68+ ->email ()
69+ ->unique (table: User::class, column: 'email ' , ignorable: fn () => $ this ->user ->id ? $ this ->user : null )
70+ ->required (),
71+ ]),
72+
73+ Grid::make ()
74+ ->extraAttributes ([
75+ 'class ' => 'border-t border-gray-200 pt-5 mt-5 '
76+ ])
77+ ->schema ([
78+ Select::make ('same_permissions_as ' )
79+ ->label (__ ('Use same permissions of ' ))
80+ ->helperText (__ ("Update the permissions of this user based on another user's permissions " ))
81+ ->searchable ()
82+ ->options (User::all ()->pluck ('name ' , 'id ' )->toArray ())
83+ ->reactive ()
84+ ->afterStateUpdated (function (Closure $ set , Closure $ get ) {
85+ if ($ get ('same_permissions_as ' )) {
86+ $ user = User::find ($ get ('same_permissions_as ' ));
87+ $ set ('permissions ' , $ user ->permissions ->pluck ('id ' )->toArray ());
88+ }
89+ })
90+ ]),
91+
92+ Placeholder::make ('permissions_buttons ' )
93+ ->content (new HtmlString ('
94+ <div class="w-full flex items-center gap-2">
95+ <button type="button" class="text-xs text-primary-500 hover:text-primary-600 hover:underline" wire:click="assignAllPermissions"> ' . __ ('Assign all permissions ' ) . '</button>
96+ <button type="button" class="text-xs text-primary-500 hover:text-primary-600 hover:underline" wire:click="removeAllPermissions"> ' . __ ('Remove all permissions ' ) . '</button>
97+ </div>
98+ ' )),
99+
100+ CheckboxList::make ('permissions ' )
101+ ->label (__ ('Permissions ' ))
60102 ->required ()
61- ->searchable ( )
62- ->options (roles_list ())
103+ ->columns ( 3 )
104+ ->options (Permission:: orderBy ( ' name ' )-> get ()-> pluck ( ' name ' , ' id ' )-> toArray ())
63105 ];
64106 }
65107
108+ /**
109+ * Assign all permissions
110+ *
111+ * @return void
112+ */
113+ public function assignAllPermissions (): void
114+ {
115+ $ this ->permissions = Permission::all ()->pluck ('id ' )->toArray ();
116+ }
117+
118+ /**
119+ * Remove all assigned permissions
120+ *
121+ * @return void
122+ */
123+ public function removeAllPermissions (): void
124+ {
125+ $ this ->permissions = [];
126+ }
127+
66128 /**
67129 * Create / Update the user
68130 *
69131 * @return void
70132 */
71- public function save (): void {
133+ public function save (): void
134+ {
72135 $ data = $ this ->form ->getState ();
73136 if (!$ this ->user ?->id) {
74137 $ user = User::create ([
75138 'name ' => $ data ['name ' ],
76139 'email ' => $ data ['email ' ],
77- 'role ' => $ data ['role ' ],
78140 'password ' => bcrypt (uniqid ()),
79141 'register_token ' => Uuid::uuid4 ()->toString ()
80142 ]);
143+ $ user ->syncPermissions ($ data ['permissions ' ]);
81144 $ user ->notify (new UserCreatedNotification ($ user ));
82145 Notification::make ()
83146 ->success ()
@@ -87,8 +150,8 @@ public function save(): void {
87150 } else {
88151 $ this ->user ->name = $ data ['name ' ];
89152 $ this ->user ->email = $ data ['email ' ];
90- $ this ->user ->role = $ data ['role ' ];
91153 $ this ->user ->save ();
154+ $ this ->user ->syncPermissions ($ data ['permissions ' ]);
92155 Notification::make ()
93156 ->success ()
94157 ->title (__ ('User updated ' ))
@@ -103,7 +166,8 @@ public function save(): void {
103166 *
104167 * @return void
105168 */
106- public function doDeleteUser (): void {
169+ public function doDeleteUser (): void
170+ {
107171 $ this ->user ->delete ();
108172 $ this ->deleteConfirmationOpened = false ;
109173 $ this ->emit ('userDeleted ' );
@@ -119,7 +183,8 @@ public function doDeleteUser(): void {
119183 *
120184 * @return void
121185 */
122- public function cancelDeleteUser (): void {
186+ public function cancelDeleteUser (): void
187+ {
123188 $ this ->deleteConfirmationOpened = false ;
124189 }
125190
@@ -129,7 +194,8 @@ public function cancelDeleteUser(): void {
129194 * @return void
130195 * @throws \Exception
131196 */
132- public function deleteUser (): void {
197+ public function deleteUser (): void
198+ {
133199 $ this ->deleteConfirmationOpened = true ;
134200 Notification::make ()
135201 ->warning ()
0 commit comments