althinect / filament-spatie-roles-permissions
Installs: 367 089
Dependents: 8
Suggesters: 0
Security: 0
Stars: 260
Watchers: 9
Forks: 88
Open Issues: 9
Requires
- php: ^8.1|^8.2
- filament/filament: ^3.0
- illuminate/support: ^9|^10|^11
- spatie/laravel-permission: ^5.4|^6.0
Requires (Dev)
- laravel/pint: ^1.10
- orchestra/testbench: ^7.0|^9.0
- phpunit/phpunit: ^9.0|^10.5
- 2.x-dev
- v2.2.38
- v2.2.37
- v2.2.36
- v2.2.35
- v2.2.34
- v2.2.33
- v2.2.32
- v2.2.31
- v2.2.30
- v2.2.29
- v2.2.28
- v2.2.27
- v2.2.26
- v2.2.25
- v2.2.24
- v2.2.23
- v2.2.22
- v2.2.21
- v2.2.20
- v2.2.19
- v2.2.18
- v2.2.17
- v2.2.16
- v2.2.15
- v2.2.14
- v2.2.13
- v2.2.12
- v2.2.11
- v2.2.10
- v2.2.9
- v2.2.8
- v2.2.7
- v2.2.6
- v2.2.5
- v2.2.4
- v2.2.3
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.1
- v2.0.14
- v2.0.13
- v2.0.12
- v2.0.11
- v2.0.10
- v2.0.9
- v2.0.8
- v2.0.7
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0
- 1.x-dev
- v1.3.10
- v1.3.9
- v1.3.8
- v1.3.7
- v1.3.6
- v1.3.5
- v1.3.4
- v1.3.3
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.4
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.9
- v1.1.8
- v1.1.7
- v1.1.6
- v1.1.5
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-main
- dev-fix/tenancy-missing-id
This package is not auto-updated.
Last update: 2024-12-17 16:21:36 UTC
README
This plugin is built on top of Spatie's Permission package.
Provides Resources for Roles and Permissions
Permission and Policy generations
- Check the
config/filament-spatie-roles-permissions-config.php
Supports permissions for teams
- Make sure the
teams
attribute in theconfig/permission.php
file is set totrue
Updating
After performing a composer update
, run
php artisan vendor:publish --tag="filament-spatie-roles-permissions-config" --force
Note that your existing settings will be overriden
If you like our work Don't forget to STAR the project
Installation
You can install the package via composer:
composer require althinect/filament-spatie-roles-permissions
Since the package depends on Spatie's Permission package. You have to publish the migrations by running:
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"
Add the plugin to the AdminPanelProvider
use Althinect\FilamentSpatieRolesPermissions\FilamentSpatieRolesPermissionsPlugin; $panel ... ->plugin(FilamentSpatieRolesPermissionsPlugin::make())
Now you should add any other configurations needed for the Spatie-Permission package.
Note: This will override your existing config file. You can publish the config file of the package with:
php artisan vendor:publish --tag="filament-spatie-roles-permissions-config" --force
You can publish translations with:
php artisan vendor:publish --tag="filament-spatie-roles-permissions-translations"
Don't forget to add the HasRoles
trait to your User model.
// The User model requires this trait use HasRoles;
Usage
Form
You can add the following to your form method in your UserResource
return $form->schema([ Select::make('roles')->multiple()->relationship('roles', 'name') ])
In addition to the field added to the UserResource. There will be 2 Resources published under Roles and Permissions. You can use these resources manage roles and permissions.
Generate Permissions
You can generate Permissions by running
php artisan permissions:sync
This will not delete any existing permissions. However, if you want to delete all existing permissions, run
php artisan permissions:sync -C|--clean
There may be an occassion where you wish to hard reset and truncate your existing permissions. To delete all permissions and reset the primary key, run
php artisan permissions:sync -H|--hard
Example:
If you have a Post model, it will generate the following permissions
view-any Post
view Post
create Post
update Post
delete Post
restore Post
force-delete Post
replicate Post
reorder Post
Generating Policies
To generate policies use the command below. This won't replace any existing policies
php artisan permissions:sync -P|--policies
Overriding existing Policies
This will override existing policy classes
php artisan permissions:sync -O|--oep
Role and Permission Policies
Create a RolePolicy and PermissionPolicy if you wish to control the visibility of the resources on the navigation menu. Make sure to add them to the AuthServiceProvider.
ℹ️ Info: Laravel 11 removed
AuthServiceProvider
, so, in this case, we need to useAppServiceProvider
instead.
use App\Policies\RolePolicy; use App\Policies\PermissionPolicy; use Spatie\Permission\Models\Role; use Spatie\Permission\Models\Permission; Gate::policy(Role::class, RolePolicy::class); Gate::policy(Permission::class, PermissionPolicy::class);
Ignoring prompts
You can ignore any prompts by add the flag -Y
or --yes-to-all
Recommended only for new projects as it will replace Policy files
php artisan permissions:sync -COPY
Adding a Super Admin
- Create a Role with the name
Super Admin
and assign the role to a User - Add the following trait to the User Model
use Althinect\FilamentSpatieRolesPermissions\Concerns\HasSuperAdmin; class User extends Authenticatable{ ... use HasSuperAdmin;
- In the
boot
method of theAuthServiceProvider
add the following
Gate::before(function (User $user, string $ability) { return $user->isSuperAdmin() ? true: null; });
Guard Names
When you use any guard other than web
you have to add the guard name to the config/auth.php
file.
Example: If you use api
guard, you should add the following to the guards
array
'guards' => [ ... 'api' => [ 'driver' => 'token', 'provider' => 'users', 'hash' => false, ], ],
Tenancy
- Make sure to set the following on the
config/permission.php
'teams' => true
- Make sure the
team_model
on theconfig/permission
is correctly set. - Create a Role model which extends
Spatie\Permission\Models\Role
- Replace the model in the
config/permission.php
with the newly created models - Add the
team
relationship in both models
... public function team(): BelongsTo { return $this->belongsTo(Team::class); }
- Add the following to the
AdminPanelProvider
to support tenancy
use Althinect\FilamentSpatieRolesPermissions\Middleware\SyncSpatiePermissionsWithFilamentTenants; $panel ... ->tenantMiddleware([ SyncSpatiePermissionsWithFilamentTenants::class, ], isPersistent: true)
- Use the following within you UserResource
Forms\Components\Select::make('roles')
->relationship(name: 'roles', titleAttribute: 'name')
->saveRelationshipsUsing(function (Model $record, $state) {
$record->roles()->syncWithPivotValues($state, [config('permission.column_names.team_foreign_key') => getPermissionsTeamId()]);
})
->multiple()
->preload()
->searchable(),
Follow the instructions on Filament Multi-tenancy
Configurations
In the filament-spatie-roles-permissions.php config file, you can customize the permission generation
Security
If you discover any security related issues, please create an issue.
Credits
License
The MIT License (MIT). Please see License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate.