Skip to content

Repository files navigation

[CraftCamp] php-abac

Attribute-Based Access Control implementation library

Latest Stable VersionLatest Unstable VersionBuild StatusCode CoverageScrutinizer Code QualityTotal DownloadsLicense

Introduction

This library is meant to implement the concept of ABAC in your PHP applications.

The concept is to manage access control using attributes : from users, from resources and environment.

It allows us to define rules based on the properties of the user object and optionally the accessed object.

These rules will be checked in your application to determine if an user is allowed to perform an action.

The following links explain what ABAC is :

Installation

Using composer :

composer require craftcamp/php-abac

Then you will have to configure the attributes and the rules of your application.

For more details about this, please refer to the dedicated documentation

Documentation

Usage Examples

Example with only user attributes defined in the rule

We have in this example a single object, representing the current user.

This object have properties, with getter methods to access the values.

For example, we can code :

<?phpusePhpAbac\AbacFactory;
class User{
protected$id;
protected$isBanned;
publicfunctiongetId() {
return$this->id;
}
publicfunctionsetIsBanned($isBanned) {
$this->isBanned = $isBanned;
return$this;
}
publicfunctiongetIsBanned() {
return$this->isBanned;
}
}
$user = newUser();
$user->setIsBanned(true);
$abac = AbacFactory::getAbac([
'policy_rule_configuration.yml'
]);
$abac->enforce('create-group', $user);

The attributes checked by the rule can be :

User
isBanned = false

Example with both user and object attributes

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

The checked attributes can be :

UserGroup
isBanned = 0isActive = 1
isPublic = 1

Example with dynamic attributes

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

Example with referenced attributes

The configuration shall be :

attributes:
group:
class: MyApp\Model\Grouptype: resourcefields:
author.id:
name: Author IDapp_user:
class: MyApp\Model\Usertype: userfields:
id:
name: User IDrules:
remove-group:
attributes:
app_user.id:
comparison: objectcomparison_type: isFieldEqualvalue: group.author.id

And then the code :

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

Example with cache

$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
]);

Example with multiple rules (ruleSet) for an unique rule. Each rule are tested and the treatment stop when the first rule of the ruleSet allow access

The configuration shall be (alcoolaw.yml):

attributes:
main_user:
class: PhpAbac\Example\Usertype: userfields:
age:
name: Agecountry:
name: Code ISO du paysrules:
alcoollaw:
-
attributes:
main_user.age:
comparison_type: numericcomparison: isGreaterThanvalue: 18main_user.country:
comparison_type: stringcomparison: isEqualvalue: FR
-
attributes:
main_user.age:
comparison_type: numericcomparison: isGreaterThanvalue: 21main_user.country:
comparison_type: stringcomparison: isNotEqualvalue: FR

And then the code :

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

Example with rules root directory passed to Abac class. This feature allow to give a policy definition rules directory path directly to the Abac class without adding to all files :

Considering we have 3 yaml files :

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

The php code can be :

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

Contribute

If you want to contribute, don't hesitate to fork the library and submit Pull Requests.

You can also report issues, suggest enhancements, feel free to give advices and your feedback about this library.

It's not finished yet, there's still a lot of features to implement to make it better. If you want to be a part of this library improvement, let us know !

See also

About

Attributes Based Access Control library

Resources

Stars

98 stars

Watchers

15 watching

Forks

Releases

Packages

Used by

Contributors

Languages