From e07991174d37d71112740bf4a908248e6a6c4439 Mon Sep 17 00:00:00 2001 From: dilanua Date: Fri, 12 Aug 2016 17:57:28 +0530 Subject: [PATCH] Adding user password change UI --- .../public/js/user-menu.js | 82 +++++++++++++++++++ .../user-menu.hbs | 66 ++++++++++++++- .../user-menu.js | 4 + 3 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.header.user-menu/public/js/user-menu.js create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.header.user-menu/user-menu.js diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.header.user-menu/public/js/user-menu.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.header.user-menu/public/js/user-menu.js new file mode 100644 index 0000000000..fe168cc0e0 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.header.user-menu/public/js/user-menu.js @@ -0,0 +1,82 @@ +/** + * Checks if provided input is valid against RegEx input. + * + * @param regExp Regular expression + * @param inputString Input string to check + * @returns {boolean} Returns true if input matches RegEx + */ +function inputIsValid(regExp, inputString) { + return regExp.test(inputString); +} + +$(document).ready(function () { + var modalPopup = ".wr-modalpopup"; + // var modalPopupContainer = modalPopup + " .modalpopup-container"; + var modalPopupContent = modalPopup + " .modalpopup-content"; + + $("#change-password").click(function () { + + $(modalPopupContent).html($('#change-password-window').html()); + showPopup(); + + $("a#change-password-yes-link").click(function () { + var oldPassword = $("#old-password").val(); + var newPassword = $("#new-password").val(); + var confirmedPassword = $("#confirmed-password").val(); + var user = $("#user").val(); + + var errorMsgWrapper = "#notification-error-msg"; + var errorMsg = "#notification-error-msg span"; + if (!oldPassword) { + $(errorMsg).text("Old password is a required field. It cannot be empty."); + $(errorMsgWrapper).removeClass("hidden"); + } else if (!newPassword) { + $(errorMsg).text("New password is a required field. It cannot be empty."); + $(errorMsgWrapper).removeClass("hidden"); + } else if (!confirmedPassword) { + $(errorMsg).text("Retyping the new password is required."); + $(errorMsgWrapper).removeClass("hidden"); + } else if (confirmedPassword != newPassword) { + $(errorMsg).text("New password doesn't match the confirmation."); + $(errorMsgWrapper).removeClass("hidden"); + } else if (!inputIsValid(/^[\S]{5,30}$/, confirmedPassword)) { + $(errorMsg).text("Password should be minimum 5 characters long, should not include any whitespaces."); + $(errorMsgWrapper).removeClass("hidden"); + } else { + var changePasswordFormData = {}; + //changePasswordFormData.username = user; + changePasswordFormData.newPassword = unescape((confirmedPassword)); + changePasswordFormData.oldPassword = unescape((oldPassword)); + + + var changePasswordAPI = "/api/device-mgt/v1.0/users" + user + "/credentials"; + + invokerUtil.put( + changePasswordAPI, + changePasswordFormData, + function (data, textStatus, jqXHR) { + if (jqXHR.status == 200 && data) { + $(modalPopupContent).html($('#change-password-success-content').html()); + $("#change-password-success-link").click(function () { + hidePopup(); + }); + } + }, function (jqXHR) { + if (jqXHR.status == 400) { + $(errorMsg).text("Old password does not match with the provided value."); + $(errorMsgWrapper).removeClass("hidden"); + } else { + $(errorMsg).text("An unexpected error occurred. Please try again later."); + $(errorMsgWrapper).removeClass("hidden"); + } + } + ); + } + + }); + + $("a#change-password-cancel-link").click(function () { + hidePopup(); + }); + }); +}); \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.header.user-menu/user-menu.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.header.user-menu/user-menu.hbs index 5fc7ac3bca..21b7c28f3e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.header.user-menu/user-menu.hbs +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.header.user-menu/user-menu.hbs @@ -17,6 +17,70 @@ }} {{#zone "userMenu-items"}}
  • - Logout + Change password
  • +
  • + Sign out +
  • + +
    + +
    +
    +
    +

    + + + + + Change Password +

    +

    + +

    + Enter old password * +

    +
    + +
    +
    + Enter new password * +

    +
    + +
    +
    + Retype new password * +

    +
    + +
    +
    +

    +
    + Update + Cancel +
    +
    +
    +
    +
    + +
    +
    +
    +
    +

    Password change is successful.

    +
    + Ok +
    +
    +
    +
    +
    +{{/zone}} +{{#zone "bottomJs"}} + {{/zone}} \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.header.user-menu/user-menu.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.header.user-menu/user-menu.js new file mode 100644 index 0000000000..e0ebe26b0a --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.header.user-menu/user-menu.js @@ -0,0 +1,4 @@ +function onRequest() { + var constants = require("/app/modules/constants.js"); + return session.get(constants["USER_SESSION_KEY"]); +}