commit aecb2568f2969d170395728ce51b9f03178475d7
parent ba4f8d91b05f7e362688b8e5a4795d6554331c4f
Author: MoiBaguette <[email protected]>
Date: Sun, 11 Jun 2023 13:34:44 +0200
structural changes
Diffstat:
14 files changed, 142 insertions(+), 152 deletions(-)
diff --git a/Controller/login/login_get.php b/Controller/login/login_get.php
@@ -0,0 +1,3 @@
+<?php
+ $templater = new Lollipop\Template();
+ echo $templater->template("views/login.html", ["msg" => ""]);
+\ No newline at end of file
diff --git a/Controller/login/login_post.php b/Controller/login/login_post.php
@@ -0,0 +1,9 @@
+<?php
+ $templater = new Lollipop\Template();
+ $login = new Login_handler();
+ if( $login->login()){
+ echo $templater->template("views/panda.html", ["nothing" => "empty"]);
+ }else{
+ echo $templater->template("views/login.html", ["msg" => "<p style=\"color:red;\">Incorrect username or password.</p>"]);
+ }
+ ?>
+\ No newline at end of file
diff --git a/Controller/user/add_get.php b/Controller/user/add_get.php
@@ -0,0 +1,4 @@
+ <?php
+ $templater = new Lollipop\Template();
+ $template["msg"] = "";
+ echo $templater->template("views/add_user.html", $template);
+\ No newline at end of file
diff --git a/Controller/user/add_post.php b/Controller/user/add_post.php
@@ -0,0 +1,42 @@
+<?php
+function add_user(){
+ $db = new Lollipop\SQLDatabase("86.92.67.21", "friedel", "hailiwa", "panda");
+ $u = $db->get(User::class);
+ $utils = new Utils();
+
+ $post_arr = $utils->post_to_array();;
+ $missing_fields = $utils->missing_fields($post_arr , $u->not_nullable);
+
+ if(sizeof($missing_fields) > 0)
+ return $missing_fields;
+
+ if($u->load($post_arr[$u->get_primary()])){
+ return ["msg" => "<p style=\"color:red;\">this email address is already taken: {$post_arr[$u->get_primary()]} </p>"];
+ }else{
+ if($post_arr["password"]){
+ $post_arr["password"] = password_hash($post_arr["password"], PASSWORD_DEFAULT);
+ }
+ foreach($u->column_names as $col){
+ if($post_arr[$col] != ""){
+ $u->$col = $post_arr[$col];
+ }
+ }
+ if($u->add())
+ return ["msg" => "<p style=\"color:green;\">succes</p>"];
+ else
+ return ["msg" => "<p style=\"color:red;\">could not add user to database</p>"];
+ }
+}
+
+
+$templater = new Lollipop\Template();
+$response = add_user();
+
+if(!array_key_exists("msg", $response)){
+ $newArray = ["msg" => ""];
+ array_push($newArray, $response);
+}
+
+echo $templater->template("views/add_user.html", $response);
+
+
diff --git a/Lollipop/DatabaseObject.php b/Lollipop/DatabaseObject.php
@@ -17,6 +17,7 @@ namespace Lollipop {
$this->db = $db;
$this->primary = $this->get_primary();
$this->table = $this->get_table();
+ $this->notNullable();
}
abstract static function get_primary(): string;
@@ -26,9 +27,9 @@ namespace Lollipop {
{
$this->data = $data;
}
- public function where(string $key, string $value)
+ public function where(string $value)
{
- $sql = "SELECT * FROM {$this->table} WHERE $key = ?";
+ $sql = "SELECT * FROM {$this->table} WHERE {$this->primary} = ?";
$value = array($value);
$stmt = $this->db->conn->prepare($sql);
$stmt->execute($value);
@@ -165,9 +166,11 @@ namespace Lollipop {
{
return $this->data;
}
- public function getCollumnNames(){
- $collumns = [];
- $sql = " SELECT COLUMN_NAME
+ private function notNullable(){
+ //non-auto-increment not-nullable collumn names query
+ $not_null = [];
+ $col_names = [];
+ $sql = " SELECT column_name, is_nullable, extra
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = '{$this->table}'
AND TABLE_SCHEMA = 'panda'";
@@ -179,9 +182,15 @@ namespace Lollipop {
return false;
}
while($tmp = $result->fetch_assoc()){
- $collumns[] = $tmp;
+ if($tmp["is_nullable"] == 'NO'){
+ if($tmp["extra"] == "auto_increment")
+ continue;
+ $not_null[] = $tmp["column_name"];
+ }
+ $col_names[] = $tmp["column_name"];
}
- $this->data["collumn_names"] = $collumns;
+ $this->data["not_nullable"] = $not_null;
+ $this->data["column_names"] = $col_names;
return true;
}
}
diff --git a/Model/Add_user.php b/Model/Add_user.php
@@ -1,74 +0,0 @@
-<?php
-class Add_user {
- function add_user() :array{
- //this function checks $_POST fields for data if all the data is present adds the user to database
- $errors = [];
- $data = [];
-
- foreach ($_POST as $key => $value) {
- if($value == ""){
- $errors[$key] = "This field is required";
- }else{
- $arr[$key] = [$value];
- }
- }
-
- if(siezof($errors) > 0){
- return $errors;
- } else {
- create_user($data);
- return ["msg" => "succes! user with email: {$data[$email]} was added to the db"];
- }
- }
- function create_permission_radials():string{
- $db = new Lollipop\SQLDatabase("86.92.67.21", "friedel", "hailiwa", "panda");
- //select the available permissions from the database
- $all_p = $db->all(Permissions::class);
- $radials = "";
- foreach($all_p as $db_permission){
- $radials .= "<div class=\"mb-3 form-check\">
- <input type=\"checkbox\" class=\"form-check-input\" name=\"permissions[]\" value=" . $db_permission->id . "\">
- <input type='hidden' value='-1' name='{$db_permission->name}'>
- <label class=\"form-check-label\" for=" . $db_permission->name . ">" . $db_permission->name . "</label>
- </div> ";
- }
- return $radials;
- }
-
- function create_user(array $data){
- $u = $db->get(User::class);
- //check if email already exists
- if($u->where("email", $data["email"])){
- $msg = "this email address is taken: " . $email;
- }else{
- $u = $db->get(User::class);
- //set new user data
- $u->email = $data["email"];
- $u->first_name = $data["first_name"];
- $u->last_name = $data["last_name"];
- //hash the pwd
- $hashed_pwd = password_hash($data["password"], PASSWORD_DEFAULT);
- $u->password = $hashed_pwd;
-
- //add user with the add function
- if(!$u->add()){
- throw new ErrorException("Could not add user to database");
- };
- $u = $db->get(User::class);
- $u->where("email", $data["email"]);
- //create a database object with table permission for each permission
- //set the data and execute the add function
- foreach($permissions as $permission){
- $p = $db->get(Permission_user::class);
- $p->user_id = $u->user_id;
- $p->permission_id = (int) $permission;
- if(!$p->add())
- {
- throw new ErrorException("Could not add user_permissions to database");
- }
- }
- }
- }
-}
-?>
-
-\ No newline at end of file
diff --git a/Model/Login_handler.php b/Model/Login_handler.php
@@ -18,7 +18,7 @@ class Login_handler
$u = $db->get(User::class);
//check if the email exists in db
- if(!$u->where('email', $email)){
+ if(!$u->where($email)){
//email does not exist
return false;
}else{
@@ -44,7 +44,7 @@ class Login_handler
$_SESSION['last_name'] = $u->last_name;
//get permissions form db and set sessions_permissions
- $p = $db->all_where(Permission_user::class, array('email' => $u->email));
+ $p = $db->all_where(Permission_user::class, array('id_user' => $u->id));
foreach($p as $permission){
$user_permissions[] = $permission->id;
}
diff --git a/Model/Permission_user.php b/Model/Permission_user.php
@@ -0,0 +1,14 @@
+<?php
+class Permission_user extends Lollipop\DatabaseObject
+{
+ static function get_table(): string
+ {
+ return "permission_user";
+ }
+
+ static function get_primary(): string
+ {
+ return "id_user";
+ }
+}
+?>
+\ No newline at end of file
diff --git a/Model/User.php b/Model/User.php
@@ -8,7 +8,7 @@ class User extends Lollipop\DatabaseObject
static function get_primary(): string
{
- return "user_id";
+ return "email";
}
}
?>
\ No newline at end of file
diff --git a/Model/Utils.php b/Model/Utils.php
@@ -0,0 +1,35 @@
+<?php
+Class Utils{
+ function post_to_array():array{
+ $arr = [];
+ foreach ($_POST as $key => $value) {
+ $arr[$key] = $value;
+ }
+ return $arr;
+ }
+
+ function missing_fields($post, $not_nullable){
+ $missing = [];
+ foreach($not_nullable as $column){
+ if($post[$column] == NULL || $post[$column] == ""){
+ $missing[$column] = "This field cannot be empty!";
+ }
+ }
+ return $missing;
+ }
+
+ function create_permission_radials():string{
+ $db = new Lollipop\SQLDatabase("86.92.67.21", "friedel", "hailiwa", "panda");
+ //select the available permissions from the database
+ $all_p = $db->all(Permissions::class);
+ $radials = "";
+ foreach($all_p as $db_permission){
+ $radials .= "<div class=\"mb-3 form-check\">
+ <input type=\"checkbox\" class=\"form-check-input\" name=\"permissions[]\" value=" . $db_permission->id . "\">
+ <input type='hidden' value='-1' name='{$db_permission->name}'>
+ <label class=\"form-check-label\" for=" . $db_permission->name . ">" . $db_permission->name . "</label>
+ </div> ";
+ }
+ return $radials;
+ }
+}
+\ No newline at end of file
diff --git a/Model/post_to_array.php b/Model/post_to_array.php
@@ -1,8 +0,0 @@
-<?php
-function to_array(){
- $arr = [];
- foreach ($_POST as $key => $value) {
- $arr[$key] = [$value];
- }
- return $arr;
-}
-\ No newline at end of file
diff --git a/index.php b/index.php
@@ -4,60 +4,13 @@ require_once "utils/autoloader.php";
$router = new Lollipop\Router();
-$router->addRoute(["GET"], "/", function($vars){
- $templater = new Lollipop\Template();
- echo $templater->template("views/login.html", ["msg" => ""]);
-});
+$router->addRoute(["GET"], "/", "Controller/login/login_get.php");
-$router->addRoute(["POST"], "/login", function($vars){
- $templater = new Lollipop\Template();
- $login = new Login_handler();
- if( $login->login()){
- echo $templater->template("view/panda.html", ["msg" => ""]);
- }else{
- echo $templater->template("views/login.html", ["msg" => "<p style=\"color:red;\">Incorrect username or password.</p>"]);
- }
-});
+$router->addRoute(["POST"], "/login", "Controller/login/login_post.php");
-$router->addRoute(["GET"], "/user/add", function($vars){
- $add_user = new Add_user();
- $templater = new Lollipop\Template();
- $template = [];
-
- if(sizeof($vars) > 0){
- //do something with vars
- //get username from uri
- }
-
- $template["msg"] = "";
- $template["radials"] = $add_user->create_permission_radials();
- echo $templater->template("views/add_user.html", $template);
-});
+$router->addRoute(["GET"], "/user/add", "Controller/user/add_get.php");
-$router->addRoute(["POST"], "/user/test", function($vars){
- $arr = [];
- $post = $_POST;
- array_pop($post);
- foreach ($post as $key => $value) {
- $arr[$key] = [$value];
- }
- //var_dump($arr);
- $db = new Lollipop\SQLDatabase("86.92.67.21", "friedel", "hailiwa", "panda");
- $tmp = $db->get(User::class);
- $tmp->getCollumnNames();
- var_dump($tmp->collumn_names);
-});
-
-
-$router->addRoute(["POST"], "/user/add", function($vars){
- $add_user = new Add_user();
- $templater = new Lollipop\Template();
- $template = [];
-
- array_push($template, $add_user->read_post());
- $template["radials"] = $add_user->create_permission_radials();
- echo $templater->template("views/add_user.html", $template);
-});
+$router->addRoute(["POST"], "/user/add", "Controller/user/add_post.php");
$router->addRoute(["GET"], "/dashboard", "views/panda.html");
$router->addRoute(["GET"], "/api/:data", function($vars) {
diff --git a/logic/graphJson.php b/logic/graphJson.php
@@ -0,0 +1 @@
+echo json_encode($graphdata);
+\ No newline at end of file
diff --git a/views/add_user.html b/views/add_user.html
@@ -10,25 +10,23 @@
<div class="container">
<h1>Add user</h1>
- <form action="test" method="post">
+ <form action="/user/add" method="post">
<div class="mb-3">
<label for="first_name" class="form-label"><b>Voornaam:</b></label>
- <input type="text" class="form-control" name="first_name" id="first_name" placeholder="{{first_name}}first_name">
+ <input type="text" class="form-control" name="first_name" id="first_name" placeholder="{{first_name}}">
</div>
<div class="mb-3">
<label for="achternaam" class="form-label"><b>Achternaam:</b></label>
- <input type="text" class="form-control" name="achternaam" id="achternaam" placeholder="Achternaam">
+ <input type="text" class="form-control" name="last_name" id="achternaam" placeholder="{{last_name}}">
</div>
<div class="mb-3">
<label for="email" class="form-label"><b>Email:</b></label>
- <input type="text" class="form-control" name="email" id="email" placeholder="Email">
+ <input type="text" class="form-control" name="email" id="email" placeholder="{{email}}">
</div>
<div class="mb-3">
<label for="password" class="form-label"><b>Wachtwoord:</b></label>
- <input type="password" class="form-control" name="password" id="password" placeholder="******">
+ <input type="password" class="form-control" name="password" id="password" placeholder="{{password}}">
</div>
- <p>Please select the user permissions:</p>
- {{radials}}
<button type="submit" class="btn btn-primary" name="submit">Add user</button>
</form>
{{msg}}