WebapiRoleLocator.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Webapi\Model;
  7. use Magento\Authorization\Model\ResourceModel\Role\CollectionFactory as RoleCollectionFactory;
  8. use Magento\Authorization\Model\Role;
  9. use Magento\Authorization\Model\UserContextInterface;
  10. use Magento\Framework\Authorization\RoleLocatorInterface;
  11. class WebapiRoleLocator implements RoleLocatorInterface
  12. {
  13. /**
  14. * @var UserContextInterface
  15. */
  16. protected $userContext;
  17. /**
  18. * @var RoleCollectionFactory
  19. */
  20. protected $roleCollectionFactory;
  21. /**
  22. * Constructs a role locator using the user context.
  23. *
  24. * @param UserContextInterface $userContext
  25. * @param RoleCollectionFactory $roleCollectionFactory
  26. */
  27. public function __construct(
  28. UserContextInterface $userContext,
  29. RoleCollectionFactory $roleCollectionFactory
  30. ) {
  31. $this->userContext = $userContext;
  32. $this->roleCollectionFactory = $roleCollectionFactory;
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function getAclRoleId()
  38. {
  39. $userId = $this->userContext->getUserId();
  40. $userType = $this->userContext->getUserType();
  41. $roleCollection = $this->roleCollectionFactory->create();
  42. /** @var Role $role */
  43. $role = $roleCollection->setUserFilter($userId, $userType)->getFirstItem();
  44. if (!$role->getId()) {
  45. return null;
  46. }
  47. return $role->getId();
  48. }
  49. }