This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathcreateCSVreport.js
147 lines (103 loc) · 3.93 KB
/
createCSVreport.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const chalk = require('chalk')
const {randomUUID} = require('crypto')
const { writeFileSync, appendFileSync, fstat, unlinkSync } = require('fs')
const { argv } = require('yargs')
const beautify = require('js-beautify').js
function rpCsv (perms,cross, unparsed,filename,expandFlags) {
try {
unlinkSync(`${filename}.csv`)
} catch(error) {
console.log('')
}
const namedLocations = require('./namedLocations.json')
var objectIdMap
var appMap
try { objectIdMap = require('./objectIds.json')} catch (error) {
console.log('no user mapping')
}
try { appMap = require('./appIds.json')} catch (error) {
console.log('no app mapping')
}
var csvReportHeader ="users,Applications,clientAppTypes,Platforms,locations,terminations, lineage \r\n"
appendFileSync(`${filename}.csv`,csvReportHeader)
if (unparsed) {
perms = perms.map( g => JSON.parse(g))
let unTerminated = perms.filter(s => s?.terminated == 0)
console.log(chalk.red(`Unterminated permutations: ${unTerminated.length}`))
} else {
let unTerminated = perms.filter(s => s?.terminated == 0)
console.log(chalk.red(`Unterminated permutations: ${unTerminated.length}`))
}
perms.map( g => g.guid = randomUUID().replace(new RegExp('-','g',),'') )
let sorted = perms.sort((a,b) => {
if ( (a.terminated?.length || a.terminated) < (b?.terminated.length || b.terminated)) {
return -1
}
})
// console.log(sorted)
let cn = 0
for (let item of sorted) {
cn++
if (cn % 1000 == 0) {
console.log(cn, 'of /', sorted.length)
}
let csvBody =""
let details = item?.lineage
if (JSON.stringify(details).match('users:')) {
let id = details.split('users:')[1].split(' ->')[0]
let resolved = objectIdMap?.find( s=> s?.id == id )
if (resolved) {
if (argv.expand && expandFlags?.length > 0) {
let flagged = expandFlags.find( s => s?.split('users:')[1] == id)
if (flagged) {
id = id.replace(id,`${resolved['@odata.type'].split('#microsoft.graph.')[1]}-${resolved?.displayName} -flagged via expand option`)
} else {
id = id.replace(id,`${resolved['@odata.type'].split('#microsoft.graph.')[1]}-${resolved?.displayName}`)
}
} else {
id = id.replace(id,`${resolved['@odata.type'].split('#microsoft.graph.')[1]}-${resolved?.displayName}`)
}
csvBody+=`"${id}",`
} else { csvBody+=`"${id}",`}
} else {
csvBody+=","
}
if (JSON.stringify(details).match('Applications:')) {
let id = details.split('Applications:')[1].split(' ->')[0]
let resolved = appMap?.find( s=> s?.appId == id )
if (resolved) {
id = id.replace(id,resolved?.displayName)
csvBody+=`"${id}",`
} else { csvBody+=`"${id}",`}
} else {
csvBody+=","
}
if (JSON.stringify(details).match('clientAppTypes:')) {
let id = details.split('clientAppTypes:')[1].split(' ->')[0]
csvBody+=`"${id}",`
} else {
csvBody+=","
}
if (JSON.stringify(details).match('Platforms:')) {
let id = details.split('Platforms:')[1].split(' ->')[0]
csvBody+=`"${id}",`
} else {
csvBody+=","
}
if (JSON.stringify(details).match('Locations:')) {
let id = details.split('Locations:')[1].split(' ->')[0]
let resolved = namedLocations?.find( s=> s?.id == id )
if (resolved) {
id = id.replace(id,resolved?.displayName)
csvBody+=`"${id}",`
} else { csvBody+=`"${id}",`}
} else {
csvBody+=","
}
csvBody+=`${item?.terminated?.length},`
csvBody+=`"${item?.lineage}"`
csvBody+="\r\n"
appendFileSync(`${filename}.csv`,csvBody)
}
}
module.exports={rpCsv}