vortexpanel / policy-permission-sync
Sync Laravel Policies to Spatie permissions using dot-style naming.
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/vortexpanel/policy-permission-sync
Requires
- php: ^8.1
- illuminate/console: ^10.0|^11.0|^12.0
- illuminate/filesystem: ^10.0|^11.0|^12.0
- illuminate/support: ^10.0|^11.0|^12.0
- spatie/laravel-permission: ^6.0
Requires (Dev)
- ext-pdo_sqlite: *
- ext-sqlite3: *
- nunomaduro/collision: ^7.0|^8.0
- orchestra/testbench: ^8.0|^9.0|^10.0
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.0|^11.0
README
Sync Laravel Policies into Spatie permissions using deterministic names (configurable):
posts.view_anyposts.viewposts.createposts.updateposts.delete
This is a tiny, UI-agnostic package intended to be reused by admin panels (VortexPanel, Filament, Nova, ...).
Requirements
- PHP 8.1+
- Laravel 10+
spatie/laravel-permission
Installation
composer require vortexpanel/policy-permission-sync
Publish config:
php artisan vendor:publish --tag=policy-permission-sync-config
Configuration
Edit config/policy-permission-sync.php.
The most reliable setup is to explicitly scan your policies directory:
'policy_paths' => [ app_path('Policies'), ],
Customize permission naming
By default, permissions are created using a dot-style pattern:
'naming' => [ 'prefix' => '', 'pattern' => '{resource}.{ability}', ],
You can change it to any pattern containing {resource} and {ability}:
// posts:view_any 'pattern' => '{resource}:{ability}', // view_any:posts 'pattern' => '{ability}:{resource}', // admin.posts.view_any 'prefix' => 'admin.', 'pattern' => '{resource}.{ability}',
Notes:
- If you use
--prune, the package scopes pruning by extracting{resource}back from permission names using this same pattern. If your pattern doesn't include{resource}, pruning can't be scoped.
If your policies live in a custom directory/namespace, set the mapping:
'policy_paths' => [ app_path('Domain/Blog/Policies'), ], 'policy_path_namespace_map' => [ app_path('Domain/Blog/Policies') => 'App\\Domain\\Blog\\Policies', ],
Usage
Preview changes (no DB writes):
php artisan permissions:sync-policies --dry-run
Apply changes:
php artisan permissions:sync-policies
Prune stale permissions (opt-in):
php artisan permissions:sync-policies --prune
Target specific guards:
php artisan permissions:sync-policies --guards=web,api
Filter resources:
php artisan permissions:sync-policies --only=posts,users php artisan permissions:sync-policies --except=audits
Show per-permission actions:
php artisan permissions:sync-policies --details
# or use artisan verbosity:
php artisan permissions:sync-policies -v
Development & Tests (inside this package repo)
If you see PDOException: could not find driver, your PHP CLI is missing the SQLite extension.
Ubuntu/WSL:
sudo apt-get update
sudo apt-get install -y php-sqlite3
php -m | grep -i sqlite
Then:
composer install
composer test
composer analyse
Testing in a real Laravel app (manual)
-
Install
spatie/laravel-permissionand run its migrations. -
Create a policy, e.g.
app/Policies/PostPolicy.phpwith methods likeviewAny,view,create,update,delete. -
Ensure your config scans the right directory:
'policy_paths' => [app_path('Policies')],
- Run:
php artisan permissions:sync-policies
Then check the permissions table.
License
MIT