yuriy-martini/laravel-authorization

此包的最新版本(v0.1.1)没有可用的许可证信息。

v0.1.1 2023-10-16 12:56 UTC

This package is not auto-updated.

Last update: 2024-09-17 17:58:17 UTC


README

Latest Version MIT License

Laravel 授权

https://laravel.net.cn/docs/master/authorization#introduction

安装

composer require yuriy-martini/laravel-authorization

使用

https://laravel.net.cn/docs/master/authorization#authorizing-actions-using-policies

  1. 可授权的(用户)
  2. 配置
  3. 策略

U/ Authorizable

U/ A/ 示例

-+ app/Models/User.php:

<?php

namespace App\Models;

class User
    implements \YuriyMartini\Laravel\Authorization\Contracts\Authorizable
{
    use \YuriyMartini\Laravel\Authorization\Concerns\Authorizable;
}

U/ 配置

U/ C/ 默认值

php artisan vendor:publish --tag=authorization-defaults-config

U/ C/ 模型

php artisan vendor:publish --tag=authorization-models-config

U/ C/ M/ 示例

+ config/authorization/models.php:

<?php

return [
    \App\Models\User::class => [
        \App\Models\User::class => [
            YuriyMartini\Laravel\Authorization\Enums\Permission::view,
        ],
    ],
];

U/ 模型

U/ M/ 示例

+ app/Models/User/Authorizations.php:

<?php

namespace App\Models\User;

trait Authorizations
{
    public function isViewable(Authorizable $user): bool
    {
        return true; // here: ur BL
    }

    public function isUpdatable(Authorizable $user): bool
    {
        return true; // here: ur BL
    }

    public function isDeletable(Authorizable $user): bool
    {
        return true; // here: ur BL
    }

    public function isRestorable(Authorizable $user): bool
    {
        return true; // here: ur BL
    }

    public function isForceDeletable(Authorizable $user): bool
    {
        return true; // here: ur BL
    }
}

-+ app/Models/User.php:

<?php

namespace App\Models;

class User
    implements \YuriyMartini\Laravel\Authorization\Contracts\Model
{
    use \App\Models\User\Authorizations;
}

U/ 策略

https://laravel.net.cn/docs/master/authorization#generating-policies

U/ P/ 示例

-+ app/Policies/UserPolicy.php:

<?php
 
namespace App\Policies;
 
class UserPolicy
    extends \YuriyMartini\Laravel\Authorization\Policy
{
    protected static function getModel(): string
    {
        return \App\Models\User::class;
    }
}

变更日志

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

测试

composer test

许可证

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