Php 对不同的方法参数使用身份验证(Restler 3)

Php 对不同的方法参数使用身份验证(Restler 3),php,authentication,basic-authentication,http-authentication,restler,Php,Authentication,Basic Authentication,Http Authentication,Restler,如果参数具有特定值,我想限制对方法的访问。让我们以此类为例: Simple.php: class Simple { function item($name) { if($name == "somerestricted") { // Here should be an authentication check (or somewhere else), hopefully, using an iAuthenticate clas

如果参数具有特定值,我想限制对方法的访问。让我们以此类为例:

Simple.php:
class Simple
{
    function item($name)
    {
        if($name == "somerestricted")
        {
            // Here should be an authentication check (or somewhere else), hopefully, using an iAuthenticate class
            // Later, there will be a check using a database to determine if authentication will be required
            // So user/password may vary
            if($authenticated)
            {
                // Proceed
            }
            else
            {
                // ???
            }
        }
        else
        {
            echo "Hi!";
        }
    }
}
使用此身份验证类:

BasicAuthentication.php:
class BasicAuthentication implements iAuthenticate
{
    const REALM = 'Restricted API';
    function __isAllowed()
    {
        if(isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW']))
        {
            $user = $_SERVER['PHP_AUTH_USER'];
            $pass = $_SERVER['PHP_AUTH_PW'];
            if($user == 'laterfetched' && $pass == 'fromdatabase')
            {
                return true;
            }
        }
        header('WWW-Authenticate: Basic realm="'.self::REALM.'"');
        throw new RestException(401, 'Basic Authentication Required');
    }
}
Index.php(网关): addAuthenticationClass('BasicAuthentication'); $r->addAPIClass('Simple'); $r->handle()

simple/item
方法现在可以公开访问。但是,如果我将
转换为
受保护的
函数,每个请求都需要身份验证。这不是我想做的。只有
simple/item/somerestricted
需要身份验证

那么,是否有办法将
iaauthenticate
限制为特定的参数值?如果没有,我怎么解决这个问题呢

用户名和密码在生产使用中会有所不同(取决于给定的参数)

我发现了这些相关问题:以及


我使用的是Restler rc4。

您已经将自己的api制作成一个混合api,它是公开的,如果用户经过身份验证,将增强结果

一种方法如下所示。它在Restler中使用隐藏属性

class Simple
{
    /**
     * @var \Luracast\Restler\Restler
     */
    public $restler;
    /**
     * @access hybrid
     */
    function item($name)
    {
        if ($name == "somerestricted") {
            if ($this->restler->_authenticated) {
                // Proceed
            } else {
                // ???
            }
        } else {
            echo "Hi!";
        }
    }
}
另一种(推荐)方法是使用iUseAuthentication接口

use Luracast\Restler\iUseAuthentication;

class Simple implements iUseAuthentication
{
    protected $authenticated;

    /**
     * @access hybrid
     */
    function item($name)
    {
        if ($name == "somerestricted") {
            if ($this->authenticated) {
                // Proceed
            } else {
                // ???
            }
        } else {
            echo "Hi!";
        }
    }

    public function __setAuthenticationStatus($isAuthenticated = false)
    {
        $this->authenticated = $isAuthenticated;
    }
}

如果用于身份验证的用户名/密码依赖于$name参数,该怎么办?不需要,在上述用例中也不需要。在上述用例中不需要,对吧。但是如果。。。假设我想使用(针对该项目)生成的用户名/密码保护每个项目?这可能吗?对于这种情况,身份验证类将使用给定的用户名和密码标识用户,并设置用户类的静态属性。Api方法将首先获取请求的对象,然后将所有者与当前用户进行比较。如果用户不是所有者,它可以抛出异常,例如403禁止。你不是主人