zeusakm
zeusakm

Reputation: 3

Bitwise mask - how to set different opts?

I use a bitmask for access to application and I've got an array:

  $arr['view_info'] = 2;
  $arr['view_terminals'] = 4;
  $arr['view_payments'] = 6;
  $arr['view_subdealers'] = 8;
  $arr['view_providers'] = 10;
  $arr['view_users'] = 12;
  $arr['view_reports'] = 14;

So, the question is - how can I add permissions, for example - view_terminals and view_reports without permit an access to opts between 4 and 14?

Just the last Q - how to add more than 8 permissions, as I know we have 255 max value in binary sys - so the last one is 128? I've heard about groups.

Upvotes: 0

Views: 423

Answers (2)

deceze
deceze

Reputation: 522322

With those values that's pretty hard to do. Your bitmask values should be powers of 2, i.e. 1, 2, 4, 8, 16, 32, 64, 128, 256 etc. Then you can do $arr['view_terminals'] | $arr['view_reports'].

Upvotes: 2

user180100
user180100

Reputation:

In order to use bitmasks, you need to have the value of your array be power of 2. see http://en.wikipedia.org/wiki/Mask_(computing)

in your case:

$arr['view_info'] = 1;            // 0000000001
$arr['view_terminals'] = 2;       // 0000000010
$arr['view_payments'] = 4;        // 0000000100
$arr['view_subdealers'] = 8;      // 0000001000
$arr['view_providers'] = 16;      // 0000010000 
$arr['view_users'] = 32;          // 0000100000
$arr['view_reports'] = 64;        // 0001000000

And set user permission to 2+64 for your user (0001000010)

Upvotes: 2

Related Questions