summaryrefslogtreecommitdiffstats
path: root/application/controllers/AuthController.php
blob: 097417b60f5973bf0e232eb12764f177d3ec7159 (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
<?php

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

	public function init()
	{
		$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()) {
			print_a('Already logged in.');
		} else {
			if (!isset($_POST["login"])){
				$loginForm = new Application_Form_AuthLogin();
			} else {
				$loginForm = new Application_Form_AuthLogin($_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('/');
        		            	return;
        		            } else {
        		            	echo "Wrong Email or Password.";
        		            }
				}
			}

			$this->view->loginForm = $loginForm;
		}
	}

	public function registerAction()
	{
		if (Zend_Auth::getInstance()->hasIdentity()) {
			print_a('Already logged in.');
		} else {
			if (!isset($_POST["register"])){
				$registerForm = new Application_Form_AuthRegister();
			} else {
				$registerForm = new Application_Form_AuthRegister($_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) . "<br/>";
						echo "Message: " . $e->getMessage() . "<br/>";
						echo "Email Address already existing..";
						return;
					}
					echo "Successfully registered. <br/>";
					echo "Continue to Login: <a href=\""."/auth/login"."\">Login</a>";
					$this->_helper->redirector('login', 'auth');
					return;
				}
			}

			$this->view->registerForm = $registerForm;
		}
	}

	public function logoutAction()
	{
		$auth = Zend_Auth::getInstance();
		$auth->clearIdentity();
		$this->_helper->redirector('login', 'auth');
	}

	public function recoverpasswordAction()
	{
		if (!isset($_POST["recoverPassword"])){
			$recoverPasswordForm = new Application_Form_AuthRecoverPassword();
		} else {
			$recoverPasswordForm = new Application_Form_AuthRecoverPassword($_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<br /><br /><a href="'. $url . '/auth/recoverpassword/?recoverid='. $recoverid . '">Passwort ändern</a>';		
			$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;
	}

	public function deleteAction()
	{
		$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) . "<br/>";
				echo "Message: " . $e->getMessage() . "<br/>";
				return;
			}
			$auth = Zend_Auth::getInstance();
			$auth->clearIdentity();
			$this->_helper->redirector('login', 'auth');
		}
	}
}