/**
* Allow users with roles to access
*/
Const HAS_ROLE = 2;
/**
* Allow users without roles to access
*/
Const NO_ROLE = 3;
/**
* Roles defined in resource-Role association can be accessed
*/
Const ALLOCATE_ROLES = 4;
Define the associated table name
Public $tbResources = ' aclresources ';
Public $tbRoles = ' aclroles ';
Public $tbRefResourcesRoles = ' ref_aclresources_aclroles ';
Public $tbRefUsersRoles = ' ref_users_aclroles ';
/**
* Format the access rights of the resource and return
*
* @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;
}
/**
* Create resource, return resource record primary key
*
* @param string $rsid
* @param int $access
* @param string $desc
*
* @return int
*/
function Createresource ($rsid, $access, $desc) {
if (empty ($rsid)) return false;
/**
* Modify roles to return to success status
*
* @param array $role
* @return int
*/
function Updaterole (array $role) {
if (!isset ($role [' ID ')]) return false;
Return Singletablecrud::update ($this->tbroles, $role, ' id ');
}
/**
* Remove Roles
*
* @param int $role _id
* @return int
*/
function DeleteRole ($role _id) {
if (Empty ($role _id)) return false;
Return Singletablecrud::d elete ($this->tbroles,array (' role_id ' = = (int) $role _id));
}
/**
* Assign a role to a resource, remove all related records from the table and insert them each time
*
* @param int $rsid
* @param mixed $roleIds
* @param boolean $setNull whether to empty the resource from the associated table when the role ID does not exist
*/
function Allocaterolesforresource ($rsid, $roleIds, $setNull =false, $defaultAccess =-1) {
if (empty ($rsid)) return false;
/**
* Assign resources to a role, remove all related records from the table and insert them each time
*
* @param int $role _id
* @param mixed $rsids
*
* @return Boolean
*/
function Allocateresourcesforrole ($role _id, $rsids) {
if (Empty ($role _id)) return false;
/**
* Assign roles to users, remove all related records from the table and insert them each time
*
* There may be performance issues when users are a lot ... How to optimize the back again
*
* @param int $user _id
* @param mixed $roleIds
*
* @return Boolean
*/
function Allocaterolesforuser ($user _id, $roleIds) {
if (Empty ($user _id)) return false;
/**
* Clear the user's role information
*
* @param int $user _id
*
* @return Boolean
*/
function Cleanrolesforuser ($user _id) {
if (Empty ($user _id)) return false;
Return Singletablecrud::d elete ($this->tbrefusersroles,array (' user_id ' = = (int) $user _id));
}
/**
* Clear user associations for roles
*
* @param int $role _id
*
* @return Boolean
*/
function Cleanusersforrole ($role _id) {
if (Empty ($role _id)) return false;
Return Singletablecrud::d elete ($this->tbrefusersroles,array (' role_id ' = = (int) $role _id));
}
}
The code for the specific test is as follows:
Copy the code code as follows:
/**
* ACL check on resources
*
* @param string $rsid resource ID
* @param array $user Specific user, do not specify to verify the current user
*
* @return Boolean
*/
function aclverity ($rsid, array $user = null) {
if (empty ($rsid)) return false;
if (! Coreapp:: $DEFAULTACL) {
Coreapp:: $defaultAcl = new Aclflat ();
}
$rsRow = Aclgetresource ($RSID);
Default access policy for resources not defined
if (! $rsRow) return false;
/**
* Allow users without roles to access
*/
if (aclbase::no_role = = $rsRow [' Access ']) return $userHasRoles? False:true;
/**
* Allow users with roles to access
*/
if (aclbase::has_role = = $rsRow [' Access ']) return $userHasRoles? True:false;
---resource <-> role verification for users
if ($userHasRoles) {
foreach ($user [' roles '] as $role _id) {
if (Aclgetrefresourcesroles ($rsid, $role _id))
return true;
}
Dump ($user);
}
return false;
}
Copy the code code as follows:
/**
* ACL check on resources
*
* @param string $rsid resource ID
* @param array $user Specific user, do not specify to verify the current user
*
* @return Boolean
*/
function aclverity ($rsid, array $user = null) {
if (empty ($rsid)) return false;
if (! Coreapp:: $DEFAULTACL) {
Coreapp:: $defaultAcl = new Aclflat ();
}
$rsRow = Aclgetresource ($RSID);
Default access policy for resources not defined
if (! $rsRow) return false;
Coreapp::writelog ($rsRow, ' test ');
/*
* The verification steps are as follows:
*
* 1. Validate the resource itself first access property
* EVERYONE = True,nobody and False * other properties continue to verify below
* 2. Get the Role ID collection from the session (or User session table)
* 3. Has_role = True if the user has a role, No_role = False, or vice versa
* 4. If resource access = = Allocate_roles
* 1. Gets the collection of role IDs for the resource from the cache (or $tbRefResourcesRoles)
* 2. To intersect the collection of role IDs owned by the user with the set of role IDs corresponding to the resource
* 3. Presence intersection = true; otherwise = False
*/
if ($return) return $rst;
}
/**
* Get Role Resource access control table data
*
* @param string $actTable ACL table name
*
* @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];
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.