Add changing email fonctionnality
continuous-integration/drone/push Build is failing Details

d_yanis 1 year ago
parent afe3daa738
commit fd6fb02255

@ -84,9 +84,10 @@ function ProfilSettings({ user }: { user: User }) {
</Form.Group> </Form.Group>
<Form.Group className="mb-3" controlId="formEmail"> <Form.Group className="mb-3" controlId="formEmail">
<Form.Label className="content">Adresse mail</Form.Label> <Form.Label className="content">Adresse mail</Form.Label>
<Form.Control readOnly onClick={() => alert("En cours de développement...")} 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.Control readOnly onClick={() => alert("En cours de développement...")} 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, user)}>Mettre à jour</Button> <Button variant="outline-primary" type="button" onClick={() => updateAccountInfos(nameRef.current!.value, emailRef.current!.value)}>Mettre à jour</Button>
</Form> </Form>
</Col> </Col>
@ -114,8 +115,7 @@ 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

@ -57,5 +57,4 @@ class Connection {
public function prepare(string $query): \PDOStatement { public function prepare(string $query): \PDOStatement {
return $this->pdo->prepare($query); return $this->pdo->prepare($query);
} }
} }

@ -36,6 +36,14 @@ class AccountGateway {
return $this->con->fetch("SELECT * FROM Account WHERE email = :email", [':email' => [$email, PDO::PARAM_STR]])[0] ?? null; return $this->con->fetch("SELECT * FROM Account WHERE email = :email", [':email' => [$email, PDO::PARAM_STR]])[0] ?? null;
} }
/**
* @param int $id
* @return array<string, mixed>|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 * @param string $email
* @return string|null the hashed user's password, or null if the given mail does not exist * @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"])); 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 * @param string $token get an account from given token
* @return Account|null * @return Account|null
@ -94,8 +115,8 @@ class AccountGateway {
]); ]);
} }
public function nameIsDifferent(string $email, string $name) : bool { public function nameIsDifferent(int $id, string $name) : bool {
$nameExist = $this->con->fetch("SELECT username FROM Account WHERE email = :email", [':email' => [$email, PDO::PARAM_STR]]) ?? null; $nameExist = $this->con->fetch("SELECT username FROM Account WHERE id = :id", [':id' => [$id, PDO::PARAM_STR]]) ?? null;
if (!empty($nameExist)) { if (!empty($nameExist)) {
if ($name != $nameExist[0]["username"]) { if ($name != $nameExist[0]["username"]) {
@ -105,16 +126,41 @@ class AccountGateway {
return false; return false;
} }
public function changeName(string $email, string $newName) { public function emailIsDifferent(int $id, string $email) : bool {
error_log($email); $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(" $this->con->exec("
UPDATE Account UPDATE Account
SET username = :username SET username = :username
WHERE email = :email WHERE id = :id
", [ ", [
':username' => [$newName, PDO::PARAM_STR], ':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]
]); ]);
} }
} }

@ -53,16 +53,21 @@ 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->getAccountFromId($id))) {
if(!empty($this->gateway->getAccountFromMail($email))) { if ($this->gateway->nameIsDifferent($id, $name)) {
if ($this->gateway->nameIsDifferent($email, $name)) { $this->gateway->changeName($id, $name);
$this->gateway->changeName($email, $name);
return [];
} }
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 [ValidationFail::error("Account doesn't exist")];
return [];
} }
/** /**

Loading…
Cancel
Save