DirektiveDesDons/User.php

53 lines
1.5 KiB
PHP
Raw Permalink Normal View History

2023-01-11 14:41:09 +01:00
<?php
require_once("BancaDati/BancaDati.php");
use BancaDati\BancaDati;
class User {
2023-01-12 15:04:15 +01:00
public string $id;
2023-01-11 14:41:09 +01:00
public string $username;
public string $email;
public string $token;
private string $password;
private BancaDati $db;
2023-01-12 14:44:52 +01:00
public function __construct() {
2023-01-12 15:04:15 +01:00
$this->db = new BancaDati();
2023-01-11 14:41:09 +01:00
return $this;
}
public function exists() {
return true;
}
2023-01-12 14:44:52 +01:00
public function loginWithUsername(string $username, string $password) : string {
2023-01-19 18:37:48 +01:00
$userObject = $this->db->select("utente", ["nomeUtente" => $username])[0];
2023-01-12 15:04:15 +01:00
2023-01-12 14:44:52 +01:00
if(!$userObject){
return false;
}
2023-01-12 15:04:15 +01:00
$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;
2023-01-11 14:41:09 +01:00
}
2023-01-12 14:44:52 +01:00
public function loginWithToken(string $token){
2023-01-19 18:37:48 +01:00
$userObject = $this->db->select("utente", ["gettone" => $token])[0];
2023-01-12 15:22:32 +01:00
if(!$userObject){
return false;
}
$this->id = $userObject["id"];
$this->username = $userObject["nomeUtente"];
$this->email = $userObject["email"];
$this->password = $userObject["parolaDordine"];
$this->token = $token;
2023-01-12 14:44:52 +01:00
2023-01-12 15:22:32 +01:00
return $this;
2023-01-12 14:44:52 +01:00
}
2023-01-11 14:41:09 +01:00
}