From e6d2101f330bef812d536a0f894ee0261b0e9117 Mon Sep 17 00:00:00 2001 From: LeonardoBizzoni Date: Mon, 18 Apr 2022 15:05:43 +0200 Subject: [PATCH] Finished registration --- README.org | 3 ++ www/Migrations/m_1650263661_Initial.php | 1 - www/Migrations/m_1650284365_addUsername.php | 21 +++++++++++ www/controllers/AuthController.php | 4 +- www/core/BaseModel.php | 14 +++++++ www/core/DbModel.php | 29 ++++++++++++++ www/core/forms/Field.php | 4 +- www/models/RegisterModel.php | 29 -------------- www/models/User.php | 42 +++++++++++++++++++++ 9 files changed, 113 insertions(+), 34 deletions(-) create mode 100644 www/Migrations/m_1650284365_addUsername.php create mode 100644 www/core/DbModel.php delete mode 100644 www/models/RegisterModel.php create mode 100644 www/models/User.php diff --git a/README.org b/README.org index 4e0bd79..f662ed8 100644 --- a/README.org +++ b/README.org @@ -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. diff --git a/www/Migrations/m_1650263661_Initial.php b/www/Migrations/m_1650263661_Initial.php index 88b7c31..59c4ed0 100644 --- a/www/Migrations/m_1650263661_Initial.php +++ b/www/Migrations/m_1650263661_Initial.php @@ -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 index 0000000..a47ef17 --- /dev/null +++ b/www/Migrations/m_1650284365_addUsername.php @@ -0,0 +1,21 @@ +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); + } +} +?> diff --git a/www/controllers/AuthController.php b/www/controllers/AuthController.php index fc58e8e..4671a29 100644 --- a/www/controllers/AuthController.php +++ b/www/controllers/AuthController.php @@ -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()); diff --git a/www/core/BaseModel.php b/www/core/BaseModel.php index 6820a5f..9960ffe 100644 --- a/www/core/BaseModel.php +++ b/www/core/BaseModel.php @@ -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 index 0000000..11d2869 --- /dev/null +++ b/www/core/DbModel.php @@ -0,0 +1,29 @@ +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); + } +} diff --git a/www/core/forms/Field.php b/www/core/forms/Field.php index 8ecb75f..af33968 100644 --- a/www/core/forms/Field.php +++ b/www/core/forms/Field.php @@ -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 {
%s
', - $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 index 75c4c89..0000000 --- a/www/models/RegisterModel.php +++ /dev/null @@ -1,29 +0,0 @@ - [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 index 0000000..4f3bb34 --- /dev/null +++ b/www/models/User.php @@ -0,0 +1,42 @@ +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"]] + ]; + } +} -- 2.52.0