diff --git a/front/views/Settings.tsx b/front/views/Settings.tsx index a207d77..29d145d 100644 --- a/front/views/Settings.tsx +++ b/front/views/Settings.tsx @@ -84,9 +84,10 @@ function ProfilSettings({ user }: { user: User }) { Adresse mail - alert("En cours de développement...")} ref={emailRef} id="control" size="sm" defaultValue={user.email} type="email" placeholder="Password" /> + + {/* alert("En cours de développement...")} ref={emailRef} id="control" size="sm" defaultValue={user.email} type="email" placeholder="Password" /> */} - + @@ -114,8 +115,7 @@ function ProfilSettings({ user }: { user: User }) { ); } - -function updateAccountInfos(name: string, email: string, user: User) { +function updateAccountInfos(name: string, email: string) { fetchAPI("account/update/profile", { name: name, email: email diff --git a/src/Core/Connection.php b/src/Core/Connection.php index 1dd559d..0a1833e 100644 --- a/src/Core/Connection.php +++ b/src/Core/Connection.php @@ -57,5 +57,4 @@ class Connection { public function prepare(string $query): \PDOStatement { return $this->pdo->prepare($query); } - } diff --git a/src/Core/Gateway/AccountGateway.php b/src/Core/Gateway/AccountGateway.php index 1d60780..46db951 100644 --- a/src/Core/Gateway/AccountGateway.php +++ b/src/Core/Gateway/AccountGateway.php @@ -36,6 +36,14 @@ class AccountGateway { return $this->con->fetch("SELECT * FROM Account WHERE email = :email", [':email' => [$email, PDO::PARAM_STR]])[0] ?? null; } + /** + * @param int $id + * @return array|null + */ + private function getRowsFromId(int $id): ?array { + return $this->con->fetch("SELECT * FROM Account WHERE id = :id", [':id' => [$id, PDO::PARAM_INT]])[0] ?? null; + } + /** * @param string $email * @return string|null the hashed user's password, or null if the given mail does not exist @@ -69,6 +77,19 @@ class AccountGateway { return new Account($acc["token"], new User($email, $acc["username"], $acc["id"], $acc["profilePicture"])); } + /** + * @param int $id + * @return Account|null + */ + public function getAccountFromId(int $id): ?Account { + $acc = $this->getRowsFromId($id); + if (empty($acc)) { + return null; + } + + return new Account($acc["token"], new User($acc["email"], $acc["username"], $id, $acc["profilePicture"])); + } + /** * @param string $token get an account from given token * @return Account|null @@ -94,8 +115,8 @@ class AccountGateway { ]); } - public function nameIsDifferent(string $email, string $name) : bool { - $nameExist = $this->con->fetch("SELECT username FROM Account WHERE email = :email", [':email' => [$email, PDO::PARAM_STR]]) ?? null; + public function nameIsDifferent(int $id, string $name) : bool { + $nameExist = $this->con->fetch("SELECT username FROM Account WHERE id = :id", [':id' => [$id, PDO::PARAM_STR]]) ?? null; if (!empty($nameExist)) { if ($name != $nameExist[0]["username"]) { @@ -105,16 +126,41 @@ class AccountGateway { return false; } - public function changeName(string $email, string $newName) { - error_log($email); + public function emailIsDifferent(int $id, string $email) : bool { + $emailExist = $this->con->fetch("SELECT email FROM Account WHERE id = :id", [':id' => [$id, PDO::PARAM_STR]]) ?? null; + + if (!empty($emailExist)) { + if ($email != $emailExist[0]["email"]) { + return true; + } + } + return false; + } + + public function emailExist(string $email) { + $res = $this->con->fetch("SELECT email FROM Account WHERE email = :email", [':email' => [$email, PDO::PARAM_STR]]) ?? null; + return $res != null; + } + public function changeName(int $id, string $newName) { $this->con->exec(" UPDATE Account SET username = :username - WHERE email = :email + WHERE id = :id ", [ ':username' => [$newName, PDO::PARAM_STR], - ':email' => [$email, PDO::PARAM_STR] + ':id' => [$id, PDO::PARAM_STR] + ]); + } + + public function changeEmail(int $id, string $newEmail) { + $this->con->exec(" + UPDATE Account + SET email = :email + WHERE id = :id + ", [ + ':email' => [$newEmail, PDO::PARAM_STR], + ':id' => [$id, PDO::PARAM_STR] ]); } } diff --git a/src/Core/Model/AuthModel.php b/src/Core/Model/AuthModel.php index ef9b839..c5f5bdc 100644 --- a/src/Core/Model/AuthModel.php +++ b/src/Core/Model/AuthModel.php @@ -53,16 +53,21 @@ class AuthModel { } public function updateProfile(string $name, string $email, int $id) : array { - - if(!empty($this->gateway->getAccountFromMail($email))) { - if ($this->gateway->nameIsDifferent($email, $name)) { - $this->gateway->changeName($email, $name); - return []; + if(!empty($this->gateway->getAccountFromId($id))) { + if ($this->gateway->nameIsDifferent($id, $name)) { + $this->gateway->changeName($id, $name); } - return [ValidationFail::unauthorized("Mail already exist")]; + if ($this->gateway->emailIsDifferent($id, $email)) { + + if (!$this->gateway->emailExist($email)) { + $this->gateway->changeEmail($id, $email); + } + return [ValidationFail::unauthorized("Mail already exist")]; + } + + return []; } - $this->gateway->updateProfile($name, $email, $id); - return []; + return [ValidationFail::error("Account doesn't exist")]; } /**