Change name is available in settings page
continuous-integration/drone/push Build is failing Details

d_yanis 1 year ago
parent 7ab9b346b6
commit 930c0cfb3f

@ -14,3 +14,18 @@ export function fetchAPI(
body: JSON.stringify(payload), body: JSON.stringify(payload),
}) })
} }
export function fetchPOST(
url: string,
payload: unknown,
method = "POST",
): Promise<Response> {
return fetch(`/${url}`, {
method,
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
})
}

@ -9,6 +9,7 @@ import Form from 'react-bootstrap/Form';
import Image from 'react-bootstrap/Image'; import Image from 'react-bootstrap/Image';
import { updateSourceFile } from "typescript"; import { updateSourceFile } from "typescript";
import { fetchAPI } from "../Fetcher"; import { fetchAPI } from "../Fetcher";
import { fetchPOST } from "../Fetcher";
export default function Settings({user} : {user : User}){ export default function Settings({user} : {user : User}){
@ -81,7 +82,7 @@ function ProfilSettings({user} : {user : User}) {
<Form.Label className="content">Adresse mail</Form.Label> <Form.Label className="content">Adresse mail</Form.Label>
<Form.Control ref={emailRef} id="control" size="sm" defaultValue={user.email} type="email" placeholder="Password" /> <Form.Control ref={emailRef} id="control" size="sm" defaultValue={user.email} type="email" placeholder="Password" />
</Form.Group> </Form.Group>
<Button variant="outline-primary" type="button" onClick={() => updateAccountInfos(nameRef.current!.value, emailRef.current!.value)}>Mettre à jour</Button> <Button variant="outline-primary" type="button" onClick={() => updateAccountInfos(nameRef.current!.value, emailRef.current!.value, user)}>Mettre à jour</Button>
</Form> </Form>
</div> </div>
</div> </div>
@ -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", { fetchAPI("account/update/profile", {
name : name, name : name,
email : email email : email
}); });
fetchPOST("account/update", {});
location.reload();
} }
// function InputSettings(){
// return(
// <div className="input-settings">
// <p className="title-input-settings">{title}</p>
// </div>
// )
// }

@ -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: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("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", "/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; return $ar;
} }

@ -31,6 +31,7 @@ class APIUserController {
* @return HttpResponse * @return HttpResponse
*/ */
public function updateProfile(Account $account): HttpResponse { public function updateProfile(Account $account): HttpResponse {
error_log("Test");
return Control::runChecked([ return Control::runChecked([
"name" => [Validators::name()], "name" => [Validators::name()],
"email" => [Validators::email()] "email" => [Validators::email()]
@ -40,6 +41,7 @@ class APIUserController {
if (!empty($failures)) { if (!empty($failures)) {
//TODO find a system to handle Unauthorized error codes more easily from failures. //TODO find a system to handle Unauthorized error codes more easily from failures.
return new JsonHttpResponse($failures, HttpCodes::BAD_REQUEST); return new JsonHttpResponse($failures, HttpCodes::BAD_REQUEST);
} }

@ -4,6 +4,7 @@ namespace IQBall\App\Controller;
use IQBall\App\Session\MutableSessionHandle; use IQBall\App\Session\MutableSessionHandle;
use IQBall\App\ViewHttpResponse; use IQBall\App\ViewHttpResponse;
use IQBall\Core\Http\HttpCodes;
use IQBall\Core\Http\HttpRequest; use IQBall\Core\Http\HttpRequest;
use IQBall\Core\Http\HttpResponse; use IQBall\Core\Http\HttpResponse;
use IQBall\Core\Model\AuthModel; use IQBall\Core\Model\AuthModel;
@ -84,4 +85,9 @@ class AuthController {
return HttpResponse::redirect("/home"); return HttpResponse::redirect("/home");
} }
public function updateAccount(MutableSessionHandle $session) {
$this->model->updateAccount($session, $session->getAccount()->getToken());
return HttpResponse::fromCode(HttpCodes::OK);
}
} }

@ -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]
]);
}
} }

@ -3,6 +3,7 @@
namespace IQBall\Core\Model; namespace IQBall\Core\Model;
use Exception; use Exception;
use IQBall\App\Session\MutableSessionHandle;
use IQBall\Core\Data\Account; use IQBall\Core\Data\Account;
use IQBall\Core\Data\User; use IQBall\Core\Data\User;
use IQBall\Core\Gateway\AccountGateway; use IQBall\Core\Gateway\AccountGateway;
@ -52,7 +53,12 @@ class AuthModel {
} }
public function updateProfile(string $name, string $email, int $id) : array { public function updateProfile(string $name, string $email, int $id) : array {
if(!empty($this->gateway->getAccountFromMail($email))) { if(!empty($this->gateway->getAccountFromMail($email))) {
if ($this->gateway->nameIsDifferent($email, $name)) {
$this->gateway->changeName($email, $name);
return [];
}
return [ValidationFail::unauthorized("Mail already exist")]; return [ValidationFail::unauthorized("Mail already exist")];
} }
$this->gateway->updateProfile($name, $email, $id); $this->gateway->updateProfile($name, $email, $id);
@ -82,4 +88,8 @@ class AuthModel {
} }
return $this->gateway->getAccountFromMail($email); return $this->gateway->getAccountFromMail($email);
} }
public function updateAccount(MutableSessionHandle $session, string $token) {
$session->setAccount($this->gateway->getAccountFromToken($token));
}
} }

Loading…
Cancel
Save