-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.js
84 lines (63 loc) · 2.28 KB
/
metadata.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
exports.metadata = function(request, response) {
// CRUD on custom templates. isCustomerAdmin() === true || isCSR() === true
var checkaccess = require('./accessControl.js');
var isCSR = checkaccess.isCSR(request, response);
var isCustomerAdmin = checkaccess.isCustomerAdmin(request, response);
console.log("/custom isCSR:" + isCSR);
console.log("/custom isCustomerAdmin:" + isCustomerAdmin);
if (isCSR || isCustomerAdmin) {
const customerid = request.headers['ldap_customerid'];
console.log("[" + customerid + "]");
var last2 = customerid.slice(-2);
var fs = require('fs');
fs.readFile("/tmp/" + last2 + "/" + customerid + "/settings.json", 'utf8', function(err, contents) {
console.log(contents);
if (typeof contents == 'undefined') {
response.statusCode = 404;
}
response.send(contents);
});
return true;
}
response.send("/custom nothing sent ");
return 0;
};
exports.storeMetadata = function(request, response) {
var checkaccess = require('./accessControl.js');
var isCSR = checkaccess.isCSR(request, response);
var isCustomerAdmin = checkaccess.isCustomerAdmin(request, response);
console.log("/custom isCSR:" + isCSR);
console.log("/custom isCustomerAdmin:" + isCustomerAdmin);
const customerid = request.headers['ldap_customerid'];
console.log("[" + customerid + "]");
if (customerid && customerid.length > 2) {
console.log("customerid:" + customerid);
var last2 = customerid.slice(-2);
const fs = require('fs');
// console.log("[" + last2 + "]");
try {
fs.statSync('/tmp/' + last2);
console.log('file or directory exists');
}
catch (err) {
if (err.code === 'ENOENT') {
// create folder
fs.mkdirSync('/tmp/' + last2);
fs.mkdirSync('/tmp/' + last2 + "/" + customerid);
console.log('Creating folder');
}
}
// create file
fs.writeFile("/tmp/" + last2 + "/" + customerid + "/settings.json", JSON.stringify(request.body), function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
response.send("/metadata called");
} else {
response.statusCode = 401;
response.send("/metadata Unauthenticated user.");
}
return 0;
};