定制 delgont/armor 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

delgont/armor

最新稳定版本:v1.9.1

Composer 安装命令:

composer create-project delgont/armor

包简介

Armor is a high‑performance Laravel package for managing user roles and permissions. It provides a flexible, cache‑friendly system to define, assign, and enforce permissions across your application, making route and action protection seamless and efficient.

README 文档

README

Delgont Armor Banner

Armor – Laravel Role & Permission Package

Armor is a powerful, flexible, and cache-friendly permission and role management package for Laravel applications. Designed with performance in mind, Armor allows you to define roles and permissions, assign them to users, and protect routes or actions effortlessly.

🔒 Built for speed
⚙️ Highly customizable
📦 Supports permission groups, caching, syncing, and more

Installation

composer require delgont:armor

php artisan migrate

Restrict Access Using Permissions

  1. Create Permission Registrar and Define the permissions.
php artisan make:permissionRegistrar ExpensePermissionRegistrar
<?php
namespace App;

use Delgont\Armor\PermissionRegistrar;

class ExpensePermissionRegistrar extends PermissionRegistrar
{
    const CAN_MANAGE_EXPENSES = 'can_manage_expenses';
    const CAN_ADD_EXPENSES = 'can_add_expenses';

    /**
     * Provide descriptions for each permission.
     *
     * @return array
     */
    public function descriptions(): array
    {
        return [
            self::CAN_MANAGE_EXPENSES => 'Allow user to manage expenses',
            self::CAN_ADD_EXPENSES => 'Allow user to add expenses',
        ];
    }
}
  1. Register Permission Register in your Service Provider.
<?php
namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Delgont\Armor\Armor;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application servipSycnces.
     *
     * @return void
     */
    public function boot()
    {

        Armor::registerPermissionRegistrars([
            \App\ExpensePermissionRegistrar::class,
        ]);

    }

}
  1. Synchronize Permisions.
php artisan armor:sync-permissions

Apply Permissions Directly to the Authenticatable Model

<?php
namespace App;

use Delgont\Armor\Concerns\ModelHasPermissions;

class User extends Authenticatable
{
    use ModelHasPermissions;

}
# Geting permissions of specific group
$permissions = app(\App\ExpensePermissionRegistrar::class)->getPermissionsFromDb();

# Get model which uses trait Delgont\Armor\Concerns\ModelHasPermissions
$user =  User::whereId(3)->first();

# Give permissions to user
$user->givePermissionTo($permissions);

# Withdrawal permissions from user
$user->withdrawPermissionsTo($permissions);

# Sync permissions from user
$user->syncPermissions($permissions);

# Check if model|user has permission
 if($user->hasPermission('can_manage_expenses')) {
    return 'User has the permission';
} else {
    return 'User does not have the permission';
}

# Check if model has any of the permissions
if($user->hasAnyPermission('can_add_expenses', 'can_manage_expenses')) {
    return 'User has the permission';
} else {
    return 'User does not have the permission';
}

# Protect Routes - Works only for user model which is authenticatable
Route::post('/expenses/add', 'ExpenseController@store')->middleware(['permission:can_add_expenses']);

📖 Developed by: Stephen Okello

统计信息

  • 总下载量: 43
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固