Skip to content

Commit 1fcf417

Browse files
author
Hatim EL OUFIR
committed
Spatie permissions integration
1 parent eed6921 commit 1fcf417

35 files changed

Lines changed: 619 additions & 297 deletions

app/Http/Livewire/Administration/Users.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
use App\Models\User;
66
use App\Notifications\UserCreatedNotification;
7+
use Filament\Forms\Components\TagsInput;
78
use Filament\Notifications\Notification;
89
use Filament\Tables\Actions\Action;
910
use Filament\Tables\Columns\BadgeColumn;
1011
use Filament\Tables\Columns\BooleanColumn;
12+
use Filament\Tables\Columns\TagsColumn;
1113
use Filament\Tables\Columns\TextColumn;
1214
use Filament\Tables\Concerns\InteractsWithTable;
1315
use Filament\Tables\Contracts\HasTable;
@@ -51,18 +53,17 @@ protected function getTableColumns(): array
5153
->searchable()
5254
->sortable(),
5355

54-
BadgeColumn::make('role')
55-
->label(__('Role'))
56-
->searchable()
57-
->sortable()
58-
->enum(roles_list())
59-
->colors(roles_list_badges()),
60-
6156
BooleanColumn::make('isAccountActivated')
6257
->label(__('Account activated'))
6358
->sortable()
6459
->searchable(),
6560

61+
TagsColumn::make('permissions.name')
62+
->label(__('Permissions'))
63+
->limit(1)
64+
->searchable()
65+
->sortable(),
66+
6667
TextColumn::make('created_at')
6768
->label(__('Created at'))
6869
->sortable()

app/Http/Livewire/Administration/UsersDialog.php

Lines changed: 89 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,23 @@
44

55
use App\Models\User;
66
use 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;
712
use Filament\Forms\Components\Select;
13+
use Filament\Forms\Components\TagsInput;
814
use Filament\Forms\Components\TextInput;
915
use Filament\Forms\Concerns\InteractsWithForms;
1016
use Filament\Forms\Contracts\HasForms;
1117
use Filament\Notifications\Actions\Action;
1218
use Filament\Notifications\Notification;
19+
use Illuminate\Support\HtmlString;
1320
use Livewire\Component;
1421
use Ramsey\Uuid\Uuid;
22+
use Spatie\Permission\Models\Permission;
23+
use Closure;
1524

1625
class 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()

