]> git.leonardobizzoni.com Git - highschool-graduation-project/commitdiff
Finished registration
authorLeonardoBizzoni <leo2002714@gmail.com>
Mon, 18 Apr 2022 13:05:43 +0000 (15:05 +0200)
committerLeonardoBizzoni <leo2002714@gmail.com>
Mon, 18 Apr 2022 13:05:43 +0000 (15:05 +0200)
README.org
www/Migrations/m_1650263661_Initial.php
www/Migrations/m_1650284365_addUsername.php [new file with mode: 0644]
www/controllers/AuthController.php
www/core/BaseModel.php
www/core/DbModel.php [new file with mode: 0644]
www/core/forms/Field.php
www/models/RegisterModel.php [deleted file]
www/models/User.php [new file with mode: 0644]

index 4e0bd79791022da34f78cb6d37735530a0ebc03a..f662ed891fae80b5ec51b7aeb19c727d5e014b2e 100644 (file)
@@ -224,6 +224,9 @@ Attraverso il metodo "applyMigrations()" la Database class è in grado di:
 - selezionare le migration presenti nella migration table
 - per ogni migration non presente nel DB, creare un'istanza ed esegue il metodo "up()"
 
+*** Database model class
+La classe DbModel si basa sulla base Database class ma viene trattata come l'effettiva SQL table.
+
 ** Migrations
 
 È importante che le migration class siano ordinate e che seguano tutte lo stesso stile di nomeclatura.
index 88b7c3130131c03e4656df2b2ded1c927635ab98..59c4ed0a61b4855a5ef9e0c546f3dd053cdbb8f3 100644 (file)
@@ -11,7 +11,6 @@ class m_1650263661_Initial {
                 email VARCHAR(255) NOT NULL,
                 firstname VARCHAR(255) NOT NULL,
                 lastname VARCHAR(255) NOT NULL,
-                status TINYINT NOT NULL,
                 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
               ) ENGINE=INNODB;";
 
diff --git a/www/Migrations/m_1650284365_addUsername.php b/www/Migrations/m_1650284365_addUsername.php
new file mode 100644 (file)
index 0000000..a47ef17
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+
+use app\core\Application;
+
+class m_1650284365_addUsername
+{
+    public function up()
+    {
+        $db = Application::$app->db;
+        $sql = "ALTER TABLE users ADD COLUMN username VARCHAR(255) NOT NULL;";
+        $db->pdo->exec($sql);
+    }
+
+    public function down()
+    {
+        $db = Application::$app->db;
+        $sql = "ALTER TABLE users DROP COLUMN username;";
+        $db->pdo->exec($sql);
+    }
+}
+?>
index fc58e8e1cd28284c23bebd35a62bc12af3efefec..4671a2986ff60dfe17e825ddd0f0ad8bc0094507 100644 (file)
@@ -3,7 +3,7 @@ namespace app\controllers;
 
 use app\core\BaseController;
 use app\core\Request;
