-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGroups.ts
More file actions
212 lines (198 loc) · 6.75 KB
/
Copy pathGroups.ts
File metadata and controls
212 lines (198 loc) · 6.75 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import * as inquirer from "inquirer";
import {GroupDetailResponse, SDK} from "../ironnode";
import {log} from "./Logger";
/**
* Convert a string of comma separated IDs into an ID list accepted by the SDK
*/
function idListToAccessList(idList: string) {
if (!idList) {
return [];
}
return idList.split(",").map((id) => id.trim());
}
/**
* Show list of groups then ask the user for a list of users. Generic method to simplify repeated
* usages of work for the add/remove admins/members.
*/
function getGroupAndListOfUsers(IronNode: SDK, userPrompt: string) {
return getFormattedGroupList(IronNode, true).then(({id}) => {
return inquirer
.prompt<{userList: string}>({
name: "userList",
type: "input",
message: userPrompt,
})
.then(({userList}) => ({id, userList: idListToAccessList(userList)}));
});
}
/**
* Displays a nicely formatted list of groups the user has access to, optionally only filtering out ones that
* the user is an admin of.
*/
function getFormattedGroupList(IronNode: SDK, filterToAdminOnly: boolean = false) {
return inquirer.prompt<{id: string}>({
type: "list",
name: "id",
message: "What's the ID of the group?",
pageSize: 10,
choices: () => {
return IronNode.group.list().then((groups) => {
let groupList = groups.result;
if (filterToAdminOnly) {
groupList = groupList.filter((group) => group.isAdmin);
}
const docInfo = groupList.map((group) => ({name: `${group.groupName} (${group.groupID})`, value: group.groupID}));
return [...docInfo, new inquirer.Separator()];
});
},
});
}
/**
* Display a list of all groups the user is either an admin or a member of
*/
export function list(IronNode: SDK) {
return IronNode.group.list().then(log);
}
/**
* Get details about a document. Shows the list
*/
export function get(IronNode: SDK) {
return getFormattedGroupList(IronNode)
.then(({id}) => IronNode.group.get(id))
.then(log);
}
/**
* Create a new group
*/
export function create(IronNode: SDK) {
return inquirer
.prompt<{id: string; name: string; addAsMember: boolean; needsRotation: boolean}>([
{
name: "id",
type: "input",
message: "Group ID (optional):",
},
{
name: "name",
type: "input",
message: "Group Name (optional):",
},
{
name: "addAsMember",
type: "confirm",
message: "Add yourself as a member? ",
},
{
name: "needsRotation",
type: "confirm",
message: "Create with needs rotation?",
default: false,
},
])
.then(({id, name, addAsMember, needsRotation}) => {
const options = {
groupID: id || undefined,
groupName: name || undefined,
addAsMember,
needsRotation,
};
return IronNode.group.create(options);
})
.then(log);
}
/**
* Update the name of an existing group to a new name or clear out the value.
*/
export function update(IronNode: SDK) {
return getFormattedGroupList(IronNode, true)
.then(({id}) => {
return inquirer
.prompt<{newName: string | null}>({
name: "newName",
type: "input",
message: "New Name (leave blank to clear name field):",
})
.then(({newName}) => IronNode.group.update(id, {groupName: newName || null}));
})
.then(log);
}
/**
* Rotate an existing groups private key
*/
export function rotatePrivateKey(IronNode: SDK) {
return getFormattedGroupList(IronNode, true)
.then(({id}) => IronNode.group.rotatePrivateKey(id))
.then(log);
}
/**
* Add admins to a group that the user is an admin of.
*/
export function addAdmins(IronNode: SDK) {
return getGroupAndListOfUsers(IronNode, "Comma separated list of users to add as admins: ")
.then(({id, userList}) => IronNode.group.addAdmins(id, userList))
.then(log);
}
/**
* Remove admins from a group that the user is an admin of.
*/
export function removeAdmins(IronNode: SDK) {
return getFormattedGroupList(IronNode, true)
.then(({id}) => IronNode.group.get(id))
.then((groupDetail) => {
return inquirer
.prompt<{userList: string[]}>({
name: "userList",
type: "checkbox",
message: "Which users do you want to remove as admins?",
choices: (groupDetail as GroupDetailResponse).groupAdmins,
})
.then(({userList}) => IronNode.group.removeAdmins(groupDetail.groupID, userList));
})
.then(log);
}
/**
* Add members to a group that the user is an admin of.
*/
export function addMembers(IronNode: SDK) {
return getGroupAndListOfUsers(IronNode, "Comma separated list of users to add as members: ")
.then(({id, userList}) => IronNode.group.addMembers(id, userList))
.then(log);
}
/**
* Remove members from a group that the user is an admin of.
*/
export function removeMembers(IronNode: SDK) {
return getFormattedGroupList(IronNode, true)
.then(({id}) => IronNode.group.get(id))
.then((groupDetail) => {
return inquirer
.prompt<{userList: string[]}>({
name: "userList",
type: "checkbox",
message: "Which users do you want to remove as members?",
choices: (groupDetail as GroupDetailResponse).groupMembers,
})
.then(({userList}) => IronNode.group.removeMembers(groupDetail.groupID, userList));
})
.then(log);
}
/**
* Delete a group. Asks the user to re-type the groups ID to confirm delete before proceeding.
*/
export function deleteGroup(IronNode: SDK) {
return getFormattedGroupList(IronNode, true).then(({id}) => {
return inquirer
.prompt<{confirmID: string}>({
name: "confirmID",
type: "input",
message: "Please re-type the groups ID to confirm its deletion",
})
.then(({confirmID}) => {
if (confirmID !== id) {
return Promise.reject(new Error("Confirmed group ID does not match group to delete."));
}
return IronNode.group.deleteGroup(id);
})
.then(log);
});
}