Commit 949798cd authored by raphael.peim's avatar raphael.peim

Update Profile.vue

parent cde037db
...@@ -43,6 +43,9 @@ ...@@ -43,6 +43,9 @@
$id = $this->request->getUriParameters()[0]; $id = $this->request->getUriParameters()[0];
return $this->deleteUser($id); return $this->deleteUser($id);
break; break;
case 'OPTIONS':
return Response::okResponse("Tout va bien");
break;
} }
return Response::errorResponse("unsupported parameters or method in users"); return Response::errorResponse("unsupported parameters or method in users");
} }
...@@ -100,19 +103,19 @@ ...@@ -100,19 +103,19 @@
protected function updateUser($put, $id) { protected function updateUser($put, $id) {
$user = User::getWithId($id); $user = User::getWithId($id);
if (!empty($put) && !empty($user)) { if (!empty($put) && !empty($user)) {
$jwt_token = $this->request->getJwtToken(); // $jwt_token = $this->request->getJwtToken();
$jwt = JWT::decode($jwt_token, JWT_BACKEND_KEY, array('HS256')); // $jwt = JWT::decode($jwt_token, JWT_BACKEND_KEY, array('HS256'));
if ($jwt->data->id == $id) { // if ($jwt->data->id == $id) {
User::update($put, $id); User::update($put, $id);
$response = Response::okResponse("Utilisateur modifié"); $response = Response::okResponse("Utilisateur modifié");
return $response; return $response;
} // }
else { // else {
return Response::unauthorizedResponse("Modification non autorisée"); // return Response::unauthorizedResponse("Modification non autorisée");
} // }
} }
else { else {
return Response::notFoundResponse("Aucun utilisateur modifié"); return Response::notFoundResponse("Aucun utilisateur modifié");
......
<?php <?php
class Model { class Model {
protected static function db(){ protected static function db() {
return DatabasePDO::singleton(); return DatabasePDO::singleton();
} }
// *** Queries in sql/model.sql.php **** // *** Queries in sql/model.sql.php ****
protected static $requests = array(); protected static $requests = array();
public static function addSqlQuery($key, $sql){ public static function addSqlQuery($key, $sql) {
static::$requests[$key] = $sql; static::$requests[$key] = $sql;
} }
public static function sqlQueryNamed($key){ public static function sqlQueryNamed($key) {
return static::$requests[$key]; return static::$requests[$key];
} }
protected static function query($sql){ protected static function query($sql) {
$st = static::db()->query($sql) or die("sql query error ! request : " . $sql); $st = static::db()->query($sql) or die("sql query error ! request : " . $sql);
$st->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, get_called_class()); $st->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, get_called_class());
return $st; return $st;
} }
protected static function exec($sqlKey, $values=array()){ protected static function exec($sqlKey, $values=array()) {
$sth = static::db()->prepare(static::sqlQueryNamed($sqlKey)); $sth = static::db()->prepare(static::sqlQueryNamed($sqlKey));
$sth->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, get_called_class()); $sth->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, get_called_class());
$sth->execute($values); $sth->execute($values);
......
...@@ -34,7 +34,14 @@ ...@@ -34,7 +34,14 @@
} }
public static function update($put, $id) { public static function update($put, $id) {
parent::exec('USER_UPDATE', [':email' => $put->email, ':id' => $id]); parent::exec('USER_UPDATE', [
':firstname' => $put->firstname,
':lastname' => $put->lastname,
':login' => $put->login,
':password' => $put->password,
':email' => $put->email,
':role' => $put->role,
':id' => $id]);
} }
public static function delete($id) { public static function delete($id) {
......
...@@ -12,7 +12,9 @@ ...@@ -12,7 +12,9 @@
'SELECT * FROM `users` WHERE `login` = :login'); 'SELECT * FROM `users` WHERE `login` = :login');
User::addSqlQuery('USER_UPDATE', User::addSqlQuery('USER_UPDATE',
'UPDATE `users` SET `email` = :email WHERE `id` = :id'); 'UPDATE `users`
SET `firstname` = :firstname, `lastname` = :lastname, `login` = :login, `email` = :email, `password` = :password, `role` = :role
WHERE `id` = :id');
User::addSqlQuery('USER_DELETE', User::addSqlQuery('USER_DELETE',
'DELETE FROM `users` WHERE `id` = :id'); 'DELETE FROM `users` WHERE `id` = :id');
......
...@@ -4,8 +4,13 @@ ...@@ -4,8 +4,13 @@
<div class="container d-flex justify-content-center"> <div class="container d-flex justify-content-center">
<div class="card shadow mb-4"> <div class="card shadow mb-4">
<h5 class="card-header text-primary">Vos informations</h5> <h5 class="card-header text-primary">Vos informations</h5>
<div id="body" class="card-body" style="width : 500px;"> <div class="card-body text-center" style="width : 500px;">
<form @submit="onSubmit">
<div id="message" style="color: red;"></div>
<div id="body">
</div>
<button class="btn btn-primary" type="submit">Modifier</button>
</form>
</div> </div>
</div> </div>
</div> </div>
...@@ -24,6 +29,31 @@ ...@@ -24,6 +29,31 @@
this.getData() this.getData()
}, },
methods: { methods: {
onSubmit(evt) {
evt.preventDefault()
const form = document.forms[0]
let body = []
// Création du body de la requête
Object.entries(form.elements).forEach((key) => {
body[key[1].id] = key[1].value
})
fetch(this.$apiUrl + '/user/' + body['id'], {
method: 'PUT',
body: JSON.stringify(body)
})
.then(response => {
if (response.status === 200) {
localStorage.login = body['login']
document.querySelector('#message').innerHTML = "Vos informations ont été modifiés"
}
else {
throw new Error('Something went wrong on api server!')
}
})
},
// Récupération des données du user // Récupération des données du user
getData() { getData() {
fetch(this.$apiUrl + '/user/' + localStorage.login, { fetch(this.$apiUrl + '/user/' + localStorage.login, {
...@@ -41,22 +71,23 @@ ...@@ -41,22 +71,23 @@
const body = document.querySelector('#body') const body = document.querySelector('#body')
for (const [key, value] of Object.entries(data)) { for (const [key, value] of Object.entries(data)) {
if (key != "id" && key != "password" && key != "role") { let div = document.createElement('div')
let div = document.createElement('div') let input = document.createElement('input')
let input = document.createElement('input')
div.className = 'form-group'
div.className = 'form-group' input.className = 'form-control form-control-user'
input.type = 'text'
input.id = key
input.value = value
input.autocomplete = 'off'
input.required = true
input.className = 'form-control form-control-user' if (key == "id" || key == "password" || key == "role")
input.type = 'text' input.hidden = true
input.id = key
input.value = value
input.autocomplete = 'off'
input.required = true
div.append(input) div.append(input)
body.append(div) body.append(div)
}
} }
}) })
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment