1. Go to this page and download the library: Download zennit/abac library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
zennit / abac example snippets
use zennit\ABAC\DTO\AccessContext;
use zennit\ABAC\Facades\Abac;
// Create an access context
$context = new AccessContext(
subject: $organization, // Any object with an ID
resource: 'posts',
operation: PermissionOperations::UPDATE->value,
resourceIds: [$postId]
);
// Check access
if (Abac::can($context)) {
// Allow action
}
// Get detailed evaluation result
$result = Abac::evaluate($context);
use zennit\ABAC\Models\Policy;
use zennit\ABAC\Enums\PolicyOperators;
// Create a policy
$policy = Policy::create([
'name' => 'Edit Own Posts',
'description' => 'Allow users to edit their own posts',
'resource' => 'posts',
'operation' => PermissionOperations::UPDATE->value
]);
// Add conditions
$policy->conditions()->create([
'operator' => PolicyOperators::EQUALS,
'attributes' => [
[
'attribute_name' => 'owner_id',
'attribute_value' => '$subject.id'
]
]
]);
use zennit\ABAC\Models\UserAttribute;
// Add attributes to a subject
UserAttribute::create([
'subject_type' => get_class($subject),
'subject_id' => $subject->id,
'attribute_name' => 'role',
'attribute_value' => 'admin'
]);
use zennit\ABAC\Models\ResourceAttribute;
// Add attributes to a resource
ResourceAttribute::create([
'resource' => 'posts',
'attribute_name' => 'status',
'attribute_value' => 'published'
]);
use zennit\ABAC\Jobs\BatchEvaluateAccessJob;
// Evaluate multiple contexts at once
BatchEvaluateAccessJob::dispatch($contexts, parallel: true);
use zennit\ABAC\Jobs\WarmPolicyCacheJob;
// Warm cache for all policies
WarmPolicyCacheJob::dispatch();
// Warm cache for specific resource
WarmPolicyCacheJob::dispatch('posts');