quarta-feira, 15 de dezembro de 2010

Zend_Auth uma Implementação Completa


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
<?php
class IndexController
    extends Zend_Controller_Action
{

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

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

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

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

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

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

    }

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


}

// library/My/Auth/Adapter.php
<?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->_login = $login;
        $this->_pass = $pass;
    }


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

        if($user) {
            if('ldap' == $user->type) {
                return $this->ldapAuth($user);
            } else {
                return $this->defaultAuth($user);
            }
        }
        /**
         * TODO: What if
         * $type = $user->type . 'Auth';
         * $this->$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->pass == $this->_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
<?php
class My_Controller_Plugin_Auth
    extends Zend_Controller_Plugin_Abstract
{
    public function  preDispatch(Zend_Controller_Request_Abstract $request) 
    {

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

}


[img por deaddamien]