承接 tamas1979/laravel-authority 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

tamas1979/laravel-authority

最新稳定版本:v1.0.1

Composer 安装命令:

composer require tamas1979/laravel-authority

包简介

Authority / hierarchy based authorization for Laravel

README 文档

README

A simple and reusable Laravel package for role hierarchy and authority-based authorization.

This package allows you to define an authority level for your users and easily manage permissions between users of different levels. It works seamlessly with API-only projects, Blade, Inertia, or Livewire, and is fully framework-agnostic, so it can also be used alongside other permission packages like Spatie’s Roles & Permissions.

Features

  • Authority hierarchy: easily compare users’ authority levels (manage logic built-in)
  • Reusability: designed as a Laravel package, works across projects
  • Flexible: integrate with any existing authorization system
  • API & Blade ready: works in controllers, policies, and views
  • Lightweight: minimal setup, no heavy dependencies

Installation

Require the package via Composer:

composer require tamas1979/laravel-authority

Configuration

Publish the config file:

php artisan vendor:publish --provider="Tamas1979\Authority\AuthorityServiceProvider" --tag=config

The default config:

return [
    'column' => 'authority_level', // the integer column in your users table
];

Setup

  1. Run migrations

The package comes with a migration that adds the authority_level column to your users table. Just run:

php artisan migrate
  • If the column already exists, the migration will not overwrite it
  • No need to manually create a migration
  1. Add the Trait and implement the contract in your User model
use Tamas1979\Authority\Contracts\AuthorityUser;
use Tamas1979\Authority\Traits\HasAuthority;

class User extends Authenticatable implements AuthorityUser
{
    use HasAuthority;

    protected $fillable = [
        'name',
        'email',
        'password',
        'authority_level', // required for authority logic
    ];
}
  • The HasAuthority trait must be added manually for IDE support, type hinting, and policies to work properly
  • Runtime-only usage without the trait is possible via macros, but not recommended for full type safety
  1. Authority Level Guide (example)
Role authority_level
SuperAdmin 1000
Owner Admin 500
Admin 400
Moderator 300
User 100

This table provides a clear example of how authority levels are mapped to roles.

Usage

Tinker example

$user1 = \App\Models\User::create([
    'name' => 'Admin1',
    'email' => 'admin1@test.com',
    'password' => bcrypt('password'),
    'authority_level' => 500, // example
]);

$user2 = \App\Models\User::create([
    'name' => 'Admin2',
    'email' => 'admin2@test.com',
    'password' => bcrypt('password'),
    'authority_level' => 400, // example
]);

$user1->authorityLevel(); // 500
$user2->authorityLevel(); // 400

Policy usage

$policy = new \Tamas1979\Authority\Policies\AuthorityPolicy();

$policy->manage($user1, $user2); // true
$policy->manage($user2, $user1); // false

Blade / Controller

@can('manage', $targetUser)
    <button>Remove Admin Role</button>
@endcan
$this->authorize('manage', $targetUser);

Contribution

  • Open for ideas, bug fixes, and feature requests
  • Follows Laravel 11+ standards
  • API-ready and Blade-compatible

License

MIT

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-23

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固