views:

12

answers:

1

Alright, Here's the gist of what I'm planning to do.

I'm going to have two tables. One with "ranks" or "roles" and one with users. I want to assign permissions on a role/user basis and retract permissions on a user basis.

So, just for general purposes lets say that we have $role_can $user_can and $user_cant

I know that for specifying which purposes, we can use bitwise OR. $permissions = ($role_can | $user_can) and that will combine the two.

Now, I want to be able to retract certain permissions after that. This is NORMALLY done using a bitwise XOR, but I want to make it idiot proof. In other words, I don't want to accidentally give them access to a permission by excluding a permission they don't already own.

What is the best way to do this?

Traditionally (($role_can | $user_can) ^ $user_cant) - but it won't work for the idiot proofing.

I'm still pretty new to bit operations, so go easy on me if the answer is obvious.

I'm using PHP, but an acceptable answer can be in any language / pseudo-code so long as it works and is easily understood.

Thanks.

+1  A: 

You can use the bitwise "not" operator.

(($role_can | $user_can) & ~$user_cant)
Dean Harding
AND NOT. Thank you. I was trying all kinds of different combinations and couldn't mentally figure it out. I previously thought NOT wouldn't work because, well, it'd set everything else wrong, but I see how it works in combination with AND. Thank you.
Navarr