2
我正在做我的第一個應用程序,其中包含多個用戶的權限和角色。 我的理解是使用BitWise格式的最好方法。這是真的,還是有更好的選擇?PHP BitWise權限和角色或替代
這是我目前的測試代碼,我得到一個相當bizzare的效果。 如果有人可以說出爲什麼會發生這種情況,那將不勝感激。
例:
$user = array('permissions' => 1); // This *should* return ONLY READ, (except it shows all)
$user = array('permissions' => 8); // Shows correct, FULL resources.
你可以用這個來測試代碼... http://writecodeonline.com/php/
define("PERM_R", 1); # Read
define("PERM_W", 2); # Write
define("PERM_E", 4); # Edit
define("PERM_D", 8); # Delete
define("ROLE_GUEST", PERM_R);
define("ROLE_EDITOR", ROLE_GUEST | PERM_W | PERM_E);
define("ROLE_FULL", ROLE_EDITOR | PERM_D);
function hasAccess($user, $action) {
if(is_array($user)) {
return $user['permissions'] & $action;
} else if (is_int($user)) {
return $user & $action;
}
}
$user = array('permissions' => 1);
echo "USER PERMISSIONS: ". $user['permissions'] ."<br /><br />";
# TEST PERMS
if(hasAccess($user, PERM_R)) {
echo PERM_R;
echo " - Yes you can see READ <br />";
}
if(hasAccess($user, PERM_R | PERM_W)) {
echo PERM_R | PERM_W;
echo " - Yes you can see READ & WRITE<br />";
}
if(hasAccess($user, PERM_R | PERM_W | PERM_E)) {
echo PERM_R | PERM_W | PERM_E;
echo " - Yes you can see READ & WRITE & EDIT<br />";
}
if(hasAccess($user, PERM_R | PERM_W | PERM_E | PERM_D)) {
echo PERM_R | PERM_W | PERM_E | PERM_D;
echo " - Yes you can see READ & WRITE & EDIT & DELETE<br />";
}
# TEST ROLES
if(hasAccess($user, ROLE_GUEST)) {
echo ROLE_GUEST;
echo " - Yes, You Are A GUEST <br />";
}
if(hasAccess($user, ROLE_EDITOR)) {
echo ROLE_EDITOR;
echo " - Yes, You Are A EDITOR <br />";
}
if(hasAccess($user, ROLE_FULL)) {
echo ROLE_FULL;
echo " - Yes, You Are A FULL <br />";
}
我發現蛋糕的ACL是管理權限的最簡單方法。它在不犧牲簡單性的情況下爲您提供良好的粒度。它的要點:http://book.cakephp.org/2.0/en/core-libraries/components/access-control-lists.html。起初看起來很複雜,但一旦你瞭解它就會變得非常有意義。 – NullUserException
會評論'編輯'=讀/寫 和缺少'創建',但這是個人的注意事項。 –
一個詳細的博客是在這裏:http://goo.gl/ATnj6j –