src/Utils/SecurityUtils.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Utils;
  3. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  4. use Symfony\Component\Security\Core\Security;
  5. use Throwable;
  6. class SecurityUtils
  7. {
  8.     const UNAUTHENTICATED_UID 'unauthenticated';
  9.     const ANONYMOUS_UID 'anon.';
  10.     const DEFAULT_ROLE_FOR_LOGGEDIN_USER "ROLE_USER";
  11.     /**
  12.      * @param TokenStorageInterface $tokenStorageInterface
  13.      * @return string uid or UNAUTHENTICATED_UID
  14.      */
  15.     public static function getUid(TokenStorageInterface $tokenStorageInterface): string
  16.     {
  17.         $result SecurityUtils::UNAUTHENTICATED_UID;
  18.         if ($tokenStorageInterface !== null && $tokenStorageInterface->getToken() !== null && is_object($tokenStorageInterface->getToken()->getUser())) {
  19.             $result $tokenStorageInterface->getToken()->getUser()->getUid();
  20.             if ($result === null || $result == '' || $result == SecurityUtils::ANONYMOUS_UID) {
  21.                 $result SecurityUtils::UNAUTHENTICATED_UID;
  22.             }
  23.         }
  24.         return $result;
  25.     }
  26.     /**
  27.      * @param TokenStorageInterface $tokenStorageInterface
  28.      * @return string objectId or UNAUTHENTICATED_UID
  29.      */
  30.     public static function getObjectId(TokenStorageInterface $tokenStorageInterface): string
  31.     {
  32.         $result SecurityUtils::UNAUTHENTICATED_UID;
  33.         if ($tokenStorageInterface !== null && $tokenStorageInterface->getToken() !== null && is_object($tokenStorageInterface->getToken()->getUser())) {
  34.             $result $tokenStorageInterface->getToken()->getUser()->getObjectId();
  35.             if ($result === null || $result == '' || $result == SecurityUtils::ANONYMOUS_UID) {
  36.                 $result SecurityUtils::UNAUTHENTICATED_UID;
  37.             }
  38.         }
  39.         return $result;
  40.     }
  41.     /**
  42.      * @param TokenStorageInterface $tokenStorageInterface
  43.      * @return string uid or UNAUTHENTICATED_UID
  44.      */
  45.     public static function getFullname(TokenStorageInterface $tokenStorageInterface): string
  46.     {
  47.         $result SecurityUtils::UNAUTHENTICATED_UID;
  48.         if ($tokenStorageInterface !== null && $tokenStorageInterface->getToken() !== null && is_object($tokenStorageInterface->getToken()->getUser())) {
  49.             $result $tokenStorageInterface->getToken()->getUser()->getFullName();
  50.             if ($result === null || $result == '' || $result == SecurityUtils::ANONYMOUS_UID) {
  51.                 $result SecurityUtils::UNAUTHENTICATED_UID;
  52.             }
  53.         }
  54.         return $result;
  55.     }
  56.     public static function getPhoneNumber(TokenStorageInterface $tokenStorageInterface): string
  57.     {
  58.         if ($tokenStorageInterface !== null && $tokenStorageInterface->getToken() !== null && is_object($tokenStorageInterface->getToken()->getUser())) {
  59.             return $tokenStorageInterface->getToken()->getUser()->getUsablePhoneNumber();
  60.         }
  61.         return '';
  62.     }
  63.     /**
  64.      * @param TokenStorageInterface $tokenStorageInterface
  65.      * @return string uid or UNAUTHENTICATED_UID
  66.      */
  67.     public static function getCodeOps(TokenStorageInterface $tokenStorageInterface): string
  68.     {
  69.         $result '';
  70.         if ($tokenStorageInterface !== null && $tokenStorageInterface->getToken() !== null && is_object($tokenStorageInterface->getToken()->getUser())) {
  71.             $result $tokenStorageInterface->getToken()->getUser()->getCompanyCodeOps();
  72.             if ($result === null || $result == '' || $result == SecurityUtils::ANONYMOUS_UID) {
  73.                 $result '';
  74.             }
  75.         }
  76.         return $result;
  77.     }
  78.     /**
  79.      * @param TokenStorageInterface $tokenStorageInterface
  80.      * @return string uid of the company or empty stringD
  81.      */
  82.     public static function getCompanyUid(TokenStorageInterface $tokenStorageInterface): string
  83.     {
  84.         $result '';
  85.         if ($tokenStorageInterface !== null && $tokenStorageInterface->getToken() !== null && is_object($tokenStorageInterface->getToken()->getUser())) {
  86.             $result $tokenStorageInterface->getToken()->getUser()->getMainCompanyUid();
  87.             if ($result === null || $result == SecurityUtils::ANONYMOUS_UID) {
  88.                 $result '';
  89.             }
  90.         }
  91.         return $result;
  92.     }
  93.     /**
  94.      * @param Security $security
  95.      * @return bool and true if authenticated
  96.      */
  97.     public static function isAuthenticated(Security $security): bool
  98.     {
  99.         try {
  100.             return $security->isGranted('IS_AUTHENTICATED_FULLY');
  101.         } catch (Throwable $throwable) {
  102.         }
  103.         return false;
  104.     }
  105.     /**
  106.      * @param TokenStorageInterface $tokenStorageInterface
  107.      * @return string uid or UNAUTHENTICATED_UID
  108.      */
  109.     public static function getContactMail(TokenStorageInterface $tokenStorageInterface): string
  110.     {
  111.         $result SecurityUtils::UNAUTHENTICATED_UID;
  112.         if ($tokenStorageInterface !== null && $tokenStorageInterface->getToken() !== null && is_object($tokenStorageInterface->getToken()->getUser())) {
  113.             $result $tokenStorageInterface->getToken()->getUser()->getContactMail();
  114.             if ($result === null || $result == '') {
  115.                 $result SecurityUtils::UNAUTHENTICATED_UID;
  116.             }
  117.         }
  118.         return $result;
  119.     }
  120.     /**
  121.      * @param array $roles
  122.      * @param Security $security
  123.      * @return string: representing all roles accessible as json
  124.      */
  125.     public static function getAuthorizationAsJson(array $rolesSecurity $security): string
  126.     {
  127.         $roleAccess = [];
  128.         foreach ($roles as $mainRole => $subRoles) {
  129.             if ($security->isGranted($mainRole)) {
  130.                 array_push($roleAccess$mainRole);
  131.             }
  132.             foreach ($subRoles as $role) {
  133.                 array_push($roleAccess$role);
  134.             }
  135.         }
  136.         $roleAccess array_unique($roleAccess);
  137.         $roleAccess array_filter(
  138.             $roleAccess, function ($var) use ($security) {
  139.             return $security->isGranted($var);
  140.         }
  141.         );
  142.         sort($roleAccess);
  143.         return \GuzzleHttp\json_encode($roleAccess);
  144.     }
  145.     public static function hasAtLeastOneRole(
  146.         Security $security,
  147.         string $rolesDelimitedByColon
  148.     ): bool {
  149.         if ($rolesDelimitedByColon == '' || empty($rolesDelimitedByColon)) {
  150.             return true;
  151.         }
  152.         $roles explode(','$rolesDelimitedByColon);
  153.         foreach ($roles as $role) {
  154.             if ($security->isGranted($role)) {
  155.                 return true;
  156.             }
  157.         }
  158.         return false;
  159.     }
  160. }