/**
  * Login User
  */
 public function loginAction()
 {
     $this->_helper->layout->setLayout('single');
     if ($this->getRequest()->isPost()) {
         $values = $this->getRequest()->getPost();
         if ($values['username'] != '' && $values['password'] != '') {
             $adapter = new Auth_Adapter_DoctrineTable();
             $adapter->setCredentials(trim($values['username']), trim($values['password']));
             $storage = new Auth_Adapter_AuthDoctrineDbStorage('auth');
             $auth = Zend_Auth::getInstance();
             $auth->setStorage($storage);
             $result = $auth->authenticate($adapter);
             if ($result->isValid()) {
                 if (isset($values['keepLoggedIn'])) {
                     $encryption = new Cryptography_EncryptionService('12675');
                     $cookievalue = trim($values['username']) . '||' . trim($values['password']);
                     $cookievalue = $encryption->encrypt($cookievalue);
                     setcookie('authPersistence', $cookievalue, time() + 2592000, '/');
                 }
                 return $this->_redirect('/profile');
             } else {
                 $this->view->error = $result->getMessages();
                 return $this->render('login');
             }
         } else {
             $errors = array('Please enter both a username and a password');
             $this->view->error = $errors;
             return $this->render('login');
         }
     } else {
         if ($cookie = $this->getRequest()->getCookie('authPersistence', FALSE)) {
             $encryption = new Cryptography_EncryptionService('12675');
             $cookie = $encryption->decrypt($cookie);
             $credentials = explode('||', $cookie);
             $adapter = new Auth_Adapter_DoctrineTable();
             $storage = new Auth_Adapter_AuthDoctrineDbStorage('auth');
             $auth = Zend_Auth::getInstance();
             $auth->setStorage($storage);
             $result = $auth->authenticate($adapter);
             if ($result->isValid()) {
                 return $this->_redirect('/profile');
             } else {
                 $this->view->error = $result->getMessages();
                 return $this->render('login');
             }
         } else {
             return $this->render('login');
         }
     }
 }
 /**
  * Clears contents from storage
  * 
  * @throws Zend_Auth_Storage_Exception If clearing contents is not successful
  * @return bool
  */
 public function clear()
 {
     $requestObject = new Zend_Controller_Request_Http();
     if ($cookie = $requestObject->getCookie(self::$_cookieName, FALSE)) {
         //Decrypt Cookie
         $encryption = new Cryptography_EncryptionService('1111834');
         $decrypted = $encryption->decrypt($cookie);
         //Separate Session ID from UserID
         $sessioncookie = explode('||', $decrypted);
         $sessionid = $sessioncookie[0];
         $query = Doctrine_Query::create()->delete('Model_Session')->where('id = ?', $sessionid);
         try {
             $query->execute();
         } catch (Doctrine_Exception $e) {
             throw new Zend_Auth_Storage_Exception();
         }
         setcookie(self::$_cookieName, "", time() - 3600, '/');
         return true;
     } else {
         throw new Zend_Auth_Storage_Exception();
     }
 }