Compare commits
25 Commits
b8562a50e0
...
feat/login
| Author | SHA1 | Date | |
|---|---|---|---|
| fbfeeb7679 | |||
|
|
2860a01cf8 | ||
|
|
a887921e5d | ||
|
|
a2178733da | ||
|
|
5fea5ad15f | ||
|
|
fe5efbe8f1 | ||
|
|
f25b2a72f7 | ||
|
|
7b5672d36b | ||
|
|
21ebecac54 | ||
|
|
acfae23029 | ||
|
|
e1a0f112a2 | ||
|
|
23cf1b54e4 | ||
|
|
95a2c3e3fe | ||
|
|
148f95087d | ||
|
|
81cdae85b0 | ||
| e31b7b6319 | |||
| 84bf6b2d36 | |||
| 143874af78 | |||
| b0ef6fc88d | |||
| 85f49f1c8c | |||
| aa90447465 | |||
| d4e275e28c | |||
| 6dffa09e88 | |||
| a568d36eb2 | |||
| d8154b9d0f |
@@ -1,71 +1,80 @@
|
|||||||
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
|
SET
|
||||||
SET time_zone = "+00:00";
|
SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
|
||||||
|
SET
|
||||||
|
time_zone = "+00:00";
|
||||||
|
|
||||||
|
|
||||||
CREATE TABLE `ingredienti` ( /*Zutaten*/
|
CREATE TABLE `ingredienti`
|
||||||
|
( /*Zutaten*/
|
||||||
`id` varchar(36) NOT NULL,
|
`id` varchar(36) NOT NULL,
|
||||||
`cognome` varchar(200) NOT NULL, /*Name*/
|
`cognome` varchar(200) NOT NULL, /*Name*/
|
||||||
`caloriePerCento` int(5) NOT NULL, /*Kalorien pro Gramm*/
|
`caloriePerCento` integer(5) NOT NULL, /*Kalorien pro Gramm*/
|
||||||
`ilPeso` int(5) NULL, /*Gewicht*/
|
`ilPeso` integer(5) NULL, /*Gewicht*/
|
||||||
`prezzo` decimal(4,2) NOT NULL, /*Preis*/
|
`prezzo` decimal(4, 2) NOT NULL, /*Preis*/
|
||||||
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
|
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||||
|
|
||||||
CREATE TABLE `folla` ( /*Menge*/
|
CREATE TABLE `folla`
|
||||||
`id` varchar(36) NOT NULL,
|
( /*Menge*/
|
||||||
`unita` varchar(200) NOT NULL, /*Einheit*/
|
`id` varchar(36) NOT NULL,
|
||||||
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
|
`unita` varchar(200) NOT NULL, /*Einheit*/
|
||||||
|
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||||
|
|
||||||
CREATE TABLE `elenco` ( /*Liste*/
|
CREATE TABLE `elenco`
|
||||||
`id` varchar(36) NOT NULL,
|
( /*Liste*/
|
||||||
`creatore` varchar(200) NOT NULL, /*Ersteller*/
|
`id` varchar(36) NOT NULL,
|
||||||
`coloreDiSfondo` integer(10) NOT NULL, /*Hintergrundfarbe*/
|
`creatore` varchar(200) NOT NULL, /*Ersteller*/
|
||||||
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
|
`coloreDiSfondo` integer(10) NOT NULL, /*Hintergrundfarbe*/
|
||||||
|
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||||
|
|
||||||
CREATE TABLE `utente` ( /*Benutzer*/
|
CREATE TABLE `utente`
|
||||||
`id` varchar(36) NOT NULL,
|
( /*Benutzer*/
|
||||||
`email` varchar(200) NOT NULL, /*Email*/
|
`id` varchar(36) NOT NULL,
|
||||||
`parolaDordine` integer(10) NOT NULL, /*Passwort*/
|
`email` varchar(200) NOT NULL, /*Email*/
|
||||||
`nomeUtente` integer(10) NOT NULL, /*Benutzernamen*/
|
`parolaDordine` varchar(255) NOT NULL, /*Passwort*/
|
||||||
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
|
`nomeUtente` varchar(50) UNIQUE NOT NULL, /*Benutzernamen*/
|
||||||
|
`gettone` varchar(255), /*Token für Session*/
|
||||||
|
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||||
|
|
||||||
CREATE TABLE `elencoIngredienti` ( /*Liste_Zutaten*/
|
CREATE TABLE `elencoIngredienti`
|
||||||
`id` varchar(36) NOT NULL,
|
( /*Liste_Zutaten*/
|
||||||
`ingredientiID` varchar(36) NOT NULL, /*ZutatenID*/
|
`id` varchar(36) NOT NULL,
|
||||||
`elencoID` varchar(36) NOT NULL, /*ListeID*/
|
`ingredientiID` varchar(36) NOT NULL, /*ZutatenID*/
|
||||||
`follaID` varchar(36) NOT NULL, /*MengeID*/
|
`elencoID` varchar(36) NOT NULL, /*ListeID*/
|
||||||
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
|
`follaID` varchar(36) NOT NULL, /*MengeID*/
|
||||||
|
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||||
|
|
||||||
CREATE TABLE `utenteElenco` ( /*Benutzer_Liste*/
|
CREATE TABLE `utenteElenco`
|
||||||
`id` varchar(36) NOT NULL,
|
( /*Benutzer_Liste*/
|
||||||
`elencoID` varchar(36) NOT NULL, /*ListeID*/
|
`id` varchar(36) NOT NULL,
|
||||||
`utenteID` varchar(36) NOT NULL, /*BenutzerID*/
|
`elencoID` varchar(36) NOT NULL, /*ListeID*/
|
||||||
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
|
`utenteID` varchar(36) NOT NULL, /*BenutzerID*/
|
||||||
|
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||||
|
|
||||||
ALTER TABLE `ingredienti`
|
ALTER TABLE `ingredienti` /*Zutaten*/
|
||||||
ADD PRIMARY KEY (`id`);
|
ADD PRIMARY KEY (`id`);
|
||||||
|
|
||||||
ALTER TABLE `folla`
|
ALTER TABLE `folla` /*Menge*/
|
||||||
ADD PRIMARY KEY (`id`);
|
ADD PRIMARY KEY (`id`);
|
||||||
|
|
||||||
ALTER TABLE `elenco`
|
ALTER TABLE `elenco` /*Liste*/
|
||||||
ADD PRIMARY KEY (`id`);
|
ADD PRIMARY KEY (`id`);
|
||||||
|
|
||||||
ALTER TABLE `utente`
|
ALTER TABLE `utente` /*Benutzer*/
|
||||||
ADD PRIMARY KEY (`id`);
|
ADD PRIMARY KEY (`id`);
|
||||||
|
|
||||||
ALTER TABLE `elencoIngredienti`
|
ALTER TABLE `elencoIngredienti` /*Liste_Zutaten*/
|
||||||
ADD PRIMARY KEY (`id`),
|
ADD PRIMARY KEY (`id`),
|
||||||
ADD CONSTRAINT `FK_ElencoIngredienti_Ingredienti` FOREIGN KEY (`ingredientiID`) REFERENCES `ingredienti`(`id`), /*Liste_Zutaten hat Foreignkey von Zutaten(id)*/
|
ADD CONSTRAINT `FK_ElencoIngredienti_Ingredienti` FOREIGN KEY (`ingredientiID`) REFERENCES `ingredienti`(`id`), /*Liste_Zutaten hat Foreignkey von Zutaten(id)*/
|
||||||
ADD CONSTRAINT `FK_ElencoIngredienti_Elenco` FOREIGN KEY (`elencoID`) REFERENCES `elenco`(`id`), /*Liste_Zutaten hat Foreignkey von Liste(id)*/
|
ADD CONSTRAINT `FK_ElencoIngredienti_Elenco` FOREIGN KEY (`elencoID`) REFERENCES `elenco`(`id`), /*Liste_Zutaten hat Foreignkey von Liste(id)*/
|
||||||
ADD CONSTRAINT `FK_ElencoIngredienti_Folla` FOREIGN KEY (`follaID`) REFERENCES `folla`(`id`); /*Liste_Zutaten hat Foreignkey von Menge(id)*/
|
ADD CONSTRAINT `FK_ElencoIngredienti_Folla` FOREIGN KEY (`follaID`) REFERENCES `folla`(`id`); /*Liste_Zutaten hat Foreignkey von Menge(id)*/
|
||||||
|
|
||||||
ALTER TABLE `utenteElenco`
|
ALTER TABLE `utenteElenco` /*Benutzer_Liste*/
|
||||||
ADD PRIMARY KEY (`id`),
|
ADD PRIMARY KEY (`id`),
|
||||||
ADD CONSTRAINT `FK_UtenteElenco_Utente` FOREIGN KEY (`utenteId`) REFERENCES `utente`(`id`), /*Benutzer_Liste hat Foreignkey von Benutzer(id)*/
|
ADD CONSTRAINT `FK_UtenteElenco_Utente` FOREIGN KEY (`utenteId`) REFERENCES `utente`(`id`), /*Benutzer_Liste hat Foreignkey von Benutzer(id)*/
|
||||||
ADD CONSTRAINT `FK_UtenteElenco_Elenco` FOREIGN KEY (`elencoId`) REFERENCES `elenco`(`id`); /*Benutzer_Liste hat Foreignkey von Liste(id)*/
|
ADD CONSTRAINT `FK_UtenteElenco_Elenco` FOREIGN KEY (`elencoId`) REFERENCES `elenco`(`id`); /*Benutzer_Liste hat Foreignkey von Liste(id)*/
|
||||||
|
|||||||
@@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
namespace BancaDati;
|
namespace BancaDati;
|
||||||
|
|
||||||
|
use PDO;
|
||||||
|
use PDOException;
|
||||||
|
|
||||||
class BancaDati {
|
class BancaDati {
|
||||||
private $dbName = "BancaDati";
|
private $dbName = "BancaDati";
|
||||||
private $linkName = "localhost";
|
private $linkName = "localhost";
|
||||||
@@ -14,11 +17,11 @@ class BancaDati {
|
|||||||
}
|
}
|
||||||
private function linkDB() {
|
private function linkDB() {
|
||||||
try {
|
try {
|
||||||
$this->pdo = new \PDO("mysql:dbname=$this->dbName;host=$this->linkName"
|
$this->pdo = new PDO("mysql:dbname=$this->dbName;host=$this->linkName"
|
||||||
, $this->user
|
, $this->user
|
||||||
, $this->pw
|
, $this->pw
|
||||||
, array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION));
|
, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
|
||||||
} catch (\PDOException $e) {
|
} catch (PDOException $e) {
|
||||||
die;
|
die;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -30,6 +33,32 @@ class BancaDati {
|
|||||||
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
|
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Einheitliche Insert Funktion
|
||||||
|
* @param string $table
|
||||||
|
* @param array $values
|
||||||
|
* @return void
|
||||||
|
* author Simon Bock
|
||||||
|
*/
|
||||||
|
public function insert(string $table, array $values){
|
||||||
|
$value = "";
|
||||||
|
$column = "";
|
||||||
|
foreach ($values as $col => $v){
|
||||||
|
$value .= $v . ",";
|
||||||
|
$column .= $col . ",";
|
||||||
|
}
|
||||||
|
$value = trim($value, ",");
|
||||||
|
$column = trim($column, ",");
|
||||||
|
|
||||||
|
$sql = "INSERT INTO $table($column) VALUES ($value);";
|
||||||
|
try {
|
||||||
|
$sth = $this->pdo->prepare($sql);
|
||||||
|
$sth->execute();
|
||||||
|
}catch (PDOException $e){
|
||||||
|
die;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Einheitliche Update Funktion
|
* Einheitliche Update Funktion
|
||||||
* @param string $table
|
* @param string $table
|
||||||
@@ -49,7 +78,7 @@ class BancaDati {
|
|||||||
try {
|
try {
|
||||||
$sth = $this->pdo->prepare($sql);
|
$sth = $this->pdo->prepare($sql);
|
||||||
$sth->execute();
|
$sth->execute();
|
||||||
}catch (\PDOException $e){
|
}catch (PDOException $e){
|
||||||
die;
|
die;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -66,7 +95,37 @@ class BancaDati {
|
|||||||
try {
|
try {
|
||||||
$sth = $this->pdo->prepare($sql);
|
$sth = $this->pdo->prepare($sql);
|
||||||
$sth->execute();
|
$sth->execute();
|
||||||
}catch (\PDOException $e){
|
}catch (PDOException $e){
|
||||||
|
die;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* einheitliche Select Funktion
|
||||||
|
* @param string $table
|
||||||
|
* @param array $where ["column"]=>"value" es wird mit LIKE verglichen und mit AND verbunden
|
||||||
|
* @param array|null $order ["by"]=>"column"; ["order"]=>"ASC|DESC"
|
||||||
|
* @return void
|
||||||
|
* @author Malte Schulze Hobeling
|
||||||
|
*/
|
||||||
|
public function select(string $table, array $where, array $order = null){
|
||||||
|
$whereString = "";
|
||||||
|
foreach ($where as $col => $v) {
|
||||||
|
if($whereString != ""){
|
||||||
|
$whereString .= " AND ";
|
||||||
|
}
|
||||||
|
$whereString .= $col . " LIKE " . $v;
|
||||||
|
}
|
||||||
|
$sql = "SELECT * FROM ".$table." WHERE ".$whereString;
|
||||||
|
if(isset($order["by"])){
|
||||||
|
$sql .= " ORDER BY ".$order["by"];
|
||||||
|
}
|
||||||
|
if(isset($order["order"])){
|
||||||
|
$sql .= $order["order"];
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return $this->pdo->query($sql);
|
||||||
|
}catch (PDOException $e){
|
||||||
die;
|
die;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ class Router
|
|||||||
$this->request = $_SERVER;
|
$this->request = $_SERVER;
|
||||||
}
|
}
|
||||||
if (isset($_POST)) {
|
if (isset($_POST)) {
|
||||||
$this->request["body"] = $_POST;
|
$this->request["body"] = json_decode(file_get_contents('php://input'), true);
|
||||||
}
|
}
|
||||||
if (isset($_POST)) {
|
if (isset($_POST)) {
|
||||||
$this->request["params"] = $_GET;
|
$this->request["params"] = $_GET;
|
||||||
|
|||||||
53
User.php
Normal file
53
User.php
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
<?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;
|
||||||
|
}
|
||||||
|
}
|
||||||
32
index.php
32
index.php
@@ -3,6 +3,7 @@
|
|||||||
require_once("Router/Router.php");
|
require_once("Router/Router.php");
|
||||||
require_once("Router/Response.php");
|
require_once("Router/Response.php");
|
||||||
require_once("BancaDati/BancaDati.php");
|
require_once("BancaDati/BancaDati.php");
|
||||||
|
require_once("User.php");
|
||||||
|
|
||||||
use Router\Response;
|
use Router\Response;
|
||||||
use Router\Router;
|
use Router\Router;
|
||||||
@@ -11,16 +12,45 @@ use BancaDati\BancaDati;
|
|||||||
$app = new Router("/DirektiveDesDons");
|
$app = new Router("/DirektiveDesDons");
|
||||||
$db = new BancaDati();
|
$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) {
|
$app->get("/", function (array $req, Response $res) {
|
||||||
$res->send("Hello World");
|
$res->send("Hello World");
|
||||||
|
//var_dump($req["user"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
$app->get("/user", function (array $req, Response $res) {
|
$app->get("/user", function (array $req, Response $res) {
|
||||||
$res->send("user");
|
$res->send("user");
|
||||||
});
|
});
|
||||||
$app->get("/user/:id", function (array $req, Response $res) {
|
$app->get("/user/:id", function (array $req, Response $res) use ($db) {
|
||||||
|
$db->select("utente", ["username" => $req["id"]]);
|
||||||
$res->send("user " . $req["params"]["id"]);
|
$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();
|
$app->start();
|
||||||
Reference in New Issue
Block a user