Skip to content
This repository has been archived by the owner on May 27, 2020. It is now read-only.

Commit

Permalink
Merge pull request #24 from eapowertools/convert-to-csv
Browse files Browse the repository at this point in the history
Convert to csv
  • Loading branch information
goldbergjeffrey authored Mar 15, 2017
2 parents 9c85c9e + e100f05 commit ed58860
Show file tree
Hide file tree
Showing 20 changed files with 617 additions and 231 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ignoring folders
tmp/
tests/

# Logs
logs
Expand Down
24 changes: 13 additions & 11 deletions config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var routePath = path.join(__dirname, 'server/routes/');
var publicPath = path.join(__dirname, 'public/');
var utilsPath = path.join(__dirname, '/../utils/');
var excelFilePath = path.join(__dirname, '/../udc/excel/iportal_users.xlsx');
var csvFilePath = path.join(__dirname, '/../udc/');

var logFile = logPath + 'iPortal_' + dateTimeString() + '.log';

Expand All @@ -16,34 +17,35 @@ var config = extend(true, {
qrsPort: 4242,
repoAccount: 'UserDirectory=Internal;UserId=sa_repository',
hostname: 'senseServerName',
virtualProxy: 'iPortal',
virtualProxy: 'iportal',
allowedConnections: 'domainOfSystems',
userDirectory: 'iportal',
sessionSecret: 'iportal-secret',
certificates: {
client: path.resolve(certPath, 'client.pem'),
client_key: path.resolve(certPath,'client_key.pem'),
server: path.resolve(certPath, 'server.pem'),
server_key: path.resolve(certPath, 'server_key.pem'),
root: path.resolve(certPath,'root.pem')
},
client_key: path.resolve(certPath, 'client_key.pem'),
server: path.resolve(certPath, 'server.pem'),
server_key: path.resolve(certPath, 'server_key.pem'),
root: path.resolve(certPath, 'root.pem')
},
routePath: routePath,
publicPath: publicPath,
logPath: logPath,
logFile: logFile,
utilsPath: utilsPath,
excelFilePath: excelFilePath,
csvFilePath: csvFilePath,
logLevel: 'debug',
theme: 'photos'
theme: 'photos',
version: '1.5.0'
});

module.exports = config;

function dateTimeString()
{
function dateTimeString() {
var now = new Date();
var strDate = now.toISOString();

return strDate.split(':').join('.');

}
28 changes: 28 additions & 0 deletions lib/appPaths.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var csv = require('csvjson');
var cfg = require("../config/config")
var path = require("path");
var winston = require("winston");
var fs = require('fs');

//set up logging
var logger = new(winston.Logger)({
level: cfg.logLevel,
transports: [
new(winston.transports.Console)(),
new(winston.transports.File)({ filename: cfg.logFile })
]
});

module.exports = function() {
var appsFile = fs.readFileSync(path.join(cfg.csvFilePath, "app_paths.csv"), { encoding: 'utf8' });

var appsArray = csv.toObject(appsFile);
if (appsArray.length > 0) {
logger.info("appPaths: ", appsArray.length, " apps loaded");
} else {
logger.info("appPaths: no apps parsed from csv file");
throw new Error("error: no apps parsed from csv file");
}
return appsArray;

};
84 changes: 43 additions & 41 deletions lib/login.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,40 @@
var express = require('express');
var https = require('https');
var cfg = require('../config/config');
var url= require('url');
var url = require('url');
var fs = require('fs');
var winston = require('winston');
var querystring = require("querystring");
var appPaths = require("./appPaths")();

//set up logging
var logger = new (winston.Logger)({
level: cfg.logLevel,
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ filename: cfg.logFile})
var logger = new(winston.Logger)({
level: cfg.logLevel,
transports: [
new(winston.transports.Console)(),
new(winston.transports.File)({ filename: cfg.logFile })
]
});

module.exports = {
requestticket: function(req, res, next, selectedUser, userDirectory, restURI, userApp) {

requestticket: function(req, res, next, selectedUser, userDirectory, restURI, userApp, path) {



var XRFKEY = rand(16);
//Configure parameters for the ticket request
try {

logger.debug(cfg.client);

var options = {
host: url.parse(restURI).hostname,
port: url.parse(restURI).port,
path: url.parse(restURI).path + '/ticket?xrfkey=' + XRFKEY,
method: 'POST',
headers:
{
'X-qlik-xrfkey': XRFKEY,
'Content-Type': 'application/json'
headers: {
'X-qlik-xrfkey': XRFKEY,
'Content-Type': 'application/json'
},
cert: fs.readFileSync(cfg.certificates.client),
key: fs.readFileSync(cfg.certificates.client_key),
Expand All @@ -41,64 +43,64 @@ module.exports = {
};
} catch (e) {
logger.error(e);
res.render('error', e);
res.render('error', e);
}

logger.info("requestTicket: Ticket Options (" , options.path.toString() , ")");
logger.info("requestTicket: Ticket Options (", options.path.toString(), ")");
//Send ticket request
var ticketreq = https.request(options, function (ticketres) {
logger.info("requestTicket: statusCode: (", ticketres.statusCode,")");
var ticketreq = https.request(options, function(ticketres) {
logger.info("requestTicket: statusCode: (", ticketres.statusCode, ")");

ticketres.on('data', function (d) {
ticketres.on('data', function(d) {
//Parse ticket response
logger.info("requestTicket: POST Response \n", d.toString());
if (ticketres.statusCode!=201) {
if (ticketres.statusCode != 201) {
var authError = {};
authError.message = "Invalid response code ("+ticketres.statusCode+") from Qlik Sense.";
authError.message = "Invalid response code (" + ticketres.statusCode + ") from Qlik Sense.";
authError.response = d.toString();
authError.ticket = options.path.toString();
authError.request = jsonrequest;
res.render('autherror', authError);
} else {
} else {
// Get the ticket returned by Qlik Sense
var ticket = JSON.parse(d.toString());
logger.info("requestTicket: Qlik Sense Ticket \n",ticket);
var ticket = JSON.parse(d.toString());
logger.info("requestTicket: Qlik Sense Ticket \n", ticket);

//Add the QlikTicket to the REDIRECTURI regardless whether the existing userApp has existing params.
var redirectUri = 'https://' + cfg.hostname + '/' + cfg.virtualProxy + '/';
logger.debug("requestTicket: (",redirectUri,")");
logger.debug("requestTicket: userApp: (",userApp,")");
var redirectUri = 'https://' + cfg.hostname + '/' + cfg.virtualProxy;
logger.debug("requestTicket: (", redirectUri, ")");
logger.debug("requestTicket: userApp: (", userApp, ")");
var myRedirect = url.parse(redirectUri);
var myQueryString = querystring.parse(myRedirect.query);
myQueryString['QlikTicket'] = ticket.Ticket;

var myQueryString = querystring.parse(myRedirect.query);
myQueryString['QlikTicket'] = ticket.Ticket;

// The redirectURI currently works for any application that is simply a path on the Qlik Sense URL (ex. /hub, /qmc, or /devhub)
// TODO: Exhance this code to support launching applications that are not an extension of the Qlik Sense URL.
var finalRedirectURI = redirectUri + userApp + '?Qlikticket=' + ticket.Ticket;
logger.debug("requestTicket: Redirecting to (", finalRedirectURI,")");
var finalRedirectURI = redirectUri + path + '?Qlikticket=' + ticket.Ticket;
logger.debug("requestTicket: Redirecting to (", finalRedirectURI, ")");

res.redirect(finalRedirectURI);
}
});
});

//Send JSON request for ticket
var jsonrequest = JSON.stringify({ 'userDirectory': userDirectory.toString() , 'UserId': selectedUser.toString(), 'Attributes': [] });
logger.debug("requestTicket: JSON request: ",jsonrequest);
var jsonrequest = JSON.stringify({ 'userDirectory': userDirectory.toString(), 'UserId': selectedUser.toString(), 'Attributes': [] });
logger.debug("requestTicket: JSON request: ", jsonrequest);

ticketreq.write(jsonrequest);
ticketreq.end();

ticketreq.on('error', function (e) {
logger.error("requestTicket: Error submitting authentication request (",e,")");
ticketreq.on('error', function(e) {
logger.error("requestTicket: Error submitting authentication request (", e, ")");
logger.error('Error' + e);
});
}
};

//Supporting functions
function rand(length, current) {
current = current ? current : '';
return length ? rand(--length, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz".charAt(Math.floor(Math.random() * 60)) + current) : current;
current = current ? current : '';
return length ? rand(--length, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz".charAt(Math.floor(Math.random() * 60)) + current) : current;
}
91 changes: 91 additions & 0 deletions lib/parseUdcFiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
var csv = require('csvjson');
var cfg = require("../config/config")
var path = require("path");
var winston = require("winston");
var fs = require('fs');

//set up logging
var logger = new(winston.Logger)({
level: cfg.logLevel,
transports: [
new(winston.transports.Console)(),
new(winston.transports.File)({ filename: cfg.logFile })
]
});

module.exports = {
loadUsers: function(userFile, attributeFile) {
logger.info('parseUdcFiles: Reading csv files (', cfg.csvFilePath, ")...");
var usersFile = fs.readFileSync(path.join(cfg.csvFilePath, userFile), { encoding: 'utf8' });

var attributesFile = fs.readFileSync(path.join(cfg.csvFilePath, attributeFile), { encoding: 'utf8' });

var userArray = csv.toObject(usersFile);
if (userArray.length > 0) {
logger.info("parseUdcFiles: ", userArray.length, " users loaded");
} else {
logger.info("parseUdcFiles: no users parsed from csv file");
throw new Error("error: no users parsed from csv file");
}

var attributesArray = csv.toObject(attributesFile);
if (attributesArray.length > 0) {
logger.info("parseUdcFiles: ", attributesArray.length, " attribute records loaded");
} else {
logger.info("parseUdcFiles: no attributes parsed from csv file");
throw new Error("error: No attributes parsed from attributes file");
}


var Users = {};

userArray.forEach(function(user, index) {
var User = {
userid: user.userid,
name: user.name
};



var userAttributes = attributesArray.filter(function(item) {
return item.userid == User.userid;
});

var userGroups = [];
var userApps = [];

userAttributes.forEach(function(item) {
switch (item.type) {
case "group":
userGroups.push(item.value);
break;
case "app":
userApps.push(item.value);
break;
case "image":
User.image = item.value;
break;
case "udc":
User.udc = item.value;
break;
case "title":
User.title = item.value;
break;
default:
User[item.type] = item.value;

}
});

User.groups = userGroups;
User.apps = userApps;
Users[index] = User;
})

var config = {};
config.users = Users;
logger.info("Completed parsing UDC csv files.");
return config;

}
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"bluebird": "^3.4.0",
"body-parser": "~1.13.2",
"cookie-parser": "~1.3.5",
"csvjson": "^4.1.3",
"csvtojson": "^1.1.4",
"debug": "~2.2.0",
"express": "~4.13.1",
"express-session": "~1.13.0",
Expand Down
Loading

0 comments on commit ed58860

Please sign in to comment.