/**
  * Process integrations from config files for the given array of integration names
  *
  * @param array $integrationNames
  * @return array
  */
 public function processIntegrationConfig(array $integrationNames)
 {
     if (empty($integrationNames)) {
         return [];
     }
     /** @var array $integrations */
     $integrations = $this->_integrationConfig->getIntegrations();
     foreach ($integrationNames 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];
         }
         $integrationData[Integration::SETUP_TYPE] = Integration::TYPE_CONFIG;
         // If it already exists, update it
         $integration = $this->_integrationService->findByName($name);
         if ($integration->getId()) {
             //If Integration already exists, update it.
             //For now we will just overwrite the integration with same name but we will need a long term solution
             $integrationData[Integration::ID] = $integration->getId();
             $this->_integrationService->update($integrationData);
         } else {
             $this->_integrationService->create($integrationData);
         }
     }
     return $integrationNames;
 }
 /**
  * @param int|null $integrationId
  * @param array $oauthRequest
  * @return void
  */
 public function setupUserId($integrationId, $oauthRequest)
 {
     $integration = $this->getMockBuilder('Magento\\Integration\\Model\\Integration')->disableOriginalConstructor()->setMethods(['getId', '__wakeup'])->getMock();
     $this->integrationService->expects($this->any())->method('findActiveIntegrationByConsumerId')->will($this->returnValue($integration));
     $this->oauthRequestHelper->expects($this->once())->method('prepareRequest')->will($this->returnValue($oauthRequest));
     $this->oauthService->expects($this->any())->method('validateAccessTokenRequest')->will($this->returnValue(1));
     $integration->expects($this->any())->method('getId')->will($this->returnValue($integrationId));
 }
Example #3
0
 /**
  * Set the selected resources, which is an array of resource ids.
  *
  * If everything is allowed, the array will contain just the root resource id, which is "Magento_Backend::all".
  *
  * @return void
  */
 protected function _construct()
 {
     parent::_construct();
     $integrationData = $this->_coreRegistry->registry(IntegrationController::REGISTRY_KEY_CURRENT_INTEGRATION);
     if (is_array($integrationData) && isset($integrationData['integration_id']) && $integrationData['integration_id']) {
         $this->_selectedResources = $this->integrationService->getSelectedResources($integrationData['integration_id']);
     } else {
         $this->_selectedResources = [];
     }
 }
 /**
  * Check whether all indices are valid or not
  *
  * @return bool
  */
 public function isDisplayed()
 {
     foreach (array_keys($this->integrationConfig->getIntegrations()) as $name) {
         $integration = $this->integrationService->findByName($name);
         if ($integration->getStatus() == Integration::STATUS_RECREATED) {
             return true;
         }
     }
     return false;
 }
