PHPackages                             larakeeps/guard-shield - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. larakeeps/guard-shield

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

larakeeps/guard-shield
======================

GuardShield é um repositório laravel com a função de aplicar regras de permissionamento, utilizando agrupamento de regras e permissões.

v1.0.44(7mo ago)11.2kMITPHP

Since Feb 2Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/douglasssantos/GuardShield)[ Packagist](https://packagist.org/packages/larakeeps/guard-shield)[ Docs](https://github.com/douglasssantos/GuardShield)[ RSS](/packages/larakeeps-guard-shield/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (19)Used By (0)

**GuardShield**

> GuardShield is a laravel repository with the function of applying permission rules, using grouping of rules and permissions. follow the step-by-step instructions below to install the repository.

### This repository is only compatible with laravel: `8.*` to `10.*`

[](#this-repository-is-only-compatible-with-laravel-8-to-10)

Installation
------------

[](#installation)

First Step, execute the command.

```
composer require larakeeps/guard-shield
```

Second step, add the service provider. Open `app/Providers/AuthServiceProvider.php` or the `GuardShield::generateGates()` method in the Service Provider's `public function boot()`.

```
use Larakeeps\GuardShield\Facades\GuardShield;

class AuthServiceProvider extends ServiceProvider
{

    public function boot(): void
    {
        GuardShield::generateGates();

        //rest of your code......
    }
}
```

Third step, add the middleware. Open `app/Http/Kernel.php` and add a new item to the middlewareAliases array.

```
protected $middlewareAliases = [
    // ...
    'rolecan' => \Larakeeps\GuardShield\Http\Middleware\GuardShieldTrustRole::class,
]
```

Fourth step add the trait and the necessary parameter to your model. Open `Models/User.php`

```
use Larakeeps\GuardShield\Traits\GuardShield;

class User extends Authenticatable
{
  use GuardShield, HasApiTokens, HasFactory;

  protected $with = ['roles'];

  //.... rest of the model code
}
```

Fifth step, run the migration to create the tables: `guard_shield_roles, guard_shield_role_user, guard_shield_assigns, guard_shield_permissions`

```
php artisan migrate
```

**Creating Group of Rules and Permissions Using Model**

```
use \Larakeeps\GuardShield\Models\Role;
use \Larakeeps\GuardShield\Models\Permission;

$role = Role::new("Administrator", "Rule group for administrators."); // Creating a new permission group.
$permission = Permission::new("Edit User", "Permission to edit user."); // Creating a new permission.
$role->assignPermission($permission); // Linking the permission to a permission group.

//Creating a Rule, permissions and Linking the created permissions to the permission group.

$permissions = [
    ["View User", "Permission to view user"],
    ["Create User", "Permission to create user"],
    ["Edit User", "Permission to edit user"],
    ["Delete User", "Permission to delete user"]
];

Role::newRoleAndPermissions("Administrator", "Rule group for administrators.", $permissions);

//Creating a Rule and Linking existing permissions to the created permission group.

$permissions = ["View User", "Create User", "Edit User", "Delete User"];

Role::newRoleAndAssignPermissions("Administrator", "Rule group for administrators.", $permissions);

// Activating and Deactivating a permission.

Permission::setActive("View User", false);
```

**Creating Rules and Permissions Groups Using Facade**

```
use Larakeeps\GuardShield\Facades\GuardShield;

$role = GuardShield::newRole("Administrator", "Rule group for administrators."); // Creating a new permission group
$permission = GuardShield::newPermission("Edit User", "Permission to edit user"); // Creating a new permission
$role->assignPermission($permission); // Linking the permission to a permission group.

// OR

GuardShield::assignPermission($role, $permission);

//Creating a Rule, permissions and Linking the created permissions to the permission group.

$permissions = [
     ["View User", "Permission to view user"],
     ["Create User", "Permission to create user"],
     ["Edit User", "Permission to edit user"],
     ["Delete User", "Permission to delete user"],
];

GuardShield::newRoleAndPermissions("Administrator", "Rule group for administrators.", $permissions);

//Creating a Rule and Linking existing permissions to the created permission group.

$permissions = ["View User", "Create User", "Edit User", "Delete User"];

GuardShield::newRoleAndAssignPermissions("Administrator", "Rule group for administrators.", $permissions);

// Activating and Deactivating a permission

GuardShield::setActivePermission("View User", false);
```

**Checking the existence of rule groups and permissions created.**

```
use Larakeeps\GuardShield\Facades\GuardShield;

$hasPermission = ["View User", "Create User", "Update User", "Delete User"];

/**
 *
 * Power one or more rules by passing an array as a parameter
 * The same goes for permissions
 * @method static bool hasRoleAndPermission(string|array $role, string|array $permission)
 *
 * */

$hasRolesAndPermissions = GuardShield::hasRoleAndPermission('user' , $hasPermission);

if($hasRolesAndPermissions){
    return "The Rule Group and permissions exist."
}

/**
 *
 * Check whether one or more rule groups exist.
 * @method static bool hasRole(string|array $role)
 *
 * */
$hasRoles = GuardShield::hasRole(['administrator', 'user']);

if($hasRoles){
    return "Rule Groups exist."
}

/**
 *
 * Check whether one or more rule groups exist.
 * @method static bool hasPermission(string|array $role)
 *
 * */

$permissions = ["View User", "Create User", "Update User", "Delete User"];
$hasPermission = GuardShield::hasPermission($permissions);

if($hasPermission){
    return "Permissions exist."
}

//Unless methods for validation.
```

**Viewing the created rules and permissions.**

```
use Larakeeps\GuardShield\Facades\GuardShield;

//@method allRoles(): Collection
return GuardShield::allRoles();

/**
 * Returned array:
 * [
        {
            "key": "administrator",
            "name": "Administrator",
            "description": "Role to administrator",
            "permissions": [
                {
                    "key": "viewuser",
                    "name": "View User",
                    "description": "Permission to view user.",
                    "params": null,
                    "active": true
                },..
            ]
        },
        {
            "key": "user",
            "name": "user",
            "description": "Role to user",
            "permissions": [
                {
                    "key": "viewuser",
                    "name": "View User",
                    "description": "Permission to view user.",
                    "params": null,
                    "active": true
                },.....
            ]
        }
    ]
 */

// @method getRole(array|string $role): Collection
return GuardShield::getRole(['administrator', 'user']);

/**
 * Returned array:
 * [
        {
            "key": "administrator",
            "name": "Administrator",
            "description": "Role to administrator",
            "permissions": [
                {
                    "key": "viewuser",
                    "name": "View User",
                    "description": "Permission to view user.",
                    "params": null,
                    "active": true
                },...
            ]
        },
        {
            "key": "user",
            "name": "user",
            "description": "Role to user",
            "permissions": [
                {
                    "key": "viewuser",
                    "name": "View User",
                    "description": "Permission to view user.",
                    "params": null,
                    "active": true
                },...
            ]
        }
    ]
 */

//@method allPermissions(): Collection
return GuardShield::allPermissions();

/**
 * Returned array:
 * [
        {
        "key": "viewuser",
        "name": "View User",
        "description": "Permission to view user.",
        "params": null,
        "active": true
        },
        {
        "key": "createuser",
        "name": "Create User",
        "description": "Permission to create user.",
        "params": null,
        "active": true
        },....
    ]
 */

//@method getPermission(array|string $permission): Collection
return GuardShield::getPermission(['View User', 'Create User']);

/**
 * Returned array:
 * [
        {
        "key": "viewuser",
        "name": "View User",
        "description": "Permission to view user.",
        "params": null,
        "active": true
        },
        {
        "key": "createuser",
        "name": "Create User",
        "description": "Permission to create user.",
        "params": null,
        "active": true
        }
    ]
 */
```

**Assign a rule group to a user**

```
User::whereId($request->user_id)->assignRole("Administrator");

// OR

Auth::user()->assignRole("Administrator");

// OR

$request->user()->assignRole("Administrator");
```

**How to Use GuardShield**

```
use Illuminate\Support\Facades\Gate;
use Larakeeps\GuardShield\Facades\GuardShield;

// Using Gates
if(Gate::allows('Edit User')){
    return "User contains the necessary permission to perform the action.";
}

// Check whether the user has the permission and whether the permission is part of a permission group.
if(Gate::allows('Edit User', "Administrator")){
    return "User contains the necessary permission to perform the action.";
}

//====================================================

// Using GuardShield
if(GuardShield::allows('Edit User')){
    return "User contains the necessary permission to perform the action.";
}

// Check if the user has the permission and if the permission is part of a permission group.
if(GuardShield::allows('Edit User', "Administrator")){
     return "User has the necessary permission to perform the action.";
}

//====================================================

// Using the Model
if(Auth::user()->hasRole("Administrator"))){
     return "User is part of the permission group.";
}

//====================================================

// Using Middleware to check permissions

Route::post("edit-user", function (\Illuminate\Http\Request $request){

      return "User has the necessary permission to perform the action."

})->middleware(['auth:sanctum', "can:Edit User,Delete User"]);

// Using Middleware to check permission group

Route::post("edit-user", function (\Illuminate\Http\Request $request){

      return "User is part of the permission group";

})->middleware(['auth:sanctum', "rolecan:admin,user"]);
```

#### Don't forget to follow me on github and star the project.

[](#dont-forget-to-follow-me-on-github-and-star-the-project)

> ### My contacts
>
> [](#my-contacts)
>
> > E-mail:
> >
> > Linkedin: [Acessa Perfil](https://www.linkedin.com/in/douglas-da-silva-santos/) [![](https://camo.githubusercontent.com/963c0acef69284b79f7e3026da343df62ad91afc0d5df4d2a283f6cd3fb69951/68747470733a2f2f63646e2e6a7364656c6976722e6e65742f67682f64657669636f6e732f64657669636f6e2f69636f6e732f6c696e6b6564696e2f6c696e6b6564696e2d6f726967696e616c2e737667)](https://camo.githubusercontent.com/963c0acef69284b79f7e3026da343df62ad91afc0d5df4d2a283f6cd3fb69951/68747470733a2f2f63646e2e6a7364656c6976722e6e65742f67682f64657669636f6e732f64657669636f6e2f69636f6e732f6c696e6b6564696e2f6c696e6b6564696e2d6f726967696e616c2e737667)

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance63

Regular maintenance activity

Popularity20

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~36 days

Recently: every ~0 days

Total

18

Last Release

225d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f4f235b981bac409a25fb2f3e110f857c20e97194ce8e1f04c4402d2855002f6?d=identicon)[douglasssantos](/maintainers/douglasssantos)

---

Top Contributors

[![douglasssantos](https://avatars.githubusercontent.com/u/44000220?v=4)](https://github.com/douglasssantos "douglasssantos (54 commits)")

---

Tags

laravelsecurityaclpermissionrolespermissionsrbaclarakeeps

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/larakeeps-guard-shield/health.svg)

```
[![Health](https://phpackages.com/badges/larakeeps-guard-shield/health.svg)](https://phpackages.com/packages/larakeeps-guard-shield)
```

###  Alternatives

[spatie/laravel-permission

Permission handling for Laravel 12 and up

12.9k89.8M1.0k](/packages/spatie-laravel-permission)[bezhansalleh/filament-shield

Filament support for `spatie/laravel-permission`.

2.8k2.9M88](/packages/bezhansalleh-filament-shield)[wnikk/laravel-access-rules

Simple system of ACR (access control rules) for Laravel, with roles, groups, unlimited inheritance and possibility of multiplayer use.

103.6k1](/packages/wnikk-laravel-access-rules)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
