-
Notifications
You must be signed in to change notification settings - Fork 3
/
smar-user.js
189 lines (176 loc) · 7.39 KB
/
smar-user.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
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
#!/usr/bin/env node
const user = require('./lib/user.js');
const program = require('commander');
const process = require('process');
const inquirer = require('inquirer');
const chalk = require('chalk');
program
.option('-f, --force', 'Skip any confirmation prompts.');
program
.command('add')
.option('--email [email]', 'The user\'s email address.')
.option('--admin', 'Make the user a system administrator.')
.option('--licensed', 'Create this as a licensed account, with permission to create and own sheets.')
.option('--first-name [name]', 'The user\'s first name.')
.option('--last-name [name]', 'The user\'s last name.')
.option('--group-admin', 'Give this user permission to create and edit groups')
.option('--resource-viewer', 'Let this user access resource views.')
.action(function () {
const info = program.args[program.args.length-1];
if (!info.email) {
console.error('You must specify a value for --email.');
process.exit(1);
}
let email = info.email;
let isAdmin = !!info.admin;
let licensed = !!info.licensed;
let firstName = info.firstName || null;
let lastName = info.lastName || null;
let groupAdmin = !!info.groupAdmin;
let resourceViewer = !!info.resourceViewer;
user.add(email, isAdmin, licensed, firstName, lastName, groupAdmin, resourceViewer)
.then(data => {
console.log('User added:');
user.display(data.result);
})
.catch(error => console.log(error));
});
program
.command('delete')
.option('--user-id [id]', 'The ID of the user to be deleted. Required.')
.option('--transfer-sheets', 'Transfer the user\'s sheets to another user.')
.option('--transfer-to [id]', 'The ID of a user to whom group (and optionally sheet) ownership should be transferred.')
.option('--remove-from-sharing', 'Remove the user from sharing within the organization.')
.action(function () {
const info = program.args[program.args.length-1];
let force = !!info.parent.force;
let userId = info.userId || null;
let transferTo = info.transferTo || null;
let transferSheets = !!info.transferSheets;
let removeFromSharing = !!info.removeFromSharing;
if (!userId) {
console.error('The --user-id parameter is required.');
process.exit(1);
}
if (transferSheets && !transferTo) {
console.error('If --transfer-sheets is used, you must specify a new owner with --transfer-to.')
process.exit(1);
}
if (force) {
user.delete(userId, transferTo, transferSheets, removeFromSharing)
.then (function (values) {
console.log('User deleted.');
})
.catch(function (error) {
console.error(error);
});
}
else {
// Fetch some information about the user to be deleted and the
// user taking over their sheets/groups.
let toDelete = user.getInfo(userId);
let replacement = user.getInfo(transferTo);
Promise.all([toDelete, replacement])
.then(function (values) {
[toastUser, replacementUser] = values;
// Confirm that this really is the correct user to delete.
let message = 'Do you really want to delete user ' + toastUser.email + ' (' + toastUser.id + ')';
if (transferTo) {
message += ' and transfer ownership of their ';
message += (transferSheets ? 'sheets and groups' : 'groups');
message += ' to ' + replacementUser.email;
}
message += '?';
let question = {
type: 'confirm',
name: 'proceed',
message: message,
default: false
};
inquirer.prompt([question]).then((answer) => {
if (answer.proceed) {
user.delete(userId, transferTo, transferSheets, removeFromSharing)
.then(() => {
console.log('User deleted.')
})
.catch(error => {console.error(error)});
}
else {
console.error('Delete cancelled.');
process.exit(1);
}
});
}).catch(function (error) {
console.log("Error:");
console.error(error);
process.exit(1);
});
}
});
program
.command('list')
.option('--json', 'Display the results as raw JSON.')
.option('-p, --page [#]', 'Which page of results to display.')
.option('-s, --page-size [#]', 'How many records to return.')
.option('-a, --include-all', 'Return all records, without pagination.')
.action(function () {
const info = program.args[program.args.length-1];
let asJson = !!info.json;
let pagination = {
'pageSize': 10,
'page': 1,
'includeAll': false
};
if (info.page) {
pagination.page = parseInt(info.page);
}
if (info.pageSize) {
pagination.pageSize = parseInt(info.pageSize);
}
if (info.includeAll) {
pagination.includeAll = true;
}
user.listUsers(pagination).then(function (results) {
if (asJson) {
console.log(results);
}
else {
for (let i = 0; i < results.data.length; i++) {
let record = results.data[i];
user.display(record);
}
console.log(chalk.bold('Page %s of %s'), results.pageNumber, results.totalPages);
}
});
});
program
.command('update')
.option('--user-id [id]', 'The id of the user account to be updated.')
.option('--add-role [role]', 'Add a role to the user account. Must be one of "admin", "group-admin", "licensed-sheet-creator", "resource-viewer".')
.option('--remove-role [role]', 'Remove a role from the user account. Must be one of "admin", "group-admin", "licensed-sheet-creator", "resource-viewer".')
.action(function () {
const info = program.args[program.args.length-1];
if (!info.userId) {
console.error('The --user-id parameter is required.');
}
if (info.addRole) {
user.editRole('add', info.addRole, info.userId)
.then(json => {
console.log('Role added:');
console.log('');
user.display(json);
})
.catch((error) => {console.error(error)});
}
if (info.removeRole) {
user.editRole('remove', info.removeRole, info.userId)
.then(json => {
console.log('Role removed:');
console.log('');
user.display(json);
})
.catch((error) => {console.error(error)});
}
});
program
.parse(process.argv);