guiltar
Well-known member
I've noticed that user group promotions fail sometimes.
I can't catch the exact cases when it happens since all my tests was ok.
But I found the possible reason in the code.
The promotion transaction is not safe and may be logged as done well when promotion failed to apply.
XenForo_Model_User
Even if $success is false for some reason, table xf_user_group_change contains failed promotion. So, the next time it won't be applied since the failed userGroup will be contained in var $oldGroups. I suggest to replace this place by:
Also, I suggest to replace chunk from XenForo_Model_User
by next line to guarantee, that promotions applied without errors.
I can't catch the exact cases when it happens since all my tests was ok.
But I found the possible reason in the code.
The promotion transaction is not safe and may be logged as done well when promotion failed to apply.
XenForo_Model_User
PHP:
public function addUserGroupChange($userId, $key, $addGroups)
{
.........................
$db->query('
INSERT INTO xf_user_group_change
(user_id, change_key, group_ids)
VALUES
(?, ?, ?)
ON DUPLICATE KEY UPDATE
group_ids = VALUES(group_ids)
', array($userId, $key, $addGroups));
$success = $this->_applyUserGroupChanges($userId, $oldGroups, $newGroups);
XenForo_Db::commit($db);
return $success;
}
Even if $success is false for some reason, table xf_user_group_change contains failed promotion. So, the next time it won't be applied since the failed userGroup will be contained in var $oldGroups. I suggest to replace this place by:
PHP:
if ($success = $this->_applyUserGroupChanges($userId, $oldGroups, $newGroups))
{
$db->query('
INSERT INTO xf_user_group_change
(user_id, change_key, group_ids)
VALUES
(?, ?, ?)
ON DUPLICATE KEY UPDATE
group_ids = VALUES(group_ids)
', array($userId, $key, $addGroups));
}
Also, I suggest to replace chunk from XenForo_Model_User
PHP:
protected function _applyUserGroupChanges($userId, array $oldGroupStrings, array $newGroupStrings)
{
....................
$dw->setSecondaryGroups($secondaryGroups);
$dw->save();
return true;
}
PHP:
protected function _applyUserGroupChanges($userId, array $oldGroupStrings, array $newGroupStrings)
{
....................
$dw->setSecondaryGroups($secondaryGroups);
return $dw->save();
}