62 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			62 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| // TODO: this shit stinks...
 | |
| class User 
 | |
| {
 | |
|     private Session $session;
 | |
| 
 | |
|     public ?bool    $loggedIn;
 | |
|     public ?string  $username;
 | |
|     public ?string  $password;
 | |
| 
 | |
|     public function __construct(Session $session)
 | |
|     {
 | |
|         $this->session = $session;
 | |
|         $this->setProps();
 | |
|     }
 | |
| 
 | |
|     private function setProps(): void
 | |
|     {
 | |
|         $this->loggedIn = $this->session->get('loggedIn');
 | |
|         $this->username = $this->session->get('username');
 | |
|         $this->password = $this->session->get('password');
 | |
| 
 | |
|         if ($this->loggedIn && !$this->authenticate($this->username, $this->password))
 | |
|         {
 | |
|             $this->logout();
 | |
|             $this->session->flash('Kontodetaljer endret, vennligst logg inn igjen', 'warning');
 | |
|         }
 | |
|    }
 | |
| 
 | |
|     // 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->setProps();
 | |
|             return TRUE;
 | |
|         }
 | |
|         return FALSE;
 | |
|     }
 | |
| 
 | |
|     // Check if user and pass match
 | |
|     private function authenticate(string $username, string $password): bool
 | |
|     {
 | |
|         if ($username === 'William' && $password === 'William')
 | |
|         {
 | |
|             return TRUE;
 | |
|         }
 | |
|         return FALSE;
 | |
|     }
 | |
| 
 | |
|     public function logout(): void
 | |
|     {
 | |
|         $this->session->remove('loggedIn');
 | |
|         $this->session->remove('username');
 | |
|         $this->session->remove('password');
 | |
|         $this->setProps();
 | |
|     }
 | |
| } |