• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Fixed [minor] [v1.1.1] XenForo_Model_User::ban() doesn't respect $viewingUser variable

Mike Tougeron

Well-known member
#1
The user ban method is defined as:
PHP:
XenForo_Model_User::ban($userId, $endDate, $reason, $update = false, &$errorKey = null, array $viewingUser = null)
but it doesn't use the $viewingUser variable; it always uses XenForo_Visitor.
Suggested fix
PHP:
//Line 838
if ( !empty($viewingUser) && isset($viewingUser['user_id']) ) {
    $banUserId = $viewingUser['user_id'];
} else {
    $banUserId = XenForo_Visitor::getUserId();
}
$dw->set('ban_user_id', $banUserId);
Thanks,
Mike
 

Pepelac

Well-known member
#3
Hmm.

Sorry to disturb you with this, but it seems to be broken in XF 1.1.1.

After upgrading forums to version 1.1.1 I can see, that all new ban records have 0 in the ban_user_id field.

And, as I can see, you call method XenForo_Model_User::ban() only in XenForo_ControllerAdmin_Banning and XenForo_Model_SpamCleaner. And none of this classes doesn't pass $viewingUser array to the XenForo_Model_User::ban()

ban_problem.png
 
R

ragtek

Guest
#4
I can confirm that it's still missing:(
I've changed it on my board to
PHP:
    public function ban($userId, $endDate, $reason, $update = false, &$errorKey = null, array $viewingUser = null)
    {
        if ($endDate < XenForo_Application::$time && $endDate !== self::PERMANENT_BAN)
        {
            $errorKey = 'please_enter_a_date_in_the_future';
            return false;
        }

        $dw = XenForo_DataWriter::create('XenForo_DataWriter_UserBan');
        if ($update)
        {
            $dw->setExistingData($userId);
        }
        else
        {
            $dw->set('user_id', $userId);
            $dw->set('ban_user_id', ($viewingUser == null) ? XenForo_Visitor::getUserId() : $viewingUser['user_id']);
        }

        $dw->set('end_date', $endDate);
        $dw->set('user_reason', $reason);
        $dw->preSave();

        if ($dw->hasErrors())
        {
            $errors = $dw->getErrors();
            $errorKey = reset($errors);
            return false;
        }

        $dw->save();
        return true;
    }