public function testProcessConfigBasedIntegrationsRecreateUpdatedConfigAfterResourceChange() { $originalData = [Integration::ID => 1, Integration::NAME => 'TestIntegration1', Integration::EMAIL => '*****@*****.**', Integration::ENDPOINT => 'http://endpoint.com', Integration::IDENTITY_LINK_URL => 'http://www.example.com/identity', Integration::SETUP_TYPE => 1]; $integrations = ['TestIntegration1' => [Integration::EMAIL => '*****@*****.**', Integration::ENDPOINT => 'http://endpoint.com', Integration::IDENTITY_LINK_URL => 'http://www.example.com/identity', 'resources' => ['Magento_Customer::manage', 'Magento_Customer::customer']]]; $originalResources = ['Magento_Customer::manage']; $newResources = ['Magento_Customer::manage', 'Magento_Customer::customer']; $integrationObject = $this->getMockBuilder('Magento\\Integration\\Model\\Integration')->disableOriginalConstructor()->setMethods([])->getMock(); // Integration already exists, so update with new data and recreate $this->integrationServiceMock->expects($this->at(0))->method('findByName')->with('TestIntegration1')->will($this->returnValue($integrationObject)); $this->aclRetriever->expects($this->once())->method('getAllowedResourcesByUser')->willReturn($originalResources); $integrationObject->expects($this->any())->method('getId')->willReturn($originalData[Integration::ID]); $this->integrationServiceMock->expects($this->once())->method('update')->willReturn($integrationObject); $integrationObject->expects($this->once())->method('getOrigData')->willReturn($originalData); $integrationObject->expects($this->once())->method('getData')->willReturn($newResources); $this->integrationServiceMock->expects($this->once())->method('create'); $this->integrationManager->processConfigBasedIntegrations($integrations); }
public function testAfterGet() { $integrationId = 1; $integrationModelMock = $this->getMockBuilder('Magento\\Integration\\Model\\Integration')->disableOriginalConstructor()->getMock(); $integrationModelMock->expects($this->exactly(2))->method('getId')->will($this->returnValue($integrationId)); $integrationModelMock->expects($this->once())->method('setData')->with('resource', ['testResource']); $this->aclRetrieverMock->expects($this->once())->method('getAllowedResourcesByUser')->with(\Magento\Authorization\Model\UserContextInterface::USER_TYPE_INTEGRATION, $integrationId)->will($this->returnValue(['testResource'])); $this->integrationPlugin->afterGet($this->subjectMock, $integrationModelMock); }
/** * Add the list of allowed resources to the integration object data by 'resource' key. * * @param IntegrationModel $integration * @return void */ protected function _addAllowedResources(IntegrationModel $integration) { if ($integration->getId()) { if ($integration->getSetupType() == IntegrationModel::TYPE_CONFIG) { $integration->setData('resource', $this->getIntegrationApiResource($integration)); } else { $integration->setData('resource', $this->aclRetriever->getAllowedResourcesByUser(UserContextInterface::USER_TYPE_INTEGRATION, (int) $integration->getId())); } } }
/** * Add the list of allowed resources to the integration object data by 'resource' key. * * @param IntegrationModel $integration * @return void */ protected function _addAllowedResources(IntegrationModel $integration) { $integrations = array_merge($this->integrationConfig->getIntegrations(), $this->consolidatedConfig->getIntegrations()); if ($integration->getId()) { if ($integration->getSetupType() == IntegrationModel::TYPE_CONFIG) { $integration->setData('resource', $integrations[$integration->getData('name')]['resource']); } else { $integration->setData('resource', $this->aclRetriever->getAllowedResourcesByUser(UserContextInterface::USER_TYPE_INTEGRATION, (int) $integration->getId())); } } }
/** * Process integrations from config files for the given array of integration names * * @param array $integrations * @return array */ public function processIntegrationConfig(array $integrations) { if (empty($integrations)) { return []; } /** @var array $integrationsResource */ $integrationsResource = $this->integrationConfig->getIntegrations(); foreach (array_keys($integrations) as $name) { $integrationDetails = $integrations[$name]; $integrationData = [Integration::NAME => $name]; if (isset($integrationDetails[Converter::KEY_EMAIL])) { $integrationData[Integration::EMAIL] = $integrationDetails[Converter::KEY_EMAIL]; } if (isset($integrationDetails[Converter::KEY_AUTHENTICATION_ENDPOINT_URL])) { $integrationData[Integration::ENDPOINT] = $integrationDetails[Converter::KEY_AUTHENTICATION_ENDPOINT_URL]; } if (isset($integrationDetails[Converter::KEY_IDENTITY_LINKING_URL])) { $integrationData[Integration::IDENTITY_LINK_URL] = $integrationDetails[Converter::KEY_IDENTITY_LINKING_URL]; } if (isset($integrationsResource[$name]['resource'])) { $integrationData['resource'] = $integrationsResource[$name]['resource']; } $integrationData[Integration::SETUP_TYPE] = Integration::TYPE_CONFIG; $integration = $this->integrationService->findByName($name); if ($integration->getId()) { $originalResources = $this->aclRetriever->getAllowedResourcesByUser(UserContextInterface::USER_TYPE_INTEGRATION, $integration->getId()); $updateData = $integrationData; $updateData[Integration::ID] = $integration->getId(); $integration = $this->integrationService->update($updateData); // If there were any changes, delete then recreate integration if ($this->hasDataChanged($integration, $originalResources)) { $this->integrationService->delete($integration->getId()); $integrationData[Integration::STATUS] = Integration::STATUS_RECREATED; } else { continue; } } $this->integrationService->create($integrationData); } return $integrations; }
public function testAfterGet() { $integrationId = 1; $integrationModelMock = $this->getMockBuilder('Magento\\Integration\\Model\\Integration')->disableOriginalConstructor()->getMock(); $integrationModelMock->expects($this->exactly(2))->method('getId')->will($this->returnValue($integrationId)); $integrationModelMock->expects($this->once())->method('setData')->with('resource', ['testResource']); $deprecatedIntegrationsData = [Integration::ID => $integrationId, Integration::NAME => 'TestIntegration1', Integration::EMAIL => '*****@*****.**', Integration::ENDPOINT => 'http://endpoint.com', Integration::SETUP_TYPE => 1, 'resource' => ['testResource']]; $consolidatedIntegrationsData = [Integration::ID => 2, Integration::NAME => 'TestIntegration2', Integration::EMAIL => '*****@*****.**', Integration::ENDPOINT => 'http://endpoint2.com', Integration::SETUP_TYPE => 1, 'resource' => ['testResource']]; $this->integrationConfigMock->method('getIntegrations')->willReturn($deprecatedIntegrationsData); $this->consolidatedConfigMock->method('getIntegrations')->willReturn($consolidatedIntegrationsData); $this->aclRetrieverMock->expects($this->once())->method('getAllowedResourcesByUser')->with(\Magento\Authorization\Model\UserContextInterface::USER_TYPE_INTEGRATION, $integrationId)->will($this->returnValue(['testResource'])); $this->integrationPlugin->afterGet($this->subjectMock, $integrationModelMock); }
/** * Get selected resources * * @return array|mixed|\string[] */ public function getSelectedResources() { $selectedResources = $this->getData('selected_resources'); if (empty($selectedResources)) { $allResource = $this->getCoreRegistry()->registry(SaveRole::RESOURCE_ALL_FORM_DATA_SESSION_KEY); if ($allResource) { $selectedResources = [$this->_rootResource->getId()]; } else { $selectedResources = $this->getCoreRegistry()->registry(SaveRole::RESOURCE_FORM_DATA_SESSION_KEY); } if (null === $selectedResources) { $rid = $this->_request->getParam('rid', false); $selectedResources = $this->_aclRetriever->getAllowedResourcesByRole($rid); } $this->setData('selected_resources', $selectedResources); } return $selectedResources; }
/** * Class constructor * * @return void */ protected function _construct() { parent::_construct(); $rid = $this->_request->getParam('rid', false); $this->setSelectedResources($this->_aclRetriever->getAllowedResourcesByRole($rid)); }
public function testGetAllowedResourcesByUser() { $this->roleMock->expects($this->any())->method('getId')->will($this->returnValue(1)); $expectedResources = ['Magento_Backend::dashboard', 'Magento_Cms::page']; $this->assertEquals($expectedResources, $this->aclRetriever->getAllowedResourcesByUser(UserContextInterface::USER_TYPE_INTEGRATION, 1)); }
/** * Add the list of allowed resources to the integration object data by 'resource' key. * * @param IntegrationModel $integration * @return void */ protected function _addAllowedResources(IntegrationModel $integration) { if ($integration->getId()) { $integration->setData('resource', $this->aclRetriever->getAllowedResourcesByUser(UserContextInterface::USER_TYPE_INTEGRATION, (int) $integration->getId())); } }