Skip to content

Commit b59aed7

Browse files
authored
Merge pull request #119 from devaslanphp/dev
Using roles instead of permissions
2 parents 3455aa5 + b208238 commit b59aed7

13 files changed

Lines changed: 730 additions & 273 deletions

File tree

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
3+
namespace App\Http\Livewire\Administration;
4+
5+
use Filament\Notifications\Notification;
6+
use Filament\Tables\Actions\Action;
7+
use Filament\Tables\Columns\TextColumn;
8+
use Filament\Tables\Concerns\InteractsWithTable;
9+
use Filament\Tables\Contracts\HasTable;
10+
use Filament\Tables\Filters\SelectFilter;
11+
use Illuminate\Database\Eloquent\Builder;
12+
use Illuminate\Database\Eloquent\Relations\Relation;
13+
use Livewire\Component;
14+
use Spatie\Permission\Models\Role;
15+
16+
class Roles extends Component implements HasTable
17+
{
18+
use InteractsWithTable;
19+
20+
public $selectedRole;
21+
22+
protected $listeners = ['roleSaved', 'roleDeleted'];
23+
24+
public function render()
25+
{
26+
return view('livewire.administration.roles');
27+
}
28+
29+
/**
30+
* Table query definition
31+
*
32+
* @return Builder|Relation
33+
*/
34+
protected function getTableQuery(): Builder|Relation
35+
{
36+
$query = Role::query();
37+
$query->orderBy('created_at', 'desc');
38+
return $query;
39+
}
40+
41+
/**
42+
* Table definition
43+
*
44+
* @return array
45+
*/
46+
protected function getTableColumns(): array
47+
{
48+
return [
49+
TextColumn::make('name')
50+
->label(__('Role name'))
51+
->searchable()
52+
->sortable(),
53+
54+
TextColumn::make('created_at')
55+
->label(__('Created at'))
56+
->sortable()
57+
->searchable()
58+
->dateTime(),
59+
];
60+
}
61+
62+
/**
63+
* Table actions definition
64+
*
65+
* @return array
66+
*/
67+
protected function getTableActions(): array
68+
{
69+
return [
70+
Action::make('edit')
71+
->icon('heroicon-o-pencil')
72+
->link()
73+
->label(__('Edit role'))
74+
->visible(fn () => auth()->user()->can('Update user roles'))
75+
->action(fn(Role $record) => $this->updateRole($record->id))
76+
];
77+
}
78+
79+
/**
80+
* Table default sort column definition
81+
*
82+
* @return string|null
83+
*/
84+
protected function getDefaultTableSortColumn(): ?string
85+
{
86+
return 'created_at';
87+
}
88+
89+
/**
90+
* Table default sort direction definition
91+
*
92+
* @return string|null
93+
*/
94+
protected function getDefaultTableSortDirection(): ?string
95+
{
96+
return 'desc';
97+
}
98+
99+
/**
100+
* Show update role dialog
101+
*
102+
* @param $id
103+
* @return void
104+
*/
105+
public function updateRole($id)
106+
{
107+
$this->selectedRole = Role::find($id);
108+
$this->dispatchBrowserEvent('toggleRoleModal');
109+
}
110+
111+
/**
112+
* Show create role dialog
113+
*
114+
* @return void
115+
*/
116+
public function createRole()
117+
{
118+
$this->selectedRole = new Role();
119+
$this->dispatchBrowserEvent('toggleRoleModal');
120+
}
121+
122+
/**
123+
* Cancel and close role create / update dialog
124+
*
125+
* @return void
126+
*/
127+
public function cancelRole()
128+
{
129+
$this->selectedRole = null;
130+
$this->dispatchBrowserEvent('toggleRoleModal');
131+
}
132+
133+
/**
134+
* Event launched after a role is created / updated
135+
*
136+
* @return void
137+
*/
138+
public function roleSaved()
139+
{
140+
$this->cancelRole();
141+
}
142+
143+
/**
144+
* Event launched after a role is deleted
145+
*
146+
* @return void
147+
*/
148+
public function roleDeleted()
149+
{
150+
$this->roleSaved();
151+
}
152+
}
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<?php
2+
3+
namespace App\Http\Livewire\Administration;
4+
5+
use App\Core\CrudDialogHelper;
6+
use Filament\Forms\Components\CheckboxList;
7+
use Filament\Forms\Components\TextInput;
8+
use Filament\Forms\Concerns\InteractsWithForms;
9+
use Filament\Forms\Contracts\HasForms;
10+
use Filament\Notifications\Notification;
11+
use Illuminate\Support\HtmlString;
12+
use Livewire\Component;
13+
use Spatie\Permission\Models\Permission;
14+
use Spatie\Permission\Models\Role;
15+
16+
class RolesDialog extends Component implements HasForms
17+
{
18+
use InteractsWithForms;
19+
use CrudDialogHelper;
20+
21+
public Role $role;
22+
23+
protected $listeners = ['doDeleteRole', 'cancelDeleteRole'];
24+
25+
public array $permissions;
26+
27+
public function mount(): void
28+
{
29+
$this->form->fill([
30+
'name' => $this->role->name,
31+
'permissions' => $this->role->permissions->pluck('id')->toArray(),
32+
]);
33+
}
34+
35+
36+
public function render()
37+
{
38+
return view('livewire.administration.roles-dialog');
39+
}
40+
41+
/**
42+
* Form schema definition
43+
*
44+
* @return array
45+
*/
46+
protected function getFormSchema(): array
47+
{
48+
return [
49+
TextInput::make('name')
50+
->label(__('Role name'))
51+
->maxLength(255)
52+
->required(),
53+
54+
CheckboxList::make('permissions')
55+
->label(__('Permissions'))
56+
->hint(new HtmlString('
57+
<div class="w-full flex items-center gap-2">
58+
<button type="button"
59+
class="text-xs text-primary-500 hover:text-primary-600 hover:underline"
60+
wire:click="assignAllPermissions">
61+
' . __('Assign all permissions') . '
62+
</button>
63+
<span class="text-xs text-gray-300">|</span>
64+
<button type="button"
65+
class="text-xs text-primary-500 hover:text-primary-600 hover:underline"
66+
wire:click="removeAllPermissions">
67+
' . __('Remove all permissions') . '
68+
</button>
69+
</div>
70+
'))
71+
->columns(3)
72+
->options(Permission::orderBy('name')->get()->pluck('name', 'id')->toArray())
73+
];
74+
}
75+
76+
/**
77+
* Assign all permissions
78+
*
79+
* @return void
80+
*/
81+
public function assignAllPermissions(): void
82+
{
83+
$this->permissions = Permission::all()->pluck('id')->toArray();
84+
}
85+
86+
/**
87+
* Remove all assigned permissions
88+
*
89+
* @return void
90+
*/
91+
public function removeAllPermissions(): void
92+
{
93+
$this->permissions = [];
94+
}
95+
96+
/**
97+
* Create / Update the role
98+
*
99+
* @return void
100+
*/
101+
public function save(): void
102+
{
103+
$data = $this->form->getState();
104+
if (!$this->role?->id) {
105+
$role = Role::create(['name' => $data['name']]);
106+
$role->syncPermissions($this->permissions);
107+
Notification::make()
108+
->success()
109+
->title(__('Role created'))
110+
->body(__('The user role has been created'))
111+
->send();
112+
} else {
113+
$this->role->name = $data['name'];
114+
$this->role->save();
115+
$this->role->syncPermissions($this->permissions);
116+
Notification::make()
117+
->success()
118+
->title(__('Role updated'))
119+
->body(__('The role\'s details has been updated'))
120+
->send();
121+
}
122+
$this->emit('roleSaved');
123+
}
124+
125+
/**
126+
* Delete an existing role
127+
*
128+
* @return void
129+
*/
130+
public function doDeleteRole(): void
131+
{
132+
$this->role->delete();
133+
$this->deleteConfirmationOpened = false;
134+
$this->emit('roleDeleted');
135+
Notification::make()
136+
->success()
137+
->title(__('Role deleted'))
138+
->body(__('The role has been deleted'))
139+
->send();
140+
}
141+
142+
/**
143+
* Cancel the deletion of a role
144+
*
145+
* @return void
146+
*/
147+
public function cancelDeleteRole(): void
148+
{
149+
$this->deleteConfirmationOpened = false;
150+
}
151+
152+
/**
153+
* Show the delete role confirmation dialog
154+
*
155+
* @return void
156+
* @throws \Exception
157+
*/
158+
public function deleteRole(): void
159+
{
160+
$this->deleteConfirmation(
161+
__('Role deletion'),
162+
__('Are you sure you want to delete this role?'),
163+
'doDeleteRole',
164+
'cancelDeleteRole'
165+
);
166+
}
167+
}

app/Http/Livewire/Administration/Users.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ protected function getTableColumns(): array
7272
BooleanColumn::make('isAccountActivated')
7373
->label(__('Account activated')),
7474

75-
TagsColumn::make('permissions.name')
76-
->label(__('Permissions'))
75+
TagsColumn::make('roles.name')
76+
->label(__('User roles'))
7777
->limit(1)
7878
->visible(fn () => auth()->user()->can('Assign permissions'))
7979
->searchable()

0 commit comments

Comments
 (0)