
September 8th, 2005
11:28 PM
Neverside Newbie
Status: Offline!
Bitwise User Permission System
Hello,
Lately I've been reading threads on a user permission system, they have all been pointed to Jeremie's bitwise tutorial..
I have read it a few times, I can understand the operators, etc.. But I can't understand how to put it to particle use, for group useage.
I.e. [ MySQL Table Layout Example ]
<?php
Group Edit Delete Global Mod Mod
-----------------------------------------------------------------
Admins Y Y Y Y
Guests N N N N
?>
Etc... How would I use bitwise operators to control user groups in the database?
I would very much appeciate any help at all, and BTW I have googled bitwise and similiar - I found one good one but like Jeremie's did not understand how to implent it.
Thanks.
Edit: Had to use php bbcode to get spacing to look correct the code bbcode didnt space it.
Last edited by johnzer, September 8th, 2005 11:30 PM (Edited 1 times)

September 9th, 2005
04:32 AM
Apathy is bliss.
Status: Offline!
You would assign a hex value to each of those as a constant, like in the tutorial, USER_CAN_EDIT = 0x01, USER_CAN_DELETE = 0x02, etc. and combine and store the permission string for each group in the groups table. Then, when you initialize the session and get the user's information, just combine the permission strings from all groups they are in, and store that along with their other information. You would then check that against the type of permission needed on a given page.
I hope that helps.
___________________
"At least I shall die as I have lived, completely surrounded by morons."
Last edited by valinorbob, September 9th, 2005 04:36 AM (Edited 2 times)

September 9th, 2005
05:22 AM
for some reason... always in #central
Status: Offline!
You could also do it using the powers of two.
Ex:
<?
$read = 1; // guest
$reg = 2; // registered user
$group1 = 4; // group 1 mod
$group2 = 8; // group 2 mod
$admin = 16; // site admin
$user1 = 23;
if ($user1 & $read) echo "can read<br />\n";
if ($user1 & $reg) echo "is registered<br />\n";
if ($user1 & $group1) echo "group 1<br />\n";
if ($user1 & $group2) echo "group 2<br />\n";
if ($user1 & $admin) echo "admin<br />\n";
/* echoes:
can read
is registered
group 1
admin
*/
?>
You can then assign what the different groups can do using MySQL.
___________________

Last edited by Logi, September 9th, 2005 05:25 AM (Edited 1 times)

September 10th, 2005
02:57 AM
Nobody fucks with my title.
Status: Offline!
As a note, powers of two in hexidecimal (base-16) are equivalent to those in decimal (base-10)...
0x01 = 1
0x02 = 2
0x04 = 4
0x08 = 8
0x10 = 16
0x20 = 32
0x40 = 64
... and so on. Just thought I'd point this out if it wasn't obvious. :)
___________________
<3

September 10th, 2005
08:19 PM
funny and cheeky
Status: Offline!
can someone post the link to the tutorial. I think it would be useful for the system I will be creating for a friend

September 10th, 2005
08:59 PM
Neverside Newbie
Status: Offline!
Thanks, I think I'm starting to get it now, although I'm unsure how to store it in my database..
My groups table at the minute is
<?php
ID Name Flags
-----------------------------------
1 Admins 1665
2 Editors 0
... ...... ...
?>
Is this layout efficient? Thanks again for the help, schoi the tutorial is in the sticky "In Depth Tutorials [May 30, 2004]" on page 3.
Last edited by johnzer, September 10th, 2005 09:01 PM (Edited 2 times)

September 10th, 2005
11:49 PM
Neverside Newbie
Status: Offline!
Yes. That's the ideal layout using this type of permission system.
___________________
I don't suffer from insanity; I enjoy every minute of it.
Unintended Theory | Cacrew v4

September 11th, 2005
05:31 PM
PHP Lurver.
Status: Offline!
This is so mind boggling :(

September 13th, 2005
02:24 AM
Lost in Berkeley, CA
Status: Offline!
Originally posted by thefallen:
This is so mind boggling :(
At first, yeah. But, just think of it as turning switches for 0s and 1s, not the actual number they make.
___________________
There is no theory of evolution. Just a list of creatures Chuck Norris has allowed to live.

September 13th, 2005
04:50 AM
Neverside Newbie
Status: Offline!
If you notice the pattern in the numbers, it's a peice of cake.
1
2
4
8
16
32
64
128
256
etc..
The next number is always twice the quantity of the previous number. Also, they are all also multiples of all previous numbers.
Example:
128 is a multiple of 64,32,16,etc...
64 is a multiple of 32,16,8,etc....
I personally find it easier to think of it in number form, and deal with it in number form, rather than dwell on the complexities of Hex or Binary notations.
Also note. There is a limit to the size of this 'flag' or 'permission' integer.
Binary operations, on a 32-bit system, can not be performed on a integer larger than 2^32 (or 4294967296). On a 64-bit system, however, binary operations can not be performed on a integer larger than 2^64 (or 18446744073709552000). So you instantly see the advantage of 64-bit systems!
Just keep this in mind when working on the permission system. You don't want TOO many permissions or you're integer will get to large, and ultimatly result in a not-so-friendly fatal PHP error.
___________________
I don't suffer from insanity; I enjoy every minute of it.
Unintended Theory | Cacrew v4