From 930c0cfb3f6331aa0f6811ca5186b7d2fca1a775 Mon Sep 17 00:00:00 2001 From: d_yanis Date: Thu, 8 Feb 2024 23:10:07 +0100 Subject: [PATCH] Change name is available in settings page --- front/Fetcher.ts | 15 +++++++++++++++ front/views/Settings.tsx | 14 +++++--------- public/index.php | 2 +- src/Api/Controller/APIUserController.php | 2 ++ src/App/Controller/AuthController.php | 6 ++++++ src/Core/Gateway/AccountGateway.php | 23 +++++++++++++++++++++++ src/Core/Model/AuthModel.php | 10 ++++++++++ 7 files changed, 62 insertions(+), 10 deletions(-) diff --git a/front/Fetcher.ts b/front/Fetcher.ts index 4c483e9..a37d485 100644 --- a/front/Fetcher.ts +++ b/front/Fetcher.ts @@ -14,3 +14,18 @@ export function fetchAPI( body: JSON.stringify(payload), }) } + +export function fetchPOST( + url: string, + payload: unknown, + method = "POST", +): Promise { + return fetch(`/${url}`, { + method, + headers: { + Accept: "application/json", + "Content-Type": "application/json", + }, + body: JSON.stringify(payload), + }) +} diff --git a/front/views/Settings.tsx b/front/views/Settings.tsx index 4e69af2..894f86c 100644 --- a/front/views/Settings.tsx +++ b/front/views/Settings.tsx @@ -9,6 +9,7 @@ import Form from 'react-bootstrap/Form'; import Image from 'react-bootstrap/Image'; import { updateSourceFile } from "typescript"; import { fetchAPI } from "../Fetcher"; +import { fetchPOST } from "../Fetcher"; export default function Settings({user} : {user : User}){ @@ -81,7 +82,7 @@ function ProfilSettings({user} : {user : User}) { Adresse mail - + @@ -89,16 +90,11 @@ function ProfilSettings({user} : {user : User}) { } -function updateAccountInfos(name : string, email : string) { +function updateAccountInfos(name : string, email : string, user : User) { fetchAPI("account/update/profile", { name : name, email : email }); + fetchPOST("account/update", {}); + location.reload(); } -// function InputSettings(){ -// return( -//
-//

{title}

-//
-// ) -// } \ No newline at end of file diff --git a/public/index.php b/public/index.php index 5d24e48..e750130 100644 --- a/public/index.php +++ b/public/index.php @@ -107,7 +107,7 @@ function getRoutes(): AltoRouter { $ar->map("GET", "/team/[i:idTeam]/remove/[i:idMember]", Action::auth(fn(int $idTeam, int $idMember, SessionHandle $s) => getTeamController()->deleteMember($idTeam, $idMember, $s))); $ar->map("GET", "/team/[i:id]/edit", Action::auth(fn(int $idTeam, SessionHandle $s) => getTeamController()->displayEditTeam($idTeam, $s))); $ar->map("POST", "/team/[i:id]/edit", Action::auth(fn(int $idTeam, SessionHandle $s) => getTeamController()->editTeam($idTeam, $_POST, $s))); - + $ar->map("POST", "/account/update", Action::auth(fn(SessionHandle $s) => getAuthController()->updateAccount($s))); return $ar; } diff --git a/src/Api/Controller/APIUserController.php b/src/Api/Controller/APIUserController.php index 5bf66f1..bae98ce 100644 --- a/src/Api/Controller/APIUserController.php +++ b/src/Api/Controller/APIUserController.php @@ -31,6 +31,7 @@ class APIUserController { * @return HttpResponse */ public function updateProfile(Account $account): HttpResponse { + error_log("Test"); return Control::runChecked([ "name" => [Validators::name()], "email" => [Validators::email()] @@ -40,6 +41,7 @@ class APIUserController { if (!empty($failures)) { //TODO find a system to handle Unauthorized error codes more easily from failures. + return new JsonHttpResponse($failures, HttpCodes::BAD_REQUEST); } diff --git a/src/App/Controller/AuthController.php b/src/App/Controller/AuthController.php index 7df241d..2ef9974 100644 --- a/src/App/Controller/AuthController.php +++ b/src/App/Controller/AuthController.php @@ -4,6 +4,7 @@ namespace IQBall\App\Controller; use IQBall\App\Session\MutableSessionHandle; use IQBall\App\ViewHttpResponse; +use IQBall\Core\Http\HttpCodes; use IQBall\Core\Http\HttpRequest; use IQBall\Core\Http\HttpResponse; use IQBall\Core\Model\AuthModel; @@ -84,4 +85,9 @@ class AuthController { return HttpResponse::redirect("/home"); } + public function updateAccount(MutableSessionHandle $session) { + $this->model->updateAccount($session, $session->getAccount()->getToken()); + return HttpResponse::fromCode(HttpCodes::OK); + } + } diff --git a/src/Core/Gateway/AccountGateway.php b/src/Core/Gateway/AccountGateway.php index 8d0b944..1d60780 100644 --- a/src/Core/Gateway/AccountGateway.php +++ b/src/Core/Gateway/AccountGateway.php @@ -94,4 +94,27 @@ 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; + + if (!empty($nameExist)) { + if ($name != $nameExist[0]["username"]) { + return true; + } + } + return false; + } + + public function changeName(string $email, string $newName) { + error_log($email); + + $this->con->exec(" + UPDATE Account + SET username = :username + WHERE email = :email + ", [ + ':username' => [$newName, PDO::PARAM_STR], + ':email' => [$email, PDO::PARAM_STR] + ]); + } } diff --git a/src/Core/Model/AuthModel.php b/src/Core/Model/AuthModel.php index 169b292..ef9b839 100644 --- a/src/Core/Model/AuthModel.php +++ b/src/Core/Model/AuthModel.php @@ -3,6 +3,7 @@ namespace IQBall\Core\Model; use Exception; +use IQBall\App\Session\MutableSessionHandle; use IQBall\Core\Data\Account; use IQBall\Core\Data\User; use IQBall\Core\Gateway\AccountGateway; @@ -52,7 +53,12 @@ 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 []; + } return [ValidationFail::unauthorized("Mail already exist")]; } $this->gateway->updateProfile($name, $email, $id); @@ -82,4 +88,8 @@ class AuthModel { } return $this->gateway->getAccountFromMail($email); } + + public function updateAccount(MutableSessionHandle $session, string $token) { + $session->setAccount($this->gateway->getAccountFromToken($token)); + } }