PHP code example of nick-scalewest / laravel-authz
1. Go to this page and download the library: Download nick-scalewest/laravel-authz 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/ */
use Enforcer;
// adds permissions to a user
Enforcer::addPermissionForUser('eve', 'articles', 'read');
// adds a role for a user.
Enforcer::addRoleForUser('eve', 'writer');
// adds permissions to a rule
Enforcer::addPolicy('writer', 'articles','edit');
// to check if a user has permission
if (Enforcer::enforce("eve", "articles", "edit")) {
// permit eve to edit articles
} else {
// deny the request, show an error
}
Enforcer::getAllRoles(); // ['writer', 'reader']
Enforcer::getPolicy();
Enforcer::getRolesForUser('eve'); // ['writer']
Enforcer::getUsersForRole('writer'); // ['eve']
Enforcer::hasRoleForUser('eve', 'writer'); // true or false
Enforcer::addRoleForUser('eve', 'writer');
// to user
Enforcer::addPermissionForUser('eve', 'articles', 'read');
// to role
Enforcer::addPermissionForUser('writer', 'articles','edit');
Enforcer::deleteRoleForUser('eve', 'writer');
Enforcer::deleteRolesForUser('eve');
Enforcer::deleteRole('writer');
Enforcer::deletePermission('articles', 'read'); // returns false if the permission does not exist (aka not affected).