GUI Code refactor
[vnfsdk/refrepo.git] / openo-portal / portal-auth / src / main / webapp / user / js / changePassword.js
index f499bfc..068ea56 100644 (file)
-/*
- * Copyright 2016 Huawei Technologies Co., Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-$(document).ready(function() {
-    var USER_SERVICE = "/openoapi/auth/v1/users";
-    var userId;
-    var $userName = $("#userName");
-    var $oldPassword = $("#oldPassword");
-    var $oldPasswordError = $("#oldPasswordError");
-    var $password = $("#password");
-    var $passwordError = $("#passwordError")
-
-    var $cfPsdError = $("#cfPsdError");
-
-    function initialPage() {
-        userId = getId();
-        getUserDetails(userId).done(function(data) {
-            listUserDetails(data);
-        });
-
-        /*initial the event*/
-        $("#confirm").click(function(e) {
-            if (!checkPassword()) {
-                return;
-            }
-            var data = getModifyUser();
-            changePsd(data).done(function() {
-                top.bootbox.alert("Password change successfully.", function(e) {
-                    Rest.turn2URI("/openoui/user/user.html");
-                })
-            }).fail(function(e) {
-                if (e.statusText == "Unauthorized") {
-                    showError($oldPasswordError, "The old password is wrong.");
-                } else {
-                    showError($oldPasswordError, e.statusText);
-                }
-            })
-        })
-        $("#cancel").click(function(e) {
-            window.document.location = "/openoui/user/user.html";
-        })
-    }
-
-    function checkPassword() {
-        if (!checkMandatory()) {
-            return false;
-        }
-
-        if (!checkCfPassword()) {
-            showError($cfPsdError, "The password is not the same.");
-            return false;
-        }
-
-        if (!checkPasswordRule()) {
-            return false;
-        }
-        return true;
-    }
-
-    function checkMandatory() {
-        if ($password.val() == "") {
-            showError($passwordError, "Mandatory.");
-            return false;
-        }
-
-        if ($oldPassword.val() == "") {
-            showError($oldPasswordError, "Mandatory.");
-            return false;
-        }
-        return true;
-    }
-
-    function checkCfPassword() {
-        return $("#password").val() == $("#cfPassword").val();
-    }
-
-    function checkPasswordRule() {
-        var password = $password.val();
-
-        if (!checkLength(8, 32, password)) {
-            showError($passwordError, "The password length should between 8 and 32.");
-            return false
-        }
-
-        if (!checkCotainSpecial(password)) {
-            showError($passwordError, "At least contain: one uppercase letter, one lowercase letter, and one digit, one special character;");
-            return false
-        }
-
-        if (!checkNoContainAndReverse(password, $userName.val())) {
-            showError($passwordError, "The password should not contain the user name or reverse.");
-            return false
-        }
-
-        if (!checkNoSpace(password)) {
-            showError($passwordError, "The password should not contain space.");
-            return false
-        }
-        return true
-    }
-
-    function checkLength(min, max, str) {
-        return str.length >= min && str.length <= max;
-    }
-
-    function checkOnlySpecials(str, reg) {
-        return str.match(reg) && str.match(reg).length == str.length
-    }
-
-    function checkCotainSpecial(password) {
-        return password.match(/\~|\`|\@|\#|\$|\%|\^|\&|\*|\-|\_|\=|\+|\||\?|\/|\(|\)|\<|\>|\[|\]|\{|\}|\"|\,|\.|\;|\'|\!/g) != null 
-                && password.match(/[0-9]/g) != null && password.match(/[a-z]/g) != null && password.match(/[A-Z]/g) != null;
-    }
-
-    function checkUderScore(str) {
-        return str.indexOf("_") != 0 && str.lastIndexOf("_") != str.length - 1;
-    }
-
-    function checkNoSpace(str) {
-        return str.indexOf(" ") == -1;
-    }
-
-    function checkNoContainAndReverse(str, str2) {
-        return str.indexOf(str2) == -1 && str.indexOf(str2.split("").reverse().join("")) == -1;
-    }
-
-    function getModifyUser() {
-        var data = {};
-        data["original_password"] = $("#oldPassword").val();
-        data.password = $("#password").val();
-        return data;
-    }
-
-    function getUserDetails(id) {
-        return Rest.http({
-            url: USER_SERVICE + "/" + id + "?=" + new Date().getTime(),
-            type: "GET",
-            async: false,
-            contentType: 'application/json',
-            dataType: "json"
-        })
-    }
-
-    function listUserDetails(data) {
-        $("#userName").val(data.name);
-    }
-
-
-    function changePsd(data) {
-        return Rest.http({
-            url: USER_SERVICE + "/" + userId + "/password" + "?=" + new Date().getTime(),
-            type: "POST",
-            async: false,
-            contentType: 'application/json',
-            dataType: "json",
-            data: JSON.stringify(data)
-        })
-    }
-
-    function getId() {
-        var qs = location.search;
-        qs = qs.indexOf("?") === 0 ? qs : ("?" + qs);
-        var start = qs.indexOf("id=") + 3;
-        var end = qs.indexOf("&") === -1 ? qs.length : qs.indexOf("&") - start;
-        return qs.substr(start, end);
-    }
-
-    function showError($Obj, message) {
-        $Obj.text(message);
-        $Obj.css("visibility", "visible");
-        setTimeout(function() {
-            hideError($Obj);
-        }, 5000)
-    }
-
-    function hideError($Obj) {
-        $Obj.css("visibility", "hidden");
-    }
-
-    initialPage();
-})
+/*\r
+ * Copyright 2016 Huawei Technologies Co., Ltd.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+$(document).ready(function() {\r
+    var USER_SERVICE = "/openoapi/auth/v1/users";\r
+    var userId;\r
+    var $userName = $("#userName");\r
+    var $oldPassword = $("#oldPassword");\r
+    var $oldPasswordError = $("#oldPasswordError");\r
+    var $password = $("#password");\r
+    var $passwordError = $("#passwordError")\r
+\r
+    var $cfPsdError = $("#cfPsdError");\r
+\r
+    function initialPage() {\r
+        userId = getId();\r
+        getUserDetails(userId).done(function(data) {\r
+            listUserDetails(data);\r
+        });\r
+\r
+        /*initial the event*/\r
+        $("#confirm").click(function(e) {\r
+            if (!checkPassword()) {\r
+                return;\r
+            }\r
+            var data = getModifyUser();\r
+            changePsd(data).done(function() {\r
+                top.bootbox.alert("Password change successfully.", function(e) {\r
+                    Rest.turn2URI("/openoui/user/user.html");\r
+                })\r
+            }).fail(function(e) {\r
+                if (e.statusText == "Unauthorized") {\r
+                    showError($oldPasswordError, "The old password is wrong.");\r
+                } else {\r
+                    showError($oldPasswordError, e.statusText);\r
+                }\r
+            })\r
+        })\r
+        $("#cancel").click(function(e) {\r
+            window.document.location = "/openoui/user/user.html";\r
+        })\r
+    }\r
+\r
+    function checkPassword() {\r
+        if (!checkMandatory()) {\r
+            return false;\r
+        }\r
+\r
+        if (!checkCfPassword()) {\r
+            showError($cfPsdError, "The password is not the same.");\r
+            return false;\r
+        }\r
+\r
+        if (!checkPasswordRule()) {\r
+            return false;\r
+        }\r
+        return true;\r
+    }\r
+\r
+    function checkMandatory() {\r
+        if ($password.val() == "") {\r
+            showError($passwordError, "Mandatory.");\r
+            return false;\r
+        }\r
+\r
+        if ($oldPassword.val() == "") {\r
+            showError($oldPasswordError, "Mandatory.");\r
+            return false;\r
+        }\r
+        return true;\r
+    }\r
+\r
+    function checkCfPassword() {\r
+        return $("#password").val() == $("#cfPassword").val();\r
+    }\r
+\r
+    function checkPasswordRule() {\r
+        var password = $password.val();\r
+\r
+        if (!checkLength(8, 32, password)) {\r
+            showError($passwordError, "The password length should between 8 and 32.");\r
+            return false\r
+        }\r
+\r
+        if (!checkCotainSpecial(password)) {\r
+            showError($passwordError, "At least contain: one uppercase letter, one lowercase letter, and one digit, one special character;");\r
+            return false\r
+        }\r
+\r
+        if (!checkNoContainAndReverse(password, $userName.val())) {\r
+            showError($passwordError, "The password should not contain the user name or reverse.");\r
+            return false\r
+        }\r
+\r
+        if (!checkNoSpace(password)) {\r
+            showError($passwordError, "The password should not contain space.");\r
+            return false\r
+        }\r
+        return true\r
+    }\r
+\r
+    function checkLength(min, max, str) {\r
+        return str.length >= min && str.length <= max;\r
+    }\r
+\r
+    function checkOnlySpecials(str, reg) {\r
+        return str.match(reg) && str.match(reg).length == str.length\r
+    }\r
+\r
+    function checkCotainSpecial(password) {\r
+        return password.match(/\~|\`|\@|\#|\$|\%|\^|\&|\*|\-|\_|\=|\+|\||\?|\/|\(|\)|\<|\>|\[|\]|\{|\}|\"|\,|\.|\;|\'|\!/g) != null \r
+                && password.match(/[0-9]/g) != null && password.match(/[a-z]/g) != null && password.match(/[A-Z]/g) != null;\r
+    }\r
+\r
+    function checkUderScore(str) {\r
+        return str.indexOf("_") != 0 && str.lastIndexOf("_") != str.length - 1;\r
+    }\r
+\r
+    function checkNoSpace(str) {\r
+        return str.indexOf(" ") == -1;\r
+    }\r
+\r
+    function checkNoContainAndReverse(str, str2) {\r
+        return str.indexOf(str2) == -1 && str.indexOf(str2.split("").reverse().join("")) == -1;\r
+    }\r
+\r
+    function getModifyUser() {\r
+        var data = {};\r
+        data["original_password"] = $("#oldPassword").val();\r
+        data.password = $("#password").val();\r
+        return data;\r
+    }\r
+\r
+    function getUserDetails(id) {\r
+        return Rest.http({\r
+            url: USER_SERVICE + "/" + id + "?=" + new Date().getTime(),\r
+            type: "GET",\r
+            async: false,\r
+            contentType: 'application/json',\r
+            dataType: "json"\r
+        })\r
+    }\r
+\r
+    function listUserDetails(data) {\r
+        $("#userName").val(data.name);\r
+    }\r
+\r
+\r
+    function changePsd(data) {\r
+        return Rest.http({\r
+            url: USER_SERVICE + "/" + userId + "/password" + "?=" + new Date().getTime(),\r
+            type: "POST",\r
+            async: false,\r
+            contentType: 'application/json',\r
+            dataType: "json",\r
+            data: JSON.stringify(data)\r
+        })\r
+    }\r
+\r
+    function getId() {\r
+        var qs = location.search;\r
+        qs = qs.indexOf("?") === 0 ? qs : ("?" + qs);\r
+        var start = qs.indexOf("id=") + 3;\r
+        var end = qs.indexOf("&") === -1 ? qs.length : qs.indexOf("&") - start;\r
+        return qs.substr(start, end);\r
+    }\r
+\r
+    function showError($Obj, message) {\r
+        $Obj.text(message);\r
+        $Obj.css("visibility", "visible");\r
+        setTimeout(function() {\r
+            hideError($Obj);\r
+        }, 5000)\r
+    }\r
+\r
+    function hideError($Obj) {\r
+        $Obj.css("visibility", "hidden");\r
+    }\r
+\r
+    initialPage();\r
+})\r