View Full Version : short of permission

03-09-2010, 01:18 PM
i wanna write permission table like vbulletin (with 6 level) how can i write following shorter.contions1-7 is in db. it is read from db like vbulletin

if (condition1){
if (condition2){
if (condition3){
if (condition4){
if (condition5){
if (condition6){
if (condition7){
///and codes //

03-09-2010, 03:43 PM
This isn't how the vbulletin tracks permissions. Long ago, the common approach was a single field represents a single privilege:

if ($user['canedit']) ...
if ($user['candelete'])...

But long ago we changed this approach for a more dynamic one using bitwise values. PHP's maximum integer size will limit you to 31 possible privileges assuming 0 is invalid. This allows you to dynamically change you're privilege sets to add / modify / remove whats available easily, as well as group available options:

// We'll just use some defines, customize as you see fit:
define('CAN_EDIT', 1);
define('CAN_DELETE', 2);
define('CAN_MODERATE', 4);

// $user is extracted from the db, 'permissions' is an bitwise integer combination
if (($user['permissions'] & (CAN_EDIT | CAN_MODERATE)) <> 0)
// Indicates that the user has the privilege to edit or they can moderate (or both)

The above would indicate is a user has the privilege to EITHER edit OR moderate they can do so. With you're nested if statements, you would instead check using bitwise XOR instead of AND in conjunction with all permissions that need to be satisfied:

if (($user['permissions'] ^ (CAN_EDIT | CAN_DELETE | CAN_MODERATE)) <> 0)
// Indicates that a user is privileged for CAN_EDIT AND CAN_DELETE AND CAN_MODERATE

The trickiest part about this approach is the limitation in the number of bits available. As mentioned, PHP is capped at 2^31 bits for its integer size since there is no way to control unsigned integers, so that limits you to 31 privileges. Control it with grouping; have a group for what the user can change in their own control panel, another group for global control, another one for say, news posting privileges, etc.

For more information on bitwise operations, refer to this page: http://php.net/manual/en/language.operators.bitwise.php

03-10-2010, 08:27 AM
But long ago we changed this approach for a more dynamic one using..

:eek: really!! OMG! God Emperor! :)