Compare commits
22 Commits
6dffa09e88
...
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 |
@@ -1,38 +1,46 @@
|
||||
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
|
||||
SET time_zone = "+00:00";
|
||||
SET
|
||||
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,
|
||||
`cognome` varchar(200) NOT NULL, /*Name*/
|
||||
`caloriePerCento` int(5) NOT NULL, /*Kalorien pro Gramm*/
|
||||
`ilPeso` int(5) NULL, /*Gewicht*/
|
||||
`caloriePerCento` integer(5) NOT NULL, /*Kalorien pro Gramm*/
|
||||
`ilPeso` integer(5) NULL, /*Gewicht*/
|
||||
`prezzo` decimal(4, 2) NOT NULL, /*Preis*/
|
||||
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
|
||||
CREATE TABLE `folla` ( /*Menge*/
|
||||
CREATE TABLE `folla`
|
||||
( /*Menge*/
|
||||
`id` varchar(36) NOT NULL,
|
||||
`unita` varchar(200) NOT NULL, /*Einheit*/
|
||||
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
|
||||
CREATE TABLE `elenco` ( /*Liste*/
|
||||
CREATE TABLE `elenco`
|
||||
( /*Liste*/
|
||||
`id` varchar(36) NOT NULL,
|
||||
`creatore` varchar(200) NOT NULL, /*Ersteller*/
|
||||
`coloreDiSfondo` integer(10) NOT NULL, /*Hintergrundfarbe*/
|
||||
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
|
||||
CREATE TABLE `utente` ( /*Benutzer*/
|
||||
CREATE TABLE `utente`
|
||||
( /*Benutzer*/
|
||||
`id` varchar(36) NOT NULL,
|
||||
`email` varchar(200) NOT NULL, /*Email*/
|
||||
`parolaDordine` integer(10) NOT NULL, /*Passwort*/
|
||||
`nomeUtente` integer(10) NOT NULL, /*Benutzernamen*/
|
||||
`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
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
|
||||
CREATE TABLE `elencoIngredienti` ( /*Liste_Zutaten*/
|
||||
CREATE TABLE `elencoIngredienti`
|
||||
( /*Liste_Zutaten*/
|
||||
`id` varchar(36) NOT NULL,
|
||||
`ingredientiID` varchar(36) NOT NULL, /*ZutatenID*/
|
||||
`elencoID` varchar(36) NOT NULL, /*ListeID*/
|
||||
@@ -40,32 +48,33 @@ CREATE TABLE `elencoIngredienti` ( /*Liste_Zutaten*/
|
||||
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
|
||||
CREATE TABLE `utenteElenco` ( /*Benutzer_Liste*/
|
||||
CREATE TABLE `utenteElenco`
|
||||
( /*Benutzer_Liste*/
|
||||
`id` varchar(36) NOT NULL,
|
||||
`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`
|
||||
ALTER TABLE `ingredienti` /*Zutaten*/
|
||||
ADD PRIMARY KEY (`id`);
|
||||
|
||||
ALTER TABLE `folla`
|
||||
ALTER TABLE `folla` /*Menge*/
|
||||
ADD PRIMARY KEY (`id`);
|
||||
|
||||
ALTER TABLE `elenco`
|
||||
ALTER TABLE `elenco` /*Liste*/
|
||||
ADD PRIMARY KEY (`id`);
|
||||
|
||||
ALTER TABLE `utente`
|
||||
ALTER TABLE `utente` /*Benutzer*/
|
||||
ADD PRIMARY KEY (`id`);
|
||||
|
||||
ALTER TABLE `elencoIngredienti`
|
||||
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)*/
|
||||
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)*/
|
||||
|
||||
ALTER TABLE `utenteElenco`
|
||||
ALTER TABLE `utenteElenco` /*Benutzer_Liste*/
|
||||
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_Elenco` FOREIGN KEY (`elencoId`) REFERENCES `elenco`(`id`); /*Benutzer_Liste hat Foreignkey von Liste(id)*/
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
|
||||
namespace BancaDati;
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
|
||||
class BancaDati {
|
||||
private $dbName = "BancaDati";
|
||||
private $linkName = "localhost";
|
||||
@@ -14,11 +17,11 @@ class BancaDati {
|
||||
}
|
||||
private function linkDB() {
|
||||
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->pw
|
||||
, array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION));
|
||||
} catch (\PDOException $e) {
|
||||
, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
|
||||
} catch (PDOException $e) {
|
||||
die;
|
||||
}
|
||||
}
|
||||
@@ -51,7 +54,7 @@ class BancaDati {
|
||||
try {
|
||||
$sth = $this->pdo->prepare($sql);
|
||||
$sth->execute();
|
||||
}catch (\PDOException $e){
|
||||
}catch (PDOException $e){
|
||||
die;
|
||||
}
|
||||
}
|
||||
@@ -75,7 +78,7 @@ class BancaDati {
|
||||
try {
|
||||
$sth = $this->pdo->prepare($sql);
|
||||
$sth->execute();
|
||||
}catch (\PDOException $e){
|
||||
}catch (PDOException $e){
|
||||
die;
|
||||
}
|
||||
}
|
||||
@@ -92,7 +95,37 @@ class BancaDati {
|
||||
try {
|
||||
$sth = $this->pdo->prepare($sql);
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ class Router
|
||||
$this->request = $_SERVER;
|
||||
}
|
||||
if (isset($_POST)) {
|
||||
$this->request["body"] = $_POST;
|
||||
$this->request["body"] = json_decode(file_get_contents('php://input'), true);
|
||||
}
|
||||
if (isset($_POST)) {
|
||||
$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/Response.php");
|
||||
require_once("BancaDati/BancaDati.php");
|
||||
require_once("User.php");
|
||||
|
||||
use Router\Response;
|
||||
use Router\Router;
|
||||
@@ -11,16 +12,45 @@ 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) {
|
||||
$app->get("/user/:id", function (array $req, Response $res) use ($db) {
|
||||
$db->select("utente", ["username" => $req["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();
|
||||
Reference in New Issue
Block a user