Skip to content

Instantly share code, notes, and snippets.

@alexisljn
Last active December 21, 2022 08:28
Show Gist options
  • Save alexisljn/ffa8eafe2ccced27a7e5e0424295277b to your computer and use it in GitHub Desktop.
Save alexisljn/ffa8eafe2ccced27a7e5e0424295277b to your computer and use it in GitHub Desktop.
Use of binary mask to create basis of a permissions system
<?php
$permA = 1;
$permB = 2;
$permC = 4;
$permD = 8;
$permE = 16;
$permF = 32;
// Malgré le '|' c'est un set de permissions
$userPermissions = $permA | $permC | $permD | $permF;
echo "User permissions: \n";
var_dump(decbin($userPermissions));
$neededPermissions = $permB | $permF;
echo "Needed permissions: \n";
var_dump(decbin($neededPermissions));
// Si la valeur est identique à $neededPermissions l'utilisateur possède toutes les permissions. Si la valeur est != 0 il en possède une des deux.
var_dump(decbin($neededPermissions & $userPermissions));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment