summaryrefslogblamecommitdiffstats
path: root/application/controllers/AuthController.php
blob: c5cb46f6a83558ec3e6dc39047fb9c93a0257f06 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16















                                                                           

                            


                              


                                                                





                                                                           













                                                                                








                                                                                                                                                                                                            








                                                                                                                                  

                                                                                                                   


                                                                                                               

                                                                                                     







                                                                                                                    

                                                                                                          

                                                                                                                 





                                                                                                                                                          













                                                                                                                            


                                                                                                                                                            
                                                 












                                                              
                                                     














                                                                                                                        


                                                                                                            



















































                                                                                                                                                                                                                          



                                                                                                  


                                                                                                                 
                                                                     
















                                                                                                       
<?php
/*
 * Copyright (c) 2011 - OpenSLX GmbH, RZ Uni Freiburg
 * This program is free software distributed under the GPL version 2.
 * See http://gpl.openslx.org/
 *
 * If you have any feedback please consult http://feedback.openslx.org/ and
 * send your suggestions, praise, or complaints to feedback@openslx.org
 *
 * General information about OpenSLX can be found at http://openslx.org/
 */

class AuthController extends Zend_Controller_Action
{
	protected $personmapper = null;
	private $db = null;
	protected $config;
	protected $pbs2host;

	public function init()
	{
		$bootstrap = $this->getInvokeArg('bootstrap');
		$this->config = $bootstrap->getOptions();
		$this->pbs2host = $this->config['pbs2']['host'];
		$this->db = Zend_Db_Table::getDefaultAdapter();
		$this->personmapper = new Application_Model_PersonMapper();
	}

	public function indexAction()
	{
		$this->_helper->redirector('login', 'auth');
	}

