Watch Dog is a package for role management and the ability to control your Laravel applications.
- 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.
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
$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);
$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);
# 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);
$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);
# 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);
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.
If you discover any security related issues, please email bug@octopy.dev instead of using the issue tracker.
This package is licensed under the MIT license.