trunow/rpac

角色/权限访问控制 (Rpac) Laravel 包

维护者

详细信息

github.com/trunow/rpac

源代码

问题

安装: 23

依赖: 0

建议者: 0

安全: 0

星星: 0

关注者: 3

分支: 1

开放问题: 0

语言:Vue

v1.0 2020-02-12 13:52 UTC

This package is auto-updated.

Last update: 2024-09-13 01:32:47 UTC


README

安装

Composer

通过 Composer (文件 composer.json) 引入此包...

{
    "require": {
        "php": ">=7.2.0",
        "laravel/framework": "^6.*",
        "trunow/rpac": "dev-master"
    }
}

...然后在您的终端运行此命令。

composer update

或者引入此包

composer require trunow/rpac:dev-master

服务提供者

将包添加到您的应用服务提供者在 config/app.php 文件中。

'providers' => [
    
    /*
     * Laravel Framework Service Providers...
     */
    Illuminate\Foundation\Providers\ArtisanServiceProvider::class,
    Illuminate\Auth\AuthServiceProvider::class,
    ...
    
    /**
     * Third Party Service Providers...
     */
    Trunow\Rpac\RpacServiceProvider::class,

],

配置文件和迁移

将包的配置文件和迁移发布到您的应用中。在您的终端运行以下命令。

php artisan vendor:publish --provider="Trunow\Rpac\RpacServiceProvider"

并且运行迁移。

php artisan migrate

这使用了 Laravel 中的默认用户表。您应该已经有用户表的迁移文件,并且已经迁移。

Rpacable 特性和 $with = [roles]

在您的 User 模型中包含 Rpacable 特性。

use Trunow\Rpac\Traits\Rpacable;

class User extends Model implements AuthenticatableContract
{
    use Authenticatable, Rpacable;

并且设置受保护的属性 $with = ['roles'](用于自动加载与用户模型一起的角色)。

protected $with = ['roles'];

创建 Su/Admin 用户

运行命令,例如 rpac su:1rpac admin:email@example.comrole:user@example.com:pa$$w0r5

php artisan rpac su:slava@trunov.me

(:

然后访问 your-domain.com/admin-rpac

:)

用法

创建策略

为您的模型创建一个扩展 RpPolicy 的空策略类。

namespace App\Policies;

use Trunow\Rpac\Policies\RpPolicy;

class PostPolicy extends RpPolicy
{
    //...
}

待办事项

创建角色

use Trunow\Rpac\Role;

$adminRole = Role::create([
    'name' => 'Admin',
    'slug' => 'admin',
    'description' => '', // optional
]);

附加和分离角色

这是标准的。在 UserRole 模型之间有一个 BelongsToMany 关系。

use App\User;

$user = User::find($id);
$user->roles()->attach($adminRole); // you can pass whole object, or just an id
$user->roles()->detach($adminRole); // in case you want to detach role
$user->roles()->detach(); // in case you want to detach all roles

检查角色

现在您可以检查用户是否有所需的角色。

if ($user->is('admin')) { // pass role slug here
    // ...
}

您也可以这样做

if ($user->isAdmin()) {
    //
}

当然,还有检查多个角色的方法

if ($user->is(['admin', 'moderator'])) { 
    /*
    | It is same as:
    | $user->isOr(['admin', 'moderator'])
    */

    // if user has at least one role
}

if ($user->is(['admin', 'moderator'], true)) {
    /*
    | Or alternatively:
    | $user->isAnd(['admin', 'moderator'])
    */

    // if user has all roles
}

创建权限

由于有 Permission 模型,这非常简单。

use Trunow\Rpac\Permission;

$createPostPermission = Permission::create([
    'name' => 'Create posts',
    'entity' => 'App\Post',
    'action' => 'create',
]);

附加和分离权限

您可以将权限附加到角色(当然也可以分离它们)。

use Trunow\Rpac\Role;

$role = Role::find($roleId);
$role->permissions()->attach($createPostPermission); // permission attached to a role
$role->permissions()->detach($createPostPermission); // in case you want to detach permission
$role->permissions()->detach(); // in case you want to detach all permissions

检查权限

待办事项

实体检查

假设您有一篇文章并想编辑它。

use App\Article;
use Trunow\Rpac\Permission;

$editArticlesPermission = Permission::create([
    'name' => 'Edit articles',
    'entity' => 'App\Article',
    'action' => 'edit',
]);

$user->roles()->first()->permissions()->attach($editArticlesPermission);

$article = Article::find(1);

if ($user->can('edit', $article)) { 
    //
}

Blade 扩展

有四个 Blade 扩展。基本上,它们是经典 if 语句的替代品。

@role('admin') // @if(Auth::check() && Auth::user()->is('admin'))
    // user is admin
@endrole

中间件

此包附带 VerifyRole 中间件。您可以轻松保护您的路由。

$router->get('/example', [
    'as' => 'example',
    'middleware' => 'role:admin,manager',
    'uses' => 'ExampleController@index',
]);

配置文件

您可以更改模型连接、模型路径,还有一个方便的模拟功能。更多信息请参阅配置文件。

许可

此包是免费软件,根据 MIT 许可协议分发。