From 3fceb49e47c845f624b9defab861bb4ba046a60b Mon Sep 17 00:00:00 2001 From: Krehan Tim Date: Fri, 9 Nov 2018 15:16:36 +0100 Subject: [PATCH] Added Password Change and implemented user change in php class --- bin/settings.js | 41 +++++++++++++++++++++++++++++++++++++++++ cont/settings.php | 11 ++++++----- index.php | 2 ++ php/classes.user.php | 25 +++++++++++++++++++++++-- php/edit-user.php | 16 ++++++++++++++++ 5 files changed, 88 insertions(+), 7 deletions(-) create mode 100644 php/edit-user.php diff --git a/bin/settings.js b/bin/settings.js index 0b4768c..4693a12 100644 --- a/bin/settings.js +++ b/bin/settings.js @@ -10,6 +10,47 @@ function downloadObjectAsJson(exportObj, exportName){ $(document).ready(function(){ $("#username-input").focus(function(){$(this).css("color", "black");}); $("#mail-input").focus(function(){$(this).css("color", "black");}); + + // change password + $("#old-password-input").focus(function(){$(this).css("color", "black");}); + $("#new-password-input").focus(function(){$(this).css("color", "black");}); + $("#check-password-input").focus(function(){$(this).css("color", "black");}); + $(".password-input").on("input", function(){ + if( + (($("#old-password-input").val()).length>0) && + (($("#new-password-input").val()).length>0) && + (($("#check-password-input").val()).length>0) && + ($("#new-password-input").val()==$("#check-password-input").val()) + ){ + $("#passwordSaveButton").prop("disabled", false); + $("#passwordSaveButton").removeClass("button-disabled"); + } + else{ + $("#passwordSaveButton").prop("disabled", true); + $("#passwordSaveButton").addClass("button-disabled"); + } + }); + $("#passwordSaveButton").click(function(){ + $.post("/php/edit-user.php", + { + function: "change-pw", + current: $("#old-password-input").val(), + new: $("#new-password-input").val() + }, + function(data){ + if(data==0){ + $("#old-password-input").val(""); + $("#new-password-input").val(""); + $("#check-password-input").val(""); + infoPopUp("Passwort erfolgreich geändert!"); + } + else { + infoPopUp("Altes Passwort Falsch!"); + } + } + ); + }); + $("#export-recipe-button").click(function(){ $.post("/php/edit-recipes.php", {function:"export"}, function(data){ downloadObjectAsJson(JSON.parse(data), "recipes"); diff --git a/cont/settings.php b/cont/settings.php index bf6f020..8e108e6 100644 --- a/cont/settings.php +++ b/cont/settings.php @@ -3,7 +3,8 @@

Settings

get_info($_COOKIE["token"]); ?>

User

@@ -17,11 +18,11 @@
- Altes Passwort - Neues Passwort - Passwort bestätigen + Altes Passwort + Neues Passwort + Passwort bestätigen
- +

Import / Export

diff --git a/index.php b/index.php index 6cfadee..eec069c 100644 --- a/index.php +++ b/index.php @@ -23,6 +23,7 @@ + Einkaufsliste @@ -80,5 +81,6 @@ echo "
"; if($site && ($site!="login")){include $_SESSION["docroot"].'/cont/nav.php';} ?> +
diff --git a/php/classes.user.php b/php/classes.user.php index ac6cd03..cd89b33 100644 --- a/php/classes.user.php +++ b/php/classes.user.php @@ -1,15 +1,36 @@ query($query); $user = $result->fetch_assoc(); $this->uid = $user["uid"]; $this->username = $user["username"]; $this->email = $user["email"]; $this->last_login = $user["last_login"]; + $this->salt = $user["salt"]; + $mysqli->close(); + } + + function change_password($current, $new){ + include $_SESSION["docroot"].'/php/hash.php'; + include $_SESSION["docroot"].'/php/connect.php'; + $current_pwhash = hash_password($current, $this->salt); + $query = "SELECT `uid` FROM `users` WHERE `uid` = $this->uid AND `password` = '$current_pwhash'"; + $result = $mysqli->query($query); + if($result->num_rows===1){ + $new_pwdhash = hash_password($new, $this->salt); + $mysqli->query("UPDATE `users` SET `password` = '$new_pwdhash' WHERE `users`.`uid` = $this->uid;"); + $mysqli->close(); + print_r("0"); + } + else{ + print_r("1"); + } } } ?> diff --git a/php/edit-user.php b/php/edit-user.php new file mode 100644 index 0000000..48c4f5f --- /dev/null +++ b/php/edit-user.php @@ -0,0 +1,16 @@ +get_info($_COOKIE["token"]); + + switch ($_POST["function"]) { + case 'change-pw': + $user->change_password($_POST["current"], $_POST["new"]); + break; + + default: + // code... + break; + } +?>