app/Http/Livewire/Analytics.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ private function loadNotAssignedTickets(): void
5555
private function loadTicketsAssignments(): void
5656
{
5757
$query = Ticket::query();
58-
if (has_all_permissions(auth()->user(), 'view-own-tickets') && !has_all_permissions(auth()->user(), 'view-all-tickets')) {
58+
if (auth()->user()->can('View own tickets') && !auth()->user()->can('View all tickets')) {
5959
$query->where(function ($query) {
6060
$query->where('owner_id', auth()->user()->id)
6161
->orWhere('responsible_id', auth()->user()->id);
@@ -78,7 +78,7 @@ private function loadTicketsAssignments(): void
7878
private function loadTicketsByStatuses(): void
7979
{
8080
$query = Ticket::query();
81-
if (has_all_permissions(auth()->user(), 'view-own-tickets') && !has_all_permissions(auth()->user(), 'view-all-tickets')) {
81+
if (auth()->user()->can('View own tickets') && !auth()->user()->can('View all tickets')) {
8282
$query->where(function ($query) {
8383
$query->where('owner_id', auth()->user()->id)
8484
->orWhere('responsible_id', auth()->user()->id);

app/Http/Livewire/Kanban.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function records(): Collection
3838
{
3939
$query = Ticket::query();
4040
$query->withCount('comments');
41-
if (has_all_permissions(auth()->user(), 'view-own-tickets') && !has_all_permissions(auth()->user(), 'view-all-tickets')) {
41+
if (auth()->user()->can('View own tickets') && !auth()->user()->can('View all tickets')) {
4242
$query->where(function ($query) {
4343
$query->where('owner_id', auth()->user()->id)
4444
->orWhere('responsible_id', auth()->user()->id);
@@ -116,7 +116,7 @@ protected function styles(): array
116116
public function onStatusChanged($recordId, $statusId, $fromOrderedIds, $toOrderedIds): void
117117
{
118118
$ticket = Ticket::find($recordId);
119-
if ((has_all_permissions(auth()->user(), 'update-all-tickets') || (has_all_permissions(auth()->user(), 'update-own-tickets') && ($ticket->owner_id === auth()->user() || $ticket->responsible_id === auth()->user()->id))) && has_all_permissions(auth()->user(), 'change-status-tickets')) {
119+
if ((auth()->user()->can('Update all tickets') || (auth()->user()->can('Update own tickets') && ($ticket->owner_id === auth()->user() || $ticket->responsible_id === auth()->user()->id))) && auth()->user()->can('Change status tickets')) {
120120
$before = __(config('system.statuses.' . $ticket->status . '.title')) ?? '-';
121121
$ticket->status = $statusId;
122122
$ticket->save();

app/Http/Livewire/Projects.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function getTableQuery(): Builder|Relation
3838
{
3939
$query = Project::query();
4040
$query->withCount('tickets');
41-
if (has_all_permissions(auth()->user(), 'view-own-projects') && !has_all_permissions(auth()->user(), 'view-all-projects')) {
41+
if (auth()->user()->can('View own projects') && !auth()->user()->can('View all projects')) {
4242
$query->where(function ($query) {
4343
$query->where('owner_id', auth()->user()->id)
4444
->orWhereHas('tickets', function ($query) {

app/Http/Livewire/Tickets.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function render()
4949
{
5050
$query = Ticket::query();
5151
$query->withCount('comments');
52-
if (has_all_permissions(auth()->user(), 'view-own-tickets') && !has_all_permissions(auth()->user(), 'view-all-tickets')) {
52+
if (auth()->user()->can('View own tickets') && !auth()->user()->can('View all tickets')) {
5353
$query->where(function ($query) {
5454
$query->where('owner_id', auth()->user()->id)
5555
->orWhere('responsible_id', auth()->user()->id);
@@ -118,7 +118,7 @@ protected function getFormSchema(): array
118118
->placeholder(__('Project'))
119119
->options(function () {
120120
$query = Project::query();
121-
if (has_all_permissions(auth()->user(), 'view-own-projects') && !has_all_permissions(auth()->user(), 'view-all-projects')) {
121+
if (auth()->user()->can('View own projects') && !auth()->user()->can('View all projects')) {
122122
$query->where('owner_id', auth()->user()->id);
123123
}
124124
return $query->get()->pluck('name', 'id');

app/Http/Livewire/TicketsDialog.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ protected function getFormSchema(): array
5252
->searchable()
5353
->options(function () {
5454
$query = Project::query();
55-
if (has_all_permissions(auth()->user(), 'view-own-projects') && !has_all_permissions(auth()->user(), 'view-all-projects')) {
55+
if (auth()->user()->can('View own projects') && !auth()->user()->can('View all projects')) {
5656
$query->where('owner_id', auth()->user()->id);
5757
}
5858
return $query->get()->pluck('name', 'id');

app/Http/Middleware/CanAccessTicket.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ public function handle(Request $request, Closure $next)
1919
{
2020
$ticket = $request->route('ticket');
2121
if (!(
22-
has_all_permissions(auth()->user(), 'view-all-tickets')
22+
auth()->user()->can('View all tickets')
2323
||
2424
(
25-
has_all_permissions(auth()->user(), 'view-own-tickets')
25+
auth()->user()->can('View own tickets')
2626
&& in_array(auth()->user()->id, [$ticket->owner_id, $ticket->responsible_id])
2727
)
2828
)) {

app/Jobs/CommentCreatedJob.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ public function handle()
3939
$users = User::whereNull('register_token')->get();
4040
foreach ($users as $user) {
4141
if (
42-
(has_all_permissions($user, 'view-all-tickets') && $this->comment->owner_id !== $user->id)
42+
(auth()->user()->can('View all tickets') && $this->comment->owner_id !== $user->id)
4343
||
44-
(has_all_permissions($user, 'view-own-tickets') && ($this->comment->ticket->owner_id === $user->id || $this->comment->ticket->responsible_id === $user->id) && $this->comment->owner_id !== $user->id)
44+
(auth()->user()->can('View own tickets') && ($this->comment->ticket->owner_id === $user->id || $this->comment->ticket->responsible_id === $user->id) && $this->comment->owner_id !== $user->id)
4545
) {
4646
$user->notify(new CommentCreateNotification($this->comment, $user));
4747
}

app/Jobs/TicketCreatedJob.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function handle()
3737
{
3838
$users = User::whereNull('register_token')->get();
3939
foreach ($users as $user) {
40-
if (has_all_permissions($user, 'view-all-tickets') && $this->ticket->owner_id !== $user->id) {
40+
if (auth()->user()->can('View all tickets') && $this->ticket->owner_id !== $user->id) {
4141
$user->notify(new TicketCreatedNotification($this->ticket, $user));
4242
}
4343
}

0 commit comments

Comments
 (0)