This repository was archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathadmins.js
83 lines (61 loc) · 2.68 KB
/
admins.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
const { axiosClient } = require("./src/axioshelpers")
const { graphBatchingBeta } = require("./src/batcher")
const getToken = require("./src/getToken")
module.exports = { admins }
async function admins() {
var graphToken = await getToken()
var { value: roles } = await genericGraph({
responseType: 'json',
"method": "get",
url: `https://graph.microsoft.com/beta/directoryRoles/`,
headers: {
'content-type': "application/json",
authorization: "Bearer " + graphToken
}
}).catch((error) => {
console.log(error)
})
// get strange stuff in headers
let rolesForBatch = roles.map(item => item = { url: `/directoryRoles/${item.id}/members?select=id,displayName,appId`, method: "GET", providedId: item?.displayName })
const admins = await graphBatchingBeta(rolesForBatch, graphToken, (item) => item?.map(s => s = { value: s?.body?.value, id: s?.id }), undefined, 5, 200)
const groups =[]
const list = []
admins.map(it => {
it.value.filter(ob => ob['@odata.type'] !== '#microsoft.graph.user').forEach(spn => {
let { appId, id, displayName } = spn
if (spn['@odata.type'] == '#microsoft.graph.group') {
groups.push({ id, displayName, appId, role: it.id })
}
list.push({ id, displayName, appId, role: it.id })
})
})
let rolesViaGroupAssignment = groups.map(item => item = { url: `/groups/${item.id}/members/microsoft.graph.servicePrincipal?select=id,displayName,appId`, method: "GET", providedId: `${item?.role}-via-${item?.displayName}` })
const adminsViaGroups = await graphBatchingBeta(rolesViaGroupAssignment, graphToken, (item) => item?.map(s => s = { value: s?.body?.value, id: s?.id }), undefined, 5, 200)
adminsViaGroups.forEach(it => {
it.value.forEach(spn => {
let { appId, id, displayName } = spn
list.push({ id, displayName, appId, role: it.id })
})
})
if (list.length > 0) {
require('fs').writeFileSync('./material/admins.json', JSON.stringify(list))
return "admin completed"
}
require('fs').writeFileSync('./material/admins.json', `[{"role":"","displayName":""}]`)
}
async function genericGraph(options) {
console.log(options.url)
if (options?.refInfo) {
var { refInfo } = options
delete options.refInfo
}
var data = await axiosClient(options).catch((error) => {
return Promise.reject(error)
})
if (refInfo) {
data.refInfo = refInfo
return data
} else {
return data
}
}