diff --git a/package-lock.json b/package-lock.json index 9bad77a871b2f3d0ab47e584a6ee00d9e010a9cb..5879f5690c050446dff40cf401cce62455b495ee 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2020,6 +2020,11 @@ "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=" }, + "denque": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/denque/-/denque-1.5.0.tgz", + "integrity": "sha512-CYiCSgIF1p6EUByQPlGkKnP1M9g0ZV3qMIrqMqZqdwazygIA/YP2vrbcyl1h/WppKJTdl1F85cXIle+394iDAQ==" + }, "depd": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", @@ -2598,6 +2603,14 @@ "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", "integrity": "sha1-pWiZ0+o8m6uHS7l3O3xe3pL0iV0=" }, + "generate-function": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/generate-function/-/generate-function-2.3.1.tgz", + "integrity": "sha512-eeB5GfMNeevm/GRYq20ShmsaGcmI81kIX2K9XQx5miC8KdHaC6Jm0qQ8ZNeGOi7wYB8OsdxKs+Y2oVuTFuVwKQ==", + "requires": { + "is-property": "^1.0.2" + } + }, "gensync": { "version": "1.0.0-beta.2", "resolved": "https://registry.npmjs.org/gensync/-/gensync-1.0.0-beta.2.tgz", @@ -3150,6 +3163,11 @@ "resolved": "https://registry.npmjs.org/is-promise/-/is-promise-2.2.2.tgz", "integrity": "sha512-+lP4/6lKUBfQjZ2pdxThZvLUAafmZb8OAxFb8XXtiQmS35INgr85hdOGoEs124ez1FCnZJt6jau/T+alh58QFQ==" }, + "is-property": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/is-property/-/is-property-1.0.2.tgz", + "integrity": "sha1-V/4cTkhHTt1lsJkR8msc1Ald2oQ=" + }, "is-redirect": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/is-redirect/-/is-redirect-1.0.0.tgz", @@ -4892,6 +4910,11 @@ "resolved": "https://registry.npmjs.org/lodash.sortby/-/lodash.sortby-4.7.0.tgz", "integrity": "sha1-7dFMgk4sycHgsKG0K7UhBRakJDg=" }, + "long": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz", + "integrity": "sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==" + }, "lowercase-keys": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-1.0.1.tgz", @@ -5154,6 +5177,57 @@ "sqlstring": "2.3.1" } }, + "mysql2": { + "version": "2.2.5", + "resolved": "https://registry.npmjs.org/mysql2/-/mysql2-2.2.5.tgz", + "integrity": "sha512-XRqPNxcZTpmFdXbJqb+/CtYVLCx14x1RTeNMD4954L331APu75IC74GDqnZMEt1kwaXy6TySo55rF2F3YJS78g==", + "requires": { + "denque": "^1.4.1", + "generate-function": "^2.3.1", + "iconv-lite": "^0.6.2", + "long": "^4.0.0", + "lru-cache": "^6.0.0", + "named-placeholders": "^1.1.2", + "seq-queue": "^0.0.5", + "sqlstring": "^2.3.2" + }, + "dependencies": { + "iconv-lite": { + "version": "0.6.2", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.2.tgz", + "integrity": "sha512-2y91h5OpQlolefMPmUlivelittSWy0rP+oYVpn6A7GwVHNE8AWzoYOBNmlwks3LobaJxgHCYZAnyNo2GgpNRNQ==", + "requires": { + "safer-buffer": ">= 2.1.2 < 3.0.0" + } + }, + "lru-cache": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", + "integrity": "sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==", + "requires": { + "yallist": "^4.0.0" + } + }, + "sqlstring": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.2.tgz", + "integrity": "sha512-vF4ZbYdKS8OnoJAWBmMxCQDkiEBkGQYU7UZPtL8flbDRSNkhaXvRJ279ZtI6M+zDaQovVU4tuRgzK5fVhvFAhg==" + }, + "yallist": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz", + "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" + } + } + }, + "named-placeholders": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/named-placeholders/-/named-placeholders-1.1.2.tgz", + "integrity": "sha512-wiFWqxoLL3PGVReSZpjLVxyJ1bRqe+KKJVbr4hGs1KWfTZTQyezHFBbuKj9hsizHyGV2ne7EMjHdxEGAybD5SA==", + "requires": { + "lru-cache": "^4.1.3" + } + }, "nanomatch": { "version": "1.2.13", "resolved": "https://registry.npmjs.org/nanomatch/-/nanomatch-1.2.13.tgz", @@ -5503,26 +5577,25 @@ } }, "passport-saml": { - "version": "1.4.2", - "resolved": "https://registry.npmjs.org/passport-saml/-/passport-saml-1.4.2.tgz", - "integrity": "sha512-RJXtuiv4KWazi4zmZGVqN5pf3bV2aFbOygYzUCDEBDdeOD0yHFL4ymPOpLPXg35HvilFYTzB94JRWqwLdI2ecw==", + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/passport-saml/-/passport-saml-2.1.0.tgz", + "integrity": "sha512-czrh1ymuAOkDi7vIl1WEa2MLHQiqKKZEEOtaf0JUlMS0UaXbqCTj12wnYkZJdkKR/l1EkmmHcudvtCSsAoYpUQ==", "requires": { - "debug": "^3.1.0", + "debug": "^4.3.1", "passport-strategy": "*", - "q": "^1.5.0", - "xml-crypto": "^2.0.0", - "xml-encryption": "1.2.1", - "xml2js": "0.4.x", - "xmlbuilder": "^11.0.0", - "xmldom": "0.1.x" + "xml-crypto": "^2.1.1", + "xml-encryption": "^1.2.3", + "xml2js": "^0.4.23", + "xmlbuilder": "^15.1.1", + "xmldom": "0.5.x" }, "dependencies": { "debug": { - "version": "3.2.6", - "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz", - "integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==", + "version": "4.3.1", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.1.tgz", + "integrity": "sha512-doEwdvm4PCeK4K3RQN2ZC2BYUBaxwLARCqZmMjtF8a51J2Rb0xpVloFRnCODwqjpwnAoao4pelN8l3RJdv3gRQ==", "requires": { - "ms": "^2.1.1" + "ms": "2.1.2" } } } @@ -5835,11 +5908,6 @@ "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==" }, - "q": { - "version": "1.5.1", - "resolved": "https://registry.npmjs.org/q/-/q-1.5.1.tgz", - "integrity": "sha1-fjL3W0E4EpHQRhHxvxQQmsAGUdc=" - }, "qs": { "version": "6.7.0", "resolved": "https://registry.npmjs.org/qs/-/qs-6.7.0.tgz", @@ -6381,6 +6449,11 @@ } } }, + "seq-queue": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/seq-queue/-/seq-queue-0.0.5.tgz", + "integrity": "sha1-1WgS4cAXpuTnw+Ojeh2m143TyT4=" + }, "serve-static": { "version": "1.14.1", "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.14.1.tgz", @@ -7337,30 +7410,30 @@ "integrity": "sha1-SWsswQnsqNus/i3HK2A8F8WHCtQ=" }, "xml-crypto": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/xml-crypto/-/xml-crypto-2.0.0.tgz", - "integrity": "sha512-/a04qr7RpONRZHOxROZ6iIHItdsQQjN3sj8lJkYDDss8tAkEaAs0VrFjb3tlhmS5snQru5lTs9/5ISSMdPDHlg==", + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/xml-crypto/-/xml-crypto-2.1.1.tgz", + "integrity": "sha512-M+m4+HIJa83lu/CnspQjA7ap8gmanNDxxRjSisU8mPD4bqhxbo5N2bdpvG2WgVYOrPpOIOq55iY8Cz8Ai40IeQ==", "requires": { - "xmldom": "0.1.27", - "xpath": "0.0.27" - }, - "dependencies": { - "xmldom": { - "version": "0.1.27", - "resolved": "https://registry.npmjs.org/xmldom/-/xmldom-0.1.27.tgz", - "integrity": "sha1-1QH5ezvbQDr4757MIFcxh6rawOk=" - } + "xmldom": "0.5.0", + "xpath": "0.0.32" } }, "xml-encryption": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/xml-encryption/-/xml-encryption-1.2.1.tgz", - "integrity": "sha512-hn5w3l5p2+nGjlmM0CAhMChDzVGhW+M37jH35Z+GJIipXbn9PUlAIRZ6I5Wm7ynlqZjFrMAr83d/CIp9VZJMTA==", + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/xml-encryption/-/xml-encryption-1.2.3.tgz", + "integrity": "sha512-oVZIicsZM1VobJ5Hxxgh2ovglIY2ZuXFTeZHmJSV7hABvgkD20PSy4G+qwRToQCkagymS1zJU2XV4wjkoCS9mQ==", "requires": { "escape-html": "^1.0.3", "node-forge": "^0.10.0", - "xmldom": "~0.1.15", + "xmldom": "~0.5.0", "xpath": "0.0.27" + }, + "dependencies": { + "xpath": { + "version": "0.0.27", + "resolved": "https://registry.npmjs.org/xpath/-/xpath-0.0.27.tgz", + "integrity": "sha512-fg03WRxtkCV6ohClePNAECYsmpKKTv5L8y/X3Dn1hQrec3POx2jHZ/0P2qQ6HvsrU1BmeqXcof3NGGueG6LxwQ==" + } } }, "xml-name-validator": { @@ -7375,12 +7448,19 @@ "requires": { "sax": ">=0.6.0", "xmlbuilder": "~11.0.0" + }, + "dependencies": { + "xmlbuilder": { + "version": "11.0.1", + "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-11.0.1.tgz", + "integrity": "sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA==" + } } }, "xmlbuilder": { - "version": "11.0.1", - "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-11.0.1.tgz", - "integrity": "sha512-fDlsI/kFEx7gLvbecc0/ohLG50fugQp8ryHzMTuW9vSa1GJ0XYWKnhsUx7oie3G98+r56aTQIUB4kht42R3JvA==" + "version": "15.1.1", + "resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-15.1.1.tgz", + "integrity": "sha512-yMqGBqtXyeN1e3TGYvgNgDVZ3j84W4cwkOXQswghol6APgZWaff9lnbvN7MHYJOiXsvGPXtjTYJEiC9J2wv9Eg==" }, "xmlchars": { "version": "2.2.0", @@ -7388,14 +7468,14 @@ "integrity": "sha512-JZnDKK8B0RCDw84FNdDAIpZK+JuJw+s7Lz8nksI7SIuU3UXJJslUthsi+uWBUYOwPFwW7W7PRLRfUKpxjtjFCw==" }, "xmldom": { - "version": "0.1.31", - "resolved": "https://registry.npmjs.org/xmldom/-/xmldom-0.1.31.tgz", - "integrity": "sha512-yS2uJflVQs6n+CyjHoaBmVSqIDevTAWrzMmjG1Gc7h1qQ7uVozNhEPJAwZXWyGQ/Gafo3fCwrcaokezLPupVyQ==" + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/xmldom/-/xmldom-0.5.0.tgz", + "integrity": "sha512-Foaj5FXVzgn7xFzsKeNIde9g6aFBxTPi37iwsno8QvApmtg7KYrr+OPyRHcJF7dud2a5nGRBXK3n0dL62Gf7PA==" }, "xpath": { - "version": "0.0.27", - "resolved": "https://registry.npmjs.org/xpath/-/xpath-0.0.27.tgz", - "integrity": "sha512-fg03WRxtkCV6ohClePNAECYsmpKKTv5L8y/X3Dn1hQrec3POx2jHZ/0P2qQ6HvsrU1BmeqXcof3NGGueG6LxwQ==" + "version": "0.0.32", + "resolved": "https://registry.npmjs.org/xpath/-/xpath-0.0.32.tgz", + "integrity": "sha512-rxMJhSIoiO8vXcWvSifKqhvV96GjiD5wYb8/QHdoRyQvraTpp4IEv944nhGausZZ3u7dhQXteZuZbaqfpB7uYw==" }, "y18n": { "version": "4.0.1", diff --git a/package.json b/package.json index aa17a9888868220005e38b88be552011df44950d..739f4247468a9d5709d69f33e94aa6eb85197c82 100644 --- a/package.json +++ b/package.json @@ -39,10 +39,11 @@ "jest": "^26.6.3", "morgan": "^1.9.1", "mysql": "^2.17.1", + "mysql2": "^2.2.5", "nodemailer": "^6.3.1", "nodemon": "^2.0.1", "passport": "0.3.2", - "passport-saml": "^1.4.2", + "passport-saml": "^2.1.0", "pug": "^3.0.2" }, "devDependencies": {}, diff --git a/routes/api_TBD.js b/routes/api_TBD.js deleted file mode 100644 index 5a6ee24d95d8e35431ea2c408e3f0f5792e1fe7a..0000000000000000000000000000000000000000 --- a/routes/api_TBD.js +++ /dev/null @@ -1,20 +0,0 @@ -// ==== USER ACOOUNT API ==== -var dbconn = require('./dbconn') -module.exports = function (app) { - - //console.log(dbconn); - //var con = dbconn.connection - - app.get('/api/v1/profile', function (req, res) { - if (req.isAuthenticated()) { - // read data based on email - dbconn.user.query('SELECT * FROM user WHERE email="'+req.user.email+'"', function (err, rows, fields) { - if (err) throw err - res.send(rows[0]) - }) - } else { - res.send('authentication required'); - } - }); - -} \ No newline at end of file diff --git a/routes/dbconn2.js b/routes/dbconn2.js new file mode 100644 index 0000000000000000000000000000000000000000..fe22f1d8cdc02d1d905b9df9da3272548bf3ab17 --- /dev/null +++ b/routes/dbconn2.js @@ -0,0 +1,64 @@ +const mysql = require('mysql2') + +var env = process.env.NODE_ENV || 'testing'; +const config = require('../config/config')[env] + +// ==== USER ACOOUNT DB CONNECTION ==== +var userConnection = mysql.createConnection({ + host: config.database.host, + user: config.database.user, + password: config.database.password, + port: config.database.port, + database: config.database.dbUser, + multipleStatements: true +}) + +userConnection.connect(function(err) { + if (err) throw err; +}) +userConnection.query('USE '+config.database.dbUser) + +// ALTERNATIVE approach: close db connection manually after every query +/* +var dbconn = function dbconn(query, values, next) { + var connection = mysql.createConnection({ + host: config.database.host, + user: config.database.user, + password: config.database.password, + port: config.database.port, + database: config.database.db + }) + connection.connect(function(err) { + if (err) throw err; + }) + connection.query(query, values, function(err) { + connection.end(); // close the connection + if (err) { + throw err; + } + // Execute the callback + next.apply(this, arguments); + }); +} +*/ + +// ==== PROJECT DB CONNECTION ==== +var projectConnection = mysql.createConnection({ + host: config.database.host_project, + user: config.database.user, + password: config.database.password, + port: config.database.port, + database: config.database.dbProject +}) + +projectConnection.connect(function(err) { + if (err) throw err; +}) +projectConnection.query('USE '+config.database.dbProject) + +var connection = { + user: userConnection, + project: projectConnection +} + +module.exports = connection \ No newline at end of file diff --git a/routes/methods.js b/routes/methods.js index 4461dea632988108faf89444b01afaabf185a338..d3d3f69c029b562775f40c36cf4e710377e008a0 100644 --- a/routes/methods.js +++ b/routes/methods.js @@ -1,16 +1,17 @@ -const dbconn = require('./dbconn'); +const dbconn_OBSOLETE = require('./dbconn') // DO NOT USE THIS FOR NEW FEATURE +const dbconn = require('./dbconn2') var methods = { // ===================== user db ===================== registerNewUser: function(data, callback) { - dbconn.user.beginTransaction(function(err) { // START TRANSACTION + dbconn_OBSOLETE.user.beginTransaction(function(err) { // START TRANSACTION if (err) { throw err } // insert profile - dbconn.user.query('INSERT INTO user SET ?', data.profile, function (err, results, fields) { + dbconn_OBSOLETE.user.query('INSERT INTO user SET ?', data.profile, function (err, results, fields) { if (err) { - return dbconn.user.rollback(function() { + return dbconn_OBSOLETE.user.rollback(function() { throw err }); } @@ -20,9 +21,9 @@ var methods = { user_id: newUserId, password: data.password } - dbconn.user.query('INSERT INTO credential SET ?', credentialData, function (err, results, fields) { + dbconn_OBSOLETE.user.query('INSERT INTO credential SET ?', credentialData, function (err, results, fields) { if (err) { - return dbconn.user.rollback(function() { + return dbconn_OBSOLETE.user.rollback(function() { throw err }); } @@ -32,9 +33,9 @@ var methods = { role_id: 2, // USER user_id: newUserId } - dbconn.user.query('INSERT INTO user_project_role SET ?', projectRoleData, function (err, results, fields) { + dbconn_OBSOLETE.user.query('INSERT INTO user_project_role SET ?', projectRoleData, function (err, results, fields) { if (err) { - return dbconn.user.rollback(function() { + return dbconn_OBSOLETE.user.rollback(function() { throw err }); } @@ -43,16 +44,16 @@ var methods = { user_id: newUserId, token: data.verificationToken } - dbconn.user.query('INSERT INTO verification SET ?', verificationData, function (err, results, fields) { + dbconn_OBSOLETE.user.query('INSERT INTO verification SET ?', verificationData, function (err, results, fields) { if (err) { - return dbconn.user.rollback(function() { + return dbconn_OBSOLETE.user.rollback(function() { throw err }); } // COMMIT - dbconn.user.commit(function(err) { + dbconn_OBSOLETE.user.commit(function(err) { if (err) { - return dbconn.user.rollback(function() { + return dbconn_OBSOLETE.user.rollback(function() { throw err }) } @@ -64,8 +65,8 @@ var methods = { callback(err) }) }, - getUserByEmail: function(email, callback) { - dbconn.user.query('SELECT id, verificationStatus, salutation, title, firstname, lastname, industry, organisation, speciality, m4lab_idp FROM user WHERE email = "' +email+'"', function (err, rows, fields) { + getUserByEmail_OBSOLETE: function(email, callback) { + dbconn_OBSOLETE.user.query('SELECT id, verificationStatus, salutation, title, firstname, lastname, industry, organisation, speciality, m4lab_idp FROM user WHERE email = "' +email+'"', function (err, rows, fields) { let user if (err) { throw err } else { @@ -76,8 +77,17 @@ var methods = { callback(user, err) }); }, + getUserByEmail: async function(email) { + try { + let rows = await dbconn.user.promise().query('SELECT id, verificationStatus, salutation, title, firstname, lastname, industry, organisation, speciality, m4lab_idp FROM user WHERE email = "' +email+'"') + return rows[0][0] + } catch (err) { + console.error(err) + return err + } + }, getUserById: function(userId, callback) { - dbconn.user.query('SELECT verificationStatus, email, salutation, title, firstname, lastname, industry, organisation, speciality FROM user WHERE id = ' +userId, function (err, rows, fields) { + dbconn_OBSOLETE.user.query('SELECT verificationStatus, email, salutation, title, firstname, lastname, industry, organisation, speciality FROM user WHERE id = ' +userId, function (err, rows, fields) { let user if (err) { throw err } else { @@ -90,7 +100,7 @@ var methods = { }, checkUserEmail: function(email, callback) { let user - dbconn.user.query('SELECT id, email FROM user WHERE email = "' +email+'"', function (err, rows) { + dbconn_OBSOLETE.user.query('SELECT id, email FROM user WHERE email = "' +email+'"', function (err, rows) { if (err) { throw err } else { if ( rows.length > 0) { @@ -102,7 +112,7 @@ var methods = { }, getUserByToken: function(token, callback) { let user - dbconn.user.query('SELECT t1.user_id, t2.email FROM userdb.credential AS t1 INNER JOIN userdb.user AS t2 ON t1.user_id = t2.id AND t1.resetPasswordToken = "' + dbconn_OBSOLETE.user.query('SELECT t1.user_id, t2.email FROM userdb.credential AS t1 INNER JOIN userdb.user AS t2 ON t1.user_id = t2.id AND t1.resetPasswordToken = "' +token+'" and resetPasswordExpires > '+Date.now(), function (err, rows, fields) { if (err) { throw err } else { @@ -115,20 +125,20 @@ var methods = { ) }, updateUserById: function(userData, callback) { - dbconn.user.query('UPDATE user SET ? WHERE id = ' +userData.id, userData, function (err, rows, fields) { + dbconn_OBSOLETE.user.query('UPDATE user SET ? WHERE id = ' +userData.id, userData, function (err, rows, fields) { if (err) throw err callback(err) }) }, updateCredential: function(data, callback) { - dbconn.user.query('UPDATE credential SET ? WHERE user_id = ' +data.user_id, data, function (err, rows, fields) { + dbconn_OBSOLETE.user.query('UPDATE credential SET ? WHERE user_id = ' +data.user_id, data, function (err, rows, fields) { if (err) throw err callback(err) }) }, getUserIdByEmail: function(email, callback) { let userId - dbconn.user.query('SELECT id FROM user WHERE email = "' +email+'"', function (err, rows, fields) { + dbconn_OBSOLETE.user.query('SELECT id FROM user WHERE email = "' +email+'"', function (err, rows, fields) { if (err) { throw err } @@ -141,20 +151,20 @@ var methods = { }); }, getUserProjectRole: function(userId, callback) { - dbconn.user.query('SELECT project_id, role_id FROM user_project_role WHERE user_id = "' +userId+'"', function (err, rows, fields) { + dbconn_OBSOLETE.user.query('SELECT project_id, role_id FROM user_project_role WHERE user_id = "' +userId+'"', function (err, rows, fields) { if (err) throw err callback(rows, err) }); }, addUserProjectRole: function(data, callback) { - dbconn.user.query('INSERT INTO user_project_role SET ?', data, function (err, results, fields){ + dbconn_OBSOLETE.user.query('INSERT INTO user_project_role SET ?', data, function (err, results, fields){ if (err) throw err callback(err) }) }, getVerificationTokenByUserId: function(userId, callback) { let token - dbconn.user.query('SELECT token FROM verification WHERE user_id = "' +userId+'"', function (err, rows, fields) { + dbconn_OBSOLETE.user.query('SELECT token FROM verification WHERE user_id = "' +userId+'"', function (err, rows, fields) { if (err) { throw err } @@ -168,7 +178,7 @@ var methods = { }, getUserIdByVerificationToken: function(token, callback) { let userId - dbconn.user.query('SELECT user_id FROM verification WHERE token = "' +token+'"', function (err, rows, fields) { + dbconn_OBSOLETE.user.query('SELECT user_id FROM verification WHERE token = "' +token+'"', function (err, rows, fields) { if (err) { throw err } @@ -179,22 +189,22 @@ var methods = { }) }, verifyUserAccount: function(userData, callback) { - dbconn.user.beginTransaction(function(err) { // START TRANSACTION + dbconn_OBSOLETE.user.beginTransaction(function(err) { // START TRANSACTION if (err) { throw err } // update user status - dbconn.user.query('UPDATE user SET ? WHERE id =' +userData.id, userData, function (err, rows, fields) { + dbconn_OBSOLETE.user.query('UPDATE user SET ? WHERE id =' +userData.id, userData, function (err, rows, fields) { if (err) { - return dbconn.user.rollback(function() { throw err }) + return dbconn_OBSOLETE.user.rollback(function() { throw err }) } // delete verification token - dbconn.user.query('DELETE FROM verification WHERE user_id = '+userData.id, function (err, rows, fields) { + dbconn_OBSOLETE.user.query('DELETE FROM verification WHERE user_id = '+userData.id, function (err, rows, fields) { if (err) { - return dbconn.user.rollback(function() { throw err }) + return dbconn_OBSOLETE.user.rollback(function() { throw err }) } // COMMIT - dbconn.user.commit(function(err) { + dbconn_OBSOLETE.user.commit(function(err) { if (err) { - return dbconn.user.rollback(function() { throw err }) + return dbconn_OBSOLETE.user.rollback(function() { throw err }) } }) }) @@ -203,9 +213,9 @@ var methods = { }) }, /* ===== GitLab ===== */ - getGitlabId: function(userId, callback){ + getGitlabId_OBSOLETE: function(userId, callback){ let gitlabUserId - dbconn.user.query('SELECT gu.gitlab_userId FROM user_gitlab gu, user u WHERE u.id = "' +userId+'" and gu.user_id = u.id', function (err, rows) { + dbconn_OBSOLETE.user.query('SELECT gu.gitlab_userId FROM user_gitlab gu, user u WHERE u.id = "' +userId+'" and gu.user_id = u.id', function (err, rows) { if (err) { throw err } @@ -215,8 +225,18 @@ var methods = { callback(gitlabUserId, err) }) }, + getGitlabId: async function(userId) { + try { + let rows = await dbconn.user.promise().query('SELECT gu.gitlab_userId FROM user_gitlab gu, user u WHERE u.id = "' +userId+'" and gu.user_id = u.id') + return rows[0][0].gitlab_userId + } + catch(err) { + console.error(err) + return err + } + }, addGitlabUser: function(data, callback){ - dbconn.user.query('INSERT INTO user_gitlab SET ?', data, function (err) { + dbconn_OBSOLETE.user.query('INSERT INTO user_gitlab SET ?', data, function (err) { if (err) throw err callback(err) }) diff --git a/routes/routes-account.js b/routes/routes-account.js index 0a62ba0fb2b148cd40783d76de2b3aea1058c508..dc18b3d9e114919314c2ff83824084206c99befa 100644 --- a/routes/routes-account.js +++ b/routes/routes-account.js @@ -21,8 +21,6 @@ const projectRepo = require('../classes/repo') module.exports = function (app, config, passport, i18n) { - var loggedInUser - // =========== PASSPORT ======= passport.serializeUser(function (user, done) { done(null, user); @@ -112,28 +110,27 @@ module.exports = function (app, config, passport, i18n) { var updatePasswordMailSubject = "Ihr Passwort für das Transferportal wurde gespeichert." var updatePasswordMailContent = '
Lieber Nutzer,

