【问题标题】:How to intercept and redirect each request on owncloud如何拦截和重定向owncloud上的每个请求
【发布时间】:2014-07-21 01:03:45
【问题描述】:

我在 owncloud 7.0 上遇到一个简单的问题

我正在创建一个必须检查条件并重定向到页面以验证某些内容的应用程序。我的目标是在条件正常之前禁用服务使用。

在标称场景下,用户登录,如果条件未验证,系统将用户重定向到验证页面。所以我使用 postLogin 钩子。

但是如果用户尝试在没有验证的情况下更改页面,我必须抓住他并将其重定向回验证页面。

我已经尝试过中间件(owncloud 拦截器),但它们不是全局的,所以第二种情况失败了。

现在我正在处理应用加载并执行类似的操作

 $app = new MyApp();
 $c = $app->getContainer();
 if ( $c->isLoggedIn() ) {
    $requestedPath = path($_SERVER['REQUEST_URI']);
    $redirectPath = $c->getServer()->getURLGenerator()->linkToRoute('myapp.page.validate');
    $refererPath  = path($_SERVER['HTTP_REFERER']);

    if ( $requestedPath !== $redirectPath && $redirectPath !== $refererPath ) {
        $location = $c->getServer()->getRouter()->generate('myapp.page.validate');
        header('Location: ' . $location);
        exit();
    }
 }

function path($url) {
    $urlArray = parse_url($url);
    return $urlArray['path'];
}

在第一种情况下它工作正常,但在第二种情况下我进行了几次重定向。 我认为它必须存在更好的解决方案。有人有想法吗?

PS:我在 IRC 频道上曝光了我的案例,但没有成功引起某人的兴趣 :)

【问题讨论】:

    标签: redirect owncloud


    【解决方案1】:

    如果您已在appinfo/info.xml 中将您的应用注册为authentication 类型,您或许可以使用appinfo/app.php 来执行此操作。这应该基本上看起来像下面的代码,但是这显然需要针对您的用例进行进一步调整。

    info.xml:

    <?xml version="1.0"?>
    <info>
        <id>appname</id>
        <name>Appname</name>
        <description>Lorem Ipsum.</description>
        <licence>AGPL</licence>
        <author>Your Name</author>
        <require>6.0.3</require>
        <types>
            <authentication/>
        </types>
    </info>
    

    app.php:

    <?php
    namespace OCA\appname\AppInfo;
    use \OCP\User;
    
    /**
     * Implement your code here
     * @return bool
     */
    function conditionMatch() {
        return true;
    }
    
    // Intercept all requests which have not already been matched
    if ($_SESSION['alreadyMatched'] !== true) {
        if(conditionMatch()) {
            $_SESSION['alreadyMatched'] = true;
        } else {
            // The session has not met the condition - enter your code here
            exit();
        }
    }
    

    【讨论】:

    • 谢谢。这是最好的方法,我现在只需要显示一个表格。非常感谢!
    猜你喜欢
    • 2018-02-03
    • 2020-09-20
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-20
    • 2015-06-25
    相关资源
    最近更新 更多