Skip to content

Instantly share code, notes, and snippets.

@minte9
Last active April 14, 2021 08:24
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 minte9/5814e8f5a6b29dc93e8101ed5dd4292c to your computer and use it in GitHub Desktop.
Save minte9/5814e8f5a6b29dc93e8101ed5dd4292c to your computer and use it in GitHub Desktop.
<?php
/**
* Given the users $role and access $rights ...
* Check if Mike has admin rights and downgrade him
* Wrong approach:
* if ($is_admin && $can_edit_articles ...
*/
$rights = (object) ["read"=>1, "write"=>2, "readwrite"=>16, "admin"=> 32];
$role = (object) ["jim"=>96, "mike"=>32];
/**
* SOLUTION
*/
echo $role->mike & 32; // 32
if ($role->mike & 32) {
$role->mike = $role->mike >> 1;
}
echo $role->mike; // 16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment