PHP code example of casbin / webman-permission

1. Go to this page and download the library: Download casbin/webman-permission 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/ */

    

casbin / webman-permission example snippets


$builder = new \DI\ContainerBuilder();
$builder->addDefinitions(config('dependence', []));
$builder->useAutowiring(true);
return $builder->build();

use Casbin\WebmanPermission\Permission;

// Add permissions to a user
Permission::addPermissionForUser('eve', 'articles', 'read');

// Add a role for a user
Permission::addRoleForUser('eve', 'writer');

// Add permissions to a role
Permission::addPolicy('writer', 'articles', 'edit');

if (\Casbin\WebmanPermission\Permission::enforce('eve', 'articles', 'edit')) {
    echo 'Congratulations! Permission granted.';
} else {
    echo 'Sorry, you do not have access to this resource.';
}

$permission = \Casbin\WebmanPermission\Permission::driver('restful_conf');

// Add permissions to a user
$permission->addPermissionForUser('eve', 'articles', 'read');

// Add a role for a user
$permission->addRoleForUser('eve', 'writer');

// Add permissions to a role
$permission->addPolicy('writer', 'articles', 'edit');

// Check permissions
if ($permission->enforce('eve', 'articles', 'edit')) {
    echo 'Congratulations! Permission granted.';
} else {
    echo 'Sorry, you do not have access to this resource.';
}

if (is_null(static::$_manager)) {
    static::$_manager = new Enforcer($model, Container::get($config['adapter']), false);
}

if (is_null(static::$_manager)) {
    if ($config['adapter'] == DatabaseAdapter::class) {
        $_model = new RuleModel();
    } elseif ($config['adapter'] == LaravelDatabaseAdapter::class) {
        $_model = new LaravelRuleModel();
    }
    static::$_manager = new Enforcer($model, new $config['adapter']($_model), false);
}
bash
# Restart in foreground
php start.php restart

# Or restart in daemon mode
php start.php restart -d

tests/
├── Adapter.php                    # Basic adapter tests
├── PermissionTest.php            # Permission class tests
├── AdapterTest.php               # Detailed adapter tests
├── EdgeCaseTest.php              # Edge case tests
├── IntegrationTest.php           # Integration tests
├── LaravelDatabase/
│   ├── LaravelDatabaseAdapterTest.php
│   └── TestCase.php
├── ThinkphpDatabase/
│   ├── DatabaseAdapterTest.php
│   └── TestCase.php
└── config/
    └── plugin/
        └── casbin/
            └── webman-permission/
                └── permission.php
bash
composer remove php-di/php-di