Zend_Auth uma Implementação Completa
Autor: Cesar Scur | Publicado: 2010-12-15T00:21:00-02:00


Sim eu gostaria muito de colocar um screencast explicando, mas é fim de ano e as coisas nem sempre são fáceis quando se tem mais tempo livre. Mas por sorte eu tive tempo de arrumar uma imagem e de terminar o código que eu vou explicar mais tarde. Com sorte vai ter algumas pessoas contentes.




//indexController.php
&lt;?php
class IndexController
    extends Zend_Controller_Action
{

    /**
     * TODO: move to a auth controller as login action
     */
    public function indexAction()
    {
        $request = $this-&gt;getRequest();
        /**
         * TODO: change to Zend_Form isValid()
         */
        if($request-&gt;isPost()
                &amp;&amp; $request-&gt;getParam('login')
                &amp;&amp; $request-&gt;getParam('pass')) {

            $adapter = new My_Auth_Adapter(
                    $request-&gt;getParam('login'),
                    $request-&gt;getParam('pass')
            );

            $auth = Zend_Auth::getInstance();
            $result = $auth-&gt;authenticate($adapter);

            if(! $auth-&gt;hasIdentity() ) {
                $this-&gt;view-&gt;msgs = $result-&gt;getMessages();
            } else {
                /**
                 * BONUS!!!
                 * Redirect to the place that the user
                 * was trying to access before
                 */
                $this-&gt;_redirect(
                    $request-&gt;getControllerName()
                  . '/'
                  . $request-&gt;getActionName()
                );
            }
        }

        echo 'Params';
        Zend_Debug::dump($this-&gt;getRequest()-&gt;getParams());
    }

    /**
     * TODO: move to a auth controller as logout action
     */
    public function logoutAction()
    {
        Zend_Auth::getInstance()-&gt;clearIdentity();
        $this-&gt;_redirect('');

    }

    /**
     * Some restrict stuff to test
     */
    public function secretAction()
    {
        echo 'Este &amp;eacute; um conteudo Secreto';
        Zend_Debug::dump(Zend_Auth::getInstance()-&gt;hasIdentity());
        //todo:bad die
        die();
    }


}

// library/My/Auth/Adapter.php
&lt;?php
class My_Auth_Adapter
    implements Zend_Auth_Adapter_Interface
{

    /**
     * Error messeges
     */
    const BAD_LOGIN = 'Login ou Senha Invalido!';

    /**
     *
     * @var string
     */
    protected $_login = null;

    /**
     *
     * @var string
     */
    protected $_pass = null;

    /**
     * Constructor
     * @param string $login
     * @param string $pass
     */
    public function __construct($login, $pass) 
    {
        $this-&gt;_login = $login;
        $this-&gt;_pass = $pass;
    }


    /**
     * Implementation of authenticate for Zend_Auth
     * @return Zend_Auth_Result
     */
    public function authenticate()
    {
        $user = Doctrine::getTable('User')-&gt;findOneByLogin($this-&gt;_login);

        if($user) {
            if('ldap' == $user-&gt;type) {
                return $this-&gt;ldapAuth($user);
            } else {
                return $this-&gt;defaultAuth($user);
            }
        }
        /**
         * TODO: What if
         * $type = $user-&gt;type . 'Auth';
         * $this-&gt;$type();
         * ???
         */

        $result = new Zend_Auth_Result(
                Zend_Auth_Result::FAILURE,
                $user,
                array(self::BAD_LOGIN)
        );
        return $result;

    }

    /**
     * Default Auth Method
     * @param User $user
     * @return Zend_Auth_Result
     */
    public function defaultAuth($user)
    {
        if($user-&gt;pass == $this-&gt;_pass) {
            $result = new Zend_Auth_Result(
                    Zend_Auth_Result::SUCCESS,
                    $user,
                     array('hoorey')
            );
            return $result;
        }

        $result = new Zend_Auth_Result(
                Zend_Auth_Result::FAILURE,
                $user,
                array(self::BAD_LOGIN)
        );
        return $result;
    }


    /**
     * Ldap Auth method
     * @param User $user
     * @return Zend_Auth_Result
     */
    public function ldapAuth($user)
    {
        /**
         * TODO: LDAP Logic with Zend_Auth_Adapter_Ldap
         */
        $result = new Zend_Auth_Result(
                Zend_Auth_Result::FAILURE,
                $user,
                array(self::BAD_LOGIN)
        );
        return $result;
    }
}

// library/My/Controller/Plugin/Auth.php
&lt;?php
class My_Controller_Plugin_Auth
    extends Zend_Controller_Plugin_Abstract
{
    public function  preDispatch(Zend_Controller_Request_Abstract $request) 
    {

        if(!Zend_Auth::getInstance()-&gt;hasIdentity()) {
            $request-&gt;setControllerName('index');
            $request-&gt;setActionName('index');
        }
        
    }

}


[img por deaddamien]