<?php
class Form_Login extends Zend_Form
{
public function init()
{
//
$ this ->setMethod( 'post' );
$ this ->addElement( 'text' , 'userName' , array(
'label' => ' :' ,
'filters' => array( 'StringTrim' )
));
$el = $ this ->getElement( 'userName' );
$el->setRequired( true )
->addValidators(array(
array( 'NotEmpty' , true , array( 'messages' => array(
'isEmpty' => ' !' ,
)))));
$ this ->addElement( 'password' , 'password' , array(
'label' => ':'
));
$el = $ this ->getElement( 'password' );
$el->setRequired( true )->addValidators(array(
array( 'NotEmpty' , true , array( 'messages' => array(
'isEmpty' => ' !' ,
)))));
$ this ->addElement( 'submit' , 'login' , array(
'label' => ''
));
}
}
* This source code was highlighted with Source Code Highlighter .
<?php
class LoginController extends Zend_Controller_Action
{
public function preDispatch()
{
if (Zend_Auth::getInstance()->hasIdentity()) {
return $ this ->_redirect( '/' ); // ,
}
}
public function indexAction()
{
$form = $ this ->_getLoginForm();
if ($ this ->_request->isPost()) {
$formData = $ this ->_request->getPost();
if ($form->isValid($formData)) {
$auth = Zend_Auth::getInstance();
$authAdapter = $ this ->_getAuthAdapter($formData[ 'userName' ],$formData[ 'password' ]);
$result = $auth->authenticate($authAdapter);
if (!$result->isValid()) {
//
$form->setDescription( ' ' );
$form->populate($formData);
$ this ->view->form = $form;
return $ this ->render( 'index' ); //
} else {
$currentUser = $authAdapter->getResultRowObject();
Zend_Auth::getInstance()->getStorage()->write( $currentUser);// auth, -
return $ this ->_redirect( '/' ); //,
}
} else {
$form->populate($formData);
}
}
$ this ->view->form = $form;
}
protected function _getLoginForm()
{
require_once APPLICATION_PATH . '/forms/Login.php' ;
return new Form_Login();
}
protected function _getAuthAdapter($userName, $userPassword)
{
$authAdapter = new Zend_Auth_Adapter_DbTable(
$registry->dbAdapter,
'user' ,
'username' ,
'passwordMD5' ,
'MD5(?) AND status = "OK"'
);
$authAdapter->setIdentity($userName)->setCredential($userPassword);
return $authAdapter;
}
}
?>
* This source code was highlighted with Source Code Highlighter .
$configuration = new Zend_Config(require APPLICATION_PATH . '/config/config.php' );
$dbAdapter = Zend_Db::factory($configuration->database);
Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter);
$registry = Zend_Registry::getInstance();
$registry->configuration = $configuration;
$registry->dbAdapter = $dbAdapter;
$registry->session = new Zend_Session_Namespace();
* This source code was highlighted with Source Code Highlighter .
Zend_Session::rememberMe(1209600);
// here everyone decides for himself how much he needs$auth = Zend_Auth::getInstance()->getIdentity();
<?php
class CheckLoginPlugin extends Zend_Controller_Plugin_Abstract
{
protected $_userModel;
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request){
$auth = Zend_Auth::getInstance();
$user = $auth->getIdentity();
$model = $ this ->_getUserModel();
$auth->getStorage()->write($model->getUserById($user->id));
}
public function _getUserModel(){
if ( null === $ this ->_userModel) {
require_once APPLICATION_PATH . '/models/User.php' ;
$ this ->_userModel = new Model_User();
}
return $ this ->_userModel;
}
}
?>
* This source code was highlighted with Source Code Highlighter .
require_once 'My/Plugin/CheckLoginPlugin.php' ;
$frontController->registerPlugin( new CheckLoginPlugin());
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/53763/
All Articles