0
我很感兴趣的是编写一个能够帮助我为不同用户提供不同权限的类(即ACL类)。 我一直在研究一种很棒的方法,并且按位操作已经出来了!如何允许一些权限而不是其他使用按位系统的权限?
我发现了一篇好文章How to write a permission system using bits and bitwise operations in PHP。文章解释如何使用按位操作处理权限。
我不知道如何做的唯一的事情是如何让用户写评论,但不发布线程。
因此,如果允许设置像这样:
<?php
$perms = array(
'can_post' => 1,
'can_comment' => 2,
'can_edit' => 4,
'can_delete' => 8
);
$user_perms = 2;
//CONDITION #1
if ($user_perms & $perms['can_comment']) {
/* He/She has permission to do this */
} else {
/* He/She doesn't have permission */
}
//CONDITION #2
if ($user_perms & $perms['can_post']) {
/* He/She has permission to do this */
} else {
/* He/She doesn't have permission */
}
?>
所以上述CONDITION #1
秀做工精细和意见应该被允许,但//CONDITION #2
不应该被允许的,因为用户没有发布许可!
我的问题,如何让用户只发表评论但不发帖?发布你将有'001',并评论你会'010'。
这是否适用于十进制数或只有二进制数?我没有看到任何二进制操作? – andrebruton 2015-03-10 13:08:21
@andrebruton它只有在每个权限都是2的权力时才有效。也就是说,你可以有一些聪明,比如'$ perms ['general_posting'] = 3;'拥有自动激活多个基本权限标志的权限。 – 2015-03-10 15:10:52
完美,得到它的工作! – andrebruton 2015-03-11 16:16:33