Download this file

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

/**
 * 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();
};