[英]How do I get the BaseDn the user is authenticated against in Zend Framework 2
[英]PHP Zend Authentication - How can I tell Zend the user is authenticated
我需要告訴Zend,用戶是在特定情況下進行身份驗證的,但我不想為此目的構建身份驗證適配器。
我想要這樣的東西:
if ($user == 'sam') {
// this user is valid, tell Zend to authenticate
Zend_Auth::authenticate(true);
}
換句話說,我不想將確定用戶是否有效的任務委托給某個Auth Adapter。我想告訴Zend:“嘿,這個家伙和我在一起,所以設置身份驗證cookie並讓他使用我的網站”。
有沒有辦法做到這一點?
您可以通過寫入Zend_Auth存儲來實現:
$userData = (object)array('username' => 'Johny23');
Zend_Auth::getInstance()->getStorage()->write($userData);
當您這樣做時,Zend會自動創建新的Zend_Auth_Storage_Session對象來處理會話。
然后,您可以檢查:
$auth = Zend_Auth::getInstance();
if($auth->hasIdentity()){
echo 'welcome back, ' . $auth->getIdentity()->username;
}
http://framework.zend.com/manual/1.12/zh/zend.auth.adapter.dbtable.html
檢查“高級用法示例”下的最后一個代碼,代碼如下
$registry = Zend_Registry::getInstance();
$DB = $registry['DB'];
$authAdapter = new Zend_Auth_Adapter_DbTable($DB,'usertable','username','password');
$authAdapter->setIdentity($request->getParam('username'));
$authAdapter->setCredential($request->getParam('password'));
$select = $authAdapter->getDbSelect();
$select->where('`active` = 1');
$result = $authAdapter->authenticate();
if($result->isValid()){
//set user proper to zend instance
$this->_redirect('/index');
}
else
{
//logout or redirect to same page
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.