CustomerAuthorization.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\Plugin;
  7. use Magento\Authorization\Model\UserContextInterface;
  8. use Magento\Integration\Api\AuthorizationServiceInterface as AuthorizationService;
  9. /**
  10. * Plugin around \Magento\Framework\Authorization::isAllowed
  11. *
  12. * Plugin to allow customer users to access resources with self permission
  13. */
  14. class CustomerAuthorization
  15. {
  16. /**
  17. * @var UserContextInterface
  18. */
  19. protected $userContext;
  20. /**
  21. * Inject dependencies.
  22. *
  23. * @param UserContextInterface $userContext
  24. */
  25. public function __construct(UserContextInterface $userContext)
  26. {
  27. $this->userContext = $userContext;
  28. }
  29. /**
  30. * Check if resource for which access is needed has self permissions defined in webapi config.
  31. *
  32. * @param \Magento\Framework\Authorization $subject
  33. * @param callable $proceed
  34. * @param string $resource
  35. * @param string $privilege
  36. *
  37. * @return bool true If resource permission is self, to allow
  38. * customer access without further checks in parent method
  39. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  40. */
  41. public function aroundIsAllowed(
  42. \Magento\Framework\Authorization $subject,
  43. \Closure $proceed,
  44. $resource,
  45. $privilege = null
  46. ) {
  47. if ($resource == AuthorizationService::PERMISSION_SELF
  48. && $this->userContext->getUserId()
  49. && $this->userContext->getUserType() === UserContextInterface::USER_TYPE_CUSTOMER
  50. ) {
  51. return true;
  52. } else {
  53. return $proceed($resource, $privilege);
  54. }
  55. }
  56. }