Last active
December 21, 2022 08:28
-
-
Save alexisljn/ffa8eafe2ccced27a7e5e0424295277b to your computer and use it in GitHub Desktop.
Use of binary mask to create basis of a permissions system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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