/** * @return $this */ protected function _prepareCollection() { $collection = $this->_userRolesFactory->create(); $collection->setRolesFilter(); $this->setCollection($collection); return parent::_prepareCollection(); }
/** * {@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(); }
protected function setUp() { $this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $userId = 'userId'; $userType = 'userType'; $this->userContext = $this->getMockBuilder('Magento\\Authorization\\Model\\CompositeUserContext')->disableOriginalConstructor()->setMethods(['getUserId', 'getUserType'])->getMock(); $this->userContext->expects($this->once())->method('getUserId')->will($this->returnValue($userId)); $this->userContext->expects($this->once())->method('getUserType')->will($this->returnValue($userType)); $this->roleCollectionFactory = $this->getMockBuilder('Magento\\Authorization\\Model\\ResourceModel\\Role\\CollectionFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); $this->roleCollection = $this->getMockBuilder('Magento\\Authorization\\Model\\ResourceModel\\Role\\Collection')->disableOriginalConstructor()->setMethods(['setUserFilter', 'getFirstItem'])->getMock(); $this->roleCollectionFactory->expects($this->once())->method('create')->will($this->returnValue($this->roleCollection)); $this->roleCollection->expects($this->once())->method('setUserFilter')->with($userId, $userType)->will($this->returnValue($this->roleCollection)); $this->role = $this->getMockBuilder('Magento\\Authorization\\Model\\Role')->disableOriginalConstructor()->setMethods(['getId', '__wakeup'])->getMock(); $this->roleCollection->expects($this->once())->method('getFirstItem')->will($this->returnValue($this->role)); $this->locator = $this->_objectManager->getObject('Magento\\Webapi\\Model\\WebapiRoleLocator', ['userContext' => $this->userContext, 'roleCollectionFactory' => $this->roleCollectionFactory]); }
/** * Identify authorization role associated with provided integration. * * @param int $integrationId * @return \Magento\Authorization\Model\Role|false Return false in case when no role associated with user was found. */ protected function _getUserRole($integrationId) { $roleCollection = $this->_roleCollectionFactory->create(); /** @var Role $role */ $role = $roleCollection->setUserFilter($integrationId, UserContextInterface::USER_TYPE_INTEGRATION)->getFirstItem(); return $role->getId() ? $role : false; }
/** * Identify user role from user identifier. * * @param string $userType * @param int $userId * @return \Magento\Authorization\Model\Role|bool False if no role associated with provided user was found. * @throws \LogicException */ protected function _getUserRole($userType, $userId) { if (!$this->_canRoleBeCreatedForUserType($userType)) { throw new \LogicException("The role with user type '{$userType}' does not exist and cannot be created"); } $roleCollection = $this->roleCollectionFactory->create(); /** @var Role $role */ $role = $roleCollection->setUserFilter($userId, $userType)->getFirstItem(); return $role->getId() ? $role : false; }