From 0a034471cadb882aa542d548cbffd15d366733ca Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Thu, 23 Jul 2015 18:16:44 +0200 Subject: [PATCH] Check for worker_id (no duplicate allowed) --- controllers/prototype/dbrequests.js | 40 ++++++++++++++++++++- controllers/prototype/index.js | 8 +++-- controllers/prototype/views/user_study.jade | 11 +++++- posts/identification/index.js | 37 +++++++++++-------- views/base.jade | 2 ++ 5 files changed, 79 insertions(+), 19 deletions(-) diff --git a/controllers/prototype/dbrequests.js b/controllers/prototype/dbrequests.js index 788a2e9..f5833c3 100644 --- a/controllers/prototype/dbrequests.js +++ b/controllers/prototype/dbrequests.js @@ -594,6 +594,40 @@ DBReq.UserIdChecker.prototype.finish = function() { this.finishAction(this.finalResult); }; +DBReq.UserNameChecker = function(name, finishAction) { + this.name = name; + this.finishAction = finishAction; + var self = this; + pg.connect(pgc.url, function(err, client, release) { + self.client = client; + self.release = release; + self.execute(); + }); +}; + +DBReq.UserNameChecker.prototype.execute = function() { + var self = this; + this.client.query( + "SELECT count(id) > 0 AS answer FROM users WHERE worker_id = $1", + [self.name], + function(err, result) { + if (err !== null) + Log.dberror(err + ' in UserNameChecker'); + self.finalResult = result.rows[0].answer; + self.finish(); + } + ); + +}; + +DBReq.UserNameChecker.prototype.finish = function() { + this.release(); + this.client = null; + this.release = null; + + this.finishAction(this.finalResult); +}; + /** * Class that creates an experiment * @param id {Number} id of the experiment to check @@ -671,7 +705,7 @@ DBReq.ExpGetter.prototype.execute = function() { this.client.query( "SELECT " + "experiment.id as exp_id, " + - "users.name as username, " + + "users.worker_id as username, " + "scene.name as scenename, " + "users.id as user_id " + "FROM experiment, users, scene " + @@ -757,6 +791,10 @@ DBReq.checkUserId = function(id, callback) { new DBReq.UserIdChecker(id, callback); }; +DBReq.checkUserName = function(name, callback) { + new DBReq.UserNameChecker(name, callback); +}; + /** * Checks if an experiment id exists * @memberof DBReq diff --git a/controllers/prototype/index.js b/controllers/prototype/index.js index dcba3fe..d1e2506 100644 --- a/controllers/prototype/index.js +++ b/controllers/prototype/index.js @@ -148,7 +148,7 @@ module.exports.tutorial = function(req, res) { req.session.save(); res.setHeader('Content-Type', 'text/html'); - res.render('tutorial.jade', res.lcals, function(err, result) { + res.render('tutorial.jade', res.locals, function(err, result) { res.send(result); }); }); @@ -211,7 +211,11 @@ module.exports.viewer = function(req, res, next) { module.exports.userstudy = function(req, res) { res.setHeader('Content-Type', 'text/html'); - res.render('user_study.jade', res.lcals, function(err, result) { + res.locals.identificationFailed = req.session.identificationFailed; + req.session.identificationFailed = false; + req.session.save(); + + res.render('user_study.jade', res.locals, function(err, result) { res.send(result); }); diff --git a/controllers/prototype/views/user_study.jade b/controllers/prototype/views/user_study.jade index bf383fd..30c9807 100644 --- a/controllers/prototype/views/user_study.jade +++ b/controllers/prototype/views/user_study.jade @@ -3,6 +3,14 @@ extends ../../../views/base.jade block extrahead link(rel="stylesheet", href="/static/css/signin.css") +block extrabody + if identificationFailed + .container + .alert.alert-danger.alert-dismissible(role="alert", style={'margin-top':'20px'}) + button.close(type="button", data-dismiss="alert", aria-label="Close") + span(aria-hidden="true") × + Error : this id is already used ! + block content form.form-signin(method="POST", action='/identification') h2 Please sign in @@ -13,10 +21,11 @@ block content div(style={'text-align': 'center', 'margin-top':'10px', 'margin-bottom':'10px'}) label.radio-inline - input(type='radio', name='inputGender', value="male") + input(type='radio', name='inputGender', value="male", checked) | Male label.radio-inline input(type='radio', name='inputGender', value="female") | Female button.btn.btn-lg.btn-primary.btn-block(type='submit') Sign in + diff --git a/posts/identification/index.js b/posts/identification/index.js index 0079579..06a78a0 100644 --- a/posts/identification/index.js +++ b/posts/identification/index.js @@ -4,24 +4,31 @@ var db = require('../../controllers/prototype/dbrequests.js'); var Log = require('../../lib/NodeLog.js'); module.exports.index = function(req, res) { + db.checkUserName(req.body.inputId, function(ok) { + if (!ok) { + db.tryUser(req.session.user_id, function(id) { + req.session.user_id = id; + req.session.save(); - db.tryUser(req.session.user_id, function(id) { - req.session.user_id = id; - req.session.save(); + pg.connect(secret.url, function(err, client, release) { + client.query( + "UPDATE Users SET worker_id = $1, age = $2, male = $3 WHERE id = $4;", + [req.body.inputId, req.body.inputAge, req.body.inputGender === 'male', req.session.user_id], + function(err, result) { + if (err !== null) + Log.dberror(err + ' in identfication'); + release(); + } + ); + }); - pg.connect(secret.url, function(err, client, release) { - client.query( - "UPDATE Users SET worker_id = $1, age = $2, male = $3 WHERE id = $4;", - [req.body.inputId, req.body.inputAge, req.body.inputGender === 'male', req.session.user_id], - function(err, result) { - if (err !== null) - Log.dberror(err + ' in identfication'); - release(); - } - ); - }); + res.redirect('/'); - res.redirect('/'); + }); + } else { + req.session.identificationFailed = true; + res.redirect('/user-study'); + } }); }; diff --git a/views/base.jade b/views/base.jade index 6887798..8b79d95 100644 --- a/views/base.jade +++ b/views/base.jade @@ -24,6 +24,8 @@ html(lang='fr') block links + block extrabody + section#main-section.container if alertCookie .alert.alert-warning.alert-dismissible(role="alert", style={'margin-top':'20px'})