Zend_Auth login using either username or email as identityColumn

怎甘沉沦 提交于 2019-12-11 15:32:19

问题


I'm using Zend_Auth with a "Database Table Authentication". What I want to do is allow the user to login with either a username or email address as the "identityColumn". How would I allow both. I'm stuck.


回答1:


Extend and implement your own Auth Adapter and use query like "WHERE username = ? or email = ?" to get Auth result :)




回答2:


Authenticate twice (if needed) with 2 different Zend_Auth_Adapter_DbTable objects and add some logic to determine if the login is username or email by guessing if what type of login user provided so that you save one query for most of the cases.




回答3:


I know this is not the cleanest Zend_Auth implementation, but it works. The only problem comes if someone registers with an different email address as a username, but of course you can prevent this in your registration form. Hope it helps.

// if the entered value validates as an email address then use the 'email' field
// if not, use the 'username' field
$validator = new Zend_Validate_EmailAddress();
if ($validator->isValid($form->getValue('password'))) {
    $identityField = 'email';
} else {
    $identityField = 'username';
}

$authAdapter = new Zend_Auth_Adapter_DbTable(
    $dbAdapter,
'users',
    $identityField,
    'password',
    'SHA1(?)'
);

$authAdapter->setIdentity($form->getValue('username'))
    ->setCredential($form->getValue('password'));


来源:https://stackoverflow.com/questions/1386153/zend-auth-login-using-either-username-or-email-as-identitycolumn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!