Zend: Auth Adapter Config

<?php
class Dropper_Auth_Adapter_Config implements Zend_Auth_Adapter_Interface
{
        protected $_credential;
        protected $_identity;        protected $_credentialField;
        protected $_identityField;
        
        /**
         * Global config         *
         * @var Zend_Config
         */
        protected $_config;
                public function __construct(Zend_Config $config, $credentialField, $identityField)
        {
                $this->_config = $config;
                $this->setCredentialField($credentialField)
                        ->setIdentityField($identityField);        }
        
        /**
         * Set credential field
         *         * @param string $credentialField
         * @return Dropper_Auth_Adapter_Config
         */
        public function setCredentialField($credentialField)
        {                $this->_credentialField = $credentialField;
                return $this;
        }
        
        /**         * Set identity field
         *
         * @param string $identityField
         * @return Dropper_Auth_Adapter_Config
         */        public function setIdentityField($identityField)
        {
                $this->_identityField = $identityField;
                return $this;
        }        
        /**
         * Set credential value
         *
         * @param string $credentia         * @return Dropper_Auth_Adapter_Config
         */
        public function setCredential($credential)
        {
                $this->_credential = $credential;                return $this;   
        }
        
        /**
         * Set identity value         *
         * @param string $identity
         * @return Dropper_Auth_Adapter_Config
         */
        public function setIdentity($identity)        {
                $this->_identity = $identity;
                return $this;
        }
                /**
         * Authenticate process
         *
         * @return Zend_Auth_Result
         */        public function authenticate()
        {
                $crField = explode(".", $this->_credentialField);
                $idField = explode(".", $this->_identityField);
                $tmpConfig = $this->_config;                foreach($crField as $part) {
                        $tmpConfig = $tmpConfig->get($part);
                        if (!$tmpConfig instanceof Zend_Config) {
                                $credential = $tmpConfig;
                        }                }
                
                
                $tmpConfig = $this->_config;
                foreach($idField as $part) {                        $tmpConfig = $tmpConfig->get($part);
                        if (!$tmpConfig instanceof Zend_Config) {
                                $identity = $tmpConfig;
                        }
                } 
                if (!$credential || !$identity) {
                        throw new Zend_Auth_Adapter_Exception("Credential and Identity fields shouldn't be empty");
                }
                                $result = array(
                        'code' => Zend_Auth_Result::FAILURE,
                        'messages' => array('Invalid identity or credential')
                );
                                if ($this->_identity == $identity && $this->_credential ==md5($credential)) {
                        $result = array(
                                'code' => Zend_Auth_Result::SUCCESS,
                                'messages' => array('Identity and credential are valid')
                        );                }
                
                return new Zend_Auth_Result($result['code'], $this->_identity, $result['messages']);
        }
}?>


/**
 *@example
 */
<?php
    try {                                        $authAdapter = new Dropper_Auth_Adapter_Config($this->_config, "user.password", "user.login");
                                        $authAdapter->setIdentity($form->getValue('username'))
                                                                ->setCredential($form->getValue('password'));
                                        $result = Zend_Auth::getInstance()->authenticate($authAdapter);
                                } catch (Zend_Auth_Adapter_Exception $e) {                                        //exception handling
                                }
?>
Processing your request, Please wait....

No Responses to “Zend: Auth Adapter Config”

Comments (Your Comments)

Leave a Reply

You must be logged in to post a comment.