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