53 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			53 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
require_once("BancaDati/BancaDati.php");
 | 
						|
use BancaDati\BancaDati;
 | 
						|
class User {
 | 
						|
    public string $id;
 | 
						|
    public string $username;
 | 
						|
    public string $email;
 | 
						|
    public string $token;
 | 
						|
    private string $password;
 | 
						|
    private BancaDati $db;
 | 
						|
 | 
						|
    public function __construct() {
 | 
						|
        $this->db = new BancaDati();
 | 
						|
        return $this;
 | 
						|
    }
 | 
						|
 | 
						|
    public function exists() {
 | 
						|
        return true;
 | 
						|
    }
 | 
						|
    public function loginWithUsername(string $username, string $password) : string {
 | 
						|
        $userObject = $this->db->select("utente", ["nomeUtente" => $username]);
 | 
						|
 | 
						|
        if(!$userObject){
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
        $this->id = $userObject["id"];
 | 
						|
        $this->username = $userObject["nomeUtente"];
 | 
						|
        $this->email = $userObject["email"];
 | 
						|
        $this->password = $userObject["parolaDordine"];
 | 
						|
        $this->token = $this->db->createUUID();
 | 
						|
 | 
						|
        if($this->password != $password){
 | 
						|
            return false; // ungültiges password
 | 
						|
        }
 | 
						|
 | 
						|
        $this->db->update("utente", $this->id, ["gettone" => $this->token]);
 | 
						|
 | 
						|
        return $this->token;
 | 
						|
    }
 | 
						|
    public function loginWithToken(string $token){
 | 
						|
        $userObject = $this->db->select("utente", ["gettone" => $token]);
 | 
						|
        if(!$userObject){
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
        $this->id = $userObject["id"];
 | 
						|
        $this->username = $userObject["nomeUtente"];
 | 
						|
        $this->email = $userObject["email"];
 | 
						|
        $this->password = $userObject["parolaDordine"];
 | 
						|
        $this->token = $token;
 | 
						|
 | 
						|
        return $this;
 | 
						|
    }
 | 
						|
} |