aboutsummaryrefslogtreecommitdiffstats
path: root/auth/src/main/webapp/user/js/createUser.js
blob: 71a8d566cb8d8a6db57d7a733370b6210dfb7043 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * Copyright 2016-2017 Huawei Technologies Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
$(document).ready(function() {

    var USER_SERVICE = "/openoapi/auth/v1/users";
    var ROLE_SERVICE = "/openoapi/auth/v1/roles";
    var $userName = $("#userName");
    var $password = $("#password");
    var $cfPsdError = $("#cfPsdError");
    var $userNameError = $("#userNameError");
    var $passwordError = $("#passwordError");
    var $rolesError = $("#rolesError");
    var roleMap=[];
    function initialPage() {
        /*initial the event*/
        $("#confirm").click(function(e) {
            if (!checkUserRules()) {
                return;
            }
            var data = getCreateUser();
            createUser(data).done(function() {
                window.document.location = "/openoui/user/user.html";
            })
        })
        $("#cancel").click(function(e) {
            window.document.location = "/openoui/user/user.html";
        })
    //get and initialize roles
     getRolesList().done(function(data) {
                                var data = formatRoles(data);
                                   for (var i = 0; i < data.length; i++) {
                                 var html = '<li><input type="checkbox" value="' + data[i].name + '"/>' + data[i].name + '</li>';
                                 $('.mutliSelect ul').append(html);
                                }
                            })
    
    //init listener
		$(".dropdown dt a").on('click', function() {
		  $(".dropdown dd ul").slideToggle('fast');
		});

		$(".dropdown dd ul li a").on('click', function() {
		  $(".dropdown dd ul").hide();
		});

		function getSelectedValue(id) {
		  return $("#" + id).find("dt a span.value").html();
		}

		$(document).bind('click', function(e) {
		  var $clicked = $(e.target);
		  if (!$clicked.parents().hasClass("dropdown")) $(".dropdown dd ul").hide();
		});

		$('.mutliSelect input[type="checkbox"]').on('click', function() {
			if($('.hida')[0].innerHTML=='Please select roles')
			{
			$('.hida')[0].innerHTML='';    
			}
			
		  var title;
		  if($('.multiSel').text() ==='')
		  {
			title = $(this).closest('.mutliSelect').find('input[type="checkbox"]').val(),
			title = $(this).val();
			}
			else
			{
			title = $(this).closest('.mutliSelect').find('input[type="checkbox"]').val(),
			title ="," + $(this).val();
			
			}
			var oldText=$('.hida')[0].innerHTML;
		  if ($(this).is(':checked')) {
			if(oldText.length>0)
			{
				$('.hida')[0].innerHTML=oldText+','+title;
			}
			else
			{
				 $('.hida')[0].innerHTML=title;    
			}

		  } else {
		   
		   var rolesData = oldText.split(',');
		   var rolesList='';
			 for (var i = 0; i < rolesData.length ; i++) {
				   if(title!=rolesData[i])
				   {
					if(i==0 || rolesList.length==0 )
					{
					rolesList=rolesData[i];
					}
					else
					{
					rolesList=rolesList+','+rolesData[i];
					}
				   }
				}
				if(rolesList.length ==0)
				{
					rolesList='Please select roles';
				}
			 $('.hida')[0].innerHTML=rolesList;   
		  
		  }
		});

    }

    function getRolesList() {
        return Rest.http({
            url: ROLE_SERVICE + "?=" + new Date().getTime(),
            type: "GET",
            async: false,
            contentType: 'application/json',
	    dataType: "json"
        })
    }

    function formatRoles(data) {
        var rolesData = [];
        for (var i = 0; i < data.roles.length; i++) {
            var temp = {};
            temp.roleid = data.roles[i].id;
            temp.name = data.roles[i].name;
            rolesData.push(temp);
            roleMap[temp.name]=temp.roleid;
        }
        return rolesData;
    }

    function getCreateUser() {
        var data = {};
        data.userName = $userName.val();
        data.password = $password.val();
        data.description = $("#description").val();
        data.email = "xxxx@xxxx.com";
        //get roles
        var roles=[];
        var rolesData = $('.hida').text().split(',');
        for (var i = 0; i < rolesData.length ; i++) {
            var temp = {};
            temp.name=rolesData[i];
            temp.id=roleMap[rolesData[i]];
            roles.push(temp);
        }
        data.roles=roles;
        return data;
    }

    function createUser(data) {
        return Rest.http({
            url: USER_SERVICE + "?=" + new Date().getTime(),
            type: "POST",
            async: false,
            contentType: 'application/json',
            dataType: "json",
            data: JSON.stringify(data)
        })
    }

    function checkUserRules() {
        if (!checkMandatory()) {
            return false;
        }

        if (!checkCfPassword()) {
            return false;
        }

        if (!checkUserNameRule()) {
            return false;
        }

        if (!checkPasswordRule()) {
            return false;
        }
        return true;
    }

    function checkMandatory() {
        if ($userName.val() == "") {
            showError($userNameError, "Mandatory.");
            return false;
        }

        if ($password.val() == "") {
            showError($passwordError, "Mandatory.");
            return false;
        }
		if($('.hida')[0].innerHTML=='Please select roles')
		{
			showError($rolesError, "Mandatory.");
            return false;
        }
        return true;
    }

    function checkUserNameRule() {
        var username = $userName.val();
        if (!checkLength(5, 30, username)) {
            showError($userNameError, "The user name length should between 5 and 30.");
            return false
        }

        if (!checkOnlySpecials(username, /[0-9]|[a-z]|[A-Z]|_/g)) {
            showError($userNameError, "Only Character(a-z\,A-Z\,0-9,_) is allowed.");
            return false
        }

        if(!checkUderScore(username)) {
            showError($userNameError, 'The character "_" is only allowed in the middle of the user name.');
            return false
        }

        if (!checkNoSpace(username)) {
            showError($userNameError, "The user name should not contain space.");
            return false
        }

        return true
    }

    function checkPasswordRule() {
        var password = $password.val();

        if (!checkLength(8, 32, password)) {
            showError($passwordError, "The password length should between 8 and 32.");
            return false
        }

        if (!checkCotainSpecial(password)) {
            showError($passwordError, "At least contain: one uppercase letter, one lowercase letter, and one digit, one special character;");
            return false
        }

        if (!checkNoContainAndReverse(password, $userName.val())) {
            showError($passwordError, "The password should not contain the user name or reverse.");
            return false
        }

        if (!checkNoSpace(password)) {
            showError($passwordError, "The password should not contain space.");
            return false
        }
        return true
    }

    function checkLength(min, max, str) {
        return str.length >= min && str.length <= max;
    }

    function checkOnlySpecials(str, reg) {
        return str.match(reg) && str.match(reg).length == str.length
    }

    function checkCotainSpecial(password) {
        return password.match(/\~|\`|\@|\#|\$|\%|\^|\&|\*|\-|\_|\=|\+|\||\?|\/|\(|\)|\<|\>|\[|\]|\{|\}|\"|\,|\.|\;|\'|\!/g) != null 
                && password.match(/[0-9]/g) != null && password.match(/[a-z]/g) != null && password.match(/[A-Z]/g) != null;
    }

    function checkUderScore(str) {
        return str.indexOf("_") != 0 && str.lastIndexOf("_") != str.length - 1;
    }

    function checkNoContainAndReverse(str, str2) {
        return str.indexOf(str2) == -1 && str.indexOf(str2.split("").reverse().join("")) == -1;
    }

    function checkNoSpace(str) {
        return str.indexOf(" ") == -1;
    }

    function checkCfPassword() {
        if ($password.val() == $("#cfPassword").val()) {
            return true;
        }
        showError($cfPsdError, "The password is not the same.");
        return false;
    }

    function showError($Obj, message) {
        $Obj.text(message);
        $Obj.css("visibility", "visible");
        setTimeout(function() {
            hideError($Obj);
        }, 5000)
    }

    function hideError($Obj) {
        $Obj.css("visibility", "hidden");
    }

    initialPage();
})