Skip to content

Commit 8789c70

Browse files
committed
Merge remote-tracking branch 'origin/2.4-develop' into Hammer_Community_Backlog_21092022
2 parents cce3a04 + eb1e3a0 commit 8789c70

File tree

73 files changed

+2900
-265
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+2900
-265
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\AdminAdobeIms\Api;
9+
10+
use Magento\Framework\Exception\CouldNotSaveException;
11+
12+
/**
13+
* Interface SaveImsUserInterface
14+
* Save Ims User & Role
15+
*/
16+
interface SaveImsUserInterface
17+
{
18+
/**
19+
* Add Admin Adobe IMS User with Default Role i.e "Adobe Ims" & No Permissions
20+
*
21+
* @param array $profile
22+
* @return void
23+
* @throws CouldNotSaveException
24+
*/
25+
public function save(array $profile): void;
26+
}

app/code/Magento/AdminAdobeIms/Model/Authorization/AdobeImsAdminTokenUserService.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Magento\AdobeImsApi\Api\OrganizationMembershipInterface;
1919
use Magento\Framework\App\RequestInterface;
2020
use Magento\Framework\Exception\AuthenticationException;
21+
use Magento\AdminAdobeIms\Api\SaveImsUserInterface;
2122

2223
/**
2324
* Adobe IMS Auth Model for getting Admin Token
@@ -63,6 +64,11 @@ class AdobeImsAdminTokenUserService
6364
*/
6465
private RequestInterface $request;
6566

