-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcloudcms-user-export.js
More file actions
executable file
·278 lines (242 loc) · 7.88 KB
/
cloudcms-user-export.js
File metadata and controls
executable file
·278 lines (242 loc) · 7.88 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env node
/*jshint -W069 */
/*jshint -W104*/
const Gitana = require("gitana");
const fs = require("fs");
const path = require("path");
const mime = require('mime-types');
const async = require("async");
const cliArgs = require('command-line-args');
const commandLineUsage = require('command-line-usage')
const util = require("./lib/util");
const writeJsonFile = require('write-json-file');
const _ = require('underscore');
const Logger = require('basic-logger');
const log = new Logger({
showMillis: false,
showTimestamp: true
});
// debug only when using charles proxy ssl proxy when intercepting cloudcms api calls:
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var options = handleOptions();
if (!options) {
return;
}
if (options["verbose"]) {
Logger.setLevel('debug', true);
} else {
Logger.setLevel('info', true);
}
var option_prompt = options["prompt"];
var option_useCredentialsFile = options["use-credentials-file"];
var option_gitanaFilePath = options["gitana-file-path"] || "./gitana.json";
var option_dataFolderPath = options["folder-path"] || "./data";
var option_allUsers = options["all-users"] || false;
var option_domainId = options["domain"] || "primary";
var option_queryFilePath = options["query-file-path"];
//
// load gitana.json config and override credentials
//
var gitanaConfig = JSON.parse("" + fs.readFileSync(option_gitanaFilePath));
if (option_useCredentialsFile) {
// override gitana.json credentials with username and password properties defined in the cloudcms-cli tool local db
var rootCredentials = JSON.parse("" + fs.readFileSync(path.join(util.homeDirectory(), ".cloudcms", "credentials.json")));
gitanaConfig.username = rootCredentials.username;
gitanaConfig.password = rootCredentials.password;
} else if (option_prompt) {
// override gitana.json credentials with username and password properties entered at command prompt
var option_prompt = require('prompt-sync')({
sigint: true
});
gitanaConfig.username = option_prompt('name: ');
gitanaConfig.password = option_prompt.hide('password: ');
} // else don't override credentials
util.parseGitana(gitanaConfig);
// if listing types
if (option_allUsers && option_queryFilePath) {
log.error("Can't export all users from a custom query. use either --all-users or query-file-path but not both");
printHelp(getOptions());
} else if (option_allUsers || option_queryFilePath) {
// export users
handleUsers();
} else {
printHelp(getOptions());
}
return;
//
// functions
//
function handleUsers() {
log.debug("handleUsers()");
util.getBranch(gitanaConfig, "master", function (err, branch, platform, stack, domain, primaryDomain, project) {
if (err) {
log.debug("Error connecting to Cloud CMS: " + err);
return;
}
log.info("primary domain id: \"" + primaryDomain._doc + "\"");
var context = {
branch: branch,
platform: platform,
stack: stack,
domain: domain,
primaryDomain: primaryDomain,
project: project,
gitanaConfig: gitanaConfig,
queryFilePath: option_queryFilePath,
dataFolderPath: option_dataFolderPath,
query: {},
nodes: []
};
if (option_queryFilePath) {
context.query = require(option_queryFilePath);
}
if (option_allUsers) {
context.query = {};
}
async.waterfall([
async.apply(listDomainUsers, context),
async.ensureAsync(async.apply(writeNodesJSONtoDisk, "users")),
], function (err, context) {
if (err) {
log.error("Error exporting: " + err);
return;
}
log.info("Export complete");
return;
});
});
}
function listDomainUsers(context, callback) {
log.debug("listDomainUsers()");
context.primaryDomain.listPrincipals().then(function () {
// console.log( JSON.stringify( this.asArray(),null,2) );
context.nodes = _.filter(this.asArray(), function (node) {
return !node.name.startsWith("appuser-");
});
callback(null, context);
});
}
function queryUsers(context, callback) {
log.debug("queryUsers()");
var query = context.query;
context.primaryDomain.queryUsers(query, {
limit: -1
}).then(function () {
// console.log( JSON.stringify( this.asArray(),null,2) );
context.nodes = _.filter(this.asArray(), function (node) {
return !node.name.startsWith("appuser-");
});
callback(null, context);
});
}
function writeNodesJSONtoDisk(pathPart, context, callback) {
log.debug("writeNodesJSONtoDisk()");
var dataFolderPath = path.posix.normalize(context.dataFolderPath);
var nodes = context.nodes;
for (var i = 0; i < nodes.length; i++) {
var node = cleanNode(nodes[i], "");
var filePath = path.normalize(path.posix.resolve(context.dataFolderPath, pathPart, node.name, "node.json"));
writeJsonFile.sync(filePath, node);
}
callback(null, context);
}
function cleanNode(node, qnameMod) {
var n = node;
util.enhanceNode(n);
n = JSON.parse(JSON.stringify(n));
// n._source_doc = n._doc;
delete n._doc;
delete n.directoryId;
delete n.identityId;
delete n.domainId;
return n;
}
function logContext(context, callback) {
log.debug("logContext() " + JSON.stringify(context.branch, null, 2));
callback(null, context);
}
function getOptions() {
return [{
name: 'help',
alias: 'h',
type: Boolean
},
{
name: 'verbose',
alias: 'v',
type: Boolean,
description: 'verbose logging'
},
{
name: 'prompt',
alias: 'p',
type: Boolean,
description: 'prompt for username and password. overrides gitana.json credentials'
},
{
name: 'use-credentials-file',
alias: 'c',
type: Boolean,
description: 'use credentials file ~/.cloudcms/credentials.json. overrides gitana.json credentials'
},
{
name: 'gitana-file-path',
alias: 'g',
type: String,
description: 'path to gitana.json file to use when connecting. defaults to ./gitana.json'
},
{
name: 'folder-path',
alias: 'f',
type: String,
description: 'folder to store exported files. defaults to ./data'
},
{
name: 'all-users',
alias: 'a',
type: Boolean,
description: 'export all users. Or use --query-file-path'
},
{
name: 'domain-id',
alias: 'd',
type: String,
description: 'id of the domain to query. defaults to "primary"'
},
{
name: 'query-file-path',
alias: 'y',
type: String,
description: 'path to a json file defining the query. required unless --all-users is specified'
}
];
}
function handleOptions() {
var options = cliArgs(getOptions());
if (options.help) {
printHelp(getOptions());
return null;
}
return options;
}
function printHelp(optionsList) {
console.log(commandLineUsage([{
header: 'Cloud CMS Export',
content: 'Export user accounts from a Cloud CMS domain.'
},
{
header: 'Options',
optionList: optionsList
},
{
header: 'Examples',
content: [{
desc: '1. Export all user accounts:',
},
{
desc: 'npx cloudcms-util export-users --all-users -g ./gitana/gitana.json --folder-path ./data'
}
]
}
]));
}