yard8/laravel-permissions

创建用户角色和应用程序用于授权的各种权限。

1.0.5 2020-04-22 11:09 UTC

This package is not auto-updated.

Last update: 2024-10-03 07:40:02 UTC


README

Latest Version on Packagist Build Status Quality Score Total Downloads

此包的目的是创建角色和权限,可以将它们分配给用户,并在授权或策略检查期间使用。

安装

您可以通过composer安装此包

composer require yard8/laravel-permissions

发布配置和迁移文件

php artisan laravel-permissions:install

向用户表添加一个role_id列,该列是unsigned bigInteger类型,将与角色和权限相关联

设置config/permissions.php配置文件,包括您应用程序中希望包含的角色和权限。

将角色和权限插入您的数据库

php artisan laravel-permissions:insert

将HasPermissions特质添加到您想要拥有角色和权限的模型中

<?php

namespace App;

use Yard8\LaravelPermissions\Traits\HasPermissions;

class User extends Authenticatable
{
    use HasPermissions;
}

用法

// Assign the authenticated user to a variable.
$user = auth()->user();

// Get the role of the user.
$role = $user->role;

// If the user is granted the role admin, both will return true. If the user has neither of these roles they will both return false.
$isAdmin = $user->hasRole('admin');
$isAdminOrManager = $user->hasRole(['admin', 'manager']);

// Get the permissions of the user.
$permissions = $user->permissions;

// If the user has the permission can-post, both will return true. If the user has neither of these permissions, they will both return false.
$canPost = $user->hasPermission('can-post');
$canPostOrComment = $user->hasAnyPermission(['can-post', 'can-comment']);

更新日志

请参阅更新日志以获取最近更改的更多信息。

安全

如果您发现任何安全相关的问题,请发送电子邮件至jason@yard8.co.za,而不是使用问题跟踪器。

鸣谢

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。

Laravel包模板

此包是使用Laravel包模板生成的。