Skip to content

Role management and the ability to control your Laravel applications.

License

Notifications You must be signed in to change notification settings

OctopyID/WatchDog

Repository files navigation

WatchDog

Tests Version Downloads License

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:

  1. Install WatchDog using Composer.
composer require octopyid/watchdog:dev-main
  1. Publish the package.
php artisan vendor:publish --provider="Octopy\WatchDog\WatchDogServiceProvider"
  1. Add WatchDog Traits to your model.
<?php

use Octopy\WatchDog\Concerns\HasAbility;
use Octopy\WatchDog\Concerns\HasRole;

class User extends Authenticatable
{
    use HasRole, HasAbility;
}
  1. 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.