Skip to content

Commit d05e25b

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

File tree

2 files changed

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

2 files changed

+41
-10
lines changed

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,17 @@ public function delete(\Magento\Framework\Model\AbstractModel $user)
258258

259259
$uid = $user->getId();
260260
$connection->beginTransaction();
261-
try {
262-
$connection->delete($this->getMainTable(), ['user_id = ?' => $uid]);
263-
$connection->delete(
264-
$this->getTable('authorization_role'),
265-
['user_id = ?' => $uid, 'user_type = ?' => UserContextInterface::USER_TYPE_ADMIN]
266-
);
267-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
268-
$connection->rollBack();
269-
return false;
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+
}
270272
}
271273
$connection->commit();
272274
$this->_afterDelete($user);

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
*/
66
namespace Magento\User\Model\ResourceModel;
77

8+
use Magento\Authorization\Model\ResourceModel\Role\Collection as UserRoleCollection;
9+
use Magento\Authorization\Model\UserContextInterface;
810
use Magento\TestFramework\Helper\Bootstrap;
9-
use Magento\User\Model\User;
1011
use Magento\User\Model\ResourceModel\User as UserResourceModel;
12+
use Magento\User\Model\User;
1113

1214
/**
1315
* @magentoAppArea adminhtml
@@ -46,6 +48,33 @@ public function testGetLatestPasswordWhenZeroPasswordLifetime()
4648
);
4749
}
4850

51+
/**
52+
* Test that user role is not deleted after deleting empty user
53+
*/
54+
public function testDelete()
55+
{
56+
$this->checkRoleCollectionSize();
57+
/** @var User $user */
58+
$user = Bootstrap::getObjectManager()->create(
59+
User::class
60+
);
61+
$this->model->delete($user);
62+
$this->checkRoleCollectionSize();
63+
}
64+
65+
/**
66+
* Ensure that role collection size is correct
67+
*/
68+
private function checkRoleCollectionSize()
69+
{
70+
/** @var UserRoleCollection $roleCollection */
71+
$roleCollection = Bootstrap::getObjectManager()->create(
72+
UserRoleCollection::class
73+
);
74+
$roleCollection->setUserFilter(0, UserContextInterface::USER_TYPE_ADMIN);
75+
$this->assertEquals(1, $roleCollection->getSize());
76+
}
77+
4978
public function testCountAll()
5079
{
5180
$this->assertSame(1, $this->model->countAll());

0 commit comments

Comments
 (0)