jgp4 Posted April 7, 2009 Share Posted April 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153038-how-many-bitmask-values-can-i-have/ Share on other sites More sharing options...
Daniel0 Posted April 7, 2009 Share Posted April 7, 2009 An n bit integer can have 2n different values, so for an unsigned 32-bit integer, that means 0 through 4,294,967,295. If you assign a larger value then it'll overflow. Quote Link to comment https://forums.phpfreaks.com/topic/153038-how-many-bitmask-values-can-i-have/#findComment-803787 Share on other sites More sharing options...
Yesideez Posted April 7, 2009 Share Posted April 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153038-how-many-bitmask-values-can-i-have/#findComment-803790 Share on other sites More sharing options...
Yesideez Posted April 7, 2009 Share Posted April 7, 2009 With that you can replace BFLG_CANDELETE with the constants and make new functions. Quote Link to comment https://forums.phpfreaks.com/topic/153038-how-many-bitmask-values-can-i-have/#findComment-803792 Share on other sites More sharing options...
Mchl Posted April 7, 2009 Share Posted April 7, 2009 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) Quote Link to comment https://forums.phpfreaks.com/topic/153038-how-many-bitmask-values-can-i-have/#findComment-803793 Share on other sites More sharing options...
jgp4 Posted April 7, 2009 Author Share Posted April 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153038-how-many-bitmask-values-can-i-have/#findComment-803849 Share on other sites More sharing options...
Daniel0 Posted April 7, 2009 Share Posted April 7, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/153038-how-many-bitmask-values-can-i-have/#findComment-803869 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.