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

class AuthController extends Zend_Controller_Action
{

    public function init()
    {
    	$db = Zend_Db_Table::getDefaultAdapter();
    	
    }
	
    public function loginAction()
    {             
 		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(
		            $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->_redirect('/');
		            return;
		        } else {
					echo "Falsche Email oder Passwort";
				} 
		    }
		}

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

    public function registerAction()
    {                        
     		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);
    		        $personmapper = new Application_Model_PersonMapper();
    		        
    		        $date = new DateTime();
    		        $person->setRegisterdate($date->getTimestamp());
    		        $person->setPasswordSalt(MD5($date->getTimestamp()));
    		        $person->setPassword(MD5($person->getPassword() . $person->getPasswordSalt()));
    		        
    		        try {    		 
    		            $personmapper->save($person);
    		        }catch(Zend_Exception $e)
    		        {
    		            echo "Caught exception: " . get_class($e) . "<br/>";
    					echo "Message: " . $e->getMessage() . "<br/>";
    					echo "Email Adresse bereits vorhanden.";
    					return;
    		        }
    		        echo "Erfolgreich registriert. <br/>";
    		        echo "Weiter zum Login: <a href=\""."/auth/login"."\">Login</a>";
    		        return;
    		     }
    		}
    
            $this->view->registerForm = $registerForm;
    }

    public function logoutAction()
    {
        // action body
    }

    public function recoverPasswordAction()
    {
        // action body
    }

    public function deleteAction()
    {
        if (!isset($_POST["delete"])){
    	   	$deleteForm = new Application_Form_AuthDelete();
    	} else {
    		    $deleteForm = new Application_Form_AuthDelete($_POST);
    	 		
    		    if ($deleteForm->isValid($_POST)) {
    		    	
    		        $person = new Application_Model_Person($_POST);
    		        $personmapper = new Application_Model_PersonMapper();
    		        
    		        
      		        $personmapper->delete($person);
    		    }
    	}
    
       	$this->view->deleteForm = $deleteForm;
    		       
    }


}