Skip to content

Instantly share code, notes, and snippets.

@Samshal
Last active May 31, 2016 07:22
Show Gist options
  • Save Samshal/06dd964a0984b06c550476fb75edca1c to your computer and use it in GitHub Desktop.
Save Samshal/06dd964a0984b06c550476fb75edca1c to your computer and use it in GitHub Desktop.
<?php
require "vendor/autoload.php";
$acl = new Samshal\Acl\Acl();
/**
* Create the roles and add them to the registry
*/
$acl->addRole("doctor");
$acl->addRole("accountant");
$acl->addRole("labTechnician");
$acl->addRole("pharmacist");
/**
* Create the resources and add them to the registry
*/
$acl->addResource("patientMedicalHistory");
$acl->addResource("patientFinancialHistory");
$acl->addResource("patientDiagnosis");
$acl->addResource("patientLabResults");
/**
* Create the permissions and add them to the registry
*/
$acl->addPermission("view");
$acl->addPermission("delete");
$acl->addPermission("edit");
$acl->addPermission("create");
/**
* give ROLES the PERMISSION to utilise a RESOURCE
*/
$acl->doctor->can->view("patientMedicalHistory");
$acl->doctor->cannot->edit("patientFinancialHistory");
$acl->doctor->cannot->edit("patientLabResults");
$acl->doctor->cannot->view("patientFinancialHistory");
/**
* use the `can` keyword to determine if a ROLE has PERMISSIONS to utilise a RESOURCE
*/
var_dump($acl->can->doctor->edit("patientLabResults"));
/**
* Another way to assign PERMISSIONS to ROLES
*/
$acl->allow("doctor", "view", "patientMedicalHistory");
var_dump($acl->can->doctor->view("patientMedicalHistory"));
<?php
require "vendor/autoload.php";
session_start();
$acl = new Samshal\Acl\Acl();
$roles = ["classTeacher", "subjectTeacher", "janitor", "principal"]; //All roles. you can get this from db or some persistent storage
$resox = ["score", "student", "staff", "payroll"]; //All resources. you can get this from db or some persistent storage
$perms = ["view", "edit", "delete", "create"]; //All permissions. you can get this from db or some persistent storage
/**
* Loop through objects to add them to the registry
*/
foreach ($roles as $role)
{
$acl->addRole($role);
}
foreach ($resox as $resos)
{
$acl->addResource($resos);
}
foreach ($perms as $perm)
{
$acl->addPermission($perm);
}
//define permissions on resources and assign them to roles
$acl->classTeacher->cannot->view("score");
$acl->classTeacher->cannot->edit("score");
$acl->classTeacher->cannot->delete("score");
$acl->classTeacher->cannot->create("score");
$acl->subjectTeacher->can->view("score");
$acl->subjectTeacher->can->edit("score");
$acl->subjectTeacher->can->delete("score");
$acl->subjectTeacher->can->create("score");
$acl->principal->can->view("score");
$acl->principal->can->edit("score");
$acl->principal->can->delete("score");
$acl->principal->can->create("score");
$acl->principal->can->view("payroll");
$acl->principal->can->edit("payroll");
$acl->principal->can->delete("payroll");
$acl->principal->can->create("payroll");
$acl->principal->can->view("staff");
$acl->principal->can->edit("staff");
$acl->principal->can->delete("staff");
$acl->principal->can->create("staff");
//store a serialized acl in session (you could use a db)
$_SESSION["acl"] = serialize($acl);
<?php
require "vendor/autoload.php";
session_start();
//unserialize an acl
$acl = unserialize($_SESSION["acl"]);
//utilise the acl to view permissions LIKE A PRO
var_dump($acl->can->subjectTeacher->edit("score"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment