routes-account.js 36.2 KB
Newer Older
1
2
3
4
const fs = require('fs')
const SamlStrategy = require('passport-saml').Strategy
const dbconn = require('./dbconn')
const methods = require('./methods')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
5
const gitlab = require('./gitlab')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
6
// pwd encryption
7
8
9
const bcrypt = require('bcryptjs');
const saltRounds = 10;
const salt = 64; // salt length
Rosanny Sihombing's avatar
Rosanny Sihombing committed
10
// forgot pwd
11
12
const async = require('async')
const crypto = require('crypto')
13
const mailer = require('./mailer')
14
15
const logoDir = 'public/upload/'
const tpGitlabURL = 'https://transfer.hft-stuttgart.de/gitlab/'
Rosanny Sihombing's avatar
Rosanny Sihombing committed
16

17
18
19
const portalUser = require('../classes/user')
const projectInformation = require('../classes/website')
const projectRepo = require('../classes/repo')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
20

21
module.exports = function (app, config, passport, i18n) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
22

23
24
  var loggedInUser

Rosanny Sihombing's avatar
Rosanny Sihombing committed
25
26
27
28
29
30
31
32
33
  // =========== PASSPORT =======
  passport.serializeUser(function (user, done) {
    done(null, user);
  });

  passport.deserializeUser(function (user, done) {
    done(null, user);
  });

Varun Srivastava's avatar
Varun Srivastava committed
34
35
36
37
  const 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/>' +
Rosanny Sihombing's avatar
Rosanny Sihombing committed
38
    '<a href="https://transfer.hft-stuttgart.de">https://transfer.hft-stuttgart.de</a><br/>' +
39
40
41
42
43
    '<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>  &nbsp;' +
    '<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>  &nbsp;' +
    '<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>  &nbsp;' +
    '<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>  &nbsp;' +
    '<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>  &nbsp;' +
Varun Srivastava's avatar
Varun Srivastava committed
44
    '<a href="http://www.hft-stuttgart.de/Aktuell/Presse-Marketing/SocialMedia/Snapcode HFT_Stuttgart.jpg/photo_view">' +
45
46
    '<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">'
Varun Srivastava's avatar
Varun Srivastava committed
47

Rosanny Sihombing's avatar
Rosanny Sihombing committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  var samlStrategy = new SamlStrategy({
      // URL that goes from the Identity Provider -> Service Provider
      callbackUrl: config.passport.saml.path,
      // Base address to call logout requests
      logoutUrl: config.passport.saml.logoutUrl,
      
      entryPoint: config.passport.saml.entryPoint,
      issuer: config.passport.saml.issuer,
      identifierFormat: null,
      
      // Service Provider private key
      decryptionPvk: fs.readFileSync(__dirname + '/cert/key.pem', 'utf8'),
      // Service Provider Certificate
      privateCert: fs.readFileSync(__dirname + '/cert/key.pem', 'utf8'),
      // Identity Provider's public key
      cert: fs.readFileSync(__dirname + '/cert/cert_idp.pem', 'utf8'),
      
      validateInResponseTo: false,
      disableRequestedAuthnContext: true
67
68
69
70
71
72
73
74
  },
  function (profile, done) {
    return done(null, {
      id: profile.nameID,
      idFormat: profile.nameIDFormat,
      email: profile.email,
      firstName: profile.givenName,
      lastName: profile.sn
Rosanny Sihombing's avatar
Rosanny Sihombing committed
75
    });
76
  });
Rosanny Sihombing's avatar
Rosanny Sihombing committed
77
78
  
  passport.use(samlStrategy);
79
80
81
82
83

  // ============= SAML ==============
  app.post(config.passport.saml.path,
    passport.authenticate(config.passport.strategy,
      {
84
        failureRedirect: '/account/',
85
86
87
        failureFlash: true
      }),
    function (req, res) {
88
      res.redirect('/account/');
89
90
91
92
    }
  );

  // to generate Service Provider's XML metadata
93
  app.get('/saml/metadata',
94
95
96
97
98
99
    function(req, res) {
      res.type('application/xml');
      var spMetadata = samlStrategy.generateServiceProviderMetadata(fs.readFileSync(__dirname + '/cert/cert.pem', 'utf8'));
      res.status(200).send(spMetadata);
    }
  );
Wolfgang Knopki's avatar
Wolfgang Knopki committed
100
101
102
103
104
105
106

  // ================ test i18n ==================
  i18n.setLocale('de');
  app.get('/de', function(req, res) {
    var greeting = i18n.__('Hello World')
    res.send(greeting)
  });
107

Wolfgang Knopki's avatar
Wolfgang Knopki committed
108
  var lang = 'DE'
Rosanny Sihombing's avatar
DE    
Rosanny Sihombing committed
109

Wolfgang Knopki's avatar
Wolfgang Knopki committed
110
  // ======== APP ROUTES - ACCOUNT ====================
Rosanny Sihombing's avatar
DE    
Rosanny Sihombing committed
111
  var updatePasswordMailSubject = "Ihr Passwort für das Transferportal wurde gespeichert."
Varun Srivastava's avatar
Varun Srivastava committed
112
  var updatePasswordMailContent = '<div>Lieber Nutzer,<br/><br/>Ihr Passwort wurde erfolgreich geändert.<br/><br/>' + mailSignature + '</div>';
Rosanny Sihombing's avatar
DE    
Rosanny Sihombing committed
113

Rosanny Sihombing's avatar
Rosanny Sihombing committed
114
  app.get('/', function (req, res) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
115
116
117
    if ( !req.isAuthenticated() ) {
      res.redirect('/login')
    } else {
118
119
      methods.getUserByEmail(req.user.email, function(data, err){
        if (!err) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
120
121
          // Initialize user
          if (!loggedInUser) {
122
123
124
            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
            )
Rosanny Sihombing's avatar
Rosanny Sihombing committed
125
126
            methods.getGitlabId(data.id, function(gitlabUserId, err){
              if(!err) {
127
                loggedInUser.setGitlabUserId(gitlabUserId)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
128
129
130
131
              }
            })
          }
          
Rosanny Sihombing's avatar
Rosanny Sihombing committed
132
          res.render(lang+'/account/home', {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
133
            user: loggedInUser
134
135
136
          });
        }
      })
137
    }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
138
139
  });

Rosanny Sihombing's avatar
Rosanny Sihombing committed
140
  app.get('/login',
Rosanny Sihombing's avatar
Rosanny Sihombing committed
141
142
143
144
145
    passport.authenticate(config.passport.strategy, {
      successRedirect: '/',
      failureRedirect: '/login'
    })
  )
Rosanny Sihombing's avatar
Rosanny Sihombing committed
146

147
148
  app.get('/logout', function (req, res) {
    if (req.user == null) {
149
      return res.redirect('/');
Rosanny Sihombing's avatar
Rosanny Sihombing committed
150
    }
Wolfgang Knopki's avatar
Wolfgang Knopki committed
151

152
153
154
155
    req.user.nameID = req.user.id;
    req.user.nameIDFormat = req.user.idFormat;
    return samlStrategy.logout(req, function(err, uri) {
      req.logout();
156

157
158
159
160
161
162
163
      if ( req.session ) {
        req.session.destroy((err) => {
          if(err) {
              return console.log(err);
          }
        });
      }
164

165
166
167
      return res.redirect(uri);
    });
  });
Rosanny Sihombing's avatar
Rosanny Sihombing committed
168
169

  app.get('/profile', function (req, res) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
170
171
    if(!req.isAuthenticated() && !loggedInUser) {
      res.redirect('/login')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
172
    } else {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
173
174
175
176
177
178
179
      if(loggedInUser.getVerificationStatus() != 1) {
        res.redirect('/account/')
      } else {
        res.render(lang+'/account/profile', {
          user: loggedInUser
        })
      }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
180
    }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
181
  })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
182

183
184
185
186
  app.get('/services', function(req, res){
    if(!req.isAuthenticated() && !loggedInUser) {
      res.redirect('/login')
    } else {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
187
188
      if(loggedInUser.getVerificationStatus() != 1) { // unverified users
        res.redirect('/account/')
189
      } else {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
190
191
192
        let gitlabReposArr = []
        let gitlabPagesArr = []
        
Rosanny Sihombing's avatar
Rosanny Sihombing committed
193
        if(loggedInUser.getGitlabUserId()) { // for users who have activated their gitlab account
Rosanny Sihombing's avatar
Rosanny Sihombing committed
194
195
196
197
          async.waterfall([
            // check projects in runners
            function(callback) {
              let gitlabRunnersProjectIdsArr
198
              gitlab.getProjectIdsFromRunners(loggedInUser.getGitlabUserId(), function(data){
Rosanny Sihombing's avatar
Rosanny Sihombing committed
199
200
201
202
203
204
205
206
                if(data.error)
                  return res.status(500).send(data.data)
                gitlabRunnersProjectIdsArr = data.data
                callback(null, gitlabRunnersProjectIdsArr)
              })
            }
          ], function(err, gitlabRunnersProjectIdsArr) {
            // get user projects
Rosanny Sihombing's avatar
Rosanny Sihombing committed
207
            gitlab.getUserProjects(loggedInUser.getGitlabUserId(), function(data){
Rosanny Sihombing's avatar
Rosanny Sihombing committed
208
209
210
211
212
213
214
215
216
217
              if (data.error)
                return res.status(500).send(data.data)
              let gitlabData = data.data
              for(let i = 0; i < gitlabData.length; i++){
                if (gitlabData[i].tag_list.includes('website')) {
                  let idxRunners = gitlabRunnersProjectIdsArr.indexOf(gitlabData[i].id)
                  let isWebsitePublished = false
                  if (idxRunners > 0) {
                    isWebsitePublished = true
                  }
218
                  let page = new projectInformation(loggedInUser.getGitlabUserId(), gitlabData[i].id, gitlabData[i].name, gitlabData[i].description, 
Rosanny Sihombing's avatar
Rosanny Sihombing committed
219
                    gitlabData[i].avatar_url, gitlabData[i].path, null, null, isWebsitePublished)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
220
221
                  gitlabPagesArr.push(page)
                } else {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
222
223
                  let repo = new projectRepo(loggedInUser.getGitlabUserId(), gitlabData[i].id, gitlabData[i].name, gitlabData[i].description,
                    gitlabData[i].avatar_url, gitlabData[i].path)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
224
                  gitlabReposArr.push(repo)
225
226
                }
              }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
227
228
              
              res.render(lang+'/account/services', {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
229
                user: loggedInUser,
Rosanny Sihombing's avatar
Rosanny Sihombing committed
230
231
232
                gitlabRepos: gitlabReposArr,
                gitlabPages: gitlabPagesArr
              })
233
            })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
234
          })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
235
236
237
238
239
        } else { // for users who have not activated their gitlab account yet
          gitlab.getUserIdByEmail(loggedInUser.getEmail(), function(data){
            if (data.error) {
              res.status(500).render(lang+'/500', { error: data.data })
            } else {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
240
              let gitlabActivationData = {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
241
                user_id: loggedInUser.getId(),
Rosanny Sihombing's avatar
Rosanny Sihombing committed
242
243
                gitlab_userId: data.data}
              methods.addGitlabUser(gitlabActivationData, function(err){
Rosanny Sihombing's avatar
Rosanny Sihombing committed
244
245
246
247
248
                if(err) {
                  res.status(500).render(lang+'/500', { error: err })
                } else {
                  loggedInUser.setGitlabUserId(gitlabActivationData.gitlab_userId)
                  res.redirect('/account/services')
249
                }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
250
251
252
              })
            }
          })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
253
        }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
254
      }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
255
    }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
256
  })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
257
258

  app.get('/security', function (req, res) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
259
260
    if (!req.isAuthenticated() && !loggedInUser) {
      res.redirect('/login')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
261
    } else {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
262
263
264
265
266
267
268
      if(loggedInUser.getVerificationStatus() == 1 && loggedInUser.getIdpStatus() == 1) {
        res.render(lang+'/account/security', {
          user: loggedInUser
        })
      } else {
        res.redirect('/account/')
      }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
269
    }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
270
  })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
271
272
273

  app.post('/updateProfile', function (req, res) {
    var userData = {
Rosanny Sihombing's avatar
DE    
Rosanny Sihombing committed
274
      salutation: req.body.inputSalutation,
Rosanny Sihombing's avatar
Rosanny Sihombing committed
275
276
277
278
279
280
281
282
      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,
    }
283

Rosanny Sihombing's avatar
Rosanny Sihombing committed
284
285
286
    if (!req.isAuthenticated() && !loggedInUser) {
      res.redirect('/login')
    } else {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
287
      if (userData.email) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
288
        dbconn.user.query('UPDATE user SET ? WHERE email = "' +userData.email+'"', userData, function (err, rows, fields) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
289
            if (err) {
290
              res.flash('error', "Failed")
Rosanny Sihombing's avatar
Rosanny Sihombing committed
291
292
            }
            else {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
293
294
              loggedInUser.updateProfile(userData.salutation, userData.title, userData.firstname, userData.lastname, userData.email,
                userData.organisation, userData.industry, userData.speciality)
295
              res.flash('success', 'Ihr Benutzerprofil wurde aktualisiert!')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
296
            }
Wolfgang Knopki's avatar
Wolfgang Knopki committed
297
            res.redirect('/account/profile');
Rosanny Sihombing's avatar
Rosanny Sihombing committed
298
299
300
301
        })
      }
    }
  });
302

Rosanny Sihombing's avatar
Rosanny Sihombing committed
303
  app.post('/changePwd', function (req, res) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
304
305
306
    if(!req.isAuthenticated() && !loggedInUser) {
      res.redirect('/login')
    } else {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
307
308
309
310
      var currPwd = req.body.inputCurrPwd
      var newPwd = req.body.inputNewPwd
      var retypePwd = req.body.inputConfirm

Rosanny Sihombing's avatar
Rosanny Sihombing committed
311
312
313
314
315
316
317
      // update - get userId from loggedInUser
      dbconn.user.query('SELECT password FROM credential WHERE user_id='+loggedInUser.getId(), function (err, rows, fields) {
        if (err) {
          console.error(err)
          res.status(500).render(lang+'/500', { error: err })
        }
        var userPwd = rows[0].password
318

Rosanny Sihombing's avatar
Rosanny Sihombing committed
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
        // check if the password is correct
        bcrypt.compare(currPwd, userPwd, function(err, isMatch) {
          if (err) {
            console.error(err)
            res.status(500).render(lang+'/500', { error: err })
          } else if (!isMatch) {
            res.flash('error', "Das Passwort ist leider falsch. Bitte überprüfen Sie Ihre Eingabe.")
            res.redirect('/account/security')
          } else {
            if ( newPwd != retypePwd ) {
              res.flash('error', 'Passwörter stimmen nicht überein. Bitte stellen Sie sicher, dass Sie das Passwort beide Male genau gleich eingeben.')
              res.redirect('/account/security')
            } else {
              // update password
              bcrypt.genSalt(saltRounds, function(err, salt) {
                bcrypt.hash(newPwd, salt, function(err, hash) {
                  var credentialData = {
                    password: hash,
                    user_id: userId
                  }
                  methods.updateCredential(credentialData, function(err){
                    if (err) {
                      res.flash('error', "Datenbankfehler: Passwort kann nicht geändert werden.")
                      throw err
                    } else {
                      res.flash('success', "Passwort aktualisiert!")
                      mailer.options.to = req.user.email
                      mailer.options.subject = updatePasswordMailSubject
                      mailer.options.html = updatePasswordMailContent
                      mailer.transport.sendMail(mailer.options, function(err) {
                        if (err) { console.log(err) }
                      });
                    }
                    res.redirect('/account/security')
                  })
                });
              });
            }
          }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
358
        })
359
      })
360
    }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
361
362
363
  });

  app.get('/forgotPwd', function (req, res) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
364
    res.render(lang+'/account/forgotPwd', {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
365
366
367
368
369
      user: req.user
    });
  });

  app.post('/forgotPwd', function(req, res, next) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
370
    let emailAddress = req.body.inputEmail
Rosanny Sihombing's avatar
Rosanny Sihombing committed
371
372
373
374
375
376
377
378
379
380
381
    async.waterfall([
      function(done) {
        crypto.randomBytes(20, function(err, buf) {
          var token = buf.toString('hex');
          done(err, token);
        });
      },
      function(token, done) {
        methods.checkUserEmail(emailAddress, function(err, user){
          if (user) {
            console.log("email: user found");
Rosanny Sihombing's avatar
DE    
Rosanny Sihombing committed
382
            var emailSubject = "Ihre Passwort-Anfrage an das Transferportal der HFT Stuttgart";
Varun Srivastava's avatar
Varun Srivastava committed
383
384
            // var emailContent = "Lieber Nutzer,\n\n"+
            //   "wir haben Ihre Anfrage zur Erneuerung Ihres Passwortes erhalten. Falls Sie diese Anfrage nicht gesendet haben, ignorieren Sie bitte diese E-Mail.\n\n"+
Rosanny Sihombing's avatar
Rosanny Sihombing committed
385
            //   "Sie können Ihr Passwort mit dem Klick auf diesen Link ändern: http://localhost:9989/reset/" + token + "\n" + // localhost
Varun Srivastava's avatar
Varun Srivastava committed
386
387
388
389
390
391
392
            //   "Dieser Link ist aus Sicherheitsgründen nur für 1 Stunde gültig.\n\n"+mailSignature

            var emailContent = '<div>Lieber Nutzer, Varun<br/><br/>' +
              '<p>wir haben Ihre Anfrage zur Erneuerung Ihres Passwortes erhalten. Falls Sie diese Anfrage nicht gesendet haben, ignorieren Sie bitte diese E-Mail.<br/><br/>' +
              'Sie können Ihr Passwort mit dem Klick auf diesen Link ändern: http://m4lab.hft-stuttgart.de/account/reset/' + token + '<br/>' + // test server
              'Dieser Link ist aus Sicherheitsgründen nur für 1 Stunde gültig.<br/></p>' + mailSignature + '</div>';
            
393
394
395
396
397
398
            var credentialData = {
              user_id: user.id,
              resetPasswordToken: token,
              resetPasswordExpires: Date.now() + 3600000 // 1 hour
            }
            methods.updateCredential(credentialData, function(err) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
399
400
              done(err, token, user);
            });
401
402

            // send email
403
404
            mailer.options.to = emailAddress;
            mailer.options.subject = emailSubject;
Varun Srivastava's avatar
Varun Srivastava committed
405
            mailer.options.html = emailContent;
406
            mailer.transport.sendMail(mailer.options, function(err) {
407
408
              done(err, 'done');
            });
Rosanny Sihombing's avatar
Rosanny Sihombing committed
409
410
          }
          else {
411
            done(err, 'no user found');
Rosanny Sihombing's avatar
Rosanny Sihombing committed
412
413
414
415
416
          }
        });
      }
    ], function(err) {
      if (err) {
417
        res.flash('error', 'Ein Fehler ist aufgetreten. Bitte versuchen Sie es erneut.');
Rosanny Sihombing's avatar
Rosanny Sihombing committed
418
419
      }
      else {
420
        res.flash('success', 'Wenn Ihre E-Mail-Adresse registriert ist, wurde eine E-Mail mit dem weiteren Vorgehen an ' + emailAddress + ' versendet.');
Rosanny Sihombing's avatar
Rosanny Sihombing committed
421
      }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
422
      res.redirect('/account/forgotPwd');
Rosanny Sihombing's avatar
Rosanny Sihombing committed
423
424
425
426
    });
  });

  app.get('/reset/:token', function(req, res) {
427
    methods.getUserByToken(req.params.token, function(err, user){
Rosanny Sihombing's avatar
Rosanny Sihombing committed
428
      if (!user) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
429
430
        res.flash('error', 'Der Schlüssel zum zurücksetzen des Passworts ist ungültig oder abgelaufen.')
        res.redirect('/account/forgotPwd')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
431
432
      }
      else {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
433
        res.render(lang+'/account/reset')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
434
435
436
437
438
      }
    });
  });

  app.post('/reset/:token', function(req, res) {
439
    var newPwd = req.body.inputNewPwd
440
    methods.getUserByToken(req.params.token, function(err, user){
Rosanny Sihombing's avatar
Rosanny Sihombing committed
441
      if (user) {
442
        // encrypt password
Rosanny Sihombing's avatar
Rosanny Sihombing committed
443
        bcrypt.genSalt(saltRounds, function(err, salt) {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
444
          bcrypt.hash(newPwd, salt, function(err, hash) {
445
446
447
448
449
450
            var credentialData = {
              password: hash,
              user_id: user.user_id
            }
            // update password
            methods.updateCredential(credentialData, function(err){
Rosanny Sihombing's avatar
Rosanny Sihombing committed
451
              if (err) {
452
                res.flash('error', "Datenbankfehler: Passwort kann nicht geändert werden.")
Rosanny Sihombing's avatar
Rosanny Sihombing committed
453
454
455
                throw err
              }
              else {
456
                res.flash('success', "Passwort aktualisiert!")
457
                // send notifiaction email
458
459
                mailer.options.to = user.email
                mailer.options.subject = updatePasswordMailSubject
Varun Srivastava's avatar
Varun Srivastava committed
460
                mailer.options.html = updatePasswordMailContent
461
                mailer.transport.sendMail(mailer.options, function(err) {
462
463
464
465
466
                  if (err) {
                    console.log(err)
                  }
                });
                // redirect to login page
467
                res.redirect('/login')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
468
469
470
471
472
473
              }
            })
          });
        });
      }
      else {
474
        res.flash('error', "User not found.")
475
        res.redirect('/login')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
476
477
      }
    });
478

Rosanny Sihombing's avatar
Rosanny Sihombing committed
479
480
  });

