8
8
9
9
namespace Magento \AdminAdobeIms \Model \Authorization ;
10
10
11
- use Magento \AdminAdobeIms \Api \TokenReaderInterface ;
12
- use Magento \AdminAdobeIms \Model \ImsConnection ;
13
- use Magento \AdminAdobeIms \Model \User ;
14
11
use Magento \AdminAdobeIms \Service \ImsConfig ;
15
- use Magento \AdobeImsApi \Api \Data \UserProfileInterface ;
16
- use Magento \AdobeImsApi \Api \Data \UserProfileInterfaceFactory ;
17
- use Magento \AdobeImsApi \Api \UserProfileRepositoryInterface ;
18
12
use Magento \Authorization \Model \UserContextInterface ;
19
13
use Magento \Framework \Exception \AuthenticationException ;
20
- use Magento \Framework \Exception \NoSuchEntityException ;
14
+ use Magento \Framework \Exception \AuthorizationException ;
15
+ use Magento \Framework \Exception \CouldNotSaveException ;
16
+ use Magento \Framework \Exception \InvalidArgumentException ;
21
17
use Magento \Framework \Webapi \Request ;
22
18
23
19
/**
@@ -28,81 +24,49 @@ class AdobeImsTokenUserContext implements UserContextInterface
28
24
private const AUTHORIZATION_METHOD_HEADER_BEARER = 'bearer ' ;
29
25
30
26
/**
31
- * @var int
27
+ * @var int|null
32
28
*/
33
- private $ userId ;
29
+ private ? int $ userId = null ;
34
30
35
31
/**
36
32
* @var bool
37
33
*/
38
- private $ isRequestProcessed ;
34
+ private bool $ isRequestProcessed = false ;
39
35
40
36
/**
41
37
* @var Request
42
38
*/
43
39
private Request $ request ;
44
40
45
- /**
46
- * @var TokenReaderInterface
47
- */
48
- private TokenReaderInterface $ tokenReader ;
49
-
50
- /**
51
- * @var ImsConnection
52
- */
53
- private ImsConnection $ imsConnection ;
54
-
55
41
/**
56
42
* @var ImsConfig
57
43
*/
58
44
private ImsConfig $ imsConfig ;
59
45
60
46
/**
61
- * @var UserProfileRepositoryInterface
47
+ * @var AdobeImsTokenUserService
62
48
*/
63
- private UserProfileRepositoryInterface $ userProfileRepository ;
64
-
65
- /**
66
- * @var UserProfileInterfaceFactory
67
- */
68
- private UserProfileInterfaceFactory $ userProfileFactory ;
69
-
70
- /**
71
- * @var User
72
- */
73
- private User $ adminUser ;
49
+ private AdobeImsTokenUserService $ tokenUserService ;
74
50
75
51
/**
76
52
* @param Request $request
77
- * @param TokenReaderInterface $tokenReader
78
- * @param ImsConnection $imsConnection
79
53
* @param ImsConfig $imsConfig
80
- * @param UserProfileRepositoryInterface $userProfileRepository
81
- * @param UserProfileInterfaceFactory $userProfileFactory
82
- * @param User $adminUser
54
+ * @param AdobeImsTokenUserService $tokenUserService
83
55
*/
84
56
public function __construct (
85
57
Request $ request ,
86
- TokenReaderInterface $ tokenReader ,
87
- ImsConnection $ imsConnection ,
88
58
ImsConfig $ imsConfig ,
89
- UserProfileRepositoryInterface $ userProfileRepository ,
90
- UserProfileInterfaceFactory $ userProfileFactory ,
91
- User $ adminUser
59
+ AdobeImsTokenUserService $ tokenUserService
92
60
) {
93
61
$ this ->request = $ request ;
94
- $ this ->tokenReader = $ tokenReader ;
95
- $ this ->imsConnection = $ imsConnection ;
96
62
$ this ->imsConfig = $ imsConfig ;
97
- $ this ->userProfileRepository = $ userProfileRepository ;
98
- $ this ->userProfileFactory = $ userProfileFactory ;
99
- $ this ->adminUser = $ adminUser ;
63
+ $ this ->tokenUserService = $ tokenUserService ;
100
64
}
101
65
102
66
/**
103
67
* @inheritdoc
104
68
*/
105
- public function getUserId ()
69
+ public function getUserId (): ? int
106
70
{
107
71
$ this ->processRequest ();
108
72
return $ this ->userId ;
@@ -111,7 +75,7 @@ public function getUserId()
111
75
/**
112
76
* @inheritdoc
113
77
*/
114
- public function getUserType ()
78
+ public function getUserType (): ? int
115
79
{
116
80
return UserContextInterface::USER_TYPE_ADMIN ;
117
81
}
@@ -120,6 +84,9 @@ public function getUserType()
120
84
* Finds the bearer token and looks up the value.
121
85
*
122
86
* @return void
87
+ * @throws AuthorizationException
88
+ * @throws CouldNotSaveException
89
+ * @throws InvalidArgumentException
123
90
*/
124
91
private function processRequest ()
125
92
{
@@ -132,28 +99,7 @@ private function processRequest()
132
99
}
133
100
134
101
try {
135
- $ tokenData = $ this ->tokenReader ->read ($ bearerToken );
136
- $ adobeUserId = $ tokenData ['adobe_user_id ' ] ?? '' ;
137
- $ userProfile = $ this ->userProfileRepository ->getByAdobeUserId ($ adobeUserId );
138
-
139
- if ($ userProfile ->getId ()) {
140
- $ adminUserId = (int ) $ userProfile ->getData ('admin_user_id ' );
141
- } else {
142
- $ profile = $ this ->imsConnection ->getProfile ($ bearerToken );
143
- if (empty ($ profile ['email ' ])) {
144
- throw new AuthenticationException (__ ('An authentication error occurred. Verify and try again. ' ));
145
- }
146
- $ adminUser = $ this ->adminUser ->loadByEmail ($ profile ['email ' ]);
147
- if (empty ($ adminUser ['user_id ' ])) {
148
- throw new AuthenticationException (__ ('An authentication error occurred. Verify and try again. ' ));
149
- }
150
-
151
- $ adminUserId = (int ) $ adminUser ['user_id ' ];
152
- $ profile ['adobe_user_id ' ] = $ adobeUserId ;
153
-
154
- $ userProfileInterface = $ this ->getUserProfileInterface ($ adminUserId );
155
- $ this ->userProfileRepository ->save ($ this ->updateUserProfile ($ userProfileInterface , $ profile ));
156
- }
102
+ $ adminUserId = $ this ->tokenUserService ->getAdminUserIdByToken ($ bearerToken );
157
103
} catch (AuthenticationException $ e ) {
158
104
$ this ->isRequestProcessed = true ;
159
105
return ;
@@ -190,43 +136,4 @@ private function getRequestedToken()
190
136
191
137
return $ headerPieces [1 ];
192
138
}
193
-
194
- /**
195
- * Get user profile entity
196
- *
197
- * @param int $adminUserId
198
- * @return UserProfileInterface
199
- */
200
- private function getUserProfileInterface (int $ adminUserId ): UserProfileInterface
201
- {
202
- try {
203
- return $ this ->userProfileRepository ->getByUserId ($ adminUserId );
204
- } catch (NoSuchEntityException $ exception ) {
205
- return $ this ->userProfileFactory ->create (
206
- [
207
- 'data ' => [
208
- 'admin_user_id ' => $ adminUserId
209
- ]
210
- ]
211
- );
212
- }
213
- }
214
-
215
- /**
216
- * Update user profile with the data from token
217
- *
218
- * @param UserProfileInterface $userProfileInterface
219
- * @param array $profile
220
- * @return UserProfileInterface
221
- */
222
- private function updateUserProfile (
223
- UserProfileInterface $ userProfileInterface ,
224
- array $ profile
225
- ): UserProfileInterface {
226
- $ userProfileInterface ->setName ($ profile ['name ' ] ?? '' );
227
- $ userProfileInterface ->setEmail ($ profile ['email ' ] ?? '' );
228
- $ userProfileInterface ->setAdobeUserId ($ profile ['adobe_user_id ' ]);
229
-
230
- return $ userProfileInterface ;
231
- }
232
139
}
0 commit comments