Skip to content

Instantly share code, notes, and snippets.

@jesobreira
Created August 1, 2015 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesobreira/d68ebb4b2430bb126fb0 to your computer and use it in GitHub Desktop.
Save jesobreira/d68ebb4b2430bb126fb0 to your computer and use it in GitHub Desktop.
Using bitwise operators to store privs
<?php
$user_rights = 3; // 1+2 (taken from database)
if(rights::can_read($user_rights)) echo "User can read\n";
if(rights::can_write($user_rights)) echo "User can write\n";
if(rights::can_modify($user_rights)) echo "User can modify\n";
class rights {
static $READ = 1;
static $WRITE = 2;
static $MODIFY = 4;
public function can_read($rights) {
return (($rights & self::$READ)==self::$READ);
}
public function can_write($rights) {
return (($rights & self::$WRITE)==self::$WRITE);
}
public function can_modify($rights) {
return (($rights & self::$MODIFY)==self::$MODIFY);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment