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