Rosanny Sihombing's avatar
Rosanny Sihombing committed
481
  // ============= NEW GITLAB PAGES ===========================
482
483
484
485
486
487
  
  app.get('/newInformation', function(req, res){
    if (!req.isAuthenticated() && !loggedInUser) {
      res.redirect('/login')
    } else {
      res.render(lang+'/account/newInformation', {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
488
        user: loggedInUser
Rosanny Sihombing's avatar
Rosanny Sihombing committed
489
490
      })
    }
491
492
493
494
495
496
  })
  app.post('/newInformation', function(req, res) {
    if(!req.isAuthenticated() && !loggedInUser) {
      res.redirect('/login')
    } else {
      if (!req.body.name && !req.body.description) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
497
        res.flash('error', 'Bitte geben Sie die benötigten Daten ein')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
498
        res.redirect('/account/newInformation')
499
500
501
      } else {
        let projectName = req.body.name.toLowerCase().replace(/\s/g, '-')
        let projectDesc = req.body.description
Rosanny Sihombing's avatar
Rosanny Sihombing committed
502
        let newInformation = new projectInformation(loggedInUser.getGitlabUserId(), null, projectName, projectDesc, null, null, null, null, false)
503
504
            
        if (!req.files) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
505
          res.flash('error', 'Bitte geben Sie ein Projektlogo an.')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
506
          res.redirect('/account/newInformation')
507
508
509
510
511
512
513
514
515
516
517
518
519
520
        } else {
          let newLogoFile = req.files.logo
          async.waterfall([
            function(callback){ // upload logo
              newLogoFile.mv(logoDir + newLogoFile.name, function(err) {
                newLogoFile = logoDir+newLogoFile.name
                callback(err, newLogoFile)
              })
            },
            function(newLogoFile, callback){ // create a new GitLab Page
              gitlab.createNewPages(newInformation, newLogoFile, function(data){
                let result = data.data
                if (data.error) {
                  if(result.message.name == "has already been taken") {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
521
                    res.flash("error", "Der Projektname '"+newInformation.getName()+"' ist bereits vergeben, bitte wählen Sie einen anderen Namen.")
522
                  } else {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
523
                    res.flash("error", "Ein Fehler ist aufgetreten. Bitte versuchen Sie es erneut. ")
524
                  }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
525
                  res.redirect('/account/newInformation')
526
527
528
                } else {
                  newInformation.setId(result.id)
                  newInformation.setLogo(result.avatar_url)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
529
                  newInformation.setPath(result.path)
530
531
                  newInformation.setSettingUrl(tpGitlabURL+result.namespace.path+'/'+result.name+'/-/edit/master/public/settings.js')
                  newInformation.setKontaktUrl(tpGitlabURL+result.namespace.path+'/'+result.name+'/-/edit/master/public/kontakt.html')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
532
533
534
        
                  res.flash("success", "Ihre Webseite wurde erstellt, aber noch nicht veröffentlicht. Bitte fahren Sie mit Schritten 2 und 3 fort, um Ihre Webseite zu veröffentlichen.")
                  res.redirect('/account/updateInformation?id='+newInformation.getId()+'&s=n')
535
536
537
538
539
540
541
542
543
544
545
546
547
                }
                callback(null)
              })
            }
          ], function (err) {
            if(err) console.log(err)
            // remove logo
            fs.unlink(newLogoFile, (err) => {
              if(err) console.log(err)
            })
          })
        }
      }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
548
549
550
    }
  })

551
552
553
554
555
556
557
558
559
  app.get('/updateInformation', function(req, res){
    if(!req.isAuthenticated() && !loggedInUser) {
      res.redirect('/login')
    } else {
      if(!req.query.id) {
        res.redirect('/account/services')
      } else {
        gitlab.getUserProjects(loggedInUser.getGitlabUserId(), function(data){
          if (data.error) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
560
            res.status(500).render(lang+'/500', { error: data.data })
561
562
563
564
565
566
567
568
569
570
571
572
          } else {
            // quick way to decide whether a website is already published or not
            let informationStatus
            if(req.query.s != "y" && req.query.s != "n") {
              res.redirect('/account/services')
            } else {
              if(req.query.s == "y") {
                informationStatus = true
              } else if(req.query.s == "n") {
                informationStatus = false
              }
              let gitlabData = data.data
Rosanny Sihombing's avatar
Rosanny Sihombing committed
573
              let curInformation = new projectInformation(loggedInUser.getGitlabUserId(), req.query.id, null, null, null, null, null, null, informationStatus)
574
575
576
577
578
              for(let i = 0; i < gitlabData.length; i++){
                if (gitlabData[i].id == req.query.id) {
                  curInformation.setName(gitlabData[i].name)
                  curInformation.setDesc(gitlabData[i].description)
                  curInformation.setLogo(gitlabData[i].avatar_url)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
579
                  curInformation.setPath(gitlabData[i].path)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
580
581
                  curInformation.setSettingUrl(tpGitlabURL+gitlabData[i].path_with_namespace+'/-/edit/master/public/settings.js')
                  curInformation.setKontaktUrl(tpGitlabURL+gitlabData[i].path_with_namespace+'/-/edit/master/public/kontakt.html')
582
583
584
585
586
                  
                  break
                }
              }
              res.render(lang+'/account/updateInformation', {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
587
                user: loggedInUser,
588
589
590
591
592
593
594
595
596
597
598
599
600
                information: curInformation
              })
            }            
          }
        })
      }
    }
  })
  app.post('/updateInformation', function(req, res){
    if(!req.isAuthenticated() && !loggedInUser) {
      res.redirect('/login')
    } else {
      if (!req.body.name && !req.body.description) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
601
        res.flash('error', 'Bitte geben Sie die benötigten Daten ein')
Rosanny Sihombing's avatar
Rosanny Sihombing committed
602
        res.redirect('/account/updateInformation')
603
      } else {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
604
605
        let projectName = req.body.name.toLowerCase().replace(/\s/g, '-')
        let projectDesc = req.body.description
Rosanny Sihombing's avatar
Rosanny Sihombing committed
606
607
608
609
        let isProjectPublished = true
        if (req.body.isPublished == "false") {
          isProjectPublished = false
        }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
610
        let updatedInformation = new projectInformation(loggedInUser.getGitlabUserId(), req.query.id, projectName, projectDesc, null, null, null, null, isProjectPublished)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
611

612
        let newLogoFile
Rosanny Sihombing's avatar
Rosanny Sihombing committed
613
        async.waterfall([
614
615
616
617
618
619
620
621
622
623
          function(callback){ // upload logo
            if(!req.files) {
              callback(null, newLogoFile)
            } else {
              newLogoFile = req.files.logo
              newLogoFile.mv(logoDir + newLogoFile.name, function(err) {
                newLogoFile = logoDir + newLogoFile.name
                callback(err, newLogoFile)
              })
            }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
624
          },
625
626
627
628
629
          function(newLogoFile, callback){ // update gitlab page
            gitlab.updateProject(updatedInformation, newLogoFile, function(data){
              let result = data.data
              if (data.error) {
                if(result.message.name == "has already been taken") {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
630
                  res.flash("error", "Der Projektname ist bereits vergeben, bitte wählen Sie einen anderen Namen.")
631
                } else {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
632
                  res.flash("error", "Ein Fehler ist aufgetreten. Bitte versuchen Sie es erneut. ")
Rosanny Sihombing's avatar
Rosanny Sihombing committed
633
                }
634
635
              } else {
                updatedInformation.setLogo(result.avatar_url)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
636
                updatedInformation.setPath(result.path)
637
638
639
                updatedInformation.setSettingUrl(tpGitlabURL+result.namespace.path+'/'+result.name+'/-/edit/master/public/settings.js')
                updatedInformation.setKontaktUrl(tpGitlabURL+result.namespace.path+'/'+result.name+'/-/edit/master/public/kontakt.html')
                res.flash("success", "Your website has been updated")
Rosanny Sihombing's avatar
Rosanny Sihombing committed
640
              }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
641
642
              res.redirect('/account/updateInformation?id='+updatedInformation.getId())
              
643
              callback(null)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
644
            })
645
646
647
648
649
650
          }
        ], function (err) {
          if(err) console.log(err)
          if(newLogoFile){ // remove logo
            fs.unlink(newLogoFile, (err) => {
              if(err) console.log(err)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
651
652
            })
          }
653
        })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
654
655
      }
    }
656
657
658
659
660
661
662
663
664
665
666
667
668
  })

  // RS: delete projektInformation?

  app.post('/sendPublishRequest', function(req, res) {
    if (!req.isAuthenticated() && loggedInUser) {
      res.redirect('/login')
    } else {
      let emailAddress = loggedInUser.getEmail()
      let supportAddress = "support-transfer@hft-stuttgart.de"
      let projectName = req.body.projectName
      let emailContent = "Guten Tag, \n\nhiermit beantrage Ich die Freischaltung einer Webseite auf dem Transferportal für folgendes Projekt: \n"
        +projectName+"\n\nVielen Dank,\n"+loggedInUser.getFullName()
Rosanny Sihombing's avatar
Rosanny Sihombing committed
669
      let emailSubject = "M4_LAB Anfrage zur Veröffentlichung einer neuen Webseite"
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
      async.waterfall([
        function(done) {
            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) {
          console.log(err)
          res.send('Ein Fehler ist aufgetreten. Bitte versuchen Sie es erneut.')
        }
        else {
          res.send('Vielen Dank für Ihre Anfrage. Eine Kopie Ihrer Anfrage wurde an ' + emailAddress + ' versandt.')
        }
      })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
689
690
    }
  })
