我正在使用bitmasks表示的权限。这些权限是
$permissionMap = [
'VIEW' => 1;
'CREATE' => 2;
'EDIT' => 4;
'DELETE' => 8
'UNDELETE' => 16;
'OPERATOR' => 32;
'MASTER' => 64;
'OWNER' => 128;
];
如果我合并多个权限,请说VIEW
,EDIT
和DELETE
以获取13的位掩码。是否可以对该整数进行反向工程以了解所使用的权限创造它?
function getPermissions($mask, $permissionMap) {
// use $permissionMap to return string[]
}
$viewEditAndDelete = 1 | 4 | 8; // 13
getPermissions($viewEditAndDelete, $permissionMap); // ['VIEW', 'EDIT', 'DELETE']
答案 0 :(得分:2)
只检查是否设置了特定的面具。如果我们遍历$map
并检查其标志是否已设置,那么它实际上非常简单:
function getPermissions($mask, $permissionMap) {
$permissions = [];
foreach( $permissionMap as $perm => $val )
if( $mask & $val )
array_push( $permissions, $perm );
return $permissions;
}