Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could use a <a href="http://en.wikipedia.org/wiki/Mask_(computing)" rel="nofollow noreferrer">bitmask</a>.</p> <p>If all your permissions are (or can be generalised to) a set of yes/no conditions, it's quite easy.</p> <p>In your example, you have Create, Read, Update and Delete. That's 4 bits, so you need a 4-bit number to store permissions. (0000 to 1111 in binary = 0 to 15 in decimal)</p> <p>Someone who can only read would have permissions 0100 (4 in decimal), and someone who can create/read/update would have persmissions 1110 (14 in decimal). Administrators who have full access would have persmissions 1111 (15 in decimal).</p> <p>The way you would check these in PHP would be with the bitwise OR operator <code>|</code>.</p> <p>For example</p> <pre><code>// you could write a function getUserPermission($strUsername) // which returns a permission number, say 10 (1010 in binary) // which means he/she can create/update but not read/delete $userPermissions = getUserPermission("TedWong"); $permissionCreate = 8; // 1000; $permissionRead = 4; // 0100; $permissionUpdate = 2; // 0010; $permissionDelete = 1; // 0001; if ($userPermissions | $permissionCreate) { //user has permission to create } if ($userPermissions | $permissionRead) { //User has permission to read } if (!($userPermissions | $permissionDelete)) { //User doesn't have permission to delete } if ($userPermissions | $permissionUpdate &amp;&amp; $userPermissions | $permissionCreate) { //User has permission to create and update. } </code></pre> <p>If you want more permissions, you just need to introduce more bits.</p> <p>As for your update all posts/delete own posts in your example, I would have a 5-bit permission structure: Create, Read, Delete, UpdateOwn, UpdateAll.</p> <p>You can have many permissions, but would be limited by the data structure storing them. For example, if you are storing the permission mask in a 32-bit integer, then you can only have up to 32 permissions.</p> <p>here is a full list of 4-bit permissions for your example:</p> <pre><code>0000 // 0: No Permissions 0001 // 1: Delete 0010 // 2: Update 0011 // 3: Delete + Update 0100 // 4: Read 0101 // 5: Read + Delete 0110 // 6: Read + Update 0111 // 7: Read + Delete + Update 1000 // 8: Create 1001 // 9: Create + Delete 1010 // 10: Create + Update 1011 // 11: Create + Delete + Update 1100 // 12: Create + Read 1101 // 13: Create + Read + Delete 1110 // 14: Create + Read + Update 1111 // 15: Create + Read + Delete + Update </code></pre> <p>So that means the <i>INTEGER</i> <b>6</b> (which in <i>BINARY</i> is equal to <b>0110</b>) gives permissions Read/Update but not Create/Delete. In the same way each <i>integer</i> has a set of permissions associated with it. You can store up to as many permissions in the integer as many bits there are that represent that integer (usually 32).</p> <p>So you can see that with a 4-bit integer (decimal numbers 0 to 15) can give you 4 yes/no permissions. If you use a 32-but integer then you can have up to 32 yes/no permissions.</p> <p>Check the <a href="http://php.net/manual/en/language.types.integer.php" rel="nofollow noreferrer">PHP documentation</a> on how to determine the maximum size of your integers. (It depends on the platform you're running your PHP parser on). I think generally speaking a 32-bit system/OS will allow for 32-bit integers, and 64-bit system/OS will allow for 64-bit integers.</p> <p>Check <a href="https://stackoverflow.com/search?q=php+bitmask+permissions">these other threads</a> on SO for discussions on pros/cons of using bitmasks vs other methods.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload