PHP code example of sunsgne / casbin

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

    

sunsgne / casbin example snippets


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

use sunsgne\Auth;

// adds permissions to a user
Auth::addPermissionForUser('user:1', '/api/backend/cap', 'get');
// adds a role for a user.
Auth::addRoleForUser('user:1', 'role:1');
// adds permissions to a rule
Auth::addPolicy('role:1', '/api/backend/login','post');

if (Auth::enforce("user:1", "/api/backend/login", "post")) {
    echo '恭喜你!通过权限认证';
} else {
    echo '对不起,您没有该资源访问权限';
}

$r =  Auth::getRolesForUser('uuid:1');
$u =  Auth::getUsersForRole('roleId:1');
$a =  Auth::enforce("uuid:1" , "roleId:1" ,"post");

 array:1 [
  0 => "roleId:1"
]
 array:1 [
  0 => "uuid:1"
]
true