I've already created the login forms and I can authenticate against a table,
Do I even need to use Zend_Auth?
Where can I find the best way of implementing Zend_Auth?
开发者_如何学GoThanks
I use the following method to authenticate:
function authenticate ($data)
{
$db = \Zend_Db_Table::getDefaultAdapter();
$authAdapter = new \Zend_Auth_Adapter_DbTable($db);
$authAdapter->setTableName('usuarios2');
$authAdapter->setIdentityColumn('user');
$authAdapter->setCredentialColumn('password');
$authAdapter->setCredentialTreatment('MD5(?) and active = 1');
$authAdapter->setIdentity($data['user']);
$authAdapter->setCredential($data['password']);
$auth = \Zend_Auth::getInstance();
$result = $auth->authenticate($authAdapter);
if ($result->isValid()) {
if ($data['public'] == "1") {
\Zend_Session::rememberMe(1209600);
} else {
\Zend_Session::forgetMe();
}
return TRUE;
} else {
return FALSE;
}
}
$data is the post request from the login form, from the controller I call the function like this: authenticate($this->_request->getPost())
In any action if you want to verify the identity of the user you just:
$auth = Zend_Auth::getInstance();
if ($auth->hasIdentity()) {
$identity = $auth->getIdentity(); //this is the user in my case
}
At the login form I have a checkbox (named public) if its checked the the authentication information will be saved in a cookie otherwhise it will be deleted when the user closes the browser (Zend_Session::forgetMe())
This is a quick review of the auth process.
Here you have a tutorial from zendcasts. They even provide some unitesting stuff.
http://www.zendcasts.com/zend-acl-with-authentication-and-reflection/2009/06/
I think it's a really good start.
精彩评论