Jump to content

How many bitmask values can I have


jgp4

Recommended Posts

Hi,

 

I've just started looking at using bitmasks for permissions in my work as they seem like a nice way to handle things, however I've just had a thought.

 

As the different permission values will be stored as a power of 2, will i only be able to have 32 of them because my server is 32bit? I.E. if I try and set a permission with a bitmask of 2^33 will the server say no?

 

I must admit I haven't tried this out much, as I'm so new to it, but if anyone can offer some insight I'd be very grateful.

 

Thanks.

Link to comment
Share on other sites

Here's how I've done this. First set some constants (this is from a class)

  const BFLG_ISADMIN  = 8;
  const BFLG_CANDELETE= 4;
  const BFLG_CANWRITE = 2;
  const BFLG_CANREAD  = 1;

 

Then I check to see if the user has delete access:

  public function canDelete($field) {
  	return (boolean) (((int)$this->{$field} & (int)db_staff_permissions::BFLG_CANDELETE) == (int)db_staff_permissions::BFLG_CANDELETE);
  }

 

$field that is passed is a value between 0 and 15:

8+4+2+1=15

 

No privileges is 0.

Link to comment
Share on other sites

And 2^33 is 8 589 934 592

In other words 32bitmask has 32bits and not a single one more.

You can however use multiple bitmasks... a bit more complicated but will work...

 

You can also think about more flexible and intuitive solution, like ACL (access control lists)

Link to comment
Share on other sites

Thanks guys, all great answers, I'll look into the multiple bitmasks and ACL like mchl suggests.

 

Just to clarify the situation, I'm working on a CMS/Ecommerce platform which has many modules each with lots of functions, so there will be more than 32 different things a user could potentially do  - at the very least add, approve and delete in each module - i may see how simple it would be to have a bitmask for each module.

 

Will update once I've found what seems to be the best solution.

 

Thanks again.

Link to comment
Share on other sites

Okay, wait it seems like I misunderstood you. Yes, you'll only be able to have 32 permission flags with a 32-bit integer. I was actually talking about the different combinations you could make using these.

 

Anyway, an ACL would be far more superior.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.