哪种数据类型适合存储这种情况? [英] Which data type is suitable for storing this situation?

查看:35
本文介绍了哪种数据类型适合存储这种情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比如我有一个用户,这个用户有不同的用户权限,比如一个用户可以有

For example, I have a user, and the user have different user right, for example, a user can have

-create file
-read file
-update file
-delete file

4个权限,我可以用4个BOOL来查找用户权限,但是如果用户有更多权限,我需要创建越来越多的BOOL来存储权限.我不认为这是一个好主意.我想为此得到一个长整数……例如,用户可以做的所有事情是 1111.create file 为 1000,read file 为 100,update 为 10,delete 为 1.因此,如果用户只有读取文件的权限为 0100.

4 rights, I can use 4 BOOL to find the user right, but if the user have more right, I need create more and more BOOL to store the right. I don't think it is a good idea. And I think of getting a long integer for this... for example, the user can do all the stuff is 1111. create file is 1000, read file is 100, update is 10, delete is 1. So, if the user only get read file right is 0100.

有没有更好的主意??谢谢.

Is there any better ideas?? Thank you.

推荐答案

我建议将权限(权限)转换为二进制状态字符串和更高版本,将其作为长整数或十六进制字符串(VARCHAR;用于存储很多权利).

I suggest to convert privileges (rights) in binary-state-string and later, store it in database as long integer or hexadecimal string (VARCHAR; for storing a lot of rights).

示例

$privileges_list = array(
  0 => 'create_file',
  1 => 'read_file',
  2 => 'update_file',
  3 => 'delete_file',
  4 => 'create_pool',
  5 => 'vote_in_pool',
  6 => 'create_gallery',
  7 => 'upload_images',
  8 => 'view_statistics'
);

因此,如果您想为用户设置create fileupdate fileview statistics 权限,只需在字符串中的适当位置设置 1(0, 2, 8) 其余为 0

So if you want to set create file, update file and view statistics rights to the user just put 1 on appropriate positions in string (0, 2, 8) and 0 for rest of them

$binary_string = "100000101";

这个字符串的最后一个字符是位置 0,第一个是位置 8.

Last character in this string is position 0, first is position 8.

现在您可以将此字符串转换为整数 (261) 或十六进制数 (105) 并将其作为该用户的权限集放入数据库(我更喜欢十六进制).

Now you can convert this string to integer (261) or hex-number (105) and put it into database as privilege-set for that user (I prefer hex).

要将这个值转换回权限列表,你可以使用这样的东西

To convert this value back to privileges-list you can use something like this

function hexvalue2privileges($hexvalue, $plist) {
  $res = array(); $res_assoc = array();
  for ($i = strlen($hexvalue) - 1; $i >= 0; $i--) {
    $bin = str_pad(decbin(hexdec(substr($hexvalue, $i, 1))), 4, '0', STR_PAD_LEFT);
    $bin_array = array_reverse(str_split($bin, 1));
    foreach ($bin_array as $bitstate) $res[] = $bitstate == '1' ? true : false;
    }
  foreach ($plist as $key => $id) {
    $res_assoc[$id] = $res[$key];
    }
  return $res_assoc;
  }

并调用这个函数

print_r(hexvalue2privileges('105', $privileges_list));

输出将是

Array
(
    [create_file] => 1      // true
    [read_file] =>          // false
    [update_file] => 1      // true
    [delete_file] =>        // false
    [create_pool] =>        // false
    [vote_in_pool] =>       // false
    [create_gallery] =>     // false
    [upload_images] =>      // false
    [view_statistics] => 1  // true
)

对于每个十六进制字符,您可以存储 4 个权限,以便使用此公式计算所需的字符数

With every hexadecimal character you can store 4 rights so to calculate number of character needed use this formula

$chars_needed = floor((count($privileges_list)-1) / 4) + 1; // result 3

获取二进制字符串的总长度

To get total length of binary-string

$binary_length = $chars_needed * 4; // result 12

固定权限集的长度

$binary_string = "100000101";
$binary_string = str_pad($binary_string, $binary_length, '0', STR_PAD_LEFT);
// result '000100000101'

将 $binary_string 转换为十六进制

To convert $binary_string to hex

$binary_string = "000100000101";

$hexvalue = "";
$groups = str_split($binary_string, 4);
foreach ($groups as $group) $hexvalue .= dechex(bindec($group));

// result $hexvalue='105'   (1=0001, 0=0000, 5=0101)   

您还可以通过为每个组(管理员、版主、访客、vip 等)创建权限集来创建权限组并将它们分配给用户.

Also you can create groups of privileges and assign them to users by creating privileges-set for every group (administrators, moderators, guests, vip, etc).

希望能帮到你

这篇关于哪种数据类型适合存储这种情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