From ed62b6df15b72d34573baea17ec8e188d623cb62 Mon Sep 17 00:00:00 2001 From: Rosanny <rosanny.sihombing@hft-stuttgart.de> Date: Wed, 5 May 2021 16:51:04 +0200 Subject: [PATCH] restructure the project --- config/const.js | 17 ++++ {routes => config}/dbconn.js | 4 +- {routes => config}/dbconn2.js | 4 +- {routes => config}/mailer.js | 2 +- {routes => functions}/gitlab.js | 0 {routes => functions}/helpers.js | 0 {routes => functions}/methods.js | 16 +--- routes/routes-account.js | 135 +------------------------------ 8 files changed, 28 insertions(+), 150 deletions(-) create mode 100644 config/const.js rename {routes => config}/dbconn.js (93%) rename {routes => config}/dbconn2.js (93%) rename {routes => config}/mailer.js (89%) rename {routes => functions}/gitlab.js (100%) rename {routes => functions}/helpers.js (100%) rename {routes => functions}/methods.js (93%) diff --git a/config/const.js b/config/const.js new file mode 100644 index 00000000..54cbdfd0 --- /dev/null +++ b/config/const.js @@ -0,0 +1,17 @@ +module.exports = { + + mailSignature: 'Mit den besten Grüßen,<br/>das Transferportal-Team der HFT Stuttgart<br/><br/>' + + 'Transferportal der Hochschule für Technik Stuttgart<br/>' + + 'Schellingstr. 24 70174 Stuttgart<br/>' + + 'm4lab@hft-stuttgart.de<br/>' + + '<a href="https://transfer.hft-stuttgart.de">https://transfer.hft-stuttgart.de</a><br/>' + + '<a href="http://www.hft-stuttgart.de/Aktuell/"><img border="0" alt="HFT" src="https://m4lab.hft-stuttgart.de/img/signature/hft_logo.png" width="30" height="30"></a> ' + + '<a href="http://www.facebook.com/hftstuttgart"><img border="0" alt="Facebook" src="https://m4lab.hft-stuttgart.de/img/signature/fb_bw.png" width="30" height="30"></a> ' + + '<a href="https://www.instagram.com/hft_stuttgart/"><img border="0" alt="Instagram" src="https://m4lab.hft-stuttgart.de/img/signature/instagram_bw.png" width="30" height="30"></a> ' + + '<a href="https://twitter.com/hft_presse"><img border="0" alt="Twitter" src="https://m4lab.hft-stuttgart.de/img/signature/twitter_bw.png" width="30" height="30"></a> ' + + '<a href="https://www.youtube.com/channel/UCi0_JfF2qMZbOhOnNH5PyHA"><img border="0" alt="Youtube" src="https://m4lab.hft-stuttgart.de/img/signature/youtube_bw.png" width="30" height="30"></a> ' + + '<a href="http://www.hft-stuttgart.de/Aktuell/Presse-Marketing/SocialMedia/Snapcode HFT_Stuttgart.jpg/photo_view">' + + '<img border="0" alt="Snapchat" src="https://m4lab.hft-stuttgart.de/img/signature/snapchat_bw.png" width="30" height="30"></a>' + + '<br/><img border="0" src="https://m4lab.hft-stuttgart.de/img/signature/inno_bw.png" width="150" height="100">' + +} \ No newline at end of file diff --git a/routes/dbconn.js b/config/dbconn.js similarity index 93% rename from routes/dbconn.js rename to config/dbconn.js index c66d0d2d..d3275c4d 100644 --- a/routes/dbconn.js +++ b/config/dbconn.js @@ -1,7 +1,7 @@ const mysql = require('mysql') -var env = process.env.NODE_ENV || 'testing'; -const config = require('../config/config')[env] +var env = process.env.NODE_ENV || 'development'; +const config = require('./config')[env] // ==== USER ACOOUNT DB CONNECTION ==== var userConnection = mysql.createConnection({ diff --git a/routes/dbconn2.js b/config/dbconn2.js similarity index 93% rename from routes/dbconn2.js rename to config/dbconn2.js index fe22f1d8..1af13587 100644 --- a/routes/dbconn2.js +++ b/config/dbconn2.js @@ -1,7 +1,7 @@ const mysql = require('mysql2') -var env = process.env.NODE_ENV || 'testing'; -const config = require('../config/config')[env] +var env = process.env.NODE_ENV || 'development'; +const config = require('./config')[env] // ==== USER ACOOUNT DB CONNECTION ==== var userConnection = mysql.createConnection({ diff --git a/routes/mailer.js b/config/mailer.js similarity index 89% rename from routes/mailer.js rename to config/mailer.js index 2c51bd7d..d1e59ff0 100644 --- a/routes/mailer.js +++ b/config/mailer.js @@ -1,7 +1,7 @@ const nodemailer = require('nodemailer') var env = process.env.NODE_ENV || 'testing'; -const config = require('../config/config')[env] +const config = require('./config')[env] var smtpTransport = nodemailer.createTransport({ host: config.mailer.host, diff --git a/routes/gitlab.js b/functions/gitlab.js similarity index 100% rename from routes/gitlab.js rename to functions/gitlab.js diff --git a/routes/helpers.js b/functions/helpers.js similarity index 100% rename from routes/helpers.js rename to functions/helpers.js diff --git a/routes/methods.js b/functions/methods.js similarity index 93% rename from routes/methods.js rename to functions/methods.js index 1e9f5127..722ca56b 100644 --- a/routes/methods.js +++ b/functions/methods.js @@ -1,5 +1,5 @@ -const dbconn_OBSOLETE = require('./dbconn') // DO NOT USE THIS FOR NEW FEATURE -const dbconn = require('./dbconn2') +const dbconn_OBSOLETE = require('../config/dbconn') // DO NOT USE THIS FOR NEW FUNCTIONS +const dbconn = require('../config/dbconn2') var methods = { // ===================== user db ===================== @@ -65,18 +65,6 @@ var methods = { callback(err) }) }, - 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 { - if ( rows.length > 0) { - user = rows[0] - } - } - 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+'"') diff --git a/routes/routes-account.js b/routes/routes-account.js index 6c931ed9..8f00bed0 100644 --- a/routes/routes-account.js +++ b/routes/routes-account.js @@ -1,8 +1,8 @@ const fs = require('fs') const SamlStrategy = require('passport-saml').Strategy -const dbconn = require('./dbconn') -const methods = require('./methods') -const gitlab = require('./gitlab') +const dbconn = require('../config/dbconn') +const methods = require('../functions/methods') +const gitlab = require('../functions/gitlab') // pwd encryption const bcrypt = require('bcryptjs'); const saltRounds = 10; @@ -10,7 +10,7 @@ const salt = 64; // salt length // forgot pwd const async = require('async') const crypto = require('crypto') -const mailer = require('./mailer') +const mailer = require('../config/mailer') const logoDir = 'public/upload/' const defaultLogo = 'public/default/logo.png' const tpGitlabURL = 'https://transfer.hft-stuttgart.de/gitlab/' @@ -668,97 +668,6 @@ module.exports = function (app, config, passport, i18n) { } }) - // ============= NEW USERS REGISTRATION =========================== - app.get('/registration', function(req, res) { - res.render(lang+'/account/registration') - }) - app.post('/registration', function(req, res) { - // user data - var curDate = new Date() - var userData = { - salutation: req.body.inputSalutation, - title: req.body.inputTitle, - firstname: req.body.inputFirstname, - lastname: req.body.inputLastname, - email: req.body.inputEmail, - organisation: req.body.inputOrganisation, - industry: req.body.inputIndustry, - speciality: req.body.inputSpeciality, - createdDate: curDate.toISOString().slice(0,10) - } - - var userEmail = userData.email - var pos = userEmail.indexOf('@') - var emailLength = userEmail.length - var emailDomain = userEmail.slice(pos, emailLength); - - if ( emailDomain.toLowerCase() == "@hft-stuttgart.de") { - res.flash('error', "Fehlgeschlagen: HFT-Account") - res.redirect('/account/registration') - } - else { - let token - async.waterfall([ - function(done) { - crypto.randomBytes(20, function(err, buf) { - token = buf.toString('hex'); - done(err, token); - }); - }, - // encrypt password - function(token, done) { - bcrypt.genSalt(saltRounds, function(err, salt) { - bcrypt.hash(req.body.inputPassword, salt, function(err, hash) { - var newAccount = { - profile: userData, - password: hash, - verificationToken: token - } - done(err, newAccount) - }); - }); - }, - // save data - function(newAccount, err) { - methods.registerNewUser(newAccount, function(err){ - if (err) { - res.flash('error', "Fehlgeschlagen") - } - else { - // send email - var emailSubject = "Bitte bestätigen Sie Ihr M4_LAB Benutzerkonto" - // var emailContent = "Lieber Nutzer,\n\n"+ - // "vielen Dank für Ihre Anmeldung am Transferportal der HFT Stuttgart.\n"+ - // "Um Ihre Anmeldung zu bestätigen, klicken Sie bitte diesen Link: "+config.app.host+"/verifyAccount?token="+token+"\n"+ - // "Ohne Bestätigung Ihres Kontos müssen wir Ihr Konto leider nach 7 Tagen löschen.\n\n"+ - // "Sollten Sie sich selbst nicht mit Ihren Daten am Transferportal registriert haben, ignorieren Sie diese E-Mail bitte.\n\n"+mailSignature - var emailContent = '<div>Lieber Nutzer,<br/><br/>' + - '<p>vielen Dank für Ihre Anmeldung am Transferportal der HFT Stuttgart. <br/>' + - 'Um Ihre Anmeldung zu bestätigen, klicken Sie bitte diesen Link: ' + config.app.host + '/verifyAccount?token=' + token + - '<br/><br/>' + - 'Ohne Bestätigung Ihres Kontos müssen wir Ihr Konto leider nach 7 Tagen löschen.</p><br/>' + mailSignature + - '</div>'; - mailer.options.to = req.body.inputEmail; - mailer.options.subject = emailSubject; - mailer.options.html = emailContent; - mailer.transport.sendMail(mailer.options, function(err) { - if (err) { - console.log('cannot send email') - throw err - } - }) - // user feedback - res.flash('success', 'Vielen Dank für Ihre Registrierung!'+'\r\n\r\n'+ - 'Wir haben Ihnen eine E-Mail an Ihre verwendete Adresse gesendet. Diese enthält einen Link zur Bestätigung Ihres Accounts.'+'\r\n'+ - 'Wenn Sie die Mail nicht in ihrem Postfach vorfinden, prüfen Sie bitte auch Ihren Spam-Ordner.') - } - res.redirect('/account/registration') - }) - } - ]) - } - }) - // ============= USER VERIFICATION ================================ app.get("/verifyAccount", function(req, res){ methods.getUserIdByVerificationToken(req.query.token, function(userId, err){ @@ -870,40 +779,4 @@ module.exports = function (app, config, passport, i18n) { }) }) - app.get('/contact', function (req, res) { - res.render(lang+'/account/contact', { - user: req.user - }) - }) - - app.post('/contact', function(req, res, next) { - //methods.currentDate(); - let emailAddress = req.body.inputEmail; - let supportAddress = "support-transfer@hft-stuttgart.de"; - let inputName = req.body.name; - let inputContent = req.body.message; - let emailContent = "Es wurde eine Anfrage an das Transferportal gestellt: \n\n NAME: " + inputName + "\n NACHRICHT:\n "+ inputContent; - let emailSubject = "Ihre Anfrage an das Transferportal"; - async.waterfall([ - function(done) { - // send email - mailer.options.to = supportAddress; - mailer.options.cc = emailAddress; - mailer.options.subject = emailSubject; - mailer.options.text = emailContent; - mailer.transport.sendMail(mailer.options, function(err) { - done(err, 'done'); - }); - } - ], function(err) { - if (err) { - res.flash('error', 'Ein Fehler ist aufgetreten. Bitte versuchen Sie es erneut.'); - } - else { - res.flash('success', 'Vielen Dank für Ihre Anfrage. Wir melden uns baldmöglichst bei Ihnen. Eine Kopie Ihrer Anfrage wurde an ' + emailAddress + ' versandt.'); - } - res.redirect('/account/contact') - }) - }) - } \ No newline at end of file -- GitLab