Example #5
0
 /**
  * @return void
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function testAfterProcessConfigBasedIntegrationsSuccess()
 {
     $firstInegrationId = 1;
     $integrationsData1 = ['id' => $firstInegrationId, Integration::NAME => 'TestIntegration1', Integration::EMAIL => '*****@*****.**', Integration::ENDPOINT => 'http://endpoint.com', Integration::SETUP_TYPE => 1, 'resource' => ['Magento_Customer::manage', 'Magento_Customer::online', 'Magento_Sales::create', 'Magento_SalesRule::quote']];
     $integrationsData1Object = new \Magento\Framework\DataObject($integrationsData1);
     $secondIntegrationId = 2;
     $integrationsData2 = ['id' => $secondIntegrationId, Integration::NAME => 'TestIntegration2', Integration::EMAIL => '*****@*****.**', Integration::SETUP_TYPE => 1, 'resource' => ['Magento_Catalog::product_read']];
     $integrationsData2Object = new \Magento\Framework\DataObject($integrationsData2);
     $this->integrationServiceMock->expects($this->at(0))->method('findByName')->with('TestIntegration1')->will($this->returnValue($integrationsData1Object));
     $this->integrationServiceMock->expects($this->at(1))->method('findByName')->with('TestIntegration2')->will($this->returnValue($integrationsData2Object));
     $this->apiSetupPlugin->afterProcessConfigBasedIntegrations($this->subjectMock, ['TestIntegration1' => $integrationsData1, 'TestIntegration2' => $integrationsData2]);
 }
Example #6
0
 public function testProcessIntegrationConfigSuccess()
 {
     $this->_integrationConfigMock->expects($this->once())->method('getIntegrations')->will($this->returnValue(['TestIntegration1' => ['email' => '*****@*****.**', 'endpoint_url' => 'http://endpoint.com', 'identity_link_url' => 'http://www.example.com/identity'], 'TestIntegration2' => ['email' => '*****@*****.**']]));
     $intLookupData1 = new \Magento\Framework\DataObject(['id' => 1, Integration::NAME => 'TestIntegration1', Integration::SETUP_TYPE => 1]);
     $intUpdateData1 = [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];
     $integrationsData2 = [Integration::NAME => 'TestIntegration2', Integration::EMAIL => '*****@*****.**', Integration::SETUP_TYPE => 1];
     $this->_integrationServiceMock->expects($this->at(0))->method('findByName')->with('TestIntegration1')->will($this->returnValue($intLookupData1));
     $this->_integrationServiceMock->expects($this->once())->method('create')->with($integrationsData2);
     $this->_integrationServiceMock->expects($this->at(2))->method('findByName')->with('TestIntegration2')->will($this->returnValue(new \Magento\Framework\DataObject([])));
     $this->_integrationServiceMock->expects($this->once())->method('update')->with($intUpdateData1);
     $this->_integrationManager->processIntegrationConfig(['TestIntegration1', 'TestIntegration2']);
 }
Example #7
0
 public function testProcessIntegrationConfigCreateNewIntegrations()
 {
     $integrations = ['TestIntegration1' => [Integration::EMAIL => '*****@*****.**', Integration::ENDPOINT => 'http://endpoint.com', Integration::IDENTITY_LINK_URL => 'http://www.example.com/identity'], 'TestIntegration2' => [Integration::EMAIL => '*****@*****.**', Integration::ENDPOINT => 'http://endpoint.com', Integration::IDENTITY_LINK_URL => 'http://www.example.com/identity']];
     $newResources = ['Magento_Customer::manage', 'Magento_Customer::customer'];
     $this->_integrationConfigMock->expects($this->once())->method('getIntegrations')->willReturn($newResources);
     $integrationObject = $this->getMockBuilder('Magento\\Integration\\Model\\Integration')->disableOriginalConstructor()->setMethods([])->getMock();
     // Integration1 does not exist, so create it
     $this->_integrationServiceMock->expects($this->at(0))->method('findByName')->with('TestIntegration1')->will($this->returnValue($integrationObject));
     $integrationObject->expects($this->any())->method('getId')->willReturn(false);
     $this->_integrationServiceMock->expects($this->any())->method('create');
     // Integration2 does not exist, so create it
     $this->_integrationServiceMock->expects($this->at(2))->method('findByName')->with('TestIntegration2')->will($this->returnValue($integrationObject));
     $this->_integrationManager->processIntegrationConfig($integrations);
 }
Example #8
0
 /**
  * {@inheritdoc}
  */
 public function getUserId()
 {
     if ($this->integrationId) {
         return $this->integrationId;
     }
     $oauthRequest = $this->oauthHelper->prepareRequest($this->request);
     //If its not a valid Oauth request no further processing is needed
     if (empty($oauthRequest)) {
         return null;
     }
     $consumerId = $this->oauthService->validateAccessTokenRequest($oauthRequest, $this->oauthHelper->getRequestUrl($this->request), $this->request->getMethod());
     $integration = $this->integrationService->findActiveIntegrationByConsumerId($consumerId);
     return $this->integrationId = $integration->getId() ? (int) $integration->getId() : null;
 }
 /**
  * @dataProvider getValidTokenData
  */
 public function testValidToken($userType, $userId, $expectedUserType, $expectedUserId)
 {
     $bearerToken = 'bearer1234';
     $this->request->expects($this->once())->method('getHeader')->with('Authorization')->will($this->returnValue("Bearer {$bearerToken}"));
     $token = $this->getMockBuilder('Magento\\Integration\\Model\\Oauth\\Token')->disableOriginalConstructor()->setMethods(['loadByToken', 'getId', 'getUserType', 'getCustomerId', 'getAdminId', '__wakeup'])->getMock();
     $this->tokenFactory->expects($this->once())->method('create')->will($this->returnValue($token));
     $token->expects($this->once())->method('loadByToken')->with($bearerToken)->will($this->returnSelf());
     $token->expects($this->once())->method('getId')->will($this->returnValue(1));
     $token->expects($this->once())->method('getUserType')->will($this->returnValue($userType));
     $integration = $this->getMockBuilder('Magento\\Integration\\Model\\Integration')->disableOriginalConstructor()->setMethods(['getId', '__wakeup'])->getMock();
     switch ($userType) {
         case UserContextInterface::USER_TYPE_INTEGRATION:
             $integration->expects($this->once())->method('getId')->will($this->returnValue($userId));
             $this->integrationService->expects($this->once())->method('findByConsumerId')->will($this->returnValue($integration));
             break;
         case UserContextInterface::USER_TYPE_ADMIN:
             $token->expects($this->once())->method('getAdminId')->will($this->returnValue($userId));
             break;
         case UserContextInterface::USER_TYPE_CUSTOMER:
             $token->expects($this->once())->method('getCustomerId')->will($this->returnValue($userId));
             break;
     }
     $this->assertEquals($expectedUserType, $this->tokenUserContext->getUserType());
     $this->assertEquals($expectedUserId, $this->tokenUserContext->getUserId());
     /* check again to make sure that the above methods were only called once */
     $this->assertEquals($expectedUserType, $this->tokenUserContext->getUserType());
     $this->assertEquals($expectedUserId, $this->tokenUserContext->getUserId());
 }
