Download this file

user.js    210 lines (184 with data), 6.1 kB

  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
/**
* Copyright (c) 2013/2014, Intel Performance Learning Solutions Ltd, Intel Corporation.
*
* 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 (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.
*
**/
/**
* Contains functions to handles user authentication/authorization/access control
*
* Created by : Sridhar Voorakkara (vsridhar420@gmail.com)
*/
//handle click of 'register' link
function registerClick() {
clearMessage();
if (userStatus().loggedIn) { //if a user is already logged in
displayMessage('Please logout first !');
setupUserScreen('', false);
}
else //display form for registration data
setupUserScreen('register', false);
}
//handle the submission of registration data
function submitRegistration() {
//remove any message on display
clearMessage();
var form = document.forms["registration"]; //get the form object
var errMsg = "";
//validation
if (form.regUserId.value.length == 0) {
errMsg = "User Id is a required field. Please enter a User Id.";
}
if (errMsg.length == 0 && form.email.value.length == 0) {
errMsg = "Email is a required field. Please enter a valid Email Id.";
}
if (errMsg.length == 0 && form.regPassWd.value == 0) {
errMsg = "Please enter a password.";
}
if (errMsg.length == 0 && form.regPassWd.value != form.confPassWd.value) {
errMsg = "Password confirmation failed.";
}
//strong password validation
var pwdOk = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).{4,8}$/g.exec(form.regPassWd.value);
if (errMsg.length == 0 && !pwdOk)
errMsg = "Password does not meet the strong password requirement. Password requires one lower case letter, one upper case letter, one digit, 6-13 length, and no spaces.";
//if there is an error, display it & exit
if (errMsg.length > 0) {
document.getElementById("feedback").innerHTML = errMsg;
return;
}
//build querystring
var qs = "userid="+ form.regUserId.value;
qs = qs + "&email="+ form.email.value;
qs = qs + "&password="+ form.regPassWd.value;
qs = qs + "&action=register";
//display waiting image
displayWait();
//Do ajax post
xmlhttpPost('user-account', qs, displayRegResult);
}
//display results of registration
var displayRegResult = function (err, result) {
clearMessage(); //remove any existing message
if (result=="registration success") {
//display success message
setupUserScreen('register', true);
document.forms['registration'].reset();
displayMessage('Registration Successful. You can login now.');
}
else
displayMessage(result);
};
//display login form
function loginClick() {
clearMessage(); //remove any existing message
setupUserScreen('login', false);
}
//post the login data to the server
function submitLogin() {
clearMessage();
//validation
var form = document.forms["login"];
var errMsg = "";
if (form.loginUserId.value.length == 0) {
errMsg = "User Id is a required field. Please enter a User Id.";
}
if (errMsg.length == 0 && form.loginPassWd.value == 0) {
errMsg = "Please enter a password.";
}
if (errMsg.length > 0) {
displayMessage(errMsg);
return;
}
//build querystring
var qs = "userid="+ form.loginUserId.value;
qs = qs + "&password="+ form.loginPassWd.value;
qs = qs + "&action=login";
//display waiting image
displayWait();
//Do ajax post
xmlhttpPost('user-account', qs, displayLoginResult);
}
//display result of login and also setup screen elements
var displayLoginResult = function (err, result) {
clearMessage();
if (result=="success") {
//display success message
docCookies.set("loginUser", document.forms["login"].loginUserId.value);
setupUserScreen('login', true);
displayMessage('Login Successful');
//change login link
setLoginBtn();
document.forms['login'].reset();
}
else
displayMessage(result);
};
//get details of logged in user
var userStatus = function() {
var stat = {};
var user = docCookies.get("loginUser");
if (user)
stat.loggedIn = true;
stat.user = user;
return stat;
};
//handle logout
function logoutClick() {
clearMessage();
//build querystring
var qs = "userid="+ docCookies.get("loginUser");
qs = qs + "&action=logout";
//display waiting image
displayWait();
//Do ajax post
xmlhttpPost('user-account', qs, displayLogoutResult);
}
//display logout result
var displayLogoutResult = function (err, result) {
clearMessage();
if (result=="success") {
//remove cookie
docCookies.remove("loginUser");
//change login link
setLoginBtn();
//display success message
setupUserScreen('login', false);
displayMessage('Logout Successful');
}
else
displayMessage(result);
};
//sets up the screen based on login/logout result
function setupUserScreen(useraction, result) {
displayTabs(''); //hide all the tabs
var userPanels = ['register', 'login'];
userPanels.forEach(function(element, idx, array) {
var panel = "#"+element+"Pane";
$(panel).hide();
if (!result && element==useraction)
$(panel).show();
});
};
//toggle login button based on the logged in status
function setLoginBtn() {
if (userStatus().loggedIn) {
$("#loginBtn").html('<a href="Javascript:logoutClick()"><img src="resources/images/Login.png"><br>logout</a>');
setupAutocompleteOnLogin();
}
else {
$("#loginBtn").html('<a href="Javascript:loginClick()"><img src="resources/images/Login.png"><br>login</a>');
setupUserScreen('login', false);
}
$("#loginBtn").button();
};