src/Service/PublicUserPermissionService.php line 81

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use Pimcore\Model\DataObject;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use App\Service\UserPermission;
  7. class PublicUserPermissionService
  8. {
  9.     public $userPermission;
  10.     public function __construct()
  11.     {
  12.         $this->userPermission  = new UserPermission();
  13.     }
  14.     public function publicUserPermissionCheck($user$parameters$translator)
  15.     {
  16.         try {
  17.             $missingPermissions = [];
  18.             $userEmail = [];
  19.             $userEmail['username'] = $user->getEmail();
  20.             $permissions $this->userPermission->getUserPermissions($userEmail$translator);
  21.             if(!$permissions['success']){
  22.                 return ["success" => false"message" => $permissions['message']];
  23.             }
  24.             foreach ($parameters as $parameter) {
  25.                 $parameterMatch false// Initialize to false for each parameter
  26.                 foreach ($permissions['grants'] as $key => $permission) {
  27.                     if (strpos($parameter$key) !== false) {
  28.                         $parameterMatch true;
  29.                         break; // If a match is found, no need to check further permissions for this parameter
  30.                     }
  31.                 }
  32.                 if (!$parameterMatch) {
  33.                     $missingPermissions[] = $parameter;
  34.                 }
  35.             }
  36.             if (!empty($missingPermissions)) {
  37.                 // User is missing permissions for these parameters
  38.                 $message sprintf($translator->trans('User is not allowed to access the following parameters: %s'), implode(", "$missingPermissions));
  39.                 return ["success" => false"message" => $message];
  40.                 // You can return the message and set success to false
  41.             } else {
  42.                 // User has all required permissions
  43.                 return ["success" => true"message" => $translator->trans("User has the required permissions.")];
  44.                 // You can return a success message if the user has all permissions
  45.             }
  46.         } catch (\Exception $ex) {
  47.             throw new \Exception($ex->getMessage());
  48.         }
  49.     }
  50.     private function checkCredentials(string $usernamestring $password$translator)
  51.     {
  52.         // Attempt to retrieve the PublicUser object by username
  53.         $publicUser \Pimcore\Model\DataObject\Customer::getByUserId($usernametrue);
  54.         if ($publicUser instanceof \Pimcore\Model\DataObject\Customer) {
  55.             // Compare the submitted password with the stored value
  56.             $storedSecretKey $publicUser->getSecretKey();
  57.             if ($password === $storedSecretKey) {
  58.                 return ["success" => true"message" => $translator->trans("Authentication successful"), "user" => $publicUser];
  59.             } else {
  60.                 // Password mismatch
  61.                 return ["success" => false"message" => $translator->trans("Invalid password")];
  62.             }
  63.         } else {
  64.             // User not found
  65.             return ["success" => false"message" => $translator->trans("User not found")];
  66.         }
  67.     }
  68.     public function isAuthorized(Request $request$translator)
  69.     {
  70.         // Check if Basic Authentication headers are present
  71.         $authHeader $request->headers->get('Authorization');
  72.         if (!$authHeader || strpos($authHeader'Basic ') !== 0) {
  73.             return ["success" => false"message" => "Unauthorized"];
  74.         }
  75.         // Extract the username and password from the Authorization header
  76.         list($username$password) = explode(':'base64_decode(substr($authHeader6)), 2);
  77.         // Validate username and password (you should implement your authentication logic here)
  78.         $response $this->checkCredentials($username$password$translator);
  79.         if ($response["success"]) {
  80.             // Perform your API logic here
  81.             return ["success" => true"user" => $response["user"]];
  82.         }
  83.         // Authentication failed, return a response with the error message
  84.         return ["success" => false"message" => $response["message"]]; // Return the error message for false case
  85.     }
  86. }