691

692
  // ============= NEW USERS REGISTRATION ===========================
693
  app.get('/registration', function(req, res) {
Rosanny Sihombing's avatar
Rosanny Sihombing committed
694
    res.render(lang+'/account/registration')
695
696
697
698
699
  })
  app.post('/registration', function(req, res) {
    // user data
    var curDate = new Date()
    var userData = {
Rosanny Sihombing's avatar
DE    
Rosanny Sihombing committed
700
      salutation: req.body.inputSalutation,
701
702
703
704
705
706
707
708
      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)
Rosanny Sihombing's avatar
Rosanny Sihombing committed
709
    }
710

711
712
713
714
715
716
    var userEmail = userData.email
    var pos = userEmail.indexOf('@')
    var emailLength = userEmail.length
    var emailDomain = userEmail.slice(pos, emailLength);

    if ( emailDomain.toLowerCase() == "@hft-stuttgart.de") {
717
718
        res.flash('error', "Fehlgeschlagen: HFT-Account")
        res.redirect('/account/registration')
719
720
721
722
723
724
725
726
    }
    else {
      let token
      async.waterfall([
        function(done) {
          crypto.randomBytes(20, function(err, buf) {
            token = buf.toString('hex');
            done(err, token);
727
          });
728
729
730
731
732
733
734
735
736
        },
        // 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
737
              }
738
739
740
741
742
743
744
745
              done(err, newAccount)
            });
          });
        },
        // save data
        function(newAccount, err) {
          methods.registerNewUser(newAccount, function(err){
            if (err) {
746
              res.flash('error', "Fehlgeschlagen")
747
748
749
750
            }
            else {
              // send email
              var emailSubject = "Bitte bestätigen Sie Ihr M4_LAB Benutzerkonto"
Varun Srivastava's avatar
Varun Srivastava committed
751
752
753
754
755
756
757
758
759
760
761
              // 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>';
762
763
              mailer.options.to = req.body.inputEmail;
              mailer.options.subject = emailSubject;
Varun Srivastava's avatar
Varun Srivastava committed
764
              mailer.options.html = emailContent;
765
766
767
768
769
770
771
              mailer.transport.sendMail(mailer.options, function(err) {
                if (err) {
                  console.log('cannot send email')
                  throw err
                }
              })
              // user feedback
772
              res.flash('success', 'Vielen Dank für Ihre Registrierung!'+'\r\n\r\n'+
773
774
775
776
777
778
779
780
                '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')
          })
        }
      ])
    }
