Commit 346b22c7 authored by Rosanny Sihombing's avatar Rosanny Sihombing
Browse files

updates on MLAB-396

parent f8f078aa
...@@ -2,6 +2,7 @@ const fs = require('fs') ...@@ -2,6 +2,7 @@ const fs = require('fs')
const SamlStrategy = require('passport-saml').Strategy const SamlStrategy = require('passport-saml').Strategy
const dbconn = require('./dbconn') const dbconn = require('./dbconn')
const methods = require('./methods') const methods = require('./methods')
const gitlab = require('./gitlab')
// pwd encryption // pwd encryption
const bcrypt = require('bcryptjs'); const bcrypt = require('bcryptjs');
const saltRounds = 10; const saltRounds = 10;
...@@ -10,7 +11,10 @@ const salt = 64; // salt length ...@@ -10,7 +11,10 @@ const salt = 64; // salt length
const async = require('async') const async = require('async')
const crypto = require('crypto') const crypto = require('crypto')
const mailer = require('./mailer') const mailer = require('./mailer')
const superagent = require('superagent') const axios = require('axios')
const myUser = require('../classes/user')
var loggedInUser
module.exports = function (app, config, passport, i18n) { module.exports = function (app, config, passport, i18n) {
...@@ -113,6 +117,29 @@ module.exports = function (app, config, passport, i18n) { ...@@ -113,6 +117,29 @@ module.exports = function (app, config, passport, i18n) {
if (req.isAuthenticated()) { if (req.isAuthenticated()) {
methods.getUserByEmail(req.user.email, function(data, err){ methods.getUserByEmail(req.user.email, function(data, err){
if (!err) { if (!err) {
// Initialize user
if (!loggedInUser) {
loggedInUser = new myUser()
loggedInUser.id = data.id
loggedInUser.email = req.user.email
loggedInUser.salutation = data.salutation
loggedInUser.title = data.title
loggedInUser.firstName = data.firstname
loggedInUser.lastName = data.lastname
loggedInUser.industry = data.industry
loggedInUser.organisation = data.organisation
loggedInUser.speciality = data.speciality
loggedInUser.m4lab_idp = data.m4lab_idp
loggedInUser.verificationStatus = data.verificationStatus
methods.getGitlabId(data.id, function(gitlabUserId, err){
if(!err) {
loggedInUser.gitlabUserId = gitlabUserId
}
//console.log(loggedInUser)
})
}
res.render(lang+'/account/home', { res.render(lang+'/account/home', {
user: data user: data
}); });
...@@ -124,12 +151,11 @@ module.exports = function (app, config, passport, i18n) { ...@@ -124,12 +151,11 @@ module.exports = function (app, config, passport, i18n) {
}); });
app.get('/login', app.get('/login',
passport.authenticate(config.passport.strategy, passport.authenticate(config.passport.strategy, {
{
successRedirect: '/', successRedirect: '/',
failureRedirect: '/login' failureRedirect: '/login'
}) })
); )
app.get('/logout', function (req, res) { app.get('/logout', function (req, res) {
if (req.user == null) { if (req.user == null) {
...@@ -177,112 +203,109 @@ module.exports = function (app, config, passport, i18n) { ...@@ -177,112 +203,109 @@ module.exports = function (app, config, passport, i18n) {
}); });
app.get('/services', function (req, res) { app.get('/services', function (req, res) {
if (req.isAuthenticated()) { if (req.isAuthenticated() && loggedInUser) {
methods.getUserByEmail(req.user.email, function(data, err){ if (loggedInUser.verificationStatus == 1) {
if (!err) { let gitlabReposArr = []
if (data.verificationStatus == 1) { let gitlabPagesArr = []
// start =============== RS: MLAB-183 let userData = {
let userId = data.id firstname: loggedInUser.firstName,
methods.getGitlabId(userId, function(data, err){ lastname: loggedInUser.lastName,
if (!err) { m4lab_idp: loggedInUser.m4lab_idp}
if (data) {
console.log("TODO: GitLab is already activated for this user. Allow project creation.") if (loggedInUser.gitlabUserId) {
} // GitLab is already activated for this user. Allow project creation.
else { /*
superagent.get('https://transfer.hft-stuttgart.de/gitlab/api/v4/users?private_token='+config.gitlab.token_readWriteProjects+'&search='+req.user.email) waterfall([
.then(res => { function(callback){
if (res.body.length > 0) { callback(null, 'one', 'two');
let gitlabActivationData = { },
user_id: userId, function(arg1, arg2, callback){
gitlab_userId: res.body[0].id callback(null, 'three');
} },
methods.addGitlabUser(gitlabActivationData, function(err){}) function(arg1, callback){
} // arg1 now equals 'three'
else { callback(null, 'done');
console.log('TODO: Show gitlab activation button: transfer.hft-stuttgart.de/gitlab')
} }
}) ], function (err, result) {
.catch(err => { // result now equals 'done'
console.log(err.message)
}); });
*/
async.waterfall([
// check projects in runners
function(callback) {
let gitlabRunnersProjectIdsArr
gitlab.getProjectIdsFromRunners (loggedInUser.gitlabUserId, function(data){
if(data.error)
return res.status(500).send(data.data)
gitlabRunnersProjectIdsArr = data.data
callback(null, gitlabRunnersProjectIdsArr)
})
}
], function(err, gitlabRunnersProjectIdsArr) {
// get user projects
gitlab.getUserProjects (loggedInUser.gitlabUserId, function(data){
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
}
let page = {
name: gitlabData[i].name,
description: gitlabData[i].description,
avatar_url: gitlabData[i].avatar_url,
web_url: gitlabData[i].web_url,
isPublished: isWebsitePublished}
gitlabPagesArr.push(page)
} else {
let repo = {
name: gitlabData[i].name,
description: gitlabData[i].description,
avatar_url: gitlabData[i].avatar_url,
web_url: gitlabData[i].web_url}
gitlabReposArr.push(repo)
} }
} }
})
// end =============== RS: MLAB-183
res.render(lang+'/account/services', { res.render(lang+'/account/services', {
user: data user: userData,
}); gitlabRepos: gitlabReposArr,
/* !!! DO NOT DELETE. TEMPORARILY DISABLED FOR FUTURE USE. !!! gitlabPages: gitlabPagesArr
async.waterfall([
// get userId by email from userdb
function(done) {
methods.getUserIdByEmail(req.user.email, function(userId, err) {
if (!err) {
done(err, userId)
}
}) })
},
// get user-project-role from userdb
function(userId, done) {
methods.getUserProjectRole(userId, function(userProjects, err) {
if (!err) {
done(err, userProjects)
}
}) })
},
// get all projects from projectdb
function(userProjects, done) {
methods.getAllProjects(function(projectsOverview, err) {
if (!err) {
done(err, userProjects, projectsOverview)
}
}) })
}, } else {
// create JSON object of projects and user status for front-end gitlab.getUserIdByEmail(req.user.email, function(data){
function(userProjects, projectsOverview, done) { if (!data.error) {
var allProjects = [] // JSON object let gitlabActivationData = {
user_id: loggedInUser.id,
var userProjectId = [] // array of user's project_id gitlab_userId: data.data}
for (var i = 0; i < userProjects.length; i++) { methods.addGitlabUser(gitlabActivationData, function(err){
userProjectId.push(userProjects[i].project_id) if(!err) {
} loggedInUser.gitlabUserId = gitlabActivationData.gitlab_userId
res.redirect('/services')
for (var i = 0; i < projectsOverview.length; i++) {
// check if projectId is exist in userProjectId[]
var status = false
if (userProjectId.indexOf(projectsOverview[i].id) > -1) {
status = true
}
// add data to JSON object
allProjects.push({
id: projectsOverview[i].id,
title: projectsOverview[i].title,
summary: projectsOverview[i].onelinesummary,
cp: projectsOverview[i].contact_email,
userStatus: status
});
} }
})
// render the page } else {
res.render(lang+'/account/services', { res.render(lang+'/account/services', {
user: data, user: userData
project: allProjects })
});
} }
]) })
*/
} }
else { } else {
res.render(lang+'/account/home', { res.render(lang+'/account/home', {
user: data user: data
});
}
}
}) })
}
} else { } else {
res.redirect('/login'); res.redirect('/login')
} }
}); })
app.get('/security', function (req, res) { app.get('/security', function (req, res) {
if (req.isAuthenticated()) { if (req.isAuthenticated()) {
...@@ -557,6 +580,118 @@ module.exports = function (app, config, passport, i18n) { ...@@ -557,6 +580,118 @@ module.exports = function (app, config, passport, i18n) {
}); });
// ============= NEW GITLAB PAGES ===========================
app.get('/newPages', function(req, res){
if (req.isAuthenticated() && loggedInUser) {
let userData = {
firstName: loggedInUser.firstName,
lastName: loggedInUser.lastName,
m4lab_idp: loggedInUser.m4lab_idp
}
let projectData = {
name: null,
description: null,
avatar: null
}
res.render(lang+'/account/newPages', {
user: userData,
project: projectData
})
}
else {
res.redirect('/login');
}
})
app.post('/newPages', function(req, res) {
if (req.isAuthenticated() && loggedInUser) {
if (req.files && req.body.name && req.body.description) {
let projectName = req.body.name.toLowerCase().replace(/\s/g, '-')
let projectDesc = req.body.description
let projectAvatar = req.files.logo
let avatarDir = 'public/upload/'
let userData = {
firstName: loggedInUser.firstName,
lastName: loggedInUser.lastName,
m4lab_idp: loggedInUser.m4lab_idp}
async.waterfall([
// upload avatar
function(done) {
projectAvatar.mv(avatarDir + projectAvatar.name, function(err) {
if (err)
return res.status(500).send(err)
console.log('avatar uploaded')
done(err)
})
},
// call gitlab
function(done) {
let newPagesData = {
gitlabUserId: loggedInUser.gitlabUserId,
name: projectName,
description: projectDesc,
avatar: avatarDir+projectAvatar.name}
gitlab.createNewPages(newPagesData, function(msg, err){
if (err) { callback(err) }
else {
console.log(msg)
if(msg.message) {
if(msg.message.name == "has already been taken") {
let data = {
status: "error",
msg: "project name has already been taken, please choose another name"}
let projectData = {
name: projectName,
description: projectDesc,
avatar: projectAvatar}
req.flash("error", data.msg)
res.render(lang+'/account/newPages', {
user: userData,
project: projectData
})
}
}
else {
let data = {
status: "success",
msg: "success"}
let projectData = {
name: projectName,
description: projectDesc,
avatar: projectAvatar,
webUrl: msg.web_url}
req.flash("success", data.msg)
res.render(lang+'/account/newPages', {
user: userData,
project: projectData
})
}
}
done(err)
})
},
// delete avatar from /public/avatar
function(){
fs.unlink(avatarDir+projectAvatar.name, (err) => {
if (err) throw err;
console.log('avatar is successfully deleted');
})
}
])
}
else {
console.log('Please provide the data')
}
}
else {
res.redirect('/login');
}
})
// ============= NEW USERS REGISTRATION =========================== // ============= NEW USERS REGISTRATION ===========================
app.get('/registration', function(req, res) { app.get('/registration', function(req, res) {
res.render(lang+'/account/registration') res.render(lang+'/account/registration')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment