From e4d3c4db0929770ebf70fd0d943821c5ca128519 Mon Sep 17 00:00:00 2001 From: Björn Geiger Date: Mon, 21 Mar 2011 10:06:49 +0100 Subject: Login in User Module --- .../modules/user/controllers/AuthController.php | 196 ++++++++++++++++++--- application/modules/user/forms/Login.php | 27 ++- application/modules/user/forms/RecoverPassword.php | 28 +++ application/modules/user/forms/Register.php | 105 +++++++++++ .../modules/user/views/scripts/auth/login.phtml | 5 + .../user/views/scripts/auth/recoverpassword.phtml | 4 + .../modules/user/views/scripts/auth/register.phtml | 4 + 7 files changed, 347 insertions(+), 22 deletions(-) create mode 100644 application/modules/user/forms/RecoverPassword.php create mode 100644 application/modules/user/forms/Register.php create mode 100644 application/modules/user/views/scripts/auth/login.phtml create mode 100644 application/modules/user/views/scripts/auth/recoverpassword.phtml create mode 100644 application/modules/user/views/scripts/auth/register.phtml (limited to 'application') diff --git a/application/modules/user/controllers/AuthController.php b/application/modules/user/controllers/AuthController.php index 80a411d..ff5893f 100644 --- a/application/modules/user/controllers/AuthController.php +++ b/application/modules/user/controllers/AuthController.php @@ -3,25 +3,179 @@ class User_AuthController extends Zend_Controller_Action { - public function init() - { - /* Initialize action controller here */ - } - - public function indexAction() - { - // action body - $membershipID = $this->_request->getParam('membershipID'); - if($membershipID == ''){ - $_SESSION['membershipID'] = 1; - } - else{ - $_SESSION['membershipID'] = $membershipID; - } - $pbsNotifier = new Pbs_Notifier(); - echo $pbsNotifier->notify("membershipID is set to ".$_SESSION['membershipID'],'ok'); - } - - -} + protected $personmapper = null; + private $db = null; + + public function init() + { + $this->db = Zend_Db_Table::getDefaultAdapter(); + $this->personmapper = new Application_Model_PersonMapper(); + } + + public function indexAction() + { + // action body + $membershipID = $this->_request->getParam('membershipID'); + if($membershipID == ''){ + $_SESSION['membershipID'] = 1; + } + else{ + $_SESSION['membershipID'] = $membershipID; + } + $pbsNotifier = new Pbs_Notifier(); + echo $pbsNotifier->notify("membershipID is set to ".$_SESSION['membershipID'],'ok'); + } + + public function loginAction() + { + if (Zend_Auth::getInstance()->hasIdentity()) { + $this->_redirect('/user/'); + } else { + if (!isset($_POST["login"])){ + $loginForm = new user_Form_Login(); + } else { + $loginForm = new user_Form_Login($_POST); + + if ($loginForm->isValid($_POST)) { + + $auth = Zend_Auth::getInstance(); + + $adapter = new Zend_Auth_Adapter_DbTable( + $this->db, + 'pbs_person', + 'email', + 'password', + 'MD5(CONCAT(?, password_salt))' + ); + + + $adapter->setIdentity($loginForm->getValue('email')); + $adapter->setCredential($loginForm->getValue('password')); + + $result = $auth->authenticate($adapter); + + // TODO: erweiterte fehlerbeschreibung des Users + + if ($result->isValid()) { + $this->personmapper = new Application_Model_PersonMapper(); + $result = $this->personmapper->findBy('email', Zend_Auth::getInstance()->getIdentity()); + $person = new Application_Model_Person($result[0]); + $person->setID($result[0]['personID']); + $date = new DateTime(); + $person->setLogindate($date->getTimestamp()); + $this->personmapper->save($person); + $this->_redirect('/user/'); + return; + } else { + echo "Wrong Email or Password."; + } + } + } + $this->view->loginForm = $loginForm; + } + } + + public function logoutAction() + { + $this->_helper-> viewRenderer-> setNoRender(); + $auth = Zend_Auth::getInstance(); + $auth->clearIdentity(); + $this->_helper->redirector('login', 'auth'); + return; + } + + public function registerAction() + { + if (Zend_Auth::getInstance()->hasIdentity()) { + print_a('Already logged in.'); + } else { + if (!isset($_POST["register"])){ + $registerForm = new user_Form_Register(); + } else { + $registerForm = new user_Form_Register($_POST); + + if ($registerForm->isValid($_POST)) { + + $person = new Application_Model_Person($_POST); + $this->personmapper = new Application_Model_PersonMapper(); + + $date = new DateTime(); + $person->setRegisterdate($date->getTimestamp()); + $person->setPasswordSalt(MD5($date->getTimestamp())); + $person->setPassword(MD5($person->getPassword() . $person->getPasswordSalt())); + print_a($person); + try { + $this->personmapper->save($person); + }catch(Zend_Exception $e) + { + echo "Caught exception: " . get_class($e) . "
"; + echo "Message: " . $e->getMessage() . "
"; + echo "Email Address already existing.."; + return; + } + echo "Successfully registered.
"; + echo "Continue to Login: Login"; + $this->_helper->redirector('login', 'auth'); + return; + } + } + $this->view->registerForm = $registerForm; + } + } + + public function deleteAction() + { + $this->_helper-> viewRenderer-> setNoRender(); + $result = $this->personmapper->findBy('email', Zend_Auth::getInstance()->getIdentity()); + $person = $result[0]; + $personID = $person["personID"]; + if (isset($personID)){ + $this->personmapper = new Application_Model_PersonMapper(); + $person = $this->personmapper->find($personID); + try { + $this->personmapper->delete($person); + }catch(Zend_Exception $e) + { + echo "Caught exception: " . get_class($e) . "
"; + echo "Message: " . $e->getMessage() . "
"; + return; + } + $auth = Zend_Auth::getInstance(); + $auth->clearIdentity(); + $this->_helper->redirector('login', 'auth'); + return; + } + } + + public function recoverpasswordAction() + { + if (!isset($_POST["recoverPassword"])){ + $recoverPasswordForm = new user_Form_RecoverPassword(); + } else { + $recoverPasswordForm = new user_Form_RecoverPassword($_POST); + # Wiederherstellung funktioniert noch nicht!!! + /*if ($recoverPasswordForm->isValid($_POST)) { + $recoverPasswordForm->getView()->url(); + $person = new Application_Model_Person($_POST); + $this->personmapper = new Application_Model_PersonMapper(); + + $result = $this->personmapper->findBy('email', $_POST['email']); + $email = $result[0]['email']; + $name = $result[0]['firstname'] . ' ' . $result[0]['name']; + $url = $this->getRequest()->getScheme() . '://' . $this->getRequest()->getHttpHost() . $this->view->url(); + $recoverid = $this->random(100); + $mailbody = 'Um das Passwort zu ändern klicken Sie auf folgenden Link

Passwort ändern'; + $mail = new Zend_Mail(); + $mail->setBodyHtml($mailbody, 'utf8'); + $mail->getBodyHtml()->getContent(); + $mail->setFrom('admin@local', 'Admin'); + $mail->addTo($email, $name); + $mail->setSubject('Password Wiederherstellung Preboot Server'); + $mail->send(); + } + */ + } + $this->view->recoverPasswordForm = $recoverPasswordForm; + } +} \ No newline at end of file diff --git a/application/modules/user/forms/Login.php b/application/modules/user/forms/Login.php index 58c5cc9..342e0b2 100644 --- a/application/modules/user/forms/Login.php +++ b/application/modules/user/forms/Login.php @@ -5,7 +5,32 @@ class user_Form_Login extends Zend_Form public function init() { - /* Form Elements & Other Definitions Here ... */ + $this->setName("Login"); + $this->setMethod('post'); + + $this->addElement('text', 'email', array( + 'filters' => array('StringTrim', 'StringToLower'), + 'validators' => array( + array('StringLength', false, array(0, 50)), + ), + 'required' => true, + 'label' => 'E-Mail:', + )); + + $this->addElement('password', 'password', array( + 'filters' => array('StringTrim'), + 'validators' => array( + array('StringLength', false, array(0, 50)), + ), + 'required' => true, + 'label' => 'Password:', + )); + + $this->addElement('submit', 'login', array( + 'required' => false, + 'ignore' => true, + 'label' => 'Login', + )); } diff --git a/application/modules/user/forms/RecoverPassword.php b/application/modules/user/forms/RecoverPassword.php new file mode 100644 index 0000000..90feb87 --- /dev/null +++ b/application/modules/user/forms/RecoverPassword.php @@ -0,0 +1,28 @@ +setName("RecoverPassword"); + $this->setMethod('post'); + + $this->addElement('text', 'email', array( + 'filters' => array('StringTrim'), + 'validators' => array( + array('StringLength', false, array(0, 30)), + ), + 'required' => true, + 'label' => 'Email:', + )); + $this->addElement('submit', 'recoverPassword', array( + 'required' => false, + 'ignore' => true, + 'label' => 'Recover Password', + )); + } + + +} + diff --git a/application/modules/user/forms/Register.php b/application/modules/user/forms/Register.php new file mode 100644 index 0000000..9c2a42d --- /dev/null +++ b/application/modules/user/forms/Register.php @@ -0,0 +1,105 @@ +setName("Register"); + $this->setMethod('post'); + + $this->addElement('text', 'title', array( + 'filters' => array('StringTrim'), + 'validators' => array( + array('StringLength', false, array(0, 50)), + ), + 'required' => true, + 'label' => 'Title:', + )); + + + $this->addElement('text', 'name', array( + 'filters' => array('StringTrim'), + 'validators' => array( + array('StringLength', false, array(0, 50)), + ), + 'required' => true, + 'label' => 'Name:', + )); + + $this->addElement('text', 'firstname', array( + 'filters' => array('StringTrim'), + 'validators' => array( + array('StringLength', false, array(0, 50)), + ), + 'required' => true, + 'label' => 'Firstname:', + )); + + $this->addElement('text', 'street', array( + 'filters' => array('StringTrim'), + 'validators' => array( + array('StringLength', false, array(0, 50)), + ), + 'required' => true, + 'label' => 'Street:', + )); + + $this->addElement('text', 'housenumber', array( + 'filters' => array('StringTrim'), + 'validators' => array( + array('StringLength', false, array(0, 50)), + ), + 'required' => true, + 'label' => 'Housenumber:', + )); + + $this->addElement('text', 'city', array( + 'filters' => array('StringTrim'), + 'validators' => array( + array('StringLength', false, array(0, 50)), + ), + 'required' => true, + 'label' => 'City:', + )); + + $this->addElement('text', 'postalcode', array( + 'filters' => array('StringTrim'), + 'validators' => array( + array('StringLength', false, array(0, 50)), + ), + 'required' => true, + 'label' => 'Postalcode:', + )); + + $this->addElement('text', 'email', array( + 'filters' => array('StringTrim', 'StringToLower'), + 'validators' => array( + array('StringLength', false, array(0, 50)), + ), + 'required' => true, + 'label' => 'Email:', + )); + + $this->addElement('password', 'password', array( + 'filters' => array('StringTrim'), + 'validators' => array( + array('StringLength', false, array(0, 50)), + ), + 'required' => true, + 'label' => 'Password:', + )); + + $this->addElement('submit', 'register', array( + 'required' => false, + 'ignore' => true, + 'label' => 'Register', + )); + + + } + + + +} + diff --git a/application/modules/user/views/scripts/auth/login.phtml b/application/modules/user/views/scripts/auth/login.phtml new file mode 100644 index 0000000..d68d2af --- /dev/null +++ b/application/modules/user/views/scripts/auth/login.phtml @@ -0,0 +1,5 @@ +loginForm->setAction($this->url()); +echo $this->loginForm; +?> +
\ No newline at end of file diff --git a/application/modules/user/views/scripts/auth/recoverpassword.phtml b/application/modules/user/views/scripts/auth/recoverpassword.phtml new file mode 100644 index 0000000..881e00e --- /dev/null +++ b/application/modules/user/views/scripts/auth/recoverpassword.phtml @@ -0,0 +1,4 @@ +recoverPasswordForm->setAction($this->url()); +echo $this->recoverPasswordForm; +?> diff --git a/application/modules/user/views/scripts/auth/register.phtml b/application/modules/user/views/scripts/auth/register.phtml new file mode 100644 index 0000000..2033b04 --- /dev/null +++ b/application/modules/user/views/scripts/auth/register.phtml @@ -0,0 +1,4 @@ +registerForm->setAction($this->url()); +echo $this->registerForm; +?> \ No newline at end of file -- cgit v1.2.3-55-g7522