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
return [
# ... other providers
zennit\ABAC\Providers\AbacServiceProvider::class,
];
use zennit\ABAC\Database\Seeders\DatabaseSeeder as ABACDatabaseSeeder;
class DatabaseSeeder extends Seeder
{
public function run()
{
// Other seeders...
$this->call(ABACDatabaseSeeder::class, []);
}
}
use zennit\ABAC\DTO\AccessContext;
use zennit\ABAC\Facades\Abac;
# Using the Facade
$context = new AccessContext(
method: $method,
subject: (new AbacAttributeLoader())->loadAllSubjectAttributes(get_class('App\Models\User')),
object: (new AbacAttributeLoader())->loadAllSubjectAttributes(get_class('App\Models\Post')),
object_type: get_class('App\Models\User'),
subject_type: get_class('App\Models\Post'),
);
# AbacCheck returns boolean
$hasAccess = Abac::can($context);
# Using the helper functions
$hasAccess = abacPolicy()->can($context);
# Cache management helper
abacCache()->warm('posts'); # Warm cache for posts
abacCache()->invalidate(); # Invalidate all cache
abacCache()->clear(); # Clear all cache
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use zennit\ABAC\Facades\Abac;
class AbacRoutesServiceProvider extends ServiceProvider
{
public function boot()
{
// Register ABAC routes with custom middleware
Abac::routes([
'middleware' => ['api', 'auth'], // Add your middleware here
'prefix' => 'abac' // Optional: customize the route prefix
]);
// Load ABAC macros so you have access to $request->abac()
Abac::macros();
}
}