//get the voter uid
var user = client.user;
var voteConn = null; // voting db connection
// get the connection
// name connection for debugging purposes
// set server timeout to 10 seconds
voteConn = project.appPool.connection("checkVoters",10);
if (voteConn == null)
{
write('Error: Unable to connect to database');
}
// all registered voters should be pre-populated into the database
// build the cursor to handle results
sql = "SELECT voter.uid, voter.hasvoted FROM voter WHERE (voter.uid = '"+ user + "')";
var voterCursor = voteConn.cursor(sql);
if (!voterCursor.next())
{
write(' No voter found matching: ' + user + '
');
client.user = "";
//here we allow the user to try again
// in a real application, something should at least logged
write('Try again');
}
// we got a "real" voter
else
{
// check to see if they have voted yet
// we don't want any ballot stuffing ;)
if ( voterCursor.hasvoted == 1)
{
write(' voter: '+ user + ' has voted
');
//again we should do better error handling
write('Try again');
}
else
{
//okay you can vote!
//first close the cursor
voterCursor.close();
//next release current connection
voteConn.release();
// now the user can go to the voting booth
redirect("ballot.htm");
}
}