XF 1.5 Only let guests see login and portal page

Nikolov69

Member
Hey everyone...

I'm very new to XenForo development and am really struggling to tamper with basic forum functions. What I want to do with the code below is only let guests view the portal, login, register, recover password, and terms&conditions pages. If they go to any other page, they are redirected to the portal page. The code below accomplishes that task with visitor_setup and front_controller_pre_view listeners. However, I'm completely locked out of the admin panel due to the way the visitor object works. Can you help point me in the right direction? I realise that what I've done is far from efficient.

PHP:
<?php

class NDZN_Listener
{

    private static $_visitor;

    public static function visitor_setup(XenForo_Visitor &$visitor) {

        // statically store visitor object
        $visitor = XenForo_Visitor::getInstance();
        self::$_visitor = $visitor;

    }

    public static function front_controller_pre_view(
        XenForo_FrontController $fc,
        XenForo_ControllerResponse_Abstract &$controllerResponse,
        XenForo_ViewRenderer_Abstract &$viewRenderer,
        array &$containerParams)
    {

        // check if visitor object has user ID
        $loggedIn = (self::$_visitor['user_id'] ? true : false);

        // ignore redirect controllers which don't have a template name
        if (!isset($controllerResponse->templateName)) {
            return false;
        }

        // get name of view template being rendered
        $templateName = $controllerResponse->templateName;
        $allowedTemplates = ['ndznPortal', 'login', 'register_form',
            'lost_password', 'help_wrapper'
        ];

        // if disallowed view template and not logged in, force login
        if (!in_array($templateName, $allowedTemplates) && !$loggedIn) {
            header("Location: /login");
            die('We tried to redirect you to /login but failed!');
        }

    }

}
 
Back
Top Bottom