67+
/**
68+
* @var SaveImsUserInterface
69+
*/
70+
private SaveImsUserInterface $saveImsUser;
71+
6672
/**
6773
* @param ImsConfig $adminImsConfig
6874
* @param OrganizationMembershipInterface $organizationMembership
@@ -71,6 +77,7 @@ class AdobeImsAdminTokenUserService
7177
* @param RequestInterface $request
7278
* @param GetTokenInterface $token
7379
* @param GetProfileInterface $profile
80+
* @param SaveImsUserInterface $saveImsUser
7481
*/
7582
public function __construct(
7683
ImsConfig $adminImsConfig,
@@ -79,7 +86,8 @@ public function __construct(
7986
AdminReauthProcessService $adminReauthProcessService,
8087
RequestInterface $request,
8188
GetTokenInterface $token,
82-
GetProfileInterface $profile
89+
GetProfileInterface $profile,
90+
SaveImsUserInterface $saveImsUser
8391
) {
8492
$this->adminImsConfig = $adminImsConfig;
8593
$this->organizationMembership = $organizationMembership;
@@ -88,6 +96,7 @@ public function __construct(
8896
$this->request = $request;
8997
$this->token = $token;
9098
$this->profile = $profile;
99+
$this->saveImsUser = $saveImsUser;
91100
}
92101

93102
/**
@@ -122,6 +131,7 @@ public function processLoginRequest(bool $isReauthorize = false): void
122131
if ($isReauthorize) {
123132
$this->adminReauthProcessService->execute($tokenResponse);
124133
} else {
134+
$this->saveImsUser->save($profile);
125135
$this->adminLoginProcessService->execute($tokenResponse, $profile);
126136
}
127137
} catch (AdobeImsAuthorizationException $e) {
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\AdminAdobeIms\Model;
9+
10+
use Magento\AdminAdobeIms\Api\SaveImsUserInterface;
11+
use Magento\User\Model\User;
12+
use Magento\User\Model\ResourceModel\User\CollectionFactory as UserCollectionFactory;
13+
use Magento\Authorization\Model\ResourceModel\Role\CollectionFactory as RoleCollectionFactory;
14+
use Magento\AdminAdobeIms\Logger\AdminAdobeImsLogger;
15+
use Magento\AdminAdobeIms\Service\ImsConfig;
16+
use Magento\Authorization\Model\Acl\Role\User as UserRoleType;
17+
use Exception;
18+
use Magento\Framework\Exception\CouldNotSaveException;
19+
20+
/**
21+
* Class SaveImsUser
22+
* Save Adobe IMS User with Default Role i.e "Adobe Ims" & No Permissions
23+
*/
24+
class SaveImsUser implements SaveImsUserInterface
25+
{
26+
private const ADMIN_IMS_ROLE = 'Adobe Ims';
27+
28+
/**
29+
* @var User
30+
*/
31+
private User $user;
32+
33+
/**
34+
* @var UserCollectionFactory
35+
*/
36+
private UserCollectionFactory $userCollectionFactory;
37+
38+
/**
39+
* @var RoleCollectionFactory
40+
*/
41+
private RoleCollectionFactory $roleCollectionFactory;
42+
43+
/**
44+
* @var AdminAdobeImsLogger
45+
*/
46+
private AdminAdobeImsLogger $logger;
47+
48+
/**
49+
* @var ImsConfig
50+
*/
51+
private ImsConfig $adminImsConfig;
52+
53+
/**
54+
* SaveImsUser constructor.
55+
* @param User $user
56+
* @param UserCollectionFactory $userCollectionFactory
57+
* @param RoleCollectionFactory $roleCollectionFactory
58+
* @param AdminAdobeImsLogger $logger
59+
* @param ImsConfig $adminImsConfig
60+
*/
61+
public function __construct(
62+
User $user,
63+
UserCollectionFactory $userCollectionFactory,
64+
RoleCollectionFactory $roleCollectionFactory,
65+
AdminAdobeImsLogger $logger,
66+
ImsConfig $adminImsConfig
67+
) {
68+
$this->user = $user;
69+
$this->userCollectionFactory = $userCollectionFactory;
70+
$this->roleCollectionFactory = $roleCollectionFactory;
71+
$this->logger = $logger;
72+
$this->adminImsConfig = $adminImsConfig;
73+
}
74+
75+
/**
76+
* @inheritdoc
77+
*/
78+
public function save(array $profile): void
79+
{
80+
if (!$this->adminImsConfig->enabled() || empty($profile['email'])) {
81+
throw new CouldNotSaveException(__('Could not save ims user.'));
82+
}
83+
84+
$username = strtolower(strstr($profile['email'], '@', true));
85+
$userCollection = $this->userCollectionFactory->create()
86+
->addFieldToFilter('email', ['eq' => $profile['email']])
87+
->addFieldToFilter('username', ['eq' => $username]);
88+
89+
if (!$userCollection->getSize()) {
90+
$roleId = $this->getImsDefaultRole();
91+
if ($roleId > 0) {
92+
try {
93+
$this->user->setFirstname($profile['first_name'])
94+
->setLastname($profile['last_name'])
95+
->setUsername($username)
96+
->setPassword($this->generateRandomPassword())
97+
->setEmail($profile['email'])
98+
->setRoleType(UserRoleType::ROLE_TYPE)
99+
->setPrivileges("")
100+
->setAssertId(0)
101+
->setRoleId((int)$roleId)
102+
->setPermission('allow')
103+
->save();
104+
unset($this->user);
105+
} catch (Exception $e) {
106+
$this->logger->critical($e->getMessage());
107+
throw new CouldNotSaveException(__('Could not save ims user.'));
108+
}
109+
}
110+
}
111+
$userCollection->clear();
112+
}
113+
114+
/**
115+
* Fetch Default Role "Adobe Ims"
116+
*
117+
* @return int
118+
*/
119+
private function getImsDefaultRole(): int
120+
{
121+
$roleId = 0;
122+
$roleCollection = $this->roleCollectionFactory->create()
123+
->addFieldToFilter('role_name', ['eq' => self::ADMIN_IMS_ROLE])
124+
->addFieldToSelect('role_id');
125+
126+
if ($roleCollection->getSize() > 0) {
127+
$objRole = $roleCollection->fetchItem();
128+
$roleId = (int) $objRole->getId();
129+
}
130+
$roleCollection->clear();
131+
132+
return $roleId;
133+
}
134+
135+
/**
136+
* Generate random password string
137+
*
138+
* @return string
139+
*/
140+
private function generateRandomPassword(): string
141+
{
142+
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-.';
143+
$pass = [];
144+
$alphaLength = strlen($characters) - 1;
145+
for ($i = 0; $i < 100; $i++) {
146+
$n = random_int(0, $alphaLength);
147+
$pass[] = $characters[$n];
148+
}
149+
return implode($pass);
150+
}
151+
}

app/code/Magento/AdminAdobeIms/Test/Unit/Model/Authorization/AdobeImsAdminTokenUserServiceTest.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@
1919
use Magento\Framework\Exception\AuthenticationException;
2020
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
2121
use PHPUnit\Framework\TestCase;
22+
use Magento\AdminAdobeIms\Service\AdminReauthProcessService;
23+
use Magento\AdminAdobeIms\Api\SaveImsUserInterface;
2224

2325
/**
2426
* Tests Magento\AdminAdobeIms\Model\Authorization\AdobeImsAdminTokenUserService
27+
*
28+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2529
*/
2630
class AdobeImsAdminTokenUserServiceTest extends TestCase
2731
{
@@ -65,6 +69,16 @@ class AdobeImsAdminTokenUserServiceTest extends TestCase
6569
*/
6670
private $requestInterfaceMock;
6771

72+
/**
73+
* @var AdminReauthProcessService
74+
*/
75+
private $adminReauthProcessService;
76+
77+
/**
78+
* @var SaveImsUserInterface
79+
*/
80+
private $saveImsUser;
81+
6882
protected function setUp(): void
6983
{
7084
$this->objectManager = new ObjectManager($this);
@@ -75,6 +89,8 @@ protected function setUp(): void
7589
$this->organizationMembership = $this->createMock(OrganizationMembershipInterface::class);
7690
$this->adminLoginProcessService = $this->createMock(AdminLoginProcessService::class);
7791
$this->requestInterfaceMock = $this->createMock(RequestInterface::class);
92+
$this->adminReauthProcessService = $this->createMock(AdminReauthProcessService::class);
93+
$this->saveImsUser = $this->createMock(SaveImsUserInterface::class);
7894

7995
$this->adminImsConfigMock->expects($this->any())
8096
->method('enabled')
@@ -86,9 +102,11 @@ protected function setUp(): void
86102
'adminImsConfig' => $this->adminImsConfigMock,
87103
'organizationMembership' => $this->organizationMembership,
88104
'adminLoginProcessService' => $this->adminLoginProcessService,
105+
'adminReauthProcessService' => $this->adminReauthProcessService,
89106
'request' => $this->requestInterfaceMock,
90107
'token' => $this->token,
91-
'profile' => $this->profile
108+
'profile' => $this->profile,
109+
'saveImsUser' => $this->saveImsUser
92110
]
93111
);
94112
}
@@ -128,6 +146,14 @@ public function testProcessLoginRequest(string $code, array $responseData)
128146
->method('checkOrganizationMembership')
129147
->with($responseData['access_token']);
130148

149+
$this->saveImsUser->expects($this->once())
150+
->method('save')
151+
->with($responseData);
152+
153+
$this->adminLoginProcessService->expects($this->once())
154+
->method('execute')
155+
->with($tokenResponse, $responseData);
156+
131157
$this->adobeImsAdminTokenUserService->processLoginRequest();
132158
}
133159

