rebuild GUI structure(only changed modules' name)
[vnfsdk/refrepo.git] / auth / src / main / webapp / user / js / createUser.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
18     var USER_SERVICE = "/openoapi/auth/v1/users";
19     var $userName = $("#userName");
20     var $password = $("#password");
21     var $cfPsdError = $("#cfPsdError");
22     var $userNameError = $("#userNameError");
23     var $passwordError = $("#passwordError");
24
25     function initialPage() {
26         /*initial the event*/
27         $("#confirm").click(function(e) {
28             if (!checkUserRules()) {
29                 return;
30             }
31             var data = getCreateUser();
32             createUser(data).done(function() {
33                 window.document.location = "/openoui/user/user.html";
34             })
35         })
36         $("#cancel").click(function(e) {
37             window.document.location = "/openoui/user/user.html";
38         })
39     }
40
41     function getCreateUser() {
42         var data = {};
43         data.userName = $userName.val();
44         data.password = $password.val();
45         data.description = $("#description").val();
46         data.email = "xxxx@xxxx.com";
47         return data;
48     }
49
50     function createUser(data) {
51         return Rest.http({
52             url: USER_SERVICE + "?=" + new Date().getTime(),
53             type: "POST",
54             async: false,
55             contentType: 'application/json',
56             dataType: "json",
57             data: JSON.stringify(data)
58         })
59     }
60
61     function checkUserRules() {
62         if (!checkMandatory()) {
63             return false;
64         }
65
66         if (!checkCfPassword()) {
67             return false;
68         }
69
70         if (!checkUserNameRule()) {
71             return false;
72         }
73
74         if (!checkPasswordRule()) {
75             return false;
76         }
77         return true;
78     }
79
80     function checkMandatory() {
81         if ($userName.val() == "") {
82             showError($userNameError, "Mandatory.");
83             return false;
84         }
85
86         if ($password.val() == "") {
87             showError($passwordError, "Mandatory.");
88             return false;
89         }
90         return true;
91     }
92
93     function checkUserNameRule() {
94         var username = $userName.val();
95         if (!checkLength(5, 30, username)) {
96             showError($userNameError, "The user name length should between 5 and 30.");
97             return false
98         }
99
100         if (!checkOnlySpecials(username, /[0-9]|[a-z]|[A-Z]|_/g)) {
101             showError($userNameError, "Only Character(a-z\,A-Z\,0-9,_) is allowed.");
102             return false
103         }
104
105         if(!checkUderScore(username)) {
106             showError($userNameError, 'The character "_" is only allowed in the middle of the user name.');
107             return false
108         }
109
110         if (!checkNoSpace(username)) {
111             showError($userNameError, "The user name should not contain space.");
112             return false
113         }
114
115         return true
116     }
117
118     function checkPasswordRule() {
119         var password = $password.val();
120
121         if (!checkLength(8, 32, password)) {
122             showError($passwordError, "The password length should between 8 and 32.");
123             return false
124         }
125
126         if (!checkCotainSpecial(password)) {
127             showError($passwordError, "At least contain: one uppercase letter, one lowercase letter, and one digit, one special character;");
128             return false
129         }
130
131         if (!checkNoContainAndReverse(password, $userName.val())) {
132             showError($passwordError, "The password should not contain the user name or reverse.");
133             return false
134         }
135
136         if (!checkNoSpace(password)) {
137             showError($passwordError, "The password should not contain space.");
138             return false
139         }
140         return true
141     }
142
143     function checkLength(min, max, str) {
144         return str.length >= min && str.length <= max;
145     }
146
147     function checkOnlySpecials(str, reg) {
148         return str.match(reg) && str.match(reg).length == str.length
149     }
150
151     function checkCotainSpecial(password) {
152         return password.match(/\~|\`|\@|\#|\$|\%|\^|\&|\*|\-|\_|\=|\+|\||\?|\/|\(|\)|\<|\>|\[|\]|\{|\}|\"|\,|\.|\;|\'|\!/g) != null 
153                 && password.match(/[0-9]/g) != null && password.match(/[a-z]/g) != null && password.match(/[A-Z]/g) != null;
154     }
155
156     function checkUderScore(str) {
157         return str.indexOf("_") != 0 && str.lastIndexOf("_") != str.length - 1;
158     }
159
160     function checkNoContainAndReverse(str, str2) {
161         return str.indexOf(str2) == -1 && str.indexOf(str2.split("").reverse().join("")) == -1;
162     }
163
164     function checkNoSpace(str) {
165         return str.indexOf(" ") == -1;
166     }
167
168     function checkCfPassword() {
169         if ($password.val() == $("#cfPassword").val()) {
170             return true;
171         }
172         showError($cfPsdError, "The password is not the same.");
173         return false;
174     }
175
176     function showError($Obj, message) {
177         $Obj.text(message);
178         $Obj.css("visibility", "visible");
179         setTimeout(function() {
180             hideError($Obj);
181         }, 5000)
182     }
183
184     function hideError($Obj) {
185         $Obj.css("visibility", "hidden");
186     }
187
188     initialPage();
189 })