781
782
783
784
  })

  // ============= USER VERIFICATION ================================
  app.get("/verifyAccount", function(req, res){
785
    console.log(req.query)
786
787
788
789
790
791
792
793
794
795
796
797
798
799
    methods.getUserIdByVerificationToken(req.query.token, function(userId, err){
      if (userId) {
        let userData = {
          id: userId,
          verificationStatus: 1
        }
        methods.verifyUserAccount(userData, function(err){
          if (err) {
            console.log("Error: "+err)
            res.render(lang+'/account/verification', {
              status: false
            });
          }
          else {
800
801
802
803
804
805
806
807
            // send welcome email after successful account verification
            methods.getUserById(userId, function(data, err){
              if (err) {
                console.log("Error: "+err)
              }
              else {
                // send email
                var emailSubject = "Herzlich willkommen"
Varun Srivastava's avatar
Varun Srivastava committed
808
809
810
                var emailContent = '<div>Lieber Nutzer,<br/><br/>' +
                  '<p>herzlich willkommen beim Transferportal der HFT Stuttgart!<br/>' +
                  'Sie können nun alle Dienste des Portals nutzen.<p/><br/>' + mailSignature;
811
812
                mailer.options.to = data.email;
                mailer.options.subject = emailSubject;
Varun Srivastava's avatar
Varun Srivastava committed
813
                mailer.options.html = emailContent;
814
815
816
817
818
819
820
821
822
                mailer.transport.sendMail(mailer.options, function(err) {
                  if (err) {
                    console.log('cannot send email')
                    throw err
                  }
                })
              }
            })

Rosanny Sihombing's avatar
Rosanny Sihombing committed
823
824
825
            if(!loggedInUser) {
              loggedInUser.setVerificationStatus(userData.verificationStatus)
            }
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
            res.render(lang+'/account/verification', {
              status: true
            });
          }
        })
      }
      else {
        res.render(lang+'/account/verification', {
          status: null
        });
      }
    })
  })
  app.get("/resendVerificationEmail", function(req, res){
    if (req.isAuthenticated()) {
      var emailAddress = req.user.email
      
      methods.getUserIdByEmail(req.user.email, function(userId, err) {
        if (!err) {
          // get token
          methods.getVerificationTokenByUserId(userId, function(token, err){
            if (!err) {
              if (token) {
                // send email
                var emailSubject = "Bitte bestätigen Sie Ihr M4_LAB Benutzerkonto"
Varun Srivastava's avatar
Varun Srivastava committed
851
852
853
854
855
856
857
858
859
860
                // var emailContent = "Lieber Nutzer,\n\n"+
                //     "vielen Dank für Ihre Anmeldung am Transferportal der HFT Stuttgart. "+ 
                //     "\nUm Ihre Anmeldung zu bestätigen, klicken Sie bitte diesen Link: "+config.app.host+"/verifyAccount?token="+token+
                //     "\n\nOhne Bestätigung Ihres Kontos müssen wir Ihr Konto leider nach 7 Tagen löschen.\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>';
861
862
                mailer.options.to = emailAddress;
                mailer.options.subject = emailSubject;
Varun Srivastava's avatar
Varun Srivastava committed
863
                mailer.options.html = emailContent;
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
                mailer.transport.sendMail(mailer.options, function(err) {
                  if (err) {
                    console.log('cannot send email')
                    throw err
                  }
                })
                res.send(true)
              }
              else {
                res.send(false)
              }
            }
            else {
              console.log(err)
            }
          })
        }
      })
    }
883
  })
Rosanny Sihombing's avatar
Rosanny Sihombing committed
884

885
886
887
888
889
890
891
892
893
894
895
896
  app.get('/email/:email', function(req, res) {
    methods.checkUserEmail(req.params.email, function(err, user){
      if (!err) {
        if (user) {
          res.send(false)
        }
        else {
          res.send(true)
        }  
      }
    })
  })
Wolfgang Knopki's avatar
Wolfgang Knopki committed
897
898

  app.get('/contact', function (req, res) {
899
900
901
902
    res.render(lang+'/account/contact', {
      user: req.user
    })
  })
Wolfgang Knopki's avatar
Wolfgang Knopki committed
903

Rosanny Sihombing's avatar
Rosanny Sihombing committed
904
  app.post('/contact', function(req, res, next) {
Wolfgang Knopki's avatar
Wolfgang Knopki committed
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
      //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) {
925
          res.flash('error', 'Ein Fehler ist aufgetreten. Bitte versuchen Sie es erneut.');
Wolfgang Knopki's avatar
Wolfgang Knopki committed
926
927
        }
        else {
928
          res.flash('success', 'Vielen Dank für Ihre Anfrage. Wir melden uns baldmöglichst bei Ihnen. Eine Kopie Ihrer Anfrage wurde an ' + emailAddress + ' versandt.');
Wolfgang Knopki's avatar
Wolfgang Knopki committed
929
        }
Rosanny Sihombing's avatar
Rosanny Sihombing committed
930
        res.redirect('/account/contact')
931
932
933
934
      })
  })

}