@@ -256,7 +282,9 @@ public function responseDataProvider(): array
256282
'email' => 'user@test.com',
257283
'access_token' => 'kladjflakdjf3423rfzddsf',
258284
'refresh_token' => 'kladjflakdjf3423rfzddsf',
259-
'expires_in' => 1642259230998
285+
'expires_in' => 1642259230998,
286+
'first_name' => 'Test',
287+
'last_name' => 'User'
260288
]
261289
]
262290
];

app/code/Magento/AdminAdobeIms/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<preference for="Magento\AdminAdobeIms\Api\Data\ImsWebapiInterface" type="Magento\AdminAdobeIms\Model\ImsWebapi"/>
1212
<preference for="Magento\AdobeImsApi\Api\GetAccessTokenInterface" type="Magento\AdminAdobeIms\Model\GetAccessTokenProxy"/>
1313
<preference for="Magento\AdobeImsApi\Api\UserAuthorizedInterface" type="Magento\AdminAdobeIms\Model\UserAuthorizedProxy"/>
14+
<preference for="Magento\AdminAdobeIms\Api\SaveImsUserInterface" type="Magento\AdminAdobeIms\Model\SaveImsUser"/>
1415

1516
<type name="Magento\Framework\Console\CommandListInterface">
1617
<arguments>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"Admin Adobe IMS integration is disabled","Admin Adobe IMS integration is disabled"
2+
"Admin Adobe IMS integration is enabled","Admin Adobe IMS integration is enabled"
3+
"The Client ID, Client Secret, Organization ID and 2FA are required when enabling the Admin Adobe IMS Module","The Client ID, Client Secret, Organization ID and 2FA are required when enabling the Admin Adobe IMS Module"
4+
"Module is disabled","Module is disabled"
5+
"Admin Adobe IMS integration is %1","Admin Adobe IMS integration is %1"
6+
"Adobe Sign-In is disabled.","Adobe Sign-In is disabled."
7+
"Authorization was successful","Authorization was successful"
8+
"Session Access Token is not valid","Session Access Token is not valid"
9+
"Login request error %1","Login request error %1"
10+
"An authentication error occurred. Verify and try again.","An authentication error occurred. Verify and try again."
11+
"You don't have access to this Commerce instance","You don't have access to this Commerce instance"
12+
"Unable to sign in with the Adobe ID","Unable to sign in with the Adobe ID"
13+
"Could not save ims token.","Could not save ims token."
14+
"Could not find ims token id: %id.","Could not find ims token id: %id."
15+
"Could not delete ims tokens for admin user id %1.","Could not delete ims tokens for admin user id %1."
16+
"Could not save ims user.","Could not save ims user."
17+
"The account sign-in was incorrect or your account is disabled temporarily. Please wait and try again later.","The account sign-in was incorrect or your account is disabled temporarily. Please wait and try again later."
18+
"More permissions are needed to access this.","More permissions are needed to access this."
19+
"Please sign in with Adobe ID","Please sign in with Adobe ID"
20+
"Admin token generation is disabled. Please use Adobe IMS ACCESS_TOKEN.","Admin token generation is disabled. Please use Adobe IMS ACCESS_TOKEN."
21+
"Identity Verification","Identity Verification"
22+
"Verify Identity with Adobe IMS","Verify Identity with Adobe IMS"
23+
"Confirm Identity","Confirm Identity"
24+
"To apply changes you need to verify your Adobe identity.","To apply changes you need to verify your Adobe identity."
25+
"Identity Verified with Adobe IMS","Identity Verified with Adobe IMS"
26+
"Please perform the AdobeIms reAuth and try again.","Please perform the AdobeIms reAuth and try again."
27+
"Use the same email user has in Adobe IMS organization.","Use the same email user has in Adobe IMS organization."
28+
"The tokens couldn't be revoked.","The tokens couldn't be revoked."
29+
"No matching admin user found for Adobe ID.","No matching admin user found for Adobe ID."
30+
"This field is required to enable the Admin Adobe IMS Module","This field is required to enable the Admin Adobe IMS Module"
31+
"No valid Organization ID provided","No valid Organization ID provided"
32+
"No valid Client ID provided","No valid Client ID provided"
33+
"No valid Client Secret provided","No valid Client Secret provided"
34+
"The ims token wasn't found.","The ims token wasn't found."
35+
"Sign in to access the Adobe Commerce for your organization.","Sign in to access the Adobe Commerce for your organization."
36+
"Sign In","Sign In"
37+
"This Commerce instance is managed by an organization. Contact your organization administrator to request access.","This Commerce instance is managed by an organization. Contact your organization administrator to request access."
38+
"Sign in with Adobe ID","Sign in with Adobe ID"
39+
Footer,Footer
40+
"User Guides","User Guides"
41+
"Customer Support","Customer Support"
42+
Forums,Forums
43+
Header,Header
44+
"%user_name, you now have access to Adobe Commerce","%user_name, you now have access to Adobe Commerce"
45+
"Your administrator at %store_name has given you access to Adobe Commerce","Your administrator at %store_name has given you access to Adobe Commerce"
46+
"Get started","Get started"
47+
"Here are a few links to help you get up and running:","Here are a few links to help you get up and running:"
48+
Documentation,Documentation
49+
"Release notes","Release notes"
50+
"If you have any questions about access to Adobe Commerce, contact your administrator or your Adobe account team for more information.","If you have any questions about access to Adobe Commerce, contact your administrator or your Adobe account team for more information."
51+
"Enable Logging for Admin Adobe IMS Module","Enable Logging for Admin Adobe IMS Module"
52+
"Adobe Commerce","Adobe Commerce"

0 commit comments

Comments
 (0)