<?phpnamespaceTaeluf\Permissions;
classArrimplementsIPerm{
protected$file = null;
publicfunction__construct($srcFile){
$this->file = $srcFile;
$this->data = $this->loadData();
}
publicfunctioncan($performerUUID, $action, $consenterUUID){
// We get consent first, because that's what matters most$consenterActions = $this->data[$consenterUUID];
if (($consenterActions[$action]['consent']??null)!==true){
returnfalse;
}
// Now we check if performer is capable of said action// which is LESS important than consent (but still very important as far as code goes)$perfActions = $this->data[$performerUUID];
if (($perfActions[$action]['perform']??null)!==true){
returnfalse;
}
returntrue;
}
publicfunctiongetPermissions($uuid, $type=null){
$perms = $this->data[$uuid];
//figure out the format we want & return it
}
publicfunctionaddPerform($uuid, $action){
$this->data[$uuid][$action]['perform'] = true;
}
publicfunctionaddConsent($uuid, $action){
$this->data[$uuid][$action]['consent'] = true;
}
publicfunctionremovePerform($uuid, $action){
unset($this->data[$uuid][$action]['perform']);
}
publicfunctionremoveConsent($uuid, $action){
unset($this->data[$uuid][$action]['consent']);
}
publicfunctionstoreChanges(){
$file = $this->file;
$ext = pathinfo($file,PATHINFO_EXTENSION);
if ($ext=='php'){
$data = var_export($this->data,true);
$put = file_put_contents($file,$data);
return$put;
} elseif ($ext=='json'){
$data = json_encode($this->data);
$put = file_put_contents($file,$data);
return$put;
}
}
protectedfunctionloadData(){
$file = $this->file;
$ext = pathinfo($file,PATHINFO_EXTENSION);
if ($ext=='php'){
$data = include($file);
if (!is_array($data)){
thrownew BaseException("Your PHP file '{$file}' must return an array.");
}
return$data;
} elseif ($ext=='json'){
$content = file_get_contents($file);
$data = json_decode($content,true);
return$data;
}
thrownew BaseException("File must be 'php' or 'json', but '{$ext}' was given");
}
}