如何为受保护的页面定义重定向页面

编程入门 行业动态 更新时间:2024-10-27 02:26:04
本文介绍了如何为受保护的页面定义重定向页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

在 TYPO3 6.1 站点中,我想让编辑者尽可能轻松地创建受限 (fe_groups) 页面.不是一个单一的受保护区域,而是整个页面树中的几个受保护页面.

In a TYPO3 6.1 site, I would like to make the creation of restricted (fe_groups) pages as easy as possible for editors. There's not one single protected area, but several protected pages all over the pagetree.

我想要实现的是,只要页面有一些登录行为/限制并且没有有效的 fe_user 登录,就会重定向到中央登录页面.

What I would like to achieve would be that whenever a page has some login behaviour/restriction and no valid fe_user is logged in, there is a redirection to a central login page.

我找到了这篇文章TYPO3 - 当用户不在时重定向到登录页面登录 指的是同一个问题 - 但解决方案需要手动设置 PID.

I have found this post TYPO3 - Redirecting to login page when user is not logged in that refers to the same issue - but the solution requires setting PIDs by hand.

我简直不敢相信这样的功能(根据访问限制设置重定向的目标页面")不可用.或者它是否存在,或者它是否在某个路线图上?如果没有,是否有解决方法?

I can hardly believe that such a feature ("set target page for redirections based on access restrictions") is not available. Or does it exist, or is it on a roadmap somewhere? And if not, is there a workaround?

推荐答案

这确实是 TYPO3 中缺失的一大功能.问题在于,由于 TYPO3 的构建方式,很难确定页面是否不存在 (404) 或访问被禁止 (403).我对完成这项工作的未发布扩展做了一些进一步的开发,请参阅 https://github/phluzern/adfc_pagenotfound

This is indeed a big missing feature in TYPO3. The problem is that because of the way TYPO3 is built it's hard to determine whether a page doesn't exist (404) or access is forbidden (403). I did some further development of an unpublished extension that does the job, see https://github/phluzern/adfc_pagenotfound

在 readme.txt 中,您将找到所需的配置.它与 TYPO3 4.7 一起使用,因此一些使用的类可能会在 6.1 中被弃用或删除.如果是这样,请 fork 项目,更改它们并提出一些拉取请求,以便我可以更新它.

In readme.txt you will find the configuration that is needed. It is in use with TYPO3 4.7, therefore some used classes may be deprecated or removed in 6.1. If so, fork the project, change them and make some pull requests so I can update it.

该扩展使用自定义参数 $arPid(访问限制 pid).访问受限页面的 ID 被发送到登录页面.您的登录表单必须能够处理此参数才能重定向,请参见此处的示例:https://github/phluzern/phzldap/blob/master/pi1/class.tx_phzldap_pi1.php#L133

The extension makes use of a custom parameter $arPid (access restriction pid). The ID to the page that is access restricted is sent to the login page. Your login form must be able to handle this parameter in order to redirect, see an example here: https://github/phluzern/phzldap/blob/master/pi1/class.tx_phzldap_pi1.php#L133

最好使用redirect_url,因为felogin 支持它.

It might be better to use a redirect_url as it is supported in felogin.

更新

与此同时,我正在使用具有以下功能的改进类:

In the meantime, I'm using an improved class with the following features:

如果禁止访问页面,则使用标准的 redirect_url 参数重定向到登录页面.这允许在使用 EXT:felogin 成功登录后进行重定向,无需修改,并且还支持朗读 URL.如果未找到符合网站当前语言的页面,则重定向到 404 页面. If access to page is forbidden, redirect to a login page with the standard redirect_url parameter. This allows a redirect after a successful fe login using EXT:felogin without modifications and also supports speaking URLs. Redirect to 404 page if page is not found respecting the current language of the site.

代码如下:

<?php

use TYPO3\CMS\Core\Utility\GeneralUtility;

class user_pageNotFound {
    /**
     * Detect language and redirect to 404 error page
     *
     * @param array $params "currentUrl", "reasonText" and "pageAccessFailureReasons"
     * @param \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController $tsfeObj
     */
    public function pageNotFound($params, $tsfeObj) {
        /*
         * If a non-existing page with a RealURL path was requested (www.mydomain.tld/foobar), a fe_group value for an empty
         * key is set:
         * $params['pageAccessFailureReasons']['fe_group'][null] = 0;
         * This is the reason why the second check was implemented.
         */
        if (!empty($params['pageAccessFailureReasons']['fe_group']) && !array_key_exists(null, $params['pageAccessFailureReasons']['fe_group'])) {
            // page access failed because of missing permissions
            header('HTTP/1.0 403 Forbidden');
            $this->initTSFE(1);
            /** @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer $cObj */
            $cObj = GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer');
            $loginUrl = $cObj->typoLink_URL(array(
                'parameter' => $GLOBALS['TYPO3_CONF_VARS']['FE']['pageNotFound_handling_loginPageID'],
                'useCacheHash' => FALSE,
                'forceAbsoluteUrl' => TRUE,
                'additionalParams' => '&redirect_url=' . $params['currentUrl']
            ));
            TYPO3\CMS\Core\Utility\HttpUtility::redirect($loginUrl);
        } else {
            // page not found
            // get first realurl configuration array (important for multidomain)
            $realurlConf = array_shift($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['realurl']);
            // look for language configuration
            foreach ($realurlConf['preVars'] as $conf) {
                if ($conf['GETvar'] == 'L') {
                    foreach ($conf['valueMap'] as $k => $v) {
                        // if the key is empty (e.g. default language without prefix), break
                        if (empty($k)) {
                            continue;
                        }
                        // we expect a part like "/de/" in requested url
                        if (GeneralUtility::isFirstPartOfStr($params['currentUrl'], '/' . $k . '/')) {
                            $tsfeObj->pageErrorHandler('/index.php?id=' . $GLOBALS['TYPO3_CONF_VARS']['FE']['pageNotFound_handling_redirectPageID'] . '&L=' . $v);
                        }
                    }
                }
            }
            // handle default language
            $tsfeObj->pageErrorHandler('/index.php?id=' . $GLOBALS['TYPO3_CONF_VARS']['FE']['pageNotFound_handling_redirectPageID']);
        }
    }

    /**
     * Initializes a TypoScript Frontend necessary for using TypoScript and TypoLink functions
     *
     * @param int $id
     * @param int $typeNum
     */
    protected function initTSFE($id = 1, $typeNum = 0) {
        \TYPO3\CMS\Frontend\Utility\EidUtility::initTCA();
        if (!is_object($GLOBALS['TT'])) {
            $GLOBALS['TT'] = new \TYPO3\CMS\Core\TimeTracker\NullTimeTracker;
            $GLOBALS['TT']->start();
        }

        $GLOBALS['TSFE'] = GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Controller\\TypoScriptFrontendController',  $GLOBALS['TYPO3_CONF_VARS'], $id, $typeNum);
        $GLOBALS['TSFE']->sys_page = GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Page\\PageRepository');
        $GLOBALS['TSFE']->sys_page->init(TRUE);
        $GLOBALS['TSFE']->connectToDB();
        $GLOBALS['TSFE']->initFEuser();
        $GLOBALS['TSFE']->determineId();
        $GLOBALS['TSFE']->initTemplate();
        $GLOBALS['TSFE']->rootLine = $GLOBALS['TSFE']->sys_page->getRootLine($id, '');
        $GLOBALS['TSFE']->getConfigArray();

        if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('realurl')) {
            $rootline = \TYPO3\CMS\Backend\Utility\BackendUtility::BEgetRootLine($id);
            $host = \TYPO3\CMS\Backend\Utility\BackendUtility::firstDomainRecord($rootline);
            $_SERVER['HTTP_HOST'] = $host;
        }
    }

}

您唯一需要配置的是未找到页面和登录页面的PID:

The only thing you need to configure are the PIDs of the page not found and login pages:

// ID of the page to redirect to if page was not found
$GLOBALS['TYPO3_CONF_VARS']['FE']['pageNotFound_handling_redirectPageID'] = 4690;
// ID of the page to redirect to if current page is access protected
$GLOBALS['TYPO3_CONF_VARS']['FE']['pageNotFound_handling_loginPageID'] = 5404;

这篇关于如何为受保护的页面定义重定向页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-29 09:03:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1187984.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:页面   何为   重定向   定义

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!