The Arcive of Official vBulletin Modifications Site.It is not a VB3 engine, just a parsed copy! |
|
A Closer Look at Bitfields
This article is helpful if you are curious about the internals of vBulletin permission checking. Sorry about the small thumbnails in advanced! Just click on them to enlarge. Introduction Do you find yourself copying and pasting the following code and not knowing the true internals of what it means? PHP Code:
But what exactly is this code doing; what is the if condition truly evaluating? Playing Detective First let us echo out these two variables as if they actual existed: PHP Code:
The & in PHP (and other programming languages) is called a Bitwise Operator , and it returns the bits that are both set in the two numbers. If you are confused, don't worry! What is a Bit and Bitfield?
Integers as binary values Any integer can be represented as a binary digit. The two values above are all divisible by 2, and so they can both be easily represented as a binary numeral, however, even larger and odd numbers can be defined as binary numbers. Let us convert the two values into binary: 1 = 001 4 = 100 Now, I am not going to go in depth into the conversion of base 10 numerals to base 2, but the basics can be achieved with a table. Since binary is base 2, each bit is a multiple of two starting from 1. Attachment 74187 Enough with the math, how does this relate to Permissions?! When creating an bitfield xml for your modification, you increase the number in between the <bitfield> </bitfield> by a multiple of two. Why? Because it can be represented as a bitfield! Let?s take a look at a sample xml snippet: Code:
<group name="myhackpermissions"> <bitfield name="candosomething" group="my_hack_permissions" phrase="canaddsomething?>1</bitfield> <bitfield name="candeletesomething" group="my_hack_permissions" phrase="candeletesomething">2</bitfield> <bitfield name="canviewsomething" group="my_hack_permissions" >4</bitfield> </group> Using the attached table above, we see that 7 represented in binary is: 1 1 1 Now, imagine that as some sort of toggling board to enable each permission. If it is set to 1 then the user has permission to do that action. Conversely, if it is set to 0 then the user does not have permission to do that action. So let us say that the if condition being evaluated above is for a guest and the guest is trying to add something to the database. The administrator has selected that guest can only viewsomething. If we go back to the binary representing of group of permissions: Attachment 74185 The binary representation of the usergroup Guest?s permissions visually are: Attachment 74186 Recall the ampersand (&) returns the bits that set in both values: PHP Code:
PHP Code:
The if condition is basically checking if the canaddsomething bit is set to yes in the users permissions. We can easily perform the bitwise AND operation (&) by vertically aligning the two values in binary form. If any equal bits are both set to 1 that bit is set to 1 in the result, otherwise it is set to 0. 4 = 1 0 0 1 = 0 0 1 ==== 0 0 0 The result is 0, which converted to a boolean means false, and therefore means that the current user does not have permission to ?addsomething?. Why Bitfields are Optimal Think of using bitfields as a pseudo database table. Instead of having to add individual columns to the user table for every possible permission, an integer such as 131072 can represent a table of yes/no (1/0) options by converting it a binary number. Using bitfields saves space and memory and is very popular method in handling permission systems across many programming languages because computers are very efficient in handling binary data. Other articles on adding custom Permissions with Bitfields Now go, go use the magic of bitfields in your next modification with the help of the following great articles! [How-To] Create User Options (via Bitfields) [How-To] Create Multiple Options Per Forum (via Bitfields) [How-To] Create Usergroup Permissions (via Bitfields) By: Danny Cassidy |
#2
|
||||
|
||||
Thanks Danny ... look forward to your next article
|
#3
|
||||
|
||||
excellent article.
Q: Is there a way to update certain bitfield in the database using MySQL command (I know the value of that bitfield, lets say '128')? |
#4
|
|||
|
|||
Great article, if only it was written 2 months earlier when I hadn't a clue about bitfields.
Still, will be very helpful to some people. |
#5
|
|||
|
|||
Quote:
|
#6
|
||||
|
||||
Quote:
I appreciate your swift reply . |
#7
|
||||
|
||||
Thank you very much
|
#8
|
||||
|
||||
This article helped me out a lot.
Can't say I fully understand bits now (still gotta reference) but through trial and error I believe I will get there. Great article Danny. |
#9
|
|||
|
|||
Great guide, found it a couple of days ago and it helped immensely on the product I was developing .
|
Thread Tools | |
|
|
X vBulletin 3.8.12 by vBS Debug Information | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|