rebuild GUI structure(only changed modules' name)
[vnfsdk/refrepo.git] / auth / src / main / webapp / user / js / changePassword.js
1 /*
2  * Copyright 2016-2017 Huawei Technologies Co., Ltd.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 $(document).ready(function() {
17     var USER_SERVICE = "/openoapi/auth/v1/users";
18     var userId;
19     var $userName = $("#userName");
20     var $oldPassword = $("#oldPassword");
21     var $oldPasswordError = $("#oldPasswordError");
22     var $password = $("#password");
23     var $passwordError = $("#passwordError")
24
25     var $cfPsdError = $("#cfPsdError");
26
27     function initialPage() {
28         userId = getId();
29         getUserDetails(userId).done(function(data) {
30             listUserDetails(data);
31         });
32
33         /*initial the event*/
34         $("#confirm").click(function(e) {
35             if (!checkPassword()) {
36                 return;
37             }
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");
42                 })
43             }).fail(function(e) {
44                 if (e.statusText == "Unauthorized") {
45                     showError($oldPasswordError, "The old password is wrong.");
46                 } else {
47                     showError($oldPasswordError, e.statusText);
48                 }
49             })
50         })
51         $("#cancel").click(function(e) {
52             window.document.location = "/openoui/user/user.html";
53         })
54     }
55
56     function checkPassword() {
57         if (!checkMandatory()) {
58             return false;
59         }
60
61         if (!checkCfPassword()) {
62             showError($cfPsdError, "The password is not the same.");
63             return false;
64         }
65
66         if (!checkPasswordRule()) {
67             return false;
68         }
69         return true;
70     }
71
72     function checkMandatory() {
73         if ($password.val() == "") {
74             showError($passwordError, "Mandatory.");
75             return false;
76         }
77
78         if ($oldPassword.val() == "") {
79             showError($oldPasswordError, "Mandatory.");
80             return false;
81         }
82         return true;
83     }
84
85     function checkCfPassword() {
86         return $("#password").val() == $("#cfPassword").val();
87     }
88
89     function checkPasswordRule() {
90         var password = $password.val();
91
92         if (!checkLength(8, 32, password)) {
93             showError($passwordError, "The password length should between 8 and 32.");
94             return false
95         }
96
97         if (!checkCotainSpecial(password)) {
98             showError($passwordError, "At least contain: one uppercase letter, one lowercase letter, and one digit, one special character;");
99             return false
100         }
101
102         if (!checkNoContainAndReverse(password, $userName.val())) {
103             showError($passwordError, "The password should not contain the user name or reverse.");
104             return false
105         }
106
107         if (!checkNoSpace(password)) {
108             showError($passwordError, "The password should not contain space.");
109             return false
110         }
111         return true
112     }
113
114     function checkLength(min, max, str) {
115         return str.length >= min && str.length <= max;
116     }
117
118     function checkOnlySpecials(str, reg) {
119         return str.match(reg) && str.match(reg).length == str.length
120     }
121
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;
125     }
126
127     function checkUderScore(str) {
128         return str.indexOf("_") != 0 && str.lastIndexOf("_") != str.length - 1;
129     }
130
131     function checkNoSpace(str) {
132         return str.indexOf(" ") == -1;
133     }
134
135     function checkNoContainAndReverse(str, str2) {
136         return str.indexOf(str2) == -1 && str.indexOf(str2.split("").reverse().join("")) == -1;
137     }
138
139     function getModifyUser() {
140         var data = {};
141         data["original_password"] = $("#oldPassword").val();
142         data.password = $("#password").val();
143         return data;
144     }
145
146     function getUserDetails(id) {
147         return Rest.http({
148             url: USER_SERVICE + "/" + id + "?=" + new Date().getTime(),
149             type: "GET",
150             async: false,
151             contentType: 'application/json',
152             dataType: "json"
153         })
154     }
155
156     function listUserDetails(data) {
157         $("#userName").val(data.name);
158     }
159
160
161     function changePsd(data) {
162         return Rest.http({
163             url: USER_SERVICE + "/" + userId + "/password" + "?=" + new Date().getTime(),
164             type: "POST",
165             async: false,
166             contentType: 'application/json',
167             dataType: "json",
168             data: JSON.stringify(data)
169         })
170     }
171
172     function getId() {
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);
178     }
179
180     function showError($Obj, message) {
181         $Obj.text(message);
182         $Obj.css("visibility", "visible");
183         setTimeout(function() {
184             hideError($Obj);
185         }, 5000)
186     }
187
188     function hideError($Obj) {
189         $Obj.css("visibility", "hidden");
190     }
191
192     initialPage();
193 })