/** 
* 对资源进行acl校验 
* 
* @param string $rsid 资源标识 
* @param array $user 特定用户,不指定则校验当前用户 
* 
* @return boolean 
*/ 
function aclVerity($rsid ,array $user = null){ 
if (empty($rsid)) return false; 
if (!CoreApp::$defaultAcl) { 
CoreApp::$defaultAcl = new AclFlat(); 
} 
$rsRow = aclGetResource($rsid); 
// 未定义资源的缺省访问策略 
if (!$rsRow) return false; 
CoreApp::writeLog($rsRow,'test'); 
/* 
* 校验步骤如下: 
* 
* 1. 先校验 资源本身 access 属性 
* EVERYONE => true,NOBODY => false * 其它的属性在下面继续校验 
* 2. 从 session(或者 用户session表)中获取角色id集合 
* 3. 如果 用户拥有角色 则 HAS_ROLE => true , NO_ROLE => false;反之亦然 
* 4. 如果资源 access == ALLOCATE_ROLES 
* 1. 从缓存(或者 $tbRefResourcesRoles)中获取 资源对应的角色id集合 
* 2. 将用户拥有的角色id集合 与 资源对应的角色id集合求交集 
* 3. 存在交集 => true;否则 => false 
*/ 
$rsRow['access'] = AclBase::formatAccessValue($rsRow['access']); 
// 允许任何人访问 
if (AclBase::EVERYONE == $rsRow['access']) return true; 
// 不允许任何人访问 
if (AclBase::NOBODY == $rsRow['access']) return false; 
// 获取用户信息 
if (empty($user)) $user = isset($_SESSION['SI-SysUser']) ? $_SESSION['SI-SysUser'] : null; 
// 用户未登录,则当成无访问权限 
if (empty($user)) return false; 
$user['roles'] = empty($user['roles']) ? null : normalize($user['roles'],';'); 
$userHasRoles = !empty($user['roles']); 
/** 
* 允许 不带有角色的用户访问 
*/ 
if (AclBase::NO_ROLE == $rsRow['access']) return $userHasRoles ? false : true; 
/** 
* 允许 带有角色的用户访问 
*/ 
if (AclBase::HAS_ROLE == $rsRow['access']) return $userHasRoles ? true : false; 
// --- 对用户进行 资源 <-> 角色 校验 
if ($userHasRoles){ 
foreach ($user['roles'] as $role_id){ 
if ( aclGetRefResourcesRoles($rsid,$role_id) ) 
return true; 
} 
dump($user); 
} 
return false; 
} 
/** 
* 重新生成 角色资源访问控制表 
* 
* @param string $actTable ACL表名称 
* @param boolean $return 是否返回重新生成的列表 
* 
* @return mixed 
*/ 
function aclRebuildACT($actTable ,$return = false){ 
if (empty($actTable)) return false; 
global $globalConf; 
$rst = null; 
$cacheId = null; 
switch($actTable){ 
case CoreApp::$defaultAcl->tbResources: 
$cacheId = 'acl-resources'; 
$rst = SingleTableCRUD::findAll(CoreApp::$defaultAcl->tbResources); 
// 转成 哈希表结构 
if ($rst){ 
$rst = array_to_hashmap($rst,'rsid'); 
} 
break; 
case CoreApp::$defaultAcl->tbRoles: 
$cacheId = 'acl-roles'; 
$rst = SingleTableCRUD::findAll(CoreApp::$defaultAcl->tbRoles); 
// 转成 哈希表结构 
if ($rst){ 
$rst = array_to_hashmap($rst,'id'); 
} 
break; 
case CoreApp::$defaultAcl->tbRefResourcesRoles: 
$cacheId = 'acl-roles_has_resources'; 
$rst = SingleTableCRUD::findAll(CoreApp::$defaultAcl->tbRefResourcesRoles); 
if ($rst){ 
$_ = array(); 
foreach ($rst as $row) { 
$ref_id = "{$row['rsid']}<-|->{$row['role_id']}"; 
$_[$ref_id] = $row; 
} 
unset($rst); 
$rst = $_; 
} 
break; 
} 
if ($cacheId) 
writeCache($globalConf['runtime']['cacheDir'] ,$cacheId ,$rst ,true); 
if ($return) return $rst; 
} 
/** 
* 获取 角色资源访问控制表 数据 
* 
* @param string $actTable ACL表名称 
* 
* @return mixed 
*/ 
function aclGetACT($actTable){ 
if (empty($actTable)) return false; 
static $rst = array(); 
$cacheId = null; 
switch($actTable){ 
case CoreApp::$defaultAcl->tbResources: 
$cacheId = 'acl-resources'; 
break; 
case CoreApp::$defaultAcl->tbRoles: 
$cacheId = 'acl-roles'; 
break; 
case CoreApp::$defaultAcl->tbRefResourcesRoles: 
$cacheId = 'acl-roles_has_resources'; 
break; 
} 
if (!$cacheId) return null; 
if (isset($rst[$cacheId])) return $rst[$cacheId]; 
global $globalConf; 
// 900 
$rst[$cacheId] = getCache($globalConf['runtime']['cacheDir'],$cacheId,0); 
if ( !$rst[$cacheId] ){ 
$rst[$cacheId] = aclRebuildACT($actTable,true); 
} 
return $rst[$cacheId]; 
} 
/** 
* 获取 资源 记录 
* 
* @param string $rsid 
* 
* @return array 
*/ 
function aclGetResource($rsid){ 
static $rst = null; 
if (!$rst){ 
$rst = aclGetACT(CoreApp::$defaultAcl->tbResources); 
if (!$rst) $rst = array(); 
} 
return isset($rst[$rsid]) ? $rst[$rsid] : null; 
} 
/** 
* 获取 角色 记录 
* 
* @param int $role_id 
* 
* @return array 
*/ 
function aclGetRole($role_id){ 
static $rst = null; 
if (!$rst){ 
$rst = aclGetACT(CoreApp::$defaultAcl->tbRoles); 
if (!$rst) $rst = array(); 
} 
return isset($rst[$role_id]) ? $rst[$role_id] : null; 
} 
/** 
* 获取 用户角色关联 记录,此方法可以校验资源是否可被此角色调用 
* 
* @param string $rsid 
* @param int $role_id 
* 
* @return array 
*/ 
function aclGetRefResourcesRoles($rsid,$role_id){ 
static $rst = null; 
if (!$rst){ 
$rst = aclGetACT(CoreApp::$defaultAcl->tbRefResourcesRoles); 
if (!$rst) $rst = array(); 
} 
$ref_id = "{$rsid}<-|->{$role_id}"; 
CoreApp::writeLog(isset($rst[$ref_id])?$rst[$ref_id]:'nodata',$ref_id); 
return isset($rst[$ref_id]) ? $rst[$ref_id] : null; 
} 
迷你好用的 EXCEL xml 输出方案
