UNIX-like permission calculation
Posted: Thu Nov 19, 2009 1:03 pm
Hello out there!
First, sorry for my english, I'm not good at explaining stuff like this in English.
I want to implement a permission-layer in my website. The plan is to add one field to the user-table, called "permissions". This field is a integer.
By using the same method as the UNIX-file permission, where:
1 = Execute
2 = Write
4 = Read
The permission number (e.g. 7) represents the permissions. By adding each permissions number to the total permission number, you get the permission number.
Say, I add four more permissions:
8 = Delete
16 = Do other stuff
32 = Yet another permission
64 = The last permission
You can calculate what permissions are granted if you have the permission number 85:
64 (The last permission), 16 (Do other stuff), 4 (read) and 1 (Execute) because: 64+16+4+1 = 85
However, I don't know HOW to extract the numbers 64+16+4+1 from 85.
How do I calculate the permissions? It has to be dynamic, so, say I call a function with the number "548" and it returns an array with the permissions (512, 32, 4)
I want to implement a permission-layer in my website. The plan is to add one field to the user-table, called "permissions". This field is a integer.
By using the same method as the UNIX-file permission, where:
1 = Execute
2 = Write
4 = Read
The permission number (e.g. 7) represents the permissions. By adding each permissions number to the total permission number, you get the permission number.
Say, I add four more permissions:
8 = Delete
16 = Do other stuff
32 = Yet another permission
64 = The last permission
You can calculate what permissions are granted if you have the permission number 85:
64 (The last permission), 16 (Do other stuff), 4 (read) and 1 (Execute) because: 64+16+4+1 = 85
However, I don't know HOW to extract the numbers 64+16+4+1 from 85.
How do I calculate the permissions? It has to be dynamic, so, say I call a function with the number "548" and it returns an array with the permissions (512, 32, 4)