Zend framework-チュートリアル:認証

ネットワーク上にはそのようなチュートリアルがすでにたくさんあることは知っていますが、私自身もZFが最初は非常に難しいことを知っていますが、それから...すべてが明確で簡単です。
まだ旅の始めにいる人たちのために、この最初の段階を促進したいと思います。

承認のためには、もちろん既製のユーザーテーブルが必要です-少なくとも2つのフィールド、userNameとpasswordMD5。
passwordMD5-誰かが一度に盗まないように、パスワードが暗黙的に保存されることがすぐにわかります。

1.ログインフォームを作成します。



<?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 .


このクラスを/ application / forms(または好きなもの)に入れます

2.ログイン用のコントローラー。
<?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 .


レジストリ:: getInstance()->セッション-bootstrap.phpでセッションを作成し、レジストリオブジェクトに慎重に挿入します。

$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); //ここで、誰もが必要な量を自分で決める

ログイン後、次の方法でコード内のどこからでもユーザーオブジェクトにアクセスできます。

$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 .


bootstrap.phpでプラグインを接続します

require_once 'My/Plugin/CheckLoginPlugin.php' ;
$frontController->registerPlugin( new CheckLoginPlugin());


* This source code was highlighted with Source Code Highlighter .


このプラグインは、ページが呼び出されるたびにデータベースからオブジェクトを更新するだけです。 もちろん、必要な場合にのみこれを行うことができます。誰かが保存すると、十分なマッチがあります:)

PS-この例には、もちろん、いくつかのエラー(論理)が含まれている可能性があり、擬似コードとして扱いますが、PCPの知識が最小限であれば、簡単に修正できると思います。

OpenIDを使用してzendプログラムの認証を行うこともできます

Source: https://habr.com/ru/post/J53763/


All Articles