20 July 2005

How the Integer-masked permission works

Honestly, it's so simple. Let's say we have the following permissions such as read, write, create, delete and administration permissions. We'll assign it like:



administer = 2^0
read = 2^1
write = 2^2
create = 2^3
delete = 2^4


Using the logical inclusive OR ("|") operator we can come up with a series of logical combinations.


read, write = 2 | 4

0010
0100
====
0110 = 6 (READ,WRITE)

read,write,create = 2 | 4 | 8

0010
0100
====
0110
1000
====
1110 = 14 (READ,WRITE,CREATE)

read,write,create,delete = 2 | 4 | 8 | 16

00010
00100
=====
00110
01000
=====
01110
10000
11110 = 30 (READ,WRITE,CREATE,DELETE)

and the list can go on...


Not perfect, but better than anything else. So the only thing needed on any permission tables are the actual integer permission values.

No comments: