<?php
/**
* super-admin and guest are only logical consideration
* please define the access for guest and super-admin in database
* see controllerbase.php: initiate session for GUEST account
*/
Class Privileges Extends EntityBase implements Observer {
private $id;
private $group;
private $access;
private $permission;
public function __construct(){
//PHYSIC TABLE DEFINITION for Privileges
//-----------------------------------------------------
$tablename = 'groupprivileges';
$field_id = 'access_id';
$field_group = 'group_id';
$field_access = 'allowed_access';
$field_permission = 'permission';
//-----------------------------------------------------
$rawfields = array('id'=>$field_id,'group'=>$field_group,'access'=>$field_access,'permission'=>$field_permission);
$group = new Group();
$condition = $group->getAbsField('id').' = '.$tablename.'.'.$field_group;
parent::__construct(array($tablename,$group->getPrimaryTable()),$field_id,
array($group->getAbsField('name'),$field_access,$field_permission),$rawfields,$condition);
}
public function notifyRemove($obj,$id){
if($obj instanceOf Group){
$this->removeWith($this->getField('group'),$id);
}
}
//this is only wrapper to provide single object
//just copy paste for all children
public function selectByID($id){
$tempobj = parent::selectByID($id);
if($tempobj){
foreach($this->rawfields as $key => $value){
$this->$key = $tempobj->$value;
}
}
}
public function getID(){
return $this->id;
}
public function getGroup(){
return $this->group;
}
public function getPermission(){
return $this->permission;
}
public function getAccess(){
return $this->access;
}
//return simple array of allowed access from array of standard object that pass the condition
public function getAuth($group_id){
$condition = $this->getField('group').'='.$group_id;
//uncomment this if you want to have SUPERADMIN privileges. see includes/startup.php for group id definition
/*
if($group_id==SUPERADMIN){
$d = opendir(site_path.'controllers') or die($php_errormsg);
$array = array();
while (false !== ($f = readdir($d))){
if(($f!='..')&&($f[0]!='.')){
$mystring = str_replace('.php','',$f);
$array[$mystring] = 'Write';
}
}
return $array;
}*/
$sql = 'SELECT '.$this->getField('access').','.$this->getField('permission').' FROM '.$this->getPrimaryTable().' WHERE '.$condition;
$temp = $this->dba->query($sql);
$result = array();
for($i=0;$i<sizeof($temp);$i++){
$result[$temp[$i]->allowed_access] = $temp[$i]->permission;
}
return $result;
}
//not tested
//custom selectRemaining. it reads a directory then substract the content
public function selectRemaining($id){
$d = opendir(site_path.'controllers') or die($php_errormsg);
$array = array();
while (false !== ($f = readdir($d))){
if(($f!='..')&&($f[0]!='.')){
$mystring = str_replace('.php','',$f);
$array[$mystring] = $mystring;
}
}
//subtract from database
$field = $this->getField('access');
$table = $this->getPrimaryTable();
$where = $this->getField('group').'='.$id;
$sql = "SELECT ".$field." FROM ".$table." WHERE ".$where;
$result = $this->dba->query($sql);
for($i=0;$i<sizeof($result);$i++){
unset($array[$result[$i]->$field]);
}
return $array;
}
public function __destruct(){
parent::__destruct();
}
}
?>