码迷,mamicode.com
首页 > 其他好文 > 详细

priv

时间:2019-09-27 19:01:55      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:function   判断   UNC   xxx   lag   方法   eset   ati   sel   

<?php

class priv
{
const GOD = 0;
const CMS = 1<<0;
const FLAG = 1<<1;
const HALL = 1<<2;
const APARTMENTS = 1<<3;

const MODULES = [
self::GOD => [‘name‘=>‘上帝‘,‘namespace‘=>[‘namespace‘]],
self::CMS => [‘name‘=>‘上帝‘,‘namespace‘=>[‘namespace‘]],
self::FLAG => [‘name‘=>‘上帝‘,‘namespace‘=>[‘namespace‘]],
self::HALL => [‘name‘=>‘上帝‘,‘namespace‘=>[‘namespace‘]],
self::APARTMENTS => [‘name‘=>‘上帝‘,‘namespace‘=>[‘namespace‘]],
];


const MODULES_METHOD = [
self::CMS => [
‘method‘ => [
‘name‘=>‘name1‘,
‘namespace‘=>‘namespace1‘,
‘priv_code‘=>1<<0,
]
],
self::FLAG => [
‘xxx‘ => [
‘name‘=>‘name1‘,
‘priv_code‘=>1<<0,
]
]
];

/**
* [resetPriv 全部重新授权]
* @param [type] $userPrivs [[array [moduleids,权限json]] moduleIds数据库默认是0
* @param [type] $moduleId [1,2,3,4]
* @param [type] $method [1=>[1,2,3],2=>[1,2,3,4]]
*/
public static function resetPriv($moduleIds,$methods=[])
{
$userPrivs[‘module_ids‘] = 0;
$userPrivs[‘method_ids‘] = []
foreach ($moduleId as $moduleId){
$userPrivs[‘module_ids‘] = $userPrivs[‘module_ids‘] | $moduleId;

if(isset($methods[$moduleId])){
$userPrivs[‘method_ids‘][$moduleId] = 0;
foreach ($methods[$moduleId] as $m){
$userPrivs[‘method_ids‘][$moduleId] = $userPrivs[‘method_ids‘][$moduleId] | $m;
}
}
}

return $userPrivs;
}

 

/**
* [checkPriv 权限判断]
* @param [type] $userPriv [array [模块id集合,权限json]
* @param [type] $systemPirv [‘namespace必填’,‘method选填‘]
* @return [bool] [true通过 false不通过]
*/
public static function checkPriv($userPrivs, $systemPirv){
if($userPrivs[‘module_ids‘] === 0){
return true;
}
$namespace = $systemPirv[‘namespace‘];
//0为不存在
$moduleId = self::moduleIdByNamespace($namespace);
if($moduleId === 0 ){
return false;
}

//模块权限
$modulePrivPass = $userPrivs[‘module_ids‘] & $moduleId;

//方法权限
//-1为命名空间下的都可以用
$methodPrivCode = $systemPirv[‘mothed‘] ?? -1;

if ($methodPrivCode == -1){
return true;
}

$methodPrivPass = $userPrivs[‘method_ids‘][$moduleId] & $methodPrivCode;

if ($modulePrivPass && $methodPrivPass){
return true;
}

return false;
}


/**
* [methodPriv description]
* @param [type] $moduleId [description]
* @param [type] $method [description]
* @return [type] [description]
*/
public static function methodPrivByMethod($moduleId,$method){
if(!isset(self::MODULES_METHOD[$moduleId])){
return 0;
}

return self::MODULES_METHOD[$moduleId][‘method‘][‘priv_code‘] ?? -1;
}
/**
* [moduleIdByNamespace 通过命名空间获取模块id]
* @param [type] $namespace [namespace]
* @return [type] [modules_id]
*/
public static function moduleIdByNamespace($namespace)
{
foreach (self::MODULES as $id => $module){
foreach ($module[‘namespace‘] as $ns){
if($systemPirv[0] == $ns) {
return $id
}
}
}

return 0;
}


public static function menus($userPrivs)
{
$menus = [];
foreach (self::MODULES as $id => $module){
$menus[$id][‘name‘] = $module[‘name‘];
$menus[$id][‘child‘] = [];
foreach (self::MODULES_METHOD[$id] as $m=> $methodDetail){
[$methodDetail[‘namespace‘],$methodDetail[‘me‘]
}

}
}
}

 

priv

标签:function   判断   UNC   xxx   lag   方法   eset   ati   sel   

原文地址:https://www.cnblogs.com/dennylau/p/11599549.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!