mrjulio/rapture-acl

Rapture ACL 组件

v1.0.1 2017-11-18 18:56 UTC

This package is not auto-updated.

Last update: 2024-09-21 14:58:06 UTC


README

PhpVersion License

关于

Rapture-ACL 是一个简单的 PHP 授权层库。

简单概念: 请求者 想在 资源 上执行 操作

要求

  • php v5.4

安装

composer require mrjulio/rapture-acl

快速开始

请求者 vs 资源

一个用户(请求者)想要编辑(操作)一个模型(资源)。

class User implements \Rapture\Acl\Definition\RequesterInterface
{
    /**
     * @return array
     */
    public function requesterGroups()
    {
        return ['admin'];
    }

    /**
     * @return int
     */
    public function requesterId()
    {
        return $this->id;
    }
}
class Model implements \Rapture\Acl\Definition\ResourceInterface
{
    /**
     * @return array
     */
    public function resourceGroups()
    {
        return ['models'];
    }

    /**
     * @return string|int
     */
    public function resourceId()
    {
        return $this->getId();
    }

    /**
     * @return int
     */
    public function ownerId()
    {
        return $this->getCreatedBy();
    }
}

可用操作

class Acl
{
    // resource, requester, owner
    const ANY   = '*';
    const OWNER = '@';

    // access
    const ALLOW = true;
    const DENY = false;

    // actions
    const VIEW = 2;         // 2^1
    const SEARCH = 4;       // 2^2
    const ADD = 8;          // 2^3
    const EDIT = 16;        // 2^4
    const DELETE = 32;      // 2^5
    const UNDO = 64;        // 2^6
    const RENAME = 128;     // 2^7
    const DESTROY = 256;    // 2^8
    const ACTION1 = 512;    // 2^9
    const ACTION2 = 1024;   // 2^10
    const ACTION3 = 2048;   // 2^11
    const ACTION4 = 4096;   // 2^12
    const ACTION5 = 8192;   // 2^13

    const ALL = 16382;
}

用法

$adapter = new \Rapture\Acl\Adapter\Php();
$adapter->setDefault(\Rapture\Acl\Acl::DENY);

$rules = [
    //  requester - resource - actions (optional) - allow (optional)

    // allow admins to all resources to ALL actions
    ['admin', Acl::ANY, Acl::ALL, Acl::ALLOW],

    // allow guest to view specific resources
    ['guest', 'resource-x', Acl::VIEW, Acl::ALLOW],
];
$adapter->addRules($rules);

$adapter->hasAccess(new Requester, new Resource, Acl::EDIT);

关于

作者

Iulian N. rapture@iuliann.ro

测试

cd ./test && phpunit

致谢

许可证

Rapture PHP ACL 采用 MIT 许可证授权 - 有关详细信息,请参阅 LICENSE 文件。