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