Zend_Auth Best Practices

My goal is to require a login for certain pages. I am using Zend Framework MVC and I am trying to find examples regarding best practices.

Some notes on what I'm looking for:

  • I want users who are not logged in to receive a login window and then reverted to the registered version of the page, after authentication
  • I want to use dependency injection and avoid singles
  • Small code size - binding to the mvc Zend structure.
  • Should the login box be a separate controller and redirect the header header? How to return to the landing page after successful success? Is the idea of ​​simply invoking an input controller to display the login window of the landing page, or is it a flaw in the indexing of search engines?
  • Be able to use an external library for processing cookies.

Or something completely different. I am new to the Zend framework and I want to do it in the “right way”.

+5
source share
3 answers
  • I want to prevent users from registering in order to get the login window and then return to the log in the page version, once authentication

Use the FrontController plugin and redirect or forward them to your loginAction.

  • I want to use dependency injection and avoid singles

Zend Framework, DI, Zend_Application_Resource_ * . ?

  • - mvc Zend.

.

  • ? ? ?

AuthController LoginAction LogoutAction. , , returnUrl , URL-, , , / .

  • cookie.

Zend_Auth , .

$auth = Zend_Auth::getInstance();
$auth->setStorage(new My_Auth_Storage());

cookie, -.

.

+4

Zend_Auth Zend_Acl. , , zend:

predispatch , . :

class Plugin_AccessCheck extends Zend_Controller_Plugin_Abstract {

    private $_acl = null;

    public function __construct(Zend_Acl $acl) {
        $this->_acl = $acl;
    }

    public function preDispatch(Zend_Controller_Request_Abstract $request) {
        //get request information
        $module = $request->getModuleName ();
        $resource = $request->getControllerName ();
        $action = $request->getActionName ();

        try {
            if(!$this->_acl->isAllowed(Zend_Registry::get('role'), 
                                $module . ':' . $resource, $action)){
                $request->setControllerName ('authentication')
                        ->setActionName ('login');
            }
        }catch(Zend_Acl_Exception $e) {
            $request->setControllerName('index')->setActionName ('uups');
        }
    }
}

, , acl. , . , preDispatch .

Zend_Acl , , , :

class Model_LibraryAcl extends Zend_Acl {
    public function __construct() {

        $this->addRole(new Zend_Acl_Role('guests'));
        $this->addRole(new Zend_Acl_Role('users'), 'guests');
        $this->addRole(new Zend_Acl_Role('admins'), 'users');                

        $this->add(new Zend_Acl_Resource('default'))
             ->add(new Zend_Acl_Resource('default:authentication'), 'default')
             ->add(new Zend_Acl_Resource('default:index'), 'default')
             ->add(new Zend_Acl_Resource('default:error'), 'default');

        $this->allow('guests', 'default:authentication', array('login'));
        $this->allow('guests', 'default:error', 'error');

        $this->allow('users', 'default:authentication', 'logout');          
    }
}

acl auth :

    private $_acl = null;

    protected function _initAutoload() {

       //...your code           
       if (Zend_Auth::getInstance()->hasIdentity()){
        Zend_Registry::set ('role',
                     Zend_Auth::getInstance()->getStorage()
                                              ->read()
                                              ->role);
        }else{
            Zend_Registry::set('role', 'guests');
        }

        $this->_acl = new Model_LibraryAcl ();
        $fc = Zend_Controller_Front::getInstance ();
        $fc->registerPlugin ( new Plugin_AccessCheck ( $this->_acl ) );

        return $modelLoader;
    }

, auth :

public function logoutAction() {
    Zend_Auth::getInstance ()->clearIdentity ();
    $this->_redirect ( 'index/index' );
}

private function getAuthAdapter() {
    $authAdapter = new Zend_Auth_Adapter_DbTable ( 
                        Zend_Db_Table::getDefaultAdapter ());
    $authAdapter->setTableName('users')
                ->setIdentityColumn('email')
                ->setCredentialColumn ('password')
                ->setCredentialTreatment ('SHA1(CONCAT(?,salt))');

    return $authAdapter;
}

auth, .

$authAdapter = $this->getAuthAdapter ();
$authAdapter->setIdentity ( $username )->setCredential ( $password );
$auth = Zend_Auth::getInstance ();
$result = $auth->authenticate ( $authAdapter );

if ($result->isValid ()) {
    $identity = $authAdapter->getResultRowObject ();
    if ($identity->approved == 'true') {
        $authStorage = $auth->getStorage ();
        $authStorage->write ( $identity );
        $this->_redirect ( 'index/index' );
    } else {
       $this->_redirect ( 'authentication/login' );
  }

. > youtube zend auth zend acl.

+4

, , , , :

  • LoginForm ( )
  • AuthService getLoginForm, login, logout : getIdentity, hasIdentity .
  • , , hasIdentity, LoginForm, , .
  • , - preDispatch. . AuthService->hasIdentity() true, . , , ..: $request->getPost( 'loginSubmitButton', null ); null, . null a login( $request->getPost() ) . , ( ).

, , - ServiceAbstract::getService( 'Auth' ), ServiceAbstract .

+1
source

All Articles