Example #10
0
 /**
  * Process integration resource permissions after the integration is created
  *
  * @param ConfigBasedIntegrationManager $subject
  * @param array $integrations integrations passed as array from the invocation chain
  *
  * @return array
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function afterProcessIntegrationConfig(ConfigBasedIntegrationManager $subject, $integrations)
 {
     if (empty($integrations)) {
         return [];
     }
     /** @var array $integrationsResource */
     $integrationsResource = $this->_integrationConfig->getIntegrations();
     foreach (array_keys($integrations) as $name) {
         if (isset($integrationsResource[$name])) {
             $integration = $this->_integrationService->findByName($name);
             if ($integration->getId()) {
                 $this->integrationAuthorizationService->grantPermissions($integration->getId(), $integrationsResource[$name]['resource']);
             }
         }
     }
     return $integrations;
 }
Example #11
0
 /**
  * Initiate AccessToken request operation
  *
  * @return void
  */
 public function execute()
 {
     try {
         $requestUrl = $this->helper->getRequestUrl($this->getRequest());
         $request = $this->helper->prepareRequest($this->getRequest(), $requestUrl);
         // Request access token in exchange of a pre-authorized token
         $response = $this->oauthService->getAccessToken($request, $requestUrl, $this->getRequest()->getMethod());
         //After sending the access token, update the integration status to active;
         $consumer = $this->intOauthService->loadConsumerByKey($request['oauth_consumer_key']);
         $integration = $this->integrationService->findByConsumerId($consumer->getId());
         $integration->setStatus(IntegrationModel::STATUS_ACTIVE);
         $integration->save();
     } catch (\Exception $exception) {
         $response = $this->helper->prepareErrorResponse($exception, $this->getResponse());
     }
     $this->getResponse()->setBody(http_build_query($response));
 }
 /**
  * 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;
 }
Example #13
0
 /**
  * Class constructor
  *
  * @return void
  */
 protected function _construct()
 {
     parent::_construct();
     $savedFromData = $this->retrieveFormResources();
     if (false !== $savedFromData) {
         $this->setSelectedResources($savedFromData);
         return;
     }
     $integrationData = $this->_coreRegistry->registry(IntegrationController::REGISTRY_KEY_CURRENT_INTEGRATION);
     if (is_array($integrationData) && isset($integrationData['integration_id']) && $integrationData['integration_id']) {
         $this->setSelectedResources($this->integrationService->getSelectedResources($integrationData['integration_id']));
     } else {
         $this->setSelectedResources([]);
     }
 }
Example #14
0
 /**
  * Test the basic Access action.
  */
 public function testAccessAction()
 {
     $this->request->expects($this->any())->method('getMethod')->willReturn('GET');
     $this->helperMock->expects($this->once())->method('getRequestUrl');
     $this->helperMock->expects($this->once())->method('prepareRequest');
     $this->frameworkOauthSvcMock->expects($this->once())->method('getAccessToken')->willReturn(['response']);
     /** @var \Magento\Integration\Model\Oauth\Consumer|\PHPUnit_Framework_MockObject_MockObject */
     $consumerMock = $this->getMock('Magento\\Integration\\Model\\Oauth\\Consumer', [], [], '', false);
     $consumerMock->expects($this->once())->method('getId');
     $this->intOauthServiceMock->expects($this->once())->method('loadConsumerByKey')->willReturn($consumerMock);
     /** @var \Magento\Integration\Model\Integration|\PHPUnit_Framework_MockObject_MockObject */
     $integrationMock = $this->getMock('Magento\\Integration\\Model\\Integration', [], [], '', false);
     $integrationMock->expects($this->once())->method('save')->willReturnSelf();
     $this->integrationServiceMock->expects($this->once())->method('findByConsumerId')->willReturn($integrationMock);
     $this->response->expects($this->once())->method('setBody');
     $this->accessAction->executeInternal();
 }
 /**
  * @param Token $token
  * @return void
  */
 protected function setUserDataViaToken(Token $token)
 {
     $this->userType = $token->getUserType();
     switch ($this->userType) {
         case UserContextInterface::USER_TYPE_INTEGRATION:
             $this->userId = $this->integrationService->findByConsumerId($token->getConsumerId())->getId();
             $this->userType = UserContextInterface::USER_TYPE_INTEGRATION;
             break;
         case UserContextInterface::USER_TYPE_ADMIN:
             $this->userId = $token->getAdminId();
             $this->userType = UserContextInterface::USER_TYPE_ADMIN;
             break;
         case UserContextInterface::USER_TYPE_CUSTOMER:
             $this->userId = $token->getCustomerId();
             $this->userType = UserContextInterface::USER_TYPE_CUSTOMER;
             break;
         default:
             /* this is an unknown user type so reset the cached user type */
             $this->userType = null;
     }
 }