定制 jftecnologia/laravel-permission 二次开发

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

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

jftecnologia/laravel-permission

Composer 安装命令:

composer require jftecnologia/laravel-permission

包简介

RBAC + scoped permissions (all/self/attached) for Laravel

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

RBAC for Laravel with scoped permissions (all, self, attached) and optional multi-tenancy support.

1:1 model: the Gate ability string equals the permission name (e.g. companies.edit.attached == permissions.name).

Features

  • Scoped permissions with all, self, and attached scopes
  • Roles and permissions with a familiar API
  • Attachment-based access for fine-grained authorization
  • Optional multi-tenancy via a feature flag
  • Configurable resolvers for tenant and self resolution

Installation

composer require jftecnologia/laravel-permission

Publish config (optional):

php artisan vendor:publish --tag="permission-config"

Run the migrations:

php artisan migrate

Configuration

config/permission.php:

  • models.permission|role|attachment: swap the Eloquent models
  • tables.*: rename tables
  • tenancy.enabled: feature flag for multi-tenancy (default: true)
  • tenancy.column: tenant column name (default: tenant_id)
  • tenant_resolver: callback to resolve the current tenant id (nullable)
  • self_resolver: callback to define what "self" means

Default self

If you don't define self_resolver, the package uses the convention:

  • resource->created_by == user->id

Usage

1) On your User model

Add the trait:

use JuniorFontenele\LaravelPermission\Traits\InteractsWithPermissions;

class User extends Authenticatable
{
    use InteractsWithPermissions;
}

2) Create and assign permissions

Permissions are unique strings (permissions.name is unique):

$user->givePermissionTo('companies.edit.all');

3) Roles

$user->assignRole('editor');
$user->syncRoles(['editor', 'viewer']);
$user->removeRole('viewer');

Permission via role:

$role = \JuniorFontenele\LaravelPermission\Models\Role::query()->firstOrCreate([
    'tenant_id' => null,
    'name' => 'editor',
    'guard_name' => 'web',
]);

$role->givePermissionTo('companies.edit.all');

4) Gate/Policies (scopes)

$user->can('companies.edit.all');
$user->can('companies.edit.self', $company);
$user->can('companies.edit.attached', $company);
  • all: checks RBAC only
  • self: RBAC + self_resolver
  • attached: RBAC + permission_attachments record

5) Attachments (attached scope)

use JuniorFontenele\LaravelPermission\Facades\Permission;

Permission::attach($user, 'companies.edit.attached', $company);

$user->can('companies.edit.attached', $company); // true

Multi-tenancy

Multi-tenancy support is a feature flag:

  • permission.tenancy.enabled = true: creates/uses tenant column in tables (migrations + queries)
  • permission.tenancy.enabled = false: ignores tenant entirely and does not create the column

The column is configurable via permission.tenancy.column (default: tenant_id).

To enable tenant scoping, define permission.tenant_resolver in config (or pass tenantId explicitly in APIs).

Testing

composer test

Credits

License

MIT License. See LICENSE.md for details.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-06

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固