1. This library should be loaded from namespace context. You can load it by
using composer. If you are using lower versions of php which does not support
namespace you need to replace namespaces with "include" statements.
2. Find out who is visiting your website. Each and every user even a guest should
have an ID that will help you differenciate one from another.
3. Every user has at least one role and as many roles you desire is possible.
Roles are different according to each site. It can be as simple as "Administrator,
user" or it can be as complex as roles are defined by administrator on fly. But
it makes no difference because you store these in "roles" table and when you find
out what is your users ID it's as easy as a simple database query. Selected the
roles from database, define them for library by "setRoles" function:
$roles = array(); // here assign what you selected from "roles" table
$dyacl = new DyACL();
$dyacl->setRoles($roles);
if there is an special role that you desire to be added on fly add it by
"setRole" function. For example you have a role named "admin" and you want to
assign it to this user:
$dyacl->setRole("admin");
It's better to use role_id if you need to load further roles later.
4. When you have a list of current user's roles you can select related "Rules"
from it's table. After that define these rules for the library by using
"setRules" function.
$rules = array();// here assign the rules you selected from database.
$dyacl->setRules($rules);
The library will define every resource that is mentioned by rules for itself so
you do not need to define any resource explicitly. Access to a resource that is
unknown for the library would be denied by default. In case you need to define
any rule on fly go on and use "setRule" function. Each "rule" consists of a
"resource" name, a "privilege", for which the possible values are 'allow' and
'deny', and finally an "action" which is by default "all" which means user is
allowed to do whatever is possible. Other possible actions are Create, Read,
Update and Delete.
For example you want to deny any access to a folder named "secrets":
$dyacl->setRule("secrets", DyACL::DENY);
or you need to allow just viewing:
$dyacl->setRule("secrets", DyACL::ALLOW, DyACL::ACTION_READ);
Remeber that DyACL::DENY is equal to "deny" and DyACL::ALLOW is equal to "allow".
5. Finally checking whether user has access to a resource or not is possible by
"isAllowed" function:
For example after all this you want to check whether user is allowed access to
the folder named "secrets" or not:
if ($dyacl->isAllowed("secrets") {
echo "Yes, you are allowed";
}
else {
echo "Access Denied!";
}
OR maybe you want to check whether the user is allowed to delete the folder "secrets":
if ($dyacl->isAllowed("secrets", DyACL::ACTION_DELETE) {
echo "Yes, you are allowed";
}
else {
echo "Access Denied!";
}
As I said previously there is a class extended from DyAcl class, named DyAclPDO, which
uses PDO to load data from database:
$dyAcl = new DyAclPDO("mysql:host={$sampleHost};dbname={$sampleDbName};", $sampleUsername, $samplePassword);
$sampleUserId = 1;
$dyAcl->prepareAcl($sampleUserId); //this function loads users' roles and related rules
//from database
$dyAcl->isAllowed('secret');
Instead of directly constructing DyAcl class it's better to use it's factory.
$dyAcl = DyAclFactory::newAcl();
or
$dyAcl = DyAclFactory::newDyAclPDO($pdo, $configFile = null)