Skip to content

Commit 592cb1e

Browse files
committed
MC-30224: [2.4] Deleting an empty user model caused deleting admin role
1 parent d05e25b commit 592cb1e

File tree

2 files changed

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

2 files changed

+67
-34
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: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,60 @@
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 */
39-
$user = Bootstrap::getObjectManager()->create(
40-
User::class
41-
);
59+
$user = $this->userFactory->create();
4260
$user->loadByUsername('dummy_username');
4361
$latestPassword = $this->model->getLatestPassword($user->getId());
4462

@@ -49,38 +67,48 @@ public function testGetLatestPasswordWhenZeroPasswordLifetime()
4967
}
5068

5169
/**
52-
* Test that user role is not deleted after deleting empty user
70+
* Test that user role is not deleted after deleting empty user.
71+
*
72+
* @return void
5373
*/
54-
public function testDelete()
74+
public function testDelete(): void
5575
{
5676
$this->checkRoleCollectionSize();
5777
/** @var User $user */
58-
$user = Bootstrap::getObjectManager()->create(
59-
User::class
60-
);
78+
$user = $this->userFactory->create();
6179
$this->model->delete($user);
6280
$this->checkRoleCollectionSize();
6381
}
6482

6583
/**
66-
* Ensure that role collection size is correct
84+
* Ensure that role collection size is correct.
85+
*
86+
* @return void
6787
*/
68-
private function checkRoleCollectionSize()
88+
private function checkRoleCollectionSize(): void
6989
{
7090
/** @var UserRoleCollection $roleCollection */
71-
$roleCollection = Bootstrap::getObjectManager()->create(
72-
UserRoleCollection::class
73-
);
91+
$roleCollection = $this->userRoleCollectionFactory->create();
7492
$roleCollection->setUserFilter(0, UserContextInterface::USER_TYPE_ADMIN);
7593
$this->assertEquals(1, $roleCollection->getSize());
7694
}
7795

78-
public function testCountAll()
96+
/**
97+
* Check total user count.
98+
*
99+
* @return void
100+
*/
101+
public function testCountAll(): void
79102
{
80103
$this->assertSame(1, $this->model->countAll());
81104
}
82105

83-
public function testGetValidationRulesBeforeSave()
106+
/**
107+
* Check validation rules has correct type.
108+
*
109+
* @return void
110+
*/
111+
public function testGetValidationRulesBeforeSave(): void
84112
{
85113
$rules = $this->model->getValidationRulesBeforeSave();
86114
$this->assertInstanceOf('Zend_Validate_Interface', $rules);

0 commit comments

Comments
 (0)