2 * Copyright 2016-2017 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 $(document).ready(function() {
17 var USER_SERVICE = "/openoapi/auth/v1/users";
19 var $userName = $("#userName");
20 var $oldPassword = $("#oldPassword");
21 var $oldPasswordError = $("#oldPasswordError");
22 var $password = $("#password");
23 var $passwordError = $("#passwordError")
25 var $cfPsdError = $("#cfPsdError");
27 function initialPage() {
29 getUserDetails(userId).done(function(data) {
30 listUserDetails(data);
34 $("#confirm").click(function(e) {
35 if (!checkPassword()) {
38 var data = getModifyUser();
39 changePsd(data).done(function() {
40 top.bootbox.alert("Password change successfully.", function(e) {
41 Rest.turn2URI("/openoui/user/user.html");
44 if (e.statusText == "Unauthorized") {
45 showError($oldPasswordError, "The old password is wrong.");
47 showError($oldPasswordError, e.statusText);
51 $("#cancel").click(function(e) {
52 window.document.location = "/openoui/user/user.html";
56 function checkPassword() {
57 if (!checkMandatory()) {
61 if (!checkCfPassword()) {
62 showError($cfPsdError, "The password is not the same.");
66 if (!checkPasswordRule()) {
72 function checkMandatory() {
73 if ($password.val() == "") {
74 showError($passwordError, "Mandatory.");
78 if ($oldPassword.val() == "") {
79 showError($oldPasswordError, "Mandatory.");
85 function checkCfPassword() {
86 return $("#password").val() == $("#cfPassword").val();
89 function checkPasswordRule() {
90 var password = $password.val();
92 if (!checkLength(8, 32, password)) {
93 showError($passwordError, "The password length should between 8 and 32.");
97 if (!checkCotainSpecial(password)) {
98 showError($passwordError, "At least contain: one uppercase letter, one lowercase letter, and one digit, one special character;");
102 if (!checkNoContainAndReverse(password, $userName.val())) {
103 showError($passwordError, "The password should not contain the user name or reverse.");
107 if (!checkNoSpace(password)) {
108 showError($passwordError, "The password should not contain space.");
114 function checkLength(min, max, str) {
115 return str.length >= min && str.length <= max;
118 function checkOnlySpecials(str, reg) {
119 return str.match(reg) && str.match(reg).length == str.length
122 function checkCotainSpecial(password) {
123 return password.match(/\~|\`|\@|\#|\$|\%|\^|\&|\*|\-|\_|\=|\+|\||\?|\/|\(|\)|\<|\>|\[|\]|\{|\}|\"|\,|\.|\;|\'|\!/g) != null
124 && password.match(/[0-9]/g) != null && password.match(/[a-z]/g) != null && password.match(/[A-Z]/g) != null;
127 function checkUderScore(str) {
128 return str.indexOf("_") != 0 && str.lastIndexOf("_") != str.length - 1;
131 function checkNoSpace(str) {
132 return str.indexOf(" ") == -1;
135 function checkNoContainAndReverse(str, str2) {
136 return str.indexOf(str2) == -1 && str.indexOf(str2.split("").reverse().join("")) == -1;
139 function getModifyUser() {
141 data["original_password"] = $("#oldPassword").val();
142 data.password = $("#password").val();
146 function getUserDetails(id) {
148 url: USER_SERVICE + "/" + id + "?=" + new Date().getTime(),
151 contentType: 'application/json',
156 function listUserDetails(data) {
157 $("#userName").val(data.name);
161 function changePsd(data) {
163 url: USER_SERVICE + "/" + userId + "/password" + "?=" + new Date().getTime(),
166 contentType: 'application/json',
168 data: JSON.stringify(data)
173 var qs = location.search;
174 qs = qs.indexOf("?") === 0 ? qs : ("?" + qs);
175 var start = qs.indexOf("id=") + 3;
176 var end = qs.indexOf("&") === -1 ? qs.length : qs.indexOf("&") - start;
177 return qs.substr(start, end);
180 function showError($Obj, message) {
182 $Obj.css("visibility", "visible");
183 setTimeout(function() {
188 function hideError($Obj) {
189 $Obj.css("visibility", "hidden");