forked from jsa2/CloudShellAadApps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmins.js
80 lines (64 loc) · 1.98 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
const { axiosClient } = require("./src/axioshelpers")
const { batchThrottledSimple } = require("./src/batcher2")
const getToken = require("./src/getToken")
module.exports={admins}
//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)
})
roles.map((item) => {
item.runContext= {
fn: genericGraph,
opts:{
refInfo:item?.displayName,
responseType: 'json',
"method": "get",
url:`https://graph.microsoft.com/beta/directoryRoles/${item.id}/members`,
headers:{
'content-type':"application/json",
authorization:"Bearer " + graphToken
},
/* timeout:2000 */
}
}
})
let admins = await batchThrottledSimple(7,roles)
var list =[]
admins.map(it => {
it.value.filter(ob => ob['@odata.type'] !== '#microsoft.graph.user' ).forEach(spn => {
let {appId, id, displayName} = spn
list.push({id, displayName, appId, role:it.refInfo })
})
})
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
}
}