-use app\models\RegisterModel;
+use app\models\User;
 
 class AuthController extends BaseController{
     public function login() {
@@ -14,7 +14,7 @@ class AuthController extends BaseController{
     public function register(Request $req) {
         // $this->setLayout("auth");
         $errors = [];
-        $registerModel = new RegisterModel;
+        $registerModel = new User;
 
         if ($req->getMethod() == "post") {
             $registerModel->loadData($req->getBody());
index 6820a5f9eef86d645d0ce25d44370c8d4272ddff..9960ffee9a0c372341057d96f36f56215108e0eb 100644 (file)
@@ -47,6 +47,19 @@ abstract class BaseModel {
                 if ($ruleName == self::RULE_MATCH && $value != $this->{$rule["match"]}) {
                     $this->addError($attribute, self::RULE_MATCH, $rule);
                 }
+                if ($ruleName == self::RULE_UNIQUE) {
+                    $className = $rule["class"];
+                    $uniqueAttr = $rule["attribute"] ?? $attribute;
+                    $tableName = $className::tableName();
+
+                    $statement = Application::$app->db->pdo->prepare("select * from $tableName where $uniqueAttr = :attr");
+                    $statement->bindValue(":attr", $value);
+                    $statement->execute();
+
+                    if($statement->fetchObject()) {
+                        $this->addError($attribute, self::RULE_UNIQUE, ["field" => $attribute]);
+                    }
+                }
             }
         }
 
@@ -68,6 +81,7 @@ abstract class BaseModel {
             self::RULE_MIN => "Min length of this field must be {min}",
             self::RULE_MAX => "Max length of this field must be {max}",
             self::RULE_MATCH => "This field must be the same as {match}",
+            self::RULE_UNIQUE => "Record with this {field} already exist"
         ];
     }
 
diff --git a/www/core/DbModel.php b/www/core/DbModel.php
new file mode 100644 (file)
index 0000000..11d2869
--- /dev/null
@@ -0,0 +1,29 @@
+<?php
+
+namespace app\core;
+
+abstract class DbModel extends BaseModel
+{
+    abstract public function tableName(): string;
+    abstract public function attributes(): array;
+
+    public function save()
+    {
+        $tableName = $this->tableName();
+        $attributes = $this->attributes();
+        $params = array_map(fn($attr) => ":$attr",  $attributes);
+
+        $statement = self::prepare("INSERT INTO $tableName (".implode(',', $attributes).") VALUES (".implode(',', $params).")");
+
+        foreach ($attributes as $attr) {
+            $statement->bindValue(":$attr", $this->{$attr});
+        }
+
+        $statement->execute();
+        return true;
+    }
+
+    public static function prepare(string $sql) {
+        return  Application::$app->db->pdo->prepare($sql);
+    }
+}
index 8ecb75f28129fc58ee15cc5adec20dac34f12eda..af33968ecf21d6087046e61ba7684988f6005549 100644 (file)
@@ -13,7 +13,7 @@ class Field {
 
     public function __construct(BaseModel $model, string $attribute) {
         $this->model = $model;
-        $this->attribute = $attribute;
+        $this->attribute = strtolower($attribute);
         $this->type = self::TYPE_TEXT;
     }
 
@@ -26,7 +26,7 @@ class Field {
     <input name="%s" type="%s" value="%s" class="form-control%s"/>
     <div class="invalid-feedback">%s</div>
 </div>',
-            $this->attribute,
+            ucfirst($this->attribute),
             $this->attribute,
             $this->type,
             $this->model->{$this->attribute},
diff --git a/www/models/RegisterModel.php b/www/models/RegisterModel.php
deleted file mode 100644 (file)
index 75c4c89..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-<?php
-namespace app\models;
-
-use app\core\BaseModel;
-
-class RegisterModel extends BaseModel {
-    public string $Firstname = "";
-    public string $Lastname = "";
-    public string $Email = "";
-    public string $Username = "";
-    public string $Password = "";
-    public string $Confirm = "";
-
-    public function register() {
-        echo "Creating new user";
-    }
-
-    public function rules(): array {
-        return [
-            "Firstname" => [self::RULE_REQUIRED],
-            "Lastname" => [self::RULE_REQUIRED],
-            "Username" => [self::RULE_REQUIRED],
-            "Email" => [self::RULE_REQUIRED, self::RULE_EMAIL],
-            "Password" => [self::RULE_REQUIRED, [ self::RULE_MIN, "min" => 20 ], [ self::RULE_MAX, "max" => 100 ]],
-            "Confirm" => [self::RULE_REQUIRED, [self::RULE_MATCH, "match" => "Password" ]]
-        ];
-    }
-}
-?>
diff --git a/www/models/User.php b/www/models/User.php
new file mode 100644 (file)
index 0000000..4f3bb34
--- /dev/null
@@ -0,0 +1,42 @@
+<?php
+
+namespace app\models;
+
+use app\core\DbModel;
+
+class User extends DbModel
+{
+    public string $firstname = "";
+    public string $lastname = "";
+    public string $email = "";
+    public string $username = "";
+    public string $password = "";
+    public string $confirm = "";
+
+    public function tableName(): string
+    {
+        return "users";
+    }
+
+    public function attributes(): array {
+        return [ "firstname", "lastname", "email", "username", "password" ];
+    }
+
+    public function register()
+    {
+        $this->password = password_hash($this->password, PASSWORD_DEFAULT);
+        return $this->save();
+    }
+
+    public function rules(): array
+    {
+        return [
+            "firstname" => [self::RULE_REQUIRED],
+            "lastname" => [self::RULE_REQUIRED],
+            "username" => [self::RULE_REQUIRED],
+            "email" => [self::RULE_REQUIRED, self::RULE_EMAIL, [self::RULE_UNIQUE, "class" => self::class ]],
+            "password" => [self::RULE_REQUIRED, [self::RULE_MIN, "min" => 20], [self::RULE_MAX, "max" => 100]],
+            "confirm" => [self::RULE_REQUIRED, [self::RULE_MATCH, "match" => "password"]]
+        ];
+    }
+}