Skip to content

Commit 3dcf3ef

Browse files
committed
CABPI-324: Change Org check to use new endpoint
1 parent 8cf6be7 commit 3dcf3ef

File tree

3 files changed

+41
-64
lines changed

3 files changed

+41
-64
lines changed

app/code/Magento/AdminAdobeIms/Model/ImsConnection.php

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@
99
namespace Magento\AdminAdobeIms\Model;
1010

1111
use Magento\AdminAdobeIms\Exception\AdobeImsAuthorizationException;
12-
use Magento\AdminAdobeIms\Exception\AdobeImsOrganizationAuthorizationException;
1312
use Magento\AdminAdobeIms\Logger\AdminAdobeImsLogger;
1413
use Magento\AdminAdobeIms\Service\ImsConfig;
1514
use Magento\AdobeIms\Model\GetToken;
1615
use Magento\AdobeImsApi\Api\Data\TokenResponseInterface;
1716
use Magento\Framework\Exception\AuthorizationException;
1817
use Magento\Framework\Exception\InvalidArgumentException;
19-
use Magento\Framework\Exception\LocalizedException;
2018
use Magento\Framework\HTTP\Client\Curl;
2119
use Magento\Framework\HTTP\Client\CurlFactory;
2220
use Magento\Framework\Serialize\Serializer\Json;
@@ -235,56 +233,4 @@ public function getProfile(string $code)
235233

236234
return $this->json->unserialize($curl->getBody());
237235
}
238-
239-
/**
240-
* Check if user is a member of Adobe IMS Organization
241-
*
242-
* @param string $orgId
243-
* @param string|null $token
244-
* @return bool
245-
* @throws AuthorizationException
246-
*/
247-
public function organizationMembership(string $orgId, ?string $token): bool
248-
{
249-
$result = false;
250-
if ($token === null) {
251-
return $result;
252-
}
253-
try {
254-
$curl = $this->curlFactory->create();
255-
256-
$curl->addHeader('Content-Type', 'application/x-www-form-urlencoded');
257-
$curl->addHeader('cache-control', 'no-cache');
258-
$curl->addHeader('Authorization', 'Bearer ' . $token);
259-
260-
$curl->get(
261-
$this->adminImsConfig->getOrganizationMembershipUrl($orgId),
262-
[]
263-
);
264-
265-
if ($curl->getBody() === '') {
266-
throw new AuthorizationException(
267-
__('Could not check Organization Membership')
268-
);
269-
}
270-
271-
$response = $curl->getBody();
272-
273-
if ($response == 'true') {
274-
$result = true;
275-
} else {
276-
throw new AdobeImsOrganizationAuthorizationException(
277-
__('User is not a member of configured Adobe Organization.')
278-
);
279-
}
280-
281-
} catch (Exception $exception) {
282-
throw new LocalizedException(
283-
__('Organization Membership check can\'t be performed')
284-
);
285-
286-
}
287-
288-
return $result;
289-
}
290236
}

app/code/Magento/AdminAdobeIms/Service/ImsOrganizationService.php

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace Magento\AdminAdobeIms\Service;
1010

1111
use Magento\AdminAdobeIms\Exception\AdobeImsOrganizationAuthorizationException;
12-
use Magento\AdminAdobeIms\Model\ImsConnection;
12+
use Magento\Framework\HTTP\Client\CurlFactory;
1313

1414
class ImsOrganizationService
1515
{
@@ -19,20 +19,20 @@ class ImsOrganizationService
1919
private ImsConfig $adminImsConfig;
2020

2121
/**
22-
* @var ImsConnection
22+
* @var CurlFactory
2323
*/
24-
private ImsConnection $adminImsConnection;
24+
private CurlFactory $curlFactory;
2525

2626
/**
2727
* @param ImsConfig $adminImsConfig
28-
* @param ImsConnection $adminImsConnection
28+
* @param CurlFactory $curlFactory
2929
*/
3030
public function __construct(
3131
ImsConfig $adminImsConfig,
32-
ImsConnection $adminImsConnection
32+
CurlFactory $curlFactory
3333
) {
3434
$this->adminImsConfig = $adminImsConfig;
35-
$this->adminImsConnection = $adminImsConnection;
35+
$this->curlFactory = $curlFactory;
3636
}
3737

3838
/**
@@ -44,14 +44,45 @@ public function __construct(
4444
*/
4545
public function checkOrganizationMembership(string $access_token): void
4646
{
47-
$configuredOrganization = $this->adminImsConfig->getOrganizationId();
47+
$configuredOrganizationId = $this->adminImsConfig->getOrganizationId();
4848

49-
if ($configuredOrganization === '' || !$access_token) {
49+
if ($configuredOrganizationId === '' || !$access_token) {
5050
throw new AdobeImsOrganizationAuthorizationException(
5151
__('Can\'t check user membership in organization.')
5252
);
5353
}
5454

55-
$this->adminImsConnection->organizationMembership($configuredOrganization, $access_token);
55+
try {
56+
$curl = $this->curlFactory->create();
57+
58+
$curl->addHeader('Content-Type', 'application/x-www-form-urlencoded');
59+
$curl->addHeader('cache-control', 'no-cache');
60+
$curl->addHeader('Authorization', 'Bearer ' . $access_token);
61+
62+
$curl->get(
63+
$this->adminImsConfig->getOrganizationMembershipUrl($configuredOrganizationId),
64+
[]
65+
);
66+
67+
if ($curl->getBody() === '') {
68+
throw new AdobeImsOrganizationAuthorizationException(
69+
__('Could not check Organization Membership. Response is empty.')
70+
);
71+
}
72+
73+
$response = $curl->getBody();
74+
75+
if ($response !== 'true') {
76+
throw new AdobeImsOrganizationAuthorizationException(
77+
__('User is not a member of configured Adobe Organization.')
78+
);
79+
}
80+
81+
} catch (\Exception $exception) {
82+
throw new AdobeImsOrganizationAuthorizationException(
83+
__('Organization Membership check can\'t be performed')
84+
);
85+
86+
}
5687
}
5788
}

app/code/Magento/AdminAdobeIms/Test/Unit/Service/ImsOrganizationServiceTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function testCheckOrganizationMembershipThrowsExceptionWhenProfileNotAssi
4747
{
4848
$this->adminImsConfigMock
4949
->method('getOrganizationId')
50-
->willReturn(self::INVALID_ORGANIZATION_ID);
50+
->willReturn('');
5151

5252
$this->expectException(AdobeImsOrganizationAuthorizationException::class);
5353
$this->expectExceptionMessage('Can\'t check user membership in organization.');

0 commit comments

Comments
 (0)