<?phpnamespace App\Utils;use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;use Symfony\Component\Security\Core\Security;use Throwable;class SecurityUtils{ const UNAUTHENTICATED_UID = 'unauthenticated'; const ANONYMOUS_UID = 'anon.'; const DEFAULT_ROLE_FOR_LOGGEDIN_USER = "ROLE_USER"; /** * @param TokenStorageInterface $tokenStorageInterface * @return string uid or UNAUTHENTICATED_UID */ public static function getUid(TokenStorageInterface $tokenStorageInterface): string { $result = SecurityUtils::UNAUTHENTICATED_UID; if ($tokenStorageInterface !== null && $tokenStorageInterface->getToken() !== null && is_object($tokenStorageInterface->getToken()->getUser())) { $result = $tokenStorageInterface->getToken()->getUser()->getUid(); if ($result === null || $result == '' || $result == SecurityUtils::ANONYMOUS_UID) { $result = SecurityUtils::UNAUTHENTICATED_UID; } } return $result; } /** * @param TokenStorageInterface $tokenStorageInterface * @return string objectId or UNAUTHENTICATED_UID */ public static function getObjectId(TokenStorageInterface $tokenStorageInterface): string { $result = SecurityUtils::UNAUTHENTICATED_UID; if ($tokenStorageInterface !== null && $tokenStorageInterface->getToken() !== null && is_object($tokenStorageInterface->getToken()->getUser())) { $result = $tokenStorageInterface->getToken()->getUser()->getObjectId(); if ($result === null || $result == '' || $result == SecurityUtils::ANONYMOUS_UID) { $result = SecurityUtils::UNAUTHENTICATED_UID; } } return $result; } /** * @param TokenStorageInterface $tokenStorageInterface * @return string uid or UNAUTHENTICATED_UID */ public static function getFullname(TokenStorageInterface $tokenStorageInterface): string { $result = SecurityUtils::UNAUTHENTICATED_UID; if ($tokenStorageInterface !== null && $tokenStorageInterface->getToken() !== null && is_object($tokenStorageInterface->getToken()->getUser())) { $result = $tokenStorageInterface->getToken()->getUser()->getFullName(); if ($result === null || $result == '' || $result == SecurityUtils::ANONYMOUS_UID) { $result = SecurityUtils::UNAUTHENTICATED_UID; } } return $result; } public static function getPhoneNumber(TokenStorageInterface $tokenStorageInterface): string { if ($tokenStorageInterface !== null && $tokenStorageInterface->getToken() !== null && is_object($tokenStorageInterface->getToken()->getUser())) { return $tokenStorageInterface->getToken()->getUser()->getUsablePhoneNumber(); } return ''; } /** * @param TokenStorageInterface $tokenStorageInterface * @return string uid or UNAUTHENTICATED_UID */ public static function getCodeOps(TokenStorageInterface $tokenStorageInterface): string { $result = ''; if ($tokenStorageInterface !== null && $tokenStorageInterface->getToken() !== null && is_object($tokenStorageInterface->getToken()->getUser())) { $result = $tokenStorageInterface->getToken()->getUser()->getCompanyCodeOps(); if ($result === null || $result == '' || $result == SecurityUtils::ANONYMOUS_UID) { $result = ''; } } return $result; } /** * @param TokenStorageInterface $tokenStorageInterface * @return string uid of the company or empty stringD */ public static function getCompanyUid(TokenStorageInterface $tokenStorageInterface): string { $result = ''; if ($tokenStorageInterface !== null && $tokenStorageInterface->getToken() !== null && is_object($tokenStorageInterface->getToken()->getUser())) { $result = $tokenStorageInterface->getToken()->getUser()->getMainCompanyUid(); if ($result === null || $result == SecurityUtils::ANONYMOUS_UID) { $result = ''; } } return $result; } /** * @param Security $security * @return bool and true if authenticated */ public static function isAuthenticated(Security $security): bool { try { return $security->isGranted('IS_AUTHENTICATED_FULLY'); } catch (Throwable $throwable) { } return false; } /** * @param TokenStorageInterface $tokenStorageInterface * @return string uid or UNAUTHENTICATED_UID */ public static function getContactMail(TokenStorageInterface $tokenStorageInterface): string { $result = SecurityUtils::UNAUTHENTICATED_UID; if ($tokenStorageInterface !== null && $tokenStorageInterface->getToken() !== null && is_object($tokenStorageInterface->getToken()->getUser())) { $result = $tokenStorageInterface->getToken()->getUser()->getContactMail(); if ($result === null || $result == '') { $result = SecurityUtils::UNAUTHENTICATED_UID; } } return $result; } /** * @param array $roles * @param Security $security * @return string: representing all roles accessible as json */ public static function getAuthorizationAsJson(array $roles, Security $security): string { $roleAccess = []; foreach ($roles as $mainRole => $subRoles) { if ($security->isGranted($mainRole)) { array_push($roleAccess, $mainRole); } foreach ($subRoles as $role) { array_push($roleAccess, $role); } } $roleAccess = array_unique($roleAccess); $roleAccess = array_filter( $roleAccess, function ($var) use ($security) { return $security->isGranted($var); } ); sort($roleAccess); return \GuzzleHttp\json_encode($roleAccess); } public static function hasAtLeastOneRole( Security $security, string $rolesDelimitedByColon ): bool { if ($rolesDelimitedByColon == '' || empty($rolesDelimitedByColon)) { return true; } $roles = explode(',', $rolesDelimitedByColon); foreach ($roles as $role) { if ($security->isGranted($role)) { return true; } } return false; }}