summaryrefslogtreecommitdiffstats
path: root/application/controllers/AuthController.php
blob: 51232f0bedc9e38039af3b005952b0a1ccd9fc59 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?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('memberships');
							$count = 0;
							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->$count = $membership;
								$count++;
							}
							
							$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;
	}
}