craftcamp/php-abac

用于在PHP应用程序中实现基于属性的访问控制(ABAC)的库

3.0.0 2018-10-13 16:26 UTC

This package is auto-updated.

Last update: 2024-08-26 01:43:01 UTC


README

基于属性的访问控制实现库

Latest Stable Version Latest Unstable Version Build Status Code Coverage Scrutinizer Code Quality Total Downloads License

介绍

这个库旨在在您的PHP应用程序中实现ABAC的概念。

该概念是使用属性来管理访问控制:来自用户、来自资源和环境。

它允许我们根据用户对象和可选的访问对象的属性来定义规则。

这些规则将在您的应用程序中进行检查,以确定用户是否允许执行操作。

以下链接解释了ABAC是什么

安装

使用composer

composer require craftcamp/php-abac

然后您需要配置应用程序的属性和规则。

有关详细信息,请参阅专用文档

文档

使用示例

仅在规则中定义了用户属性的示例

在这个例子中,我们有一个对象,代表当前用户。

此对象有属性,有获取器方法来访问值。

例如,我们可以编写

<?php

use PhpAbac\AbacFactory;

class User{
    protected $id;

    protected $isBanned;

    public function getId() {
        return $this->id;
    }

    public function setIsBanned($isBanned) {
        $this->isBanned = $isBanned;

        return $this;
    }

    public function getIsBanned() {
        return $this->isBanned;
    }
}

$user = new User();
$user->setIsBanned(true);

$abac = AbacFactory::getAbac([
    'policy_rule_configuration.yml'
]);
$abac->enforce('create-group', $user);

规则检查的属性可以是

具有用户和对象属性的示例

use PhpAbac\AbacFactory;

$abac = AbacFactory::getAbac([
    'policy_rule_configuration.yml'
]);
$check = $abac->enforce('read-public-group', $user, $group);

检查的属性可以是

具有动态属性的示例

<?php

use PhpAbac\AbacFactory;

$abac = AbacFactory::getAbac([
    'policy_rule_configuration.yml'
]);
$check = $abac->enforce('edit-group', $user, $group, [
    'dynamic-attributes' => [
        'group-owner' => $user->getId()
    ]
]);

具有引用属性的示例

配置应为

attributes:
    group:
        class: MyApp\Model\Group
        type: resource
        fields:
            author.id:
                name: Author ID
    app_user:
        class: MyApp\Model\User
        type: user
        fields:
            id:
                name: User ID

rules:
    remove-group:
        attributes:
            app_user.id:
                comparison: object
                comparison_type: isFieldEqual
                value: group.author.id

然后代码

<?php

use PhpAbac\AbacFactory;

$abac = AbacFactory::getAbac([
    'policy_rule_configuration.yml'
]);
$check = $abac->enforce('remove-group', $user, $group);

具有缓存的示例

$check = $abac->enforce('edit-group', $user, $group, [
    'cache_result' => true,
    'cache_ttl' => 3600, // Time To Live in seconds
    'cache_driver' => 'memory' // memory is the default driver, you can avoid this option
]);

具有单个规则(ruleSet)的多个规则示例。每个规则都会被测试,当ruleSet中的第一个规则允许访问时,处理停止

配置应为(alcoolaw.yml)

attributes:
    main_user:
        class: PhpAbac\Example\User
        type: user
        fields:
            age:
                name: Age
            country:
                name: Code ISO du pays
rules:
    alcoollaw:
        -
            attributes:
                main_user.age:
                    comparison_type: numeric
                    comparison: isGreaterThan
                    value: 18
                main_user.country:
                    comparison_type: string
                    comparison: isEqual
                    value: FR
        -
            attributes:
                main_user.age:
                    comparison_type: numeric
                    comparison: isGreaterThan
                    value: 21
                main_user.country:
                    comparison_type: string
                    comparison: isNotEqual
                    value: FR

然后代码

<?php

use PhpAbac\AbacFactory;

$abac = AbacFactory::getAbac([
    'alcoollaw.yml'
]);
$check = $abac->enforce('alcoollaw', $user);

将规则根目录传递给Abac类的示例。此功能允许将策略定义规则目录路径直接传递给Abac类,而无需添加到所有文件

假设我们有3个yaml文件

  • rest/conf/policy/user_def.yml
  • rest/conf/policy/gunlaw.yml

PHP代码可以是

<?php

use PhpAbac\AbacFactory;

$abac = AbacFactory::getAbac([
    'user_def.yml',
    'gunlaw.yml',
],[],'rest/conf/policy/');
$check = $abac->enforce('gunlaw', $user);
 

贡献

如果您想做出贡献,请不要犹豫,将该库分叉并提交Pull Requests。

您还可以报告问题、提出改进建议、自由提供建议和关于此库的反馈。

它还没有完成,还有许多功能要实现以使其更好。如果您想成为此库改进的一部分,请告诉我们!

另请参阅