Compare commits

4 Commits

Author SHA1 Message Date
c23b77ff0d add: DB primary key update 2023-01-12 14:49:58 +01:00
03a42f3ba7 add: DB primary key update 2023-01-12 14:46:46 +01:00
1d80d93b1d add: DB auto_increment 2023-01-12 14:43:28 +01:00
151b1ab549 phpdoc 2023-01-12 14:05:19 +01:00
4 changed files with 11 additions and 106 deletions

View File

@@ -6,7 +6,7 @@ time_zone = "+00:00";
CREATE TABLE `ingredienti`
( /*Zutaten*/
`id` varchar(36) NOT NULL,
`id` int auto_increment NOT NULL PRIMARY KEY,
`cognome` varchar(200) NOT NULL, /*Name*/
`caloriePerCento` integer(5) NOT NULL, /*Kalorien pro Gramm*/
`ilPeso` integer(5) NULL, /*Gewicht*/
@@ -16,14 +16,14 @@ CREATE TABLE `ingredienti`
CREATE TABLE `folla`
( /*Menge*/
`id` varchar(36) NOT NULL,
`id` int auto_increment NOT NULL PRIMARY KEY,
`unita` varchar(200) NOT NULL, /*Einheit*/
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `elenco`
( /*Liste*/
`id` varchar(36) NOT NULL,
`id` int auto_increment NOT NULL PRIMARY KEY,
`creatore` varchar(200) NOT NULL, /*Ersteller*/
`coloreDiSfondo` integer(10) NOT NULL, /*Hintergrundfarbe*/
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
@@ -31,17 +31,17 @@ CREATE TABLE `elenco`
CREATE TABLE `utente`
( /*Benutzer*/
`id` varchar(36) NOT NULL,
`email` varchar(200) NOT NULL, /*Email*/
`parolaDordine` varchar(255) NOT NULL, /*Passwort*/
`id` int auto_increment NOT NULL PRIMARY KEY,
`email` varchar(200) NOT NULL, /*Email*/
`parolaDordine` varchar(255) NOT NULL, /*Passwort*/
`nomeUtente` varchar(50) UNIQUE NOT NULL, /*Benutzernamen*/
`gettone` varchar(255), /*Token für Session*/
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `elencoIngredienti`
( /*Liste_Zutaten*/
`id` varchar(36) NOT NULL,
`id` int auto_increment NOT NULL PRIMARY KEY,
`ingredientiID` varchar(36) NOT NULL, /*ZutatenID*/
`elencoID` varchar(36) NOT NULL, /*ListeID*/
`follaID` varchar(36) NOT NULL, /*MengeID*/
@@ -50,24 +50,12 @@ CREATE TABLE `elencoIngredienti`
CREATE TABLE `utenteElenco`
( /*Benutzer_Liste*/
`id` varchar(36) NOT NULL,
`id` int auto_increment NOT NULL PRIMARY KEY,
`elencoID` varchar(36) NOT NULL, /*ListeID*/
`utenteID` varchar(36) NOT NULL, /*BenutzerID*/
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `ingredienti` /*Zutaten*/
ADD PRIMARY KEY (`id`);
ALTER TABLE `folla` /*Menge*/
ADD PRIMARY KEY (`id`);
ALTER TABLE `elenco` /*Liste*/
ADD PRIMARY KEY (`id`);
ALTER TABLE `utente` /*Benutzer*/
ADD PRIMARY KEY (`id`);
ALTER TABLE `elencoIngredienti` /*Liste_Zutaten*/
ADD PRIMARY KEY (`id`),
ADD CONSTRAINT `FK_ElencoIngredienti_Ingredienti` FOREIGN KEY (`ingredientiID`) REFERENCES `ingredienti`(`id`), /*Liste_Zutaten hat Foreignkey von Zutaten(id)*/

View File

@@ -30,7 +30,7 @@ class Router
$this->request = $_SERVER;
}
if (isset($_POST)) {
$this->request["body"] = json_decode(file_get_contents('php://input'), true);
$this->request["body"] = $_POST;
}
if (isset($_POST)) {
$this->request["params"] = $_GET;

View File

@@ -1,53 +0,0 @@
<?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;
}
}

View File

@@ -3,7 +3,6 @@
require_once("Router/Router.php");
require_once("Router/Response.php");
require_once("BancaDati/BancaDati.php");
require_once("User.php");
use Router\Response;
use Router\Router;
@@ -12,45 +11,16 @@ use BancaDati\BancaDati;
$app = new Router("/DirektiveDesDons");
$db = new BancaDati();
$app->use("/", function (array &$req, Response $res) {
if(isset($_COOKIE["TOKEN"])){
$user = new User();
if($user->loginWithToken($_COOKIE["TOKEN"])){
$req["user"] = $user;
}
}
});
$app->get("/", function (array $req, Response $res) {
$res->send("Hello World");
//var_dump($req["user"]);
});
$app->get("/user", function (array $req, Response $res) {
$res->send("user");
});
$app->get("/user/:id", function (array $req, Response $res) use ($db) {
$db->select("utente", ["username" => $req["id"]]);
$app->get("/user/:id", function (array $req, Response $res) {
$res->send("user " . $req["params"]["id"]);
});
$app->post("/createuser", function (array $req, Response $res) use ($db) {
$db->insert("utente", ["email" => "test@email.com", "parolaDordine" => "password", "nomeUtente" => "testuser"]);
$res->send("user ");
});
$app->post("/login", function( array $req, Response $res) use ($db) {
$username = $req["body"]["username"];
$password = $req["body"]["password"];
$user = new User();
$usertoken = $user->loginWithUsername($username, $password);
if($usertoken){
setcookie("TOKEN", $usertoken, time()+3600); // 1h
$res->send("Login successful" . "token: " . $usertoken, 200);
}else{
$res->send("Login failed", 403);
}
});
$app->start();