-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazure_remove_user_from_app.js
73 lines (62 loc) · 2.42 KB
/
azure_remove_user_from_app.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
const axios = require('axios');
module.exports = function (RED) {
function azure_remove_user_from_app_Node(config) {
RED.nodes.createNode(this, config);
var node = this;
node.auth = RED.nodes.getNode(config.auth);
node.on('input', async function (msg) {
if (!node.auth || !node.auth.has_credentials) {
node.error("auth configuration is missing");
return
}
const access_token = await node.auth.get_access_token();
const headers = { Authorization: 'Bearer ' + access_token, 'Content-Type': 'application/json' };
const appObjectId = RED.util.evaluateNodeProperty(
config.appObjectId, config.appObjectIdType, node, msg
)
const principalId = RED.util.evaluateNodeProperty(
config.principalId, config.principalIdType, node, msg
)
const appRoleAssignedToId = RED.util.evaluateNodeProperty(
config.appRoleAssignedToId, config.appRoleAssignedToIdType, node, msg
)
var url = 'https://graph.microsoft.com/v1.0/servicePrincipals/' + appObjectId + '/appRoleAssignedTo/';
try {
axios.get(url, { headers })
.then(response => {
const appRoleAssignments = response.data.value;
for (const assignment of appRoleAssignments) {
if (assignment.principalId === principalId) {
appRoleAssignedToId = assignment.id;
break;
}
}
var url_delete = 'https://graph.microsoft.com/v1.0/servicePrincipals/' + appObjectId + '/appRoleAssignedTo/' + appRoleAssignedToId;
try {
axios.delete(url_delete, { headers })
.then(response => {
node.send(msg);
node.warn(principalId + ' was removed from ' + appObjectId);
})
.catch(error => {
node.warn('!!!!!!!!!!!!!!!!!!!!' + principalId + ' was NOT removed from ' + appObjectId);
node.warn(error);
node.warn(error.message);
});
} catch (error) {
node.warn(error);
node.warn(error.message);
}
})
.catch(error => {
node.warn(error);
node.warn(error.message);
});
} catch (error) {
node.warn(error);
node.warn(error.message);
}
});
}
RED.nodes.registerType('azure_remove_user_from_app', azure_remove_user_from_app_Node);
};