Ihr Passwort wurde erfolgreich geändert.

' + mailSignature + '
'; - app.get('/', function (req, res) { + async function getLoggedInUserData(email) { + let user = await methods.getUserByEmail(email) + let loggedInUser = new portalUser( + user.id, email, user.salutation, user.title, user.firstname, user.lastname, user.industry, user.organisation, user.speciality, user.m4lab_idp, null, user.verificationStatus + ) + + let userGitlabId = await methods.getGitlabId(loggedInUser.id) + loggedInUser.setGitlabUserId(userGitlabId) + + return loggedInUser + } + + app.get('/', async function (req, res) { if ( !req.isAuthenticated() ) { res.redirect('/login') } else { - methods.getUserByEmail(req.user.email, function(data, err){ - if (!err) { - // Initialize user - if (!loggedInUser) { - loggedInUser = new portalUser( - data.id, req.user.email, data.salutation, data.title, data.firstname, data.lastname, data.industry, data.organisation, data.speciality, data.m4lab_idp, null, data.verificationStatus - ) - methods.getGitlabId(data.id, function(gitlabUserId, err){ - if(!err) { - loggedInUser.setGitlabUserId(gitlabUserId) - } - }) - } - res.render(lang+'/account/home', { - user: loggedInUser - }); - } - }) + let loggedInUser = await getLoggedInUserData(req.user.email) + + res.render(lang+'/account/home', { + user: loggedInUser + }); } }); @@ -166,10 +163,11 @@ module.exports = function (app, config, passport, i18n) { }); }); - app.get('/profile', function (req, res) { - if(!req.isAuthenticated() && !loggedInUser) { + app.get('/profile', async function (req, res) { + if ( !req.isAuthenticated() ) { res.redirect('/login') } else { + let loggedInUser = await getLoggedInUserData(req.user.email) if(loggedInUser.getVerificationStatus() != 1) { res.redirect('/account/') } else { @@ -181,9 +179,10 @@ module.exports = function (app, config, passport, i18n) { }) app.get('/services', async function(req, res){ - if(!req.isAuthenticated() && !loggedInUser) { + if( !req.isAuthenticated() ) { res.redirect('/login') } else { + let loggedInUser = await getLoggedInUserData(req.user.email) if(loggedInUser.getVerificationStatus() != 1) { // unverified users res.redirect('/account/') } else { @@ -235,10 +234,11 @@ module.exports = function (app, config, passport, i18n) { } }) - app.get('/security', function (req, res) { - if (!req.isAuthenticated() && !loggedInUser) { + app.get('/security', async function (req, res) { + if ( !req.isAuthenticated() ) { res.redirect('/login') } else { + let loggedInUser = await getLoggedInUserData(req.user.email) if(loggedInUser.getVerificationStatus() == 1 && loggedInUser.getIdpStatus() == 1) { res.render(lang+'/account/security', { user: loggedInUser @@ -249,7 +249,7 @@ module.exports = function (app, config, passport, i18n) { } }) - app.post('/updateProfile', function (req, res) { + app.post('/updateProfile', async function (req, res) { var userData = { salutation: req.body.inputSalutation, title: req.body.inputTitle, @@ -261,9 +261,10 @@ module.exports = function (app, config, passport, i18n) { speciality: req.body.inputSpeciality, } - if (!req.isAuthenticated() && !loggedInUser) { + if ( !req.isAuthenticated() ) { res.redirect('/login') } else { + let loggedInUser = await getLoggedInUserData(req.user.email) if (userData.email) { dbconn.user.query('UPDATE user SET ? WHERE email = "' +userData.email+'"', userData, function (err, rows, fields) { if (err) { @@ -280,10 +281,12 @@ module.exports = function (app, config, passport, i18n) { } }); - app.post('/changePwd', function (req, res) { - if(!req.isAuthenticated() && !loggedInUser) { + app.post('/changePwd', async function (req, res) { + if( !req.isAuthenticated() ) { res.redirect('/login') } else { + let loggedInUser = await getLoggedInUserData(req.user.email) + var currPwd = req.body.inputCurrPwd var newPwd = req.body.inputNewPwd var retypePwd = req.body.inputConfirm @@ -461,9 +464,10 @@ module.exports = function (app, config, passport, i18n) { // ============= NEW GITLAB PAGES =========================== app.get('/newInformation', async function(req, res){ - if (!req.isAuthenticated() && !loggedInUser) { + if ( !req.isAuthenticated() ) { res.redirect('/login') } else { + let loggedInUser = await getLoggedInUserData(req.user.email) let gitlabUser = await gitlab.getUserByEmail(loggedInUser.getEmail()) if (!gitlabUser) { // no user found res.redirect('/account/service') @@ -475,10 +479,12 @@ module.exports = function (app, config, passport, i18n) { } } }) - app.post('/newInformation', function(req, res) { - if(!req.isAuthenticated() && !loggedInUser) { + app.post('/newInformation', async function(req, res) { + if( !req.isAuthenticated() ) { res.redirect('/login') } else { + let loggedInUser = await getLoggedInUserData(req.user.email) + if (!req.body.name && !req.body.description) { res.flash('error', 'Bitte geben Sie die benötigten Daten ein') res.redirect('/account/newInformation') @@ -531,9 +537,11 @@ module.exports = function (app, config, passport, i18n) { }) app.get('/updateInformation', async function(req, res){ - if(!req.isAuthenticated() && !loggedInUser) { + if( !req.isAuthenticated() ) { res.redirect('/login') } else { + let loggedInUser = await getLoggedInUserData(req.user.email) + if(!req.query.id) { res.redirect('/account/services') } else { @@ -557,10 +565,12 @@ module.exports = function (app, config, passport, i18n) { } }) - app.post('/updateInformation', function(req, res){ - if(!req.isAuthenticated() && !loggedInUser) { + app.post('/updateInformation', async function(req, res){ + if( !req.isAuthenticated() ) { res.redirect('/login') } else { + let loggedInUser = await getLoggedInUserData(req.user.email) + if (!req.body.name && !req.body.description) { res.flash('error', 'Bitte geben Sie die benötigten Daten ein') res.redirect('/account/updateInformation') @@ -705,7 +715,6 @@ module.exports = function (app, config, passport, i18n) { // ============= USER VERIFICATION ================================ app.get("/verifyAccount", function(req, res){ - console.log(req.query) methods.getUserIdByVerificationToken(req.query.token, function(userId, err){ if (userId) { let userData = { @@ -743,9 +752,6 @@ module.exports = function (app, config, passport, i18n) { } }) - if(!loggedInUser) { - loggedInUser.setVerificationStatus(userData.verificationStatus) - } res.render(lang+'/account/verification', { status: true }); diff --git a/views/DE/account/profile.pug b/views/DE/account/profile.pug index 282fe411d91eda495e1f90fcc857872fc41c0d7c..135678b79946617377fa787cb045e503039898b4 100644 --- a/views/DE/account/profile.pug +++ b/views/DE/account/profile.pug @@ -38,7 +38,7 @@ html(lang="de") nav(aria-label="breadcrumb") ol(class="breadcrumb") li(class="breadcrumb-item") - a(href="/account") Konto + a(href="/account/") Konto li(class="breadcrumb-item active" aria-current="page") Benutzerprofil if flash.success