octopyid / watchdog
Manage access to records or models
Fund package maintenance!
Ko Fi
Requires
- php: ^8.0|^8.1
Requires (Dev)
- nunomaduro/collision: ^6.2
- octopyid/phpunit-extra: ^1.0
- orchestra/testbench: ^7.5
README
Laravel Watch Dog
Watch Dog is a package for role management and the ability to control your Laravel applications.
Features
- Roles, permissions and abilities.
- The ability of the role of the model or record.
- The ability of the entity to the model or record.
It also includes middleware and configurable cache.
Installation
To install the package, simply follow the steps below.
Install the package using Composer:
- Install WatchDog using Composer.
composer require octopyid/watchdog:dev-main
- Publish the package.
php artisan vendor:publish --provider="Octopy\WatchDog\WatchDogServiceProvider"
- Add WatchDog Traits to your model.
<?php use Octopy\WatchDog\Concerns\HasAbility; use Octopy\WatchDog\Concerns\HasRole; class User extends Authenticatable { use HasRole, HasAbility; }
- Finally, run the migrations:
php artisan migrate
Usage
Assign Role to User
$role = Role::create([ 'name' => 'foo', ]); # Assign $user->role->assign('foo'); $user->role->assign($role); # Check $user->role->has('foo'); $user->role->has($role); # Remove $user->role->retract('foo'); $user->role->retract($role);
Assign Ability to Role
Ability Without Model
$ability = Ability::create([ 'name' => 'delete', ]); # Assign $role->ability->assign('delete'); $role->ability->assign($ability); # Check By User $user->ability->able('delete'); # Check By Role $role->ability->able('delete'); # Remove $user->ability->retract('foo'); $user->ability->retract($ability);
Ability With Model
# You want to give abilities only to certain records of a model. $ability = Ability::create([ 'name' => 'delete', 'entity_id' => 1, 'entity_type' => \App\Models\Post::class, ]); # Or do you want to give abilities to all records of a model. $ability = Ability::create([ 'name' => 'delete', 'entity_id' => null, // null means all records 'entity_type' => \App\Models\Post::class, ]); # Check By User $user->ability->able('delete', $post); $user->ability->able('delete', Post::class);
Assign Ability to Entity
Ability Without Model
$ability = Ability::create([ 'name' => 'delete', ]); # Assign $user->ability->assign('delete'); $user->ability->assign($ability); # Check $user->ability->able('delete'); # Remove $user->ability->retract('foo'); $user->ability->retract($ability);
Ability With Model
# You want to give abilities only to certain records of a model. $ability = Ability::create([ 'name' => 'delete', 'entity_id' => 1, 'entity_type' => \App\Models\Post::class, ]); # Or do you want to give abilities to all records of a model. $ability = Ability::create([ 'name' => 'delete', 'entity_id' => null, // null means all records 'entity_type' => \App\Models\Post::class, ]); # To check $user->ability->able('delete', $post); $user->ability->able('delete', Post::class);
Disclaimer
All maintainers, contributors, and the package itself are not responsible for any damages, direct or indirect, that may occur as a result of using this package.
Security
If you discover any security related issues, please email bug@octopy.dev instead of using the issue tracker.
License
This package is licensed under the MIT license.