Post by [dmsuperman] on Apr 15, 2006 5:37:14 GMT
I just learned how this work...cookie was too busy or something to explain it when I asked him when he mentioned it, so I finally figured it out myself. Here's a pretty easy to use function that doesn't require you understand how the system itself works:
<?PHP
//Set up the permissions
$possible_perms = array(1 => "read",
2 => "list",
4 => "rename",
8 => "create",
16 => "delete",
32 => "write",
64 => "freeze",
128 => "chmod",
);
//Actually get the permissions
function get_permissions($permLevel, $possible_perms){
//Set up the data
$perms = array();
$highest_perm = 1;
$startTest = 1;
//Dynamically form the highest permission level and modulus the given one to remove impossible permissions
foreach($possible_perms as $key => $current_perm){
$perms[$current_perm] = false;
$highest_perm += intval($key);
$startTest = intval($key) * 2;
}
$permLevel = ($permLevel % $highest_perm);
//See which permissions this user has and return them
while($permLevel > 0){
if($startTest <= $permLevel){
$perms[$possible_perms[$startTest]] = true;
$permLevel = $permLevel - $startTest;
}
$startTest = $startTest / 2;
}
return($perms);
}
$user = get_permissions(511, $possible_perms);
print("<pre>");
print_r($user);
print("</pre>");
?>
Basically, you just create the permissions you want, all independant of each other (some examples were listed above, for folder permissions), doubling the key before the previous, and that's it for set up. Then, to give a user a certain permission, you just add whatever the key is for that permission to their permission level...Here's an example using the above permissions:
I want to give bob read, write, and list permissions. I take the numbers for read, write, and list, and add them together: 1 + 32 + 2 = 35. I give bob permission level of 35 and now he can read, write, and list. I store 35 in the database and when I want to know what he can do, I grab it, pass 35 to the function, and it returns an array of what he can do and what he can't do.
With the current system, the highest permission level a person can have is 255 (1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255) so anything higher can't be possible. This will subtract 255 until it is a valid number...so if you pass 256 then it will say he has level 1 permission, because 256 - 255 = 1. It's fairly foolproof
You can also add more permissions, just double the previous key and add them to the array:
...
256 => "set_options",
512 => "create_user"
);
<?PHP
//Set up the permissions
$possible_perms = array(1 => "read",
2 => "list",
4 => "rename",
8 => "create",
16 => "delete",
32 => "write",
64 => "freeze",
128 => "chmod",
);
//Actually get the permissions
function get_permissions($permLevel, $possible_perms){
//Set up the data
$perms = array();
$highest_perm = 1;
$startTest = 1;
//Dynamically form the highest permission level and modulus the given one to remove impossible permissions
foreach($possible_perms as $key => $current_perm){
$perms[$current_perm] = false;
$highest_perm += intval($key);
$startTest = intval($key) * 2;
}
$permLevel = ($permLevel % $highest_perm);
//See which permissions this user has and return them
while($permLevel > 0){
if($startTest <= $permLevel){
$perms[$possible_perms[$startTest]] = true;
$permLevel = $permLevel - $startTest;
}
$startTest = $startTest / 2;
}
return($perms);
}
$user = get_permissions(511, $possible_perms);
print("<pre>");
print_r($user);
print("</pre>");
?>
Basically, you just create the permissions you want, all independant of each other (some examples were listed above, for folder permissions), doubling the key before the previous, and that's it for set up. Then, to give a user a certain permission, you just add whatever the key is for that permission to their permission level...Here's an example using the above permissions:
I want to give bob read, write, and list permissions. I take the numbers for read, write, and list, and add them together: 1 + 32 + 2 = 35. I give bob permission level of 35 and now he can read, write, and list. I store 35 in the database and when I want to know what he can do, I grab it, pass 35 to the function, and it returns an array of what he can do and what he can't do.
With the current system, the highest permission level a person can have is 255 (1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255) so anything higher can't be possible. This will subtract 255 until it is a valid number...so if you pass 256 then it will say he has level 1 permission, because 256 - 255 = 1. It's fairly foolproof
You can also add more permissions, just double the previous key and add them to the array:
...
256 => "set_options",
512 => "create_user"
);