-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from devaslanphp/dev
Using roles instead of permissions
- Loading branch information
Showing
13 changed files
with
730 additions
and
273 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
<?php | ||
|
||
namespace App\Http\Livewire\Administration; | ||
|
||
use Filament\Notifications\Notification; | ||
use Filament\Tables\Actions\Action; | ||
use Filament\Tables\Columns\TextColumn; | ||
use Filament\Tables\Concerns\InteractsWithTable; | ||
use Filament\Tables\Contracts\HasTable; | ||
use Filament\Tables\Filters\SelectFilter; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Relations\Relation; | ||
use Livewire\Component; | ||
use Spatie\Permission\Models\Role; | ||
|
||
class Roles extends Component implements HasTable | ||
{ | ||
use InteractsWithTable; | ||
|
||
public $selectedRole; | ||
|
||
protected $listeners = ['roleSaved', 'roleDeleted']; | ||
|
||
public function render() | ||
{ | ||
return view('livewire.administration.roles'); | ||
} | ||
|
||
/** | ||
* Table query definition | ||
* | ||
* @return Builder|Relation | ||
*/ | ||
protected function getTableQuery(): Builder|Relation | ||
{ | ||
$query = Role::query(); | ||
$query->orderBy('created_at', 'desc'); | ||
return $query; | ||
} | ||
|
||
/** | ||
* Table definition | ||
* | ||
* @return array | ||
*/ | ||
protected function getTableColumns(): array | ||
{ | ||
return [ | ||
TextColumn::make('name') | ||
->label(__('Role name')) | ||
->searchable() | ||
->sortable(), | ||
|
||
TextColumn::make('created_at') | ||
->label(__('Created at')) | ||
->sortable() | ||
->searchable() | ||
->dateTime(), | ||
]; | ||
} | ||
|
||
/** | ||
* Table actions definition | ||
* | ||
* @return array | ||
*/ | ||
protected function getTableActions(): array | ||
{ | ||
return [ | ||
Action::make('edit') | ||
->icon('heroicon-o-pencil') | ||
->link() | ||
->label(__('Edit role')) | ||
->visible(fn () => auth()->user()->can('Update user roles')) | ||
->action(fn(Role $record) => $this->updateRole($record->id)) | ||
]; | ||
} | ||
|
||
/** | ||
* Table default sort column definition | ||
* | ||
* @return string|null | ||
*/ | ||
protected function getDefaultTableSortColumn(): ?string | ||
{ | ||
return 'created_at'; | ||
} | ||
|
||
/** | ||
* Table default sort direction definition | ||
* | ||
* @return string|null | ||
*/ | ||
protected function getDefaultTableSortDirection(): ?string | ||
{ | ||
return 'desc'; | ||
} | ||
|
||
/** | ||
* Show update role dialog | ||
* | ||
* @param $id | ||
* @return void | ||
*/ | ||
public function updateRole($id) | ||
{ | ||
$this->selectedRole = Role::find($id); | ||
$this->dispatchBrowserEvent('toggleRoleModal'); | ||
} | ||
|
||
/** | ||
* Show create role dialog | ||
* | ||
* @return void | ||
*/ | ||
public function createRole() | ||
{ | ||
$this->selectedRole = new Role(); | ||
$this->dispatchBrowserEvent('toggleRoleModal'); | ||
} | ||
|
||
/** | ||
* Cancel and close role create / update dialog | ||
* | ||
* @return void | ||
*/ | ||
public function cancelRole() | ||
{ | ||
$this->selectedRole = null; | ||
$this->dispatchBrowserEvent('toggleRoleModal'); | ||
} | ||
|
||
/** | ||
* Event launched after a role is created / updated | ||
* | ||
* @return void | ||
*/ | ||
public function roleSaved() | ||
{ | ||
$this->cancelRole(); | ||
} | ||
|
||
/** | ||
* Event launched after a role is deleted | ||
* | ||
* @return void | ||
*/ | ||
public function roleDeleted() | ||
{ | ||
$this->roleSaved(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
<?php | ||
|
||
namespace App\Http\Livewire\Administration; | ||
|
||
use App\Core\CrudDialogHelper; | ||
use Filament\Forms\Components\CheckboxList; | ||
use Filament\Forms\Components\TextInput; | ||
use Filament\Forms\Concerns\InteractsWithForms; | ||
use Filament\Forms\Contracts\HasForms; | ||
use Filament\Notifications\Notification; | ||
use Illuminate\Support\HtmlString; | ||
use Livewire\Component; | ||
use Spatie\Permission\Models\Permission; | ||
use Spatie\Permission\Models\Role; | ||
|
||
class RolesDialog extends Component implements HasForms | ||
{ | ||
use InteractsWithForms; | ||
use CrudDialogHelper; | ||
|
||
public Role $role; | ||
|
||
protected $listeners = ['doDeleteRole', 'cancelDeleteRole']; | ||
|
||
public array $permissions; | ||
|
||
public function mount(): void | ||
{ | ||
$this->form->fill([ | ||
'name' => $this->role->name, | ||
'permissions' => $this->role->permissions->pluck('id')->toArray(), | ||
]); | ||
} | ||
|
||
|
||
public function render() | ||
{ | ||
return view('livewire.administration.roles-dialog'); | ||
} | ||
|
||
/** | ||
* Form schema definition | ||
* | ||
* @return array | ||
*/ | ||
protected function getFormSchema(): array | ||
{ | ||
return [ | ||
TextInput::make('name') | ||
->label(__('Role name')) | ||
->maxLength(255) | ||
->required(), | ||
|
||
CheckboxList::make('permissions') | ||
->label(__('Permissions')) | ||
->hint(new HtmlString(' | ||
<div class="w-full flex items-center gap-2"> | ||
<button type="button" | ||
class="text-xs text-primary-500 hover:text-primary-600 hover:underline" | ||
wire:click="assignAllPermissions"> | ||
' . __('Assign all permissions') . ' | ||
</button> | ||
<span class="text-xs text-gray-300">|</span> | ||
<button type="button" | ||
class="text-xs text-primary-500 hover:text-primary-600 hover:underline" | ||
wire:click="removeAllPermissions"> | ||
' . __('Remove all permissions') . ' | ||
</button> | ||
</div> | ||
')) | ||
->columns(3) | ||
->options(Permission::orderBy('name')->get()->pluck('name', 'id')->toArray()) | ||
]; | ||
} | ||
|
||
/** | ||
* Assign all permissions | ||
* | ||
* @return void | ||
*/ | ||
public function assignAllPermissions(): void | ||
{ | ||
$this->permissions = Permission::all()->pluck('id')->toArray(); | ||
} | ||
|
||
/** | ||
* Remove all assigned permissions | ||
* | ||
* @return void | ||
*/ | ||
public function removeAllPermissions(): void | ||
{ | ||
$this->permissions = []; | ||
} | ||
|
||
/** | ||
* Create / Update the role | ||
* | ||
* @return void | ||
*/ | ||
public function save(): void | ||
{ | ||
$data = $this->form->getState(); | ||
if (!$this->role?->id) { | ||
$role = Role::create(['name' => $data['name']]); | ||
$role->syncPermissions($this->permissions); | ||
Notification::make() | ||
->success() | ||
->title(__('Role created')) | ||
->body(__('The user role has been created')) | ||
->send(); | ||
} else { | ||
$this->role->name = $data['name']; | ||
$this->role->save(); | ||
$this->role->syncPermissions($this->permissions); | ||
Notification::make() | ||
->success() | ||
->title(__('Role updated')) | ||
->body(__('The role\'s details has been updated')) | ||
->send(); | ||
} | ||
$this->emit('roleSaved'); | ||
} | ||
|
||
/** | ||
* Delete an existing role | ||
* | ||
* @return void | ||
*/ | ||
public function doDeleteRole(): void | ||
{ | ||
$this->role->delete(); | ||
$this->deleteConfirmationOpened = false; | ||
$this->emit('roleDeleted'); | ||
Notification::make() | ||
->success() | ||
->title(__('Role deleted')) | ||
->body(__('The role has been deleted')) | ||
->send(); | ||
} | ||
|
||
/** | ||
* Cancel the deletion of a role | ||
* | ||
* @return void | ||
*/ | ||
public function cancelDeleteRole(): void | ||
{ | ||
$this->deleteConfirmationOpened = false; | ||
} | ||
|
||
/** | ||
* Show the delete role confirmation dialog | ||
* | ||
* @return void | ||
* @throws \Exception | ||
*/ | ||
public function deleteRole(): void | ||
{ | ||
$this->deleteConfirmation( | ||
__('Role deletion'), | ||
__('Are you sure you want to delete this role?'), | ||
'doDeleteRole', | ||
'cancelDeleteRole' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.