9
9
namespace Magento \AdminAdobeIms \Service ;
10
10
11
11
use Magento \AdminAdobeIms \Exception \AdobeImsOrganizationAuthorizationException ;
12
+ use Magento \Framework \HTTP \Client \CurlFactory ;
12
13
13
14
class ImsOrganizationService
14
15
{
@@ -17,33 +18,68 @@ class ImsOrganizationService
17
18
*/
18
19
private ImsConfig $ adminImsConfig ;
19
20
21
+ /**
22
+ * @var CurlFactory
23
+ */
24
+ private CurlFactory $ curlFactory ;
25
+
20
26
/**
21
27
* @param ImsConfig $adminImsConfig
28
+ * @param CurlFactory $curlFactory
22
29
*/
23
30
public function __construct (
24
- ImsConfig $ adminImsConfig
31
+ ImsConfig $ adminImsConfig ,
32
+ CurlFactory $ curlFactory
25
33
) {
26
34
$ this ->adminImsConfig = $ adminImsConfig ;
35
+ $ this ->curlFactory = $ curlFactory ;
27
36
}
28
37
29
38
/**
30
- * Check if user is assigned to organization
39
+ * Check if user is a member of Adobe Organization
31
40
*
32
- * @param string $token
33
- * @return bool
41
+ * @param string $access_token
42
+ * @return void
34
43
* @throws AdobeImsOrganizationAuthorizationException
35
44
*/
36
- public function checkOrganizationAllocation (string $ token ): bool
45
+ public function checkOrganizationMembership (string $ access_token ): void
37
46
{
38
- $ configuredOrganization = $ this ->adminImsConfig ->getOrganizationId ();
47
+ $ configuredOrganizationId = $ this ->adminImsConfig ->getOrganizationId ();
39
48
40
- //@TODO CABPI-324: Change Org check to use new endpoint
41
- if ($ configuredOrganization === '' || !$ token ) {
49
+ if ($ configuredOrganizationId === '' || !$ access_token ) {
42
50
throw new AdobeImsOrganizationAuthorizationException (
43
- __ ('User is not assigned to defined organization. ' )
51
+ __ ('Can \' t check user membership in organization. ' )
44
52
);
45
53
}
46
54
47
- return true ;
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
+ $ orgCheckUrl = $ this ->adminImsConfig ->getOrganizationMembershipUrl ($ configuredOrganizationId );
63
+ $ curl ->get ($ orgCheckUrl );
64
+
65
+ if ($ curl ->getBody () === '' ) {
66
+ throw new AdobeImsOrganizationAuthorizationException (
67
+ __ ('Could not check Organization Membership. Response is empty. ' )
68
+ );
69
+ }
70
+
71
+ $ response = $ curl ->getBody ();
72
+
73
+ if ($ response !== 'true ' ) {
74
+ throw new AdobeImsOrganizationAuthorizationException (
75
+ __ ('User is not a member of configured Adobe Organization. ' )
76
+ );
77
+ }
78
+
79
+ } catch (\Exception $ exception ) {
80
+ throw new AdobeImsOrganizationAuthorizationException (
81
+ __ ('Organization Membership check can \'t be performed ' )
82
+ );
83
+ }
48
84
}
49
85
}
0 commit comments