wsdl soap 代码中使用Yii::app()->user->isGuest 一直返回true,请问是Bug吗?
wsdl soap 代码中使用Yii::app()->user->isGuest 一直返回true,请问是Bug吗?
不是! soap 是webservice 惯用的一种协议 但常规webservice 基本设计原则之一就是无状态的 你想想程序基本不是对user 就是对其他程序 。webservice就是对其他程序的 不是通过浏览器访问的(常用curl来调用webservice) 所以基本不能标识谁是访问者 如果你要实现验证机制 需要自己模拟session机制的实现。
常规用户通过浏览器访问你的web程序时是有session支持的(一般在cookie中传递sessionId 如果cookie不能用会通过get或者post来传递 你可以看下firebug的firecookie插件–已内置了现在 就可以看清楚这个session_id了) 所以如果要验证机制你需要自己模拟php端如何标识sessionId 和过期GC的功能来实现 有点小复杂的功能呢 可能需要某些持久化(比如db配合)
note: 我也没搞过这方面的东西 但我想大概如此吧 或者就是要你实现类似Oauth协议那种机制
谢谢,这个我也想过,但yii demo/phonebook 中有这么使用,还以为真可以这样用呢。
/**
* This method is required by IWebServiceProvider.
* It makes sure the user is logged in before making changes to data.
* @param CWebService the currently requested Web service.
* @return boolean whether the remote method should be executed.
*/
public function beforeWebMethod($service)
{
$safeMethods=array(
'login',
'getContacts',
);
$pattern='/^('.implode('|',$safeMethods).')$/i';
if(!Yii::app()->user->isGuest || preg_match($pattern,$service->methodName))
return true;
else
throw new CException('Login required.');
}