定制 ahrengot/laravel-roles-and-permissions 二次开发

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

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

ahrengot/laravel-roles-and-permissions

Composer 安装命令:

composer require ahrengot/laravel-roles-and-permissions

包简介

User roles and permissions for Laravel

README 文档

README

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

Roles and permissions for laravel

Installation

Install the package via composer

composer require ahrengot/laravel-roles-and-permissions

Run the install command to publish the migration, stubs, config file and a basic test

php artisan roles-and-permissions:install

Run the migration to add a role column to your users table. Feel free to modify this migration as needed.

php artisan migrate

Configuring your User model

Add the HasPermissions trait to your user model and an enum cast for the role column. Optionally you can add a default value for the role using the built-in $attributes property.

use \Ahrengot\RolesAndPermissions\Traits\HasPermissions;
use App\Enums\UserRole;

class User extends Authenticatable
{
    use HasPermissions;
    
    protected $casts = [
        'role' => UserRole::class,
    ];
    
    // Optional default role
    protected $attributes = [
        'role' => UserRole::User,
    ]
}

Configuring roles and permissions

Your permissions are configured in config/permissions.php.

User roles are defined in App\Enums\UserRole.php. Update these roles to fit your application needs.

Permissions are just simple strings, but this package provides a helper class in App/Permissions/Permission.php that declare each permission as a constant. This provides better editor support and helps prevent typos.

The config file contains an example of defining various permissions for each user role:

return [
    'roles' => [
        UserRole::Admin->value => [
            Permission::AccessAdminPanel,
            Permission::CreateApiTokens,
        ],
    ],
];

Usage

In blade

<nav>
    @can(Permission::AccessAdminPanel)
        <a href="...">Admin panel</a>
    @endcan
    <a href="...">Other link</a>
</nav>

In policies

public function create(User $user)
{
    return $user->can(Permission::CreatePosts);
}

Comparing user roles

The UserRole enum has two comparison methods

    $user->role->is(UserRole::Admin);
    
    $user->role->isNot(UserRole::Admin);

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-02

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固