实现简单的ACL
Php代码
/**
* 简单的ACL 权限控制功能
*
* 表定义
*
* 1. 资源定义 (rsid,access,desc)
* 2. 角色定义 (id,rolename,desc)
* 3. 资源-角色关联(rsid,role_id)
* 4. 用户-角色关联(user_id,role_id)
*
* 依赖db.php sqlobject.php
*
* @author vb2005xu.iteye.com
*/
class AclBase {
/**
* 不允许任何人访问
*/
const NOBODY = 0;
/**
* 允许任何人访问
*/
const EVERYONE = 1;
/**
* 允许 拥有角色的用户访问
*/
const HAS_ROLE = 2;
/**
* 允许 不带有角色的用户访问
*/
const NO_ROLE = 3;
/**
* 在 资源-角色关联 定义的 角色才能访问
*/
const ALLOCATE_ROLES = 4;
// 定义相关的 表名
public $tbResources = 'aclresources';
public $tbRoles = 'aclroles';
public $tbRefResourcesRoles = 'aclresources_aclroles';
public $tbRefUsersRoles = 'users_aclroles';
/**
* 格式化 资源的访问权限并返回
*
* @return int
*/
static function formatAccessValue($access){
static $arr = array(self::NOBODY,self::EVERYONE,self::HAS_ROLE,self::NO_ROLE,self::ALLOCATE_ROLES);
return in_array($access,$arr) ? $access : self::NOBODY;
}
/**
* 创建资源,返回资源记录主键
*
* @param string $rsid
* @param int $access
* @param string $desc
*
* @return int
*/
function createResource($rsid,$access,$desc){
if (emptyempty($rsid)) return false;
$resource = array(
'rsid' => $rsid,
'access' => self::formatAccessValue($access),
'desc' => $desc,
'created_at' => CURRENT_TIMESTAMP
);
return SingleTableCRUD::insert($this->tbResources,$resource);
}
/**
* 修改资源,返回成功状态
*
* @param array $resource
* @return int
*/
function updateResource(array $resource){
if (!isset($resource['rsid'])) return false;
$resource['updated_at'] = CURRENT_TIMESTAMP;
return SingleTableCRUD::update($this->tbResources,$resource,'rsid');
}
/**
* 删除资源
*
* @param string $rsid
* @return int
*/
function deleteResource($rsid){
if (emptyempty($rsid)) return false;
return SingleTableCRUD::delete($this->tbResources,array('rsid'=>$rsid));
}
/**
* 创建角色,返回角色记录主键
*
* @param string $rolename
* @param string $desc
*
* @return int
*/
function createRole($rolename,$desc){
if (emptyempty($rolename)) return false;
$role = array(
'rolename' => $rolename,
'desc' => $desc,
'created_at' => CURRENT_TIMESTAMP
);
return SingleTableCRUD::insert($this->tbRoles,$role);
}
/**
* 修改角色,返回成功状态
*
* @param array $role
* @return int
*/
function updateRole(array $role){
if (!isset($role['id'])) return false;
if (isset($role['rolename'])) unset($role['rolename']);
$role['updated_at'] = CURRENT_TIMESTAMP;
return SingleTableCRUD::update($this->tbRoles,$role,'id');
}
/**
* 删除角色
*
* @param int $role_id
* @return int
*/
function deleteRole($role_id){
if (emptyempty($role_id)) return false;
return SingleTableCRUD::delete($this->tbRoles,array('role_id'=>(int) $role_id));
}
/**
* 为资源指定角色,每次均先全部移除表中相关记录再插入
*
* @param int $rsid
* @param mixed $roleIds
* @param boolean $setNull 当角色id不存在时,是否将资源从关联表中清空
*/
function allocateRolesForResource($rsid,$roleIds,$setNull=false,$defaultAccess=-1){
if (emptyempty($rsid)) return false;
$roleIds = normalize($roleIds,',');
if (emptyempty($roleIds)){
if ($setNull){
SingleTableCRUD::delete($this->tbRefResourcesRoles,array('rsid'=>$rsid));
if ($defaultAccess != -1){
$defaultAccess = self::formatAccessValue($defaultAccess);
$this->updateResource(array('rsid'=>$rsid,'access'=>$defaultAccess));
}
return true;
}
return false;
}
SingleTableCRUD::delete($this->tbRefResourcesRoles,array('rsid'=>$rsid));
$roleIds = array_unique($roleIds);
foreach ($roleIds as $role_id){
SingleTableCRUD::insert($this->tbRefResourcesRoles,array('rsid'=>$rsid,'role_id'=>(int)$role_id));
}
return true;
}
function cleanRolesForResource($rsid){
if (emptyempty($rsid)) return false;
return SingleTableCRUD::delete($this->tbRefResourcesRoles,array('rsid'=>$rsid));
}
function cleanResourcesForRole($role_id){
if (emptyempty($role_id)) return false;
return SingleTableCRUD::delete($this->tbRefResourcesRoles,array('role_id'=>(int) $role_id));
}
/**
* 为角色分配资源,每次均先全部移除表中相关记录再插入
*
* @param int $role_id
* @param mixed $rsids
*
* @return boolean
*/
function allocateResourcesForRole($role_id,$rsids){
if (emptyempty($role_id)) return false;
$role_id = (int) $role_id;
$rsids = normalize($rsids,',');
if (emptyempty($rsids)){
return false;
}
SingleTableCRUD::delete($this->tbRefResourcesRoles,array('role_id'=>$role_id));
$rsids = array_unique($rsids);
foreach ($rsids as $rsid){
SingleTableCRUD::insert($this->tbRefResourcesRoles,array('rsid'=>$rsid,'role_id'=>$role_id));
}
return true;
}
/**
* 为用户指派角色,每次均先全部移除表中相关记录再插入
*
* 此处在用户很多的时候可能会有性能问题... 后面再想怎么优化
*
* @param int $user_id
* @param mixed $roleIds
*
* @return boolean
*/
function allocateRolesForUser($user_id,$roleIds){
if (emptyempty($user_id)) return false;
$user_id = (int) $user_id;
$rsids = normalize($rsids,',');
if (emptyempty($rsids)){
return false;
}
SingleTableCRUD::delete($this->tbRefUsersRoles,array('user_id'=>$user_id));
$roleIds = array_unique($roleIds);
foreach ($roleIds as $roleId){
SingleTableCRUD::insert($this->tbRefUsersRoles,array('user_id'=>$user_id,'role_id'=>$role_id));
}
return true;
}
function cleanRolesForUser($user_id){
if (emptyempty($user_id)) return false;
return SingleTableCRUD::delete($this->tbRefUsersRoles,array('user_id'=>(int) $user_id));
}
function cleanUsersForRole($role_id){
if (emptyempty($role_id)) return false;
return SingleTableCRUD::delete($this->tbRefUsersRoles,array('role_id'=>(int) $role_id));
}
}
/**
* 对资源进行acl校验
*
* @param string $rsid 资源标识
* @param array $user 特定用户,不指定则校验当前用户
*
* @return boolean
*/
function aclVerity($rsid,array $user = null){
if (emptyempty($rsid)) return false;
}
Java代码
/*
* 校验步骤如下:
*
* 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
*/
花了半个小时 晕死 明天再抽空完善....

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1
Easy-to-use and free code editor
