From 19797b8731969a33f4ed8479371ed47308c93e14 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sat, 26 May 2018 15:50:33 +0200 Subject: [PATCH] Offer the user to reset his password --- assets/css/pages/resetPassword/main.css | 15 ++ assets/js/common/functionsSchema.js | 14 ++ assets/js/components/account/interface.js | 29 ++++ assets/js/pages/resetPassword/main.js | 190 ++++++++++++++++++++++ assets/js/pagesList.js | 9 + system/config/dev.config.php | 8 +- 6 files changed, 264 insertions(+), 1 deletion(-) create mode 100644 assets/css/pages/resetPassword/main.css create mode 100644 assets/js/pages/resetPassword/main.js diff --git a/assets/css/pages/resetPassword/main.css b/assets/css/pages/resetPassword/main.css new file mode 100644 index 00000000..615a3e6d --- /dev/null +++ b/assets/css/pages/resetPassword/main.css @@ -0,0 +1,15 @@ +/** + * Reset password main script + * + * @author Pierre HUBERT + */ + +.reset-password-box { + max-width: 300px; + margin: auto; + margin-top: 30px; +} + +.reset-password-box .btn { + display: block; +} \ No newline at end of file diff --git a/assets/js/common/functionsSchema.js b/assets/js/common/functionsSchema.js index 9a947191..d9ae969c 100644 --- a/assets/js/common/functionsSchema.js +++ b/assets/js/common/functionsSchema.js @@ -1315,6 +1315,20 @@ var ComunicWeb = { }, + /** + * Password reset page + */ + resetPassword: { + + /** + * Main script + */ + main: { + //TODO : implement + }, + + }, + /** * Logout controller */ diff --git a/assets/js/components/account/interface.js b/assets/js/components/account/interface.js index 93799c4a..32413377 100644 --- a/assets/js/components/account/interface.js +++ b/assets/js/components/account/interface.js @@ -104,6 +104,35 @@ ComunicWeb.components.account.interface = { }, + /** + * Check the validity of a reset password token + * + * @param {String} token The token to check + * @param {Function} callback + */ + checkPasswordResetToken: function(token, callback){ + var apiURI = "account/check_password_reset_token"; + var params = { + token: token + }; + ComunicWeb.common.api.makeAPIrequest(apiURI, params, false, callback); + }, + + /** + * Reset user password + * + * @param {String} token The token to check + * @param {String} passwd The new password for the user + * @param {Function} callback + */ + resetUserPassword: function(token, passwd, callback){ + var apiURI = "account/reset_user_passwd"; + var params = { + token: token, + password: passwd + }; + ComunicWeb.common.api.makeAPIrequest(apiURI, params, false, callback); + }, /** * Request the export of all the data of the user diff --git a/assets/js/pages/resetPassword/main.js b/assets/js/pages/resetPassword/main.js new file mode 100644 index 00000000..2bf52d4b --- /dev/null +++ b/assets/js/pages/resetPassword/main.js @@ -0,0 +1,190 @@ +/** + * Reset password page main script + * + * @author Pierre HUBERT + */ +ComunicWeb.pages.resetPassword.main = { + + /** + * Open page + * + * @param {Object} args Additionnal data passed in the method + * @param {element} target Where the page will be applied + */ + open: function(args, target){ + + //Create page box + var pageBox = createElem2({ + appendTo: target, + type: "div", + class: "box box-primary reset-password-box" + }); + + //Box header + var boxHeader = createElem2({ + appendTo: pageBox, + type: "div", + class: "box-header" + }); + + //Box title + createElem2({ + appendTo: boxHeader, + type: "h3", + class: "box-title", + innerHTML: "Reset password" + }); + + //Box body + var boxBody = createElem2({ + appendTo: pageBox, + type: "div", + class: "box-body" + }); + + //Create messages target + var messagesTarget = createElem2({ + appendTo: boxBody, + type: "div" + }); + + //Add loading message + messagesTarget.appendChild(ComunicWeb.common.messages.createCalloutElem( + "Loading", + "Please wait while we retrieve a few information...", + "info")); + + //Get the token (specified after the "#") + var token = document.location.toString().split("#")[1]; + + //Check the token + if(token == null){ + emptyElem(messagesTarget); + messagesTarget.appendChild(ComunicWeb.common.messages.createCalloutElem( + "Error", + "It seems you followed an invalid link...", + "danger" + )); + return; + } + + //Check the token's validity on the server + ComunicWeb.components.account.interface.checkPasswordResetToken(token, function(result){ + + emptyElem(messagesTarget); + + //Check for errors + if(result.error){ + messagesTarget.appendChild(ComunicWeb.common.messages.createCalloutElem( + "Error", + "Your reset password request timed out, or is invalid. Please try again to reset your password...", + "danger" + )); + } + + //Display reset password form + ComunicWeb.pages.resetPassword.main.displayResetPasswordForm(boxBody, messagesTarget, token); + }); + }, + + /** + * Display reset password form + * + * @param {HTMLElement} target The target for the form + * @param {HTMLElement} messagesTarget The target for the messages + * @param {String} token The reset token of the user + */ + displayResetPasswordForm: function(target, messagesTarget, token){ + + //Create form container + var formTarget = createElem2({ + appendTo: target, + type: "div" + }); + + //Message + add_p(formTarget, "Please enter now your new password."); + + //Prompt for new password + var passwordInput = createFormGroup({ + target: formTarget, + type: "password", + label: "Your new password", + placeholder: "Your new password" + }); + + //Ask password confirmation + var confirmPasswordInput = createFormGroup({ + target: formTarget, + type: "password", + label: "Confirm your password", + placeholder: "Confirm your password" + }); + + //Submit button + var submit = createElem2({ + appendTo: formTarget, + type: "div", + class: "btn btn-primary", + innerHTML: "Submit" + }); + + //Make submit button lives + submit.addEventListener("click", function(e){ + + //Empty messages target + emptyElem(messagesTarget); + + //Check password validity + if(!ComunicWeb.common.formChecker.checkInput(passwordInput, true)) + return notify("Please specify a valid password!", "danger"); + + //Check password confirmation + if(passwordInput.value != confirmPasswordInput.value) + return notify("Password and its confirmation do not mach !", "danger"); + + submit.style.display = "none"; + + //Send a request to the server + ComunicWeb.components.account.interface.resetUserPassword(token, passwordInput.value, function(result){ + + //Make submit button visible + submit.style.display = "block"; + + //Check for errors + if(result.error){ + messagesTarget.appendChild(ComunicWeb.common.messages.createCalloutElem( + "Error", + "The server rejected your request, please refresh the page and try again...", + "danger" + )); + return; + } + + //Success + //Remove the form + emptyElem(formTarget); + + //Success message + messagesTarget.appendChild(ComunicWeb.common.messages.createCalloutElem( + "Success", + "Your password has been successfully changed !", + "success" + )); + + //Add a button to go to login page + var goLogin = createElem2({ + appendTo: formTarget, + type: "div", + class: "btn btn-primary", + innerHTML: "Go login" + }); + + goLogin.addEventListener("click", function(e){ + openPage("login"); + }); + }); + + }); + } +} \ No newline at end of file diff --git a/assets/js/pagesList.js b/assets/js/pagesList.js index 9d9312ed..0c7b16cd 100644 --- a/assets/js/pagesList.js +++ b/assets/js/pagesList.js @@ -105,6 +105,15 @@ ComunicWeb.pagesList = { disableMenus: false, }, + /** + * Password reset page + */ + reset_password: { + pageTitle: "Reset password", + methodHandler: "ComunicWeb.pages.resetPassword.main.open", + disableMenus: false + }, + /** * 404 Page not found */ diff --git a/system/config/dev.config.php b/system/config/dev.config.php index 0eacf684..3fef473e 100644 --- a/system/config/dev.config.php +++ b/system/config/dev.config.php @@ -236,7 +236,10 @@ class Dev { //Password forgotten page "css/pages/passwordForgotten/main.css", "css/pages/passwordForgotten/promptOption.css", - "css/pages/passwordForgotten/mailAdmin.css" + "css/pages/passwordForgotten/mailAdmin.css", + + //Password reset page + "css/pages/resetPassword/main.css" ); /** @@ -427,6 +430,9 @@ class Dev { "js/pages/passwordForgotten/mailAdmin.js", "js/pages/passwordForgotten/promptSecurityQuestions.js", + //Reset password page + "js/pages/resetPassword/main.js", + //Logout page "js/pages/logout.js",