	public function loginAction()
	{
		if (Zend_Auth::getInstance()->hasIdentity()) {
			$this->_redirect('/');
		} else {
			if (!isset($_POST["login"])){
				$loginForm = new Application_Form_Login();
			} else {
				$loginForm = new Application_Form_Login($_POST);

				if ($loginForm->isValid($_POST)) {
					$loginquery = "email=" . $loginForm->getValue('email') . "&password=" . $loginForm->getValue('password');
					$loginApiResult = PostToHost($this->pbs2host, $this->config['pbs2']['login'], 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], 'poolctrl', $loginquery);
					$loginXMLString = $loginApiResult['http-body'];
					if(strlen($loginXMLString) > 0) {
						$loginXML = new SimpleXMLElement($loginXMLString);
						$login = $loginXML->login;
						$success = sprintf("%s", $login->success);
						if ($success === "true") {
							$personid = sprintf("%s", $login->personid);
							$membershipSession = new Zend_Session_Namespace('user');
							foreach($login->membershiplist->membership as $membershipXML)
							{
								$membership['membershipID'] = sprintf("%s", $membershipXML->id);
								$membership['personID'] = sprintf("%s", $membershipXML->personid);
								$membership['groupID'] = sprintf("%s", $membershipXML->groupid);
								$membership['apikey'] = sprintf("%s", $membershipXML->apikey);
								$membershipSession->memberships[] = $membership;
							}
							$this->personmapper = new Application_Model_PersonMapper();
							$person = $this->personmapper->find($personid);
							$person->setEmail($loginForm->getValue('email'));
							$person->setPassword($loginForm->getValue('password'));
							$person->setSuspend(0);
							$date = new DateTime();
							$person->setLogindate($date->getTimestamp());
							try {
								$this->personmapper->save($person);
							} catch(Zend_Exception $e)
							{
								echo "Caught exception: " . get_class($e) . "<br/>";
								echo "Message: " . $e->getMessage() . "<br/>";
								return;
							}
							$authSession = new Zend_Session_Namespace('auth');
							$authSession->storage = $person->getEmail();
							$this->_helper->redirector('selectmembership', 'person');
							return;
						} else {
							$error = sprintf("%s", $login->error);
							if($error == "wrong email or password") {
								$poolctrlNotifier = new Poolctrl_Notifier();
								$this->view->notification = $poolctrlNotifier->notify('Wrong Email or Password', 'error');
							} else if($error == "person suspended") {
								$personid = sprintf("%s", $login->personid);
								$this->personmapper = new Application_Model_PersonMapper();
								$person = $this->personmapper->find($personid);
								$person->setEmail($loginForm->getValue('email'));
								$person->setPassword($loginForm->getValue('password'));
								$person->setSuspend(1);
								try {
									$this->personmapper->save($person);
								} catch(Zend_Exception $e)
								{
									echo "Caught exception: " . get_class($e) . "<br/>";
									echo "Message: " . $e->getMessage() . "<br/>";
									return;
								}
								$poolctrlNotifier = new Poolctrl_Notifier();
								$this->view->notification = $poolctrlNotifier->notify('Your Account is suspended', 'error');
							}
						}
					}
				}
			}
			$this->view->loginForm = $loginForm;
		}
	}

	public function logoutAction()
	{
		$this->_helper-> viewRenderer-> setNoRender();
		$auth = Zend_Auth::getInstance();
		$auth->clearIdentity();
		Zend_Session::namespaceUnset('userIDs');
		Zend_Session::namespaceUnset('user');
		Zend_Session::forgetMe();
		$this->_helper->redirector('login', 'auth');
		return;
	}

	public function recoverpasswordAction()
	{
		if (isset($_POST["savePassword"])){
			$personID = $_POST['personID'];
			$recoverPasswordForm = new Application_Form_NewPassword(array("personID" => $personID, $_POST));
			if ($recoverPasswordForm->isValid($_POST)) {
				$this->personmapper = new Application_Model_PersonMapper();
				$person = $this->personmapper->find($personID);
				$date = new DateTime();
				$person->setPassword($_POST['password'])
				->setPasswordSalt(MD5($date->getTimestamp()))
				->setLoginPassword(crypt($person->getPassword(), '$6$'.randomString(8).'$'))
				->setPassword(MD5($person->getPassword() . $person->getPasswordSalt()));
				try {
					$this->personmapper->save($person);
				} catch(Zend_Exception $e)
				{
					echo "Caught exception: " . get_class($e) . "<br/>";
					echo "Message: " . $e->getMessage() . "<br/>";
					echo "Email Address already existing.";
					return;
				}
				$this->_helper->redirector('login', 'auth');
				return;
			}
		} else if(isset($_GET['recoveryid'])) {
			$recoveryid = $_GET['recoveryid'];
			$passwordRecoveryMapper = new Application_Model_PasswordRecoveryMapper();
			$passwordRecovery = $passwordRecoveryMapper->findBy(array("recoveryID" => $recoveryid),true);
			if(count($passwordRecovery) > 0) {
				$passwordRecoveryObject = new Application_Model_PasswordRecovery();
				$passwordRecoveryObject->setID($passwordRecovery[0]['personID']);
				$passwordRecoveryObject->setRecoveryID($passwordRecovery[0]['recoveryID']);
				$personID = $passwordRecoveryObject->getID();
				$recoverPasswordForm = new Application_Form_NewPassword(array("personID" => $personID));
				try {
					$passwordRecoveryMapper->delete($passwordRecoveryObject);
				} catch(Zend_Exception $e)
				{
					echo "Caught exception: " . get_class($e) . "<br/>";
					echo "Message: " . $e->getMessage() . "<br/>";
					return;
				}
			} else {
				$this->_helper->redirector('login', 'auth');
				return;
			}
		} else {
			if (!isset($_POST["recoverPassword"])){
				$recoverPasswordForm = new Application_Form_RecoverPassword();
			} else {
				$recoverPasswordForm = new Application_Form_RecoverPassword($_POST);
				if ($recoverPasswordForm->isValid($_POST)) {
					$recoverPasswordForm->getView()->url();
					$this->personmapper = new Application_Model_PersonMapper();
					$result = $this->personmapper->findBy(array('email' => $_POST['email']),true);
					$person = new Application_Model_Person($result[0]);
					$person->setID($result[0]['personID']);
					$email = $person->getEmail();
					$name = $person->getFirstname() . ' ' . $person->getName();
					$url = $this->getRequest()->getScheme() . '://' . $this->getRequest()->getHttpHost() . $this->view->url();
					$recoveryid = randomString(100);
					$mailbody = 'Um das Passwort zu ändern klicken Sie auf folgenden Link<br /><br /><a href="'. $url . '/auth/recoverpassword/?recoveryid='. $recoveryid . '">Passwort ändern</a>';
					$mail = new Zend_Mail();
					$mail->setBodyHtml($mailbody, 'utf8')
					->getBodyHtml()->getContent()
					->setFrom('admin@local', 'Admin')
					->addTo($email, $name)
					->setSubject('Password Wiederherstellung Preboot Server');
					$passwordRecoveryMapper = new Application_Model_PasswordRecoveryMapper();
					$passwordRecoveryObject = new Application_Model_PasswordRecovery();
					$passwordRecoveryObject->setID($person->getID())
					->setRecoveryID($recoveryid);
					try {
						$passwordRecoveryMapper->save($passwordRecoveryObject);
						$mail->send();
					}catch(Zend_Exception $e)
					{
						echo "Caught exception: " . get_class($e) . "<br/>";
						echo "Message: " . $e->getMessage() . "<br/>";
						return;
					}
					$this->_helper->redirector('login', 'auth');
					return;
				}
			}
		}
		$this->view->recoverPasswordForm = $recoverPasswordForm;
	}
}