XF 2.1 Using canBan to add additional criteria

AndrewSimm

Well-known member
I am creating an addon that requires moderators to have an additional permission to ban users in a defined user group. What I am trying to do is add some additional conditions into canBan. Right now I can only get it to work if I copy over all the code in canBan, but I don't want to do that in case XF changes this function. Any insight into what I am doing wrong would be helpful.


My addon (Does not work)
PHP:
    public function canBan(&$error = null)
    {

        $visitor = \XF::visitor();
        $options = $this->app()->options();

        $protectedUser = $options->andrewModeratorPanelProtected;

        foreach ($this->secondary_group_ids as $groupId)
        {
            if ($groupId == $protectedUser && !$visitor->canBanProtectedUsers())
            {
                return false;
            }
        }

        parent::canBan();
        
    }


Xenforo Function
PHP:
    public function canBan(&$error = null)
    {
        $visitor = \XF::visitor();

        if (!$this->user_id || !$visitor->is_moderator || $this->user_id == $visitor->user_id)
        {
            return false;
        }

        if ($this->is_admin || $this->is_moderator)
        {
            $error = \XF::phraseDeferred('this_user_is_an_admin_or_moderator_choose_another');
            return false;
        }

        return $visitor->hasPermission('general', 'banUser');
    }

It works when I repeat code, which I don't want to do
PHP:
    public function canBan(&$error = null)
    {

        $visitor = \XF::visitor();
        $options = $this->app()->options();

        $protectedUser = $options->andrewModeratorPanelProtected;

        foreach ($this->secondary_group_ids as $groupId)
        {
            if ($groupId == $protectedUser && !$visitor->canBanProtectedUsers())
            {
                return false;
            }
        }

        if (!$this->user_id || !$visitor->is_moderator || $this->user_id == $visitor->user_id)
        {
            return false;
        }

        if ($this->is_admin || $this->is_moderator)
        {
            $error = \XF::phraseDeferred('this_user_is_an_admin_or_moderator_choose_another');
            return false;
        }

        return $visitor->hasPermission('general', 'banUser');

    }
 
Top Bottom