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
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
25 function initialPage() {
\r
26 /*initial the event*/
\r
27 $("#confirm").click(function(e) {
\r
28 if (!checkUserRules()) {
\r
31 var data = getCreateUser();
\r
32 createUser(data).done(function() {
\r
33 window.document.location = "/openoui/user/user.html";
\r
36 $("#cancel").click(function(e) {
\r
37 window.document.location = "/openoui/user/user.html";
\r
41 function getCreateUser() {
\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
50 function createUser(data) {
\r
52 url: USER_SERVICE + "?=" + new Date().getTime(),
\r
55 contentType: 'application/json',
\r
57 data: JSON.stringify(data)
\r
61 function checkUserRules() {
\r
62 if (!checkMandatory()) {
\r
66 if (!checkCfPassword()) {
\r
70 if (!checkUserNameRule()) {
\r
74 if (!checkPasswordRule()) {
\r
80 function checkMandatory() {
\r
81 if ($userName.val() == "") {
\r
82 showError($userNameError, "Mandatory.");
\r
86 if ($password.val() == "") {
\r
87 showError($passwordError, "Mandatory.");
\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
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
105 if(!checkUderScore(username)) {
\r
106 showError($userNameError, 'The character "_" is only allowed in the middle of the user name.');
\r
110 if (!checkNoSpace(username)) {
\r
111 showError($userNameError, "The user name should not contain space.");
\r
118 function checkPasswordRule() {
\r
119 var password = $password.val();
\r
121 if (!checkLength(8, 32, password)) {
\r
122 showError($passwordError, "The password length should between 8 and 32.");
\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
131 if (!checkNoContainAndReverse(password, $userName.val())) {
\r
132 showError($passwordError, "The password should not contain the user name or reverse.");
\r
136 if (!checkNoSpace(password)) {
\r
137 showError($passwordError, "The password should not contain space.");
\r
143 function checkLength(min, max, str) {
\r
144 return str.length >= min && str.length <= max;
\r
147 function checkOnlySpecials(str, reg) {
\r
148 return str.match(reg) && str.match(reg).length == str.length
\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
156 function checkUderScore(str) {
\r
157 return str.indexOf("_") != 0 && str.lastIndexOf("_") != str.length - 1;
\r
160 function checkNoContainAndReverse(str, str2) {
\r
161 return str.indexOf(str2) == -1 && str.indexOf(str2.split("").reverse().join("")) == -1;
\r
164 function checkNoSpace(str) {
\r
165 return str.indexOf(" ") == -1;
\r
168 function checkCfPassword() {
\r
169 if ($password.val() == $("#cfPassword").val()) {
\r
172 showError($cfPsdError, "The password is not the same.");
\r
176 function showError($Obj, message) {
\r
177 $Obj.text(message);
\r
178 $Obj.css("visibility", "visible");
\r
179 setTimeout(function() {
\r
184 function hideError($Obj) {
\r
185 $Obj.css("visibility", "hidden");
\r