From e18df207e28aac9ef831a563d86cf5beddb17276 Mon Sep 17 00:00:00 2001 From: Pierre Date: Fri, 20 Apr 2018 08:35:43 +0200 Subject: [PATCH] Send a request over the server to update user password --- .../css/pages/settings/sections/password.css | 11 ++ assets/js/common/functionsSchema.js | 7 + assets/js/components/settings/interface.js | 17 ++- assets/js/pages/settings/sections/password.js | 125 ++++++++++++++++++ assets/js/pages/settings/sectionsList.js | 8 +- system/config/dev.config.php | 2 + 6 files changed, 168 insertions(+), 2 deletions(-) create mode 100644 assets/css/pages/settings/sections/password.css create mode 100644 assets/js/pages/settings/sections/password.js diff --git a/assets/css/pages/settings/sections/password.css b/assets/css/pages/settings/sections/password.css new file mode 100644 index 00000000..39a59a50 --- /dev/null +++ b/assets/css/pages/settings/sections/password.css @@ -0,0 +1,11 @@ +/** + * Settings password section stylesheet + * + * @author Pierre HUBERT + */ + +.box-password-settings .submit-form { + width: 150px; + margin: auto; + display: block; +} \ No newline at end of file diff --git a/assets/js/common/functionsSchema.js b/assets/js/common/functionsSchema.js index a2d16469..ecc9f346 100644 --- a/assets/js/common/functionsSchema.js +++ b/assets/js/common/functionsSchema.js @@ -1115,6 +1115,13 @@ var ComunicWeb = { security: { //TODO : implement }, + + /** + * Password section + */ + password: { + //TODO : implement + }, }, }, diff --git a/assets/js/components/settings/interface.js b/assets/js/components/settings/interface.js index 4d6de3be..8b25f424 100644 --- a/assets/js/components/settings/interface.js +++ b/assets/js/components/settings/interface.js @@ -68,6 +68,21 @@ ComunicWeb.components.settings.interface = { setSecurity: function(settings, callback){ var apiURI = "settings/set_security"; ComunicWeb.common.api.makeAPIrequest(apiURI, settings, true, callback); - } + }, + /** + * Update the password of the user + * + * @param {string} oldPassword The old password of the user + * @param {string} newPassword The new password + * @param {function} callback + */ + updatePassword: function(oldPassword, newPassword, callback){ + var apiURI = "settings/update_password"; + var params = { + oldPassword: oldPassword, + newPassword: newPassword + }; + ComunicWeb.common.api.makeAPIrequest(apiURI, params, true, callback); + }, } \ No newline at end of file diff --git a/assets/js/pages/settings/sections/password.js b/assets/js/pages/settings/sections/password.js new file mode 100644 index 00000000..58191e3a --- /dev/null +++ b/assets/js/pages/settings/sections/password.js @@ -0,0 +1,125 @@ +/** + * Password update section + * + * @author Pierre HUBERT + */ + +ComunicWeb.pages.settings.sections.password = { + + /** + * Open settings section + * + * @param {object} args Additionnal arguments + * @param {HTMLElement} target The target for the page + */ + open: function(args, target){ + + //Create a box + var box = createElem2({ + appendTo: target, + type: "div", + class: "box box-primary box-password-settings" + }); + + //Add box header + var boxHead = createElem2({ + appendTo: box, + type: "div", + class: "box-header", + }); + var boxTitle = createElem2({ + appendTo: boxHead, + type: "h3", + class: "box-title", + innerHTML: "Password update" + }); + + //Create box body + var boxBody = createElem2({ + appendTo: box, + type: "div", + class: "box-body" + }); + + //Create a form contener + var formContener = createElem2({ + appendTo: boxBody, + type: "div" + }); + + //Ask the user to enter its old password + var oldPasswordInput = createFormGroup({ + target: formContener, + label: "Old password", + placeholder: "Your old (current) password", + type: "password" + }); + + //Ask the user to enter its new password + var newPasswordInput = createFormGroup({ + target: formContener, + label: "New password", + placeholder: "Your new password", + type: "password" + }); + + //Ask the user to confirm its new password + var confirmNewPasswordInput = createFormGroup({ + target: formContener, + label: "Confirm your password", + placeholder: "Your new password", + type: "password" + }); + + //Add submit button + var sendButton = createElem2({ + appendTo: formContener, + type: "div", + class: "btn btn-primary submit-form", + innerHTML: "Update password" + }); + + //Make submit button lives + sendButton.onclick = function(){ + + //Check the old and the new password + if(!ComunicWeb.common.formChecker.checkInput(oldPasswordInput, true)){ + notify("Please specify your old password to update your password!", "danger"); + return; + } + + if(!ComunicWeb.common.formChecker.checkInput(newPasswordInput, true)){ + notify("Please specify a new password to update your password!", "danger"); + return; + } + + //Check if the old and the new password are the same or not + if(newPasswordInput.value !== confirmNewPasswordInput.value){ + notify("The new password and its confirmation are not the same!", "danger"); + return; + } + + //Perform a request over the server + sendButton.style.visibility = "hidden"; + + //Perform a request over the server + ComunicWeb.components.settings.interface.updatePassword(oldPasswordInput.value, newPasswordInput.value, function(result){ + + sendButton.style.visibility = "visible"; + + //Check for errors + if(result.error){ + notify("An error occurred while trying to udpate user password!", "danger"); + return; + } + + //Success + notify("Your password has been successfully updated !"); + + //Refresh current page to remove passwords (security) + ComunicWeb.common.page.refresh_current_page(); + }); + } + } + +}; \ No newline at end of file diff --git a/assets/js/pages/settings/sectionsList.js b/assets/js/pages/settings/sectionsList.js index e1324dbb..07d33560 100644 --- a/assets/js/pages/settings/sectionsList.js +++ b/assets/js/pages/settings/sectionsList.js @@ -22,5 +22,11 @@ ComunicWeb.pages.settings.sectionsList = { handler: "ComunicWeb.pages.settings.sections.security.open", }, - + /** + * Password settings + */ + password: { + title: "Password", + handler: "ComunicWeb.pages.settings.sections.password.open", + }, } \ No newline at end of file diff --git a/system/config/dev.config.php b/system/config/dev.config.php index 3e7408ad..68defdba 100644 --- a/system/config/dev.config.php +++ b/system/config/dev.config.php @@ -168,6 +168,7 @@ class Dev { //Sections sections "css/pages/settings/sections/general.css", "css/pages/settings/sections/security.css", + "css/pages/settings/sections/password.css", //Latest post page stylesheet "css/pages/latestPosts/main.css", @@ -334,6 +335,7 @@ class Dev { //Settings sections "js/pages/settings/sections/general.js", "js/pages/settings/sections/security.js", + "js/pages/settings/sections/password.js", //Login page "js/pages/login.js",