66 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			66 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| // TODO: this stinks...
 | |
| class User 
 | |
| {
 | |
|     private Session $session;
 | |
| 
 | |
|     public bool    $loggedIn;
 | |
|     public string  $username;
 | |
|     public string  $password;
 | |
| 
 | |
|     public function __construct(Session $session)
 | |
|     {
 | |
|         $this->session = $session;
 | |
|         $this->setProperties();
 | |
|     }
 | |
| 
 | |
|     private function setProperties(): void
 | |
|     {
 | |
|         if ($this->session->get('loggedIn'))
 | |
|         {
 | |
|             $this->loggedIn = $this->session->get('loggedIn');
 | |
|             $this->username = $this->session->get('username');
 | |
|             $this->password = $this->session->get('password');
 | |
| 
 | |
|             if (!$this->authenticate($this->username, $this->password)) {
 | |
|                 $this->logout();
 | |
|                 $this->session->flash('Kontodetaljer endret, vennligst logg inn igjen', 'warning');
 | |
|             }
 | |
|         } else {
 | |
|             $this->loggedIn = FALSE;
 | |
|         }
 | |
|    }
 | |
| 
 | |
|     // Set session if user and password match
 | |
|     public function login(string $username, string $password): bool
 | |
|     {
 | |
|         if ($this->authenticate($username, $password))
 | |
|         {
 | |
|             $this->session->set('loggedIn', TRUE);
 | |
|             $this->session->set('username', $username);
 | |
|             $this->session->set('password', $password);
 | |
|             $this->setProperties();
 | |
|             return TRUE;
 | |
|         }
 | |
|         return FALSE;
 | |
|     }
 | |
| 
 | |
|     // Check if user and pass match
 | |
|     private function authenticate(string $username, string $password): bool
 | |
|     {
 | |
|         if ($username === 'Willaiam' && $password === 'William')
 | |
|         {
 | |
|             return TRUE;
 | |
|         }
 | |
|         return FALSE;
 | |
|     }
 | |
| 
 | |
|     public function logout(): void
 | |
|     {
 | |
|         $this->session->set('loggedIn', FALSE);
 | |
|         $this->session->remove('username', FALSE);
 | |
|         $this->session->remove('password', FALSE);
 | |
|         $this->setProperties();
 | |
|     }
 | |
| } |