Skip to content

Commit cfa4082

Browse files
committed
MC-30109: Deleting an empty user model caused deleting admin role
1 parent c1a066b commit cfa4082

File tree

2 files changed

+66
-31
lines changed
  • app/code/Magento/User/Model/ResourceModel
  • dev/tests/integration/testsuite/Magento/User/Model/ResourceModel

2 files changed

+66
-31
lines changed

app/code/Magento/User/Model/ResourceModel/User.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Authorization\Model\UserContextInterface;
1414
use Magento\Framework\Acl\Data\CacheInterface;
1515
use Magento\Framework\App\ObjectManager;
16+
use Magento\Framework\Exception\LocalizedException;
1617
use Magento\User\Model\Backend\Config\ObserverConfig;
1718
use Magento\User\Model\User as ModelUser;
1819

@@ -249,29 +250,33 @@ protected function _afterLoad(\Magento\Framework\Model\AbstractModel $user)
249250
*
250251
* @param \Magento\Framework\Model\AbstractModel $user
251252
* @return bool
252-
* @throws \Magento\Framework\Exception\LocalizedException
253+
* @throws LocalizedException
253254
*/
254255
public function delete(\Magento\Framework\Model\AbstractModel $user)
255256
{
257+
$uid = $user->getId();
258+
if (!$uid) {
259+
return false;
260+
}
261+
256262
$this->_beforeDelete($user);
257263
$connection = $this->getConnection();
258-
259-
$uid = $user->getId();
260264
$connection->beginTransaction();
261-
if ($uid) {
262-
try {
263-
$connection->delete($this->getMainTable(), ['user_id = ?' => $uid]);
264-
$connection->delete(
265-
$this->getTable('authorization_role'),
266-
['user_id = ?' => $uid, 'user_type = ?' => UserContextInterface::USER_TYPE_ADMIN]
267-
);
268-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
269-
$connection->rollBack();
270-
return false;
271-
}
265+
try {
266+
$connection->delete($this->getMainTable(), ['user_id = ?' => $uid]);
267+
$connection->delete(
268+
$this->getTable('authorization_role'),
269+
['user_id = ?' => $uid, 'user_type = ?' => UserContextInterface::USER_TYPE_ADMIN]
270+
);
271+
} catch (LocalizedException $e) {
272+
$connection->rollBack();
273+
274+
return false;
272275
}
276+
273277
$connection->commit();
274278
$this->_afterDelete($user);
279+
275280
return true;
276281
}
277282

dev/tests/integration/testsuite/Magento/User/Model/ResourceModel/UserTest.php

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,57 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\User\Model\ResourceModel;
79

810
use Magento\Authorization\Model\ResourceModel\Role\Collection as UserRoleCollection;
11+
use Magento\Authorization\Model\ResourceModel\Role\CollectionFactory as UserRoleCollectionFactory;
912
use Magento\Authorization\Model\UserContextInterface;
1013
use Magento\TestFramework\Helper\Bootstrap;
1114
use Magento\User\Model\ResourceModel\User as UserResourceModel;
1215
use Magento\User\Model\User;
16+
use Magento\User\Model\UserFactory;
1317

1418
/**
1519
* @magentoAppArea adminhtml
1620
*/
1721
class UserTest extends \PHPUnit\Framework\TestCase
1822
{
19-
/** @var UserResourceModel */
23+
/**
24+
* @var UserResourceModel
25+
*/
2026
private $model;
2127

28+
/**
29+
* @var UserRoleCollectionFactory
30+
*/
31+
private $userRoleCollectionFactory;
32+
33+
/**
34+
* @var UserFactory
35+
*/
36+
private $userFactory;
37+
38+
/**
39+
* @inheritdoc
40+
*/
2241
protected function setUp()
2342
{
24-
$this->model = Bootstrap::getObjectManager()->get(
25-
UserResourceModel::class
26-
);
43+
$this->model = Bootstrap::getObjectManager()->get(UserResourceModel::class);
44+
$this->userRoleCollectionFactory = Bootstrap::getObjectManager()->get(UserRoleCollectionFactory::class);
45+
$this->userFactory = Bootstrap::getObjectManager()->get(UserFactory::class);
2746
}
2847

2948
/**
3049
* Tests if latest password is stored after user creating
3150
* when password lifetime config value is zero (disabled as fact)
3251
*
52+
* @return void
3353
* @magentoConfigFixture current_store admin/security/password_lifetime 0
3454
* @magentoDataFixture Magento/User/_files/dummy_user.php
3555
*/
36-
public function testGetLatestPasswordWhenZeroPasswordLifetime()
56+
public function testGetLatestPasswordWhenZeroPasswordLifetime(): void
3757
{
3858
/** @var User $user */
3959
$user = Bootstrap::getObjectManager()->create(
@@ -49,38 +69,48 @@ public function testGetLatestPasswordWhenZeroPasswordLifetime()
4969
}
5070

5171
/**
52-
* Test that user role is not deleted after deleting empty user
72+
* Test that user role is not deleted after deleting empty user.
73+
*
74+
* @return void
5375
*/
54-
public function testDelete()
76+
public function testDelete(): void
5577
{
5678
$this->checkRoleCollectionSize();
5779
/** @var User $user */
58-
$user = Bootstrap::getObjectManager()->create(
59-
User::class
60-
);
80+
$user = $this->userFactory->create();
6181
$this->model->delete($user);
6282
$this->checkRoleCollectionSize();
6383
}
6484

6585
/**
66-
* Ensure that role collection size is correct
86+
* Ensure that role collection size is correct.
87+
*
88+
* @return void
6789
*/
68-
private function checkRoleCollectionSize()
90+
private function checkRoleCollectionSize(): void
6991
{
7092
/** @var UserRoleCollection $roleCollection */
71-
$roleCollection = Bootstrap::getObjectManager()->create(
72-
UserRoleCollection::class
73-
);
93+
$roleCollection = $this->userRoleCollectionFactory->create();
7494
$roleCollection->setUserFilter(0, UserContextInterface::USER_TYPE_ADMIN);
7595
$this->assertEquals(1, $roleCollection->getSize());
7696
}
7797

78-
public function testCountAll()
98+
/**
99+
* Check total user count.
100+
*
101+
* @return void
102+
*/
103+
public function testCountAll(): void
79104
{
80105
$this->assertSame(1, $this->model->countAll());
81106
}
82107

83-
public function testGetValidationRulesBeforeSave()
108+
/**
109+
* Check validation rules has correct type.
110+
*
111+
* @return void
112+
*/
113+
public function testGetValidationRulesBeforeSave(): void
84114
{
85115
$rules = $this->model->getValidationRulesBeforeSave();
86116
$this->assertInstanceOf('Zend_Validate_Interface', $rules);

0 commit comments

Comments
 (0)