-
Notifications
You must be signed in to change notification settings - Fork 0
/
access-report.js
278 lines (264 loc) · 7.69 KB
/
access-report.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
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
'use strict';
var nextList = [],
soap = require('soap-cascade'),
client = {},
soapArgs = {
authentication: {
password: '',
username: ''
},
identifier: {
path: {
path: '',
siteName: ''
},
type: 'folder',
recycled: 'false'
}
},
rules,
prompt = require('prompt'),
authenticationPrompt = [
{
name: 'server',
message: 'server:', // 'Server'.grey + ':'.white,
validator: /^https?:\/\/(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?$/,
warning: 'Should be in the form of http:\/\/cms.your.org with an optional port https:\/\/cms.your.org:8080',
'default': 'https:\/\/cascade.yoursite.edu'
},
{
name: 'username',
message: 'Username'.grey + ':'.white,
validator: /^[a-zA-Z\s\-]+$/,
warning: 'Username must be only letters, spaces, or dashes',
'default': 'username'
},
{
name: 'password',
message: 'Password'.grey + ':'.white,
hidden: true
},
{
name: 'site',
message: 'Cascade Server Site'.grey + ':'.white,
'default': 'siteName'
},
{
name: 'rootPath',
message: 'Starting path'.grey + ':'.white,
'default': '/'
}
],
accessLevels = [
'none',
'read',
'write'
];
// Utility Functions
/*
*
*/
function next() {
var todo,
current,
task,
args = {};
if (arguments.length > 0) { // if we were called with something to add to the list
if (!Array.isArray(arguments['0'])) { // if it was not an array make it into one
todo = [arguments];
} else { // break out the array
todo = [];
arguments['0'].forEach(function (item) {
todo.push(item);
});
}
nextList = todo.concat(nextList); // add the items to the front of the array and Wes suggests unshifting these onto the existing array as being faster
}
if (nextList.length > 0) { // if there are things to process
current = Array.prototype.slice.apply(nextList.shift());
task = current[0];
args = current.slice(1);
task.apply(null, args);
}
}
function repeat(string, number) {
var result = '',
index = 0;
for (index = 0; index < number; index++) {
result += string;
}
return result;
}
function findByMatchingProperties(set, properties) {
return set.filter(function (entry) {
return Object.keys(properties).every(function (key) {
return entry[key] === properties[key];
});
});
}
// ACL specific functions
function report(message) {
if (typeof message === 'string') {
console.log(message);
} else {
console.dir(message);
}
next();
}
function handleError(err) {
if (err.code === 'ECONNRESET') {
next(report, 'SAW ECONNRESET');
} else {
next(report, err);
}
}
/*
*
*
*
*
*/
function readFolder(args, parentACL, path, type, depth) {
var calls = [],
i = 0;
args.identifier.path.path = path;
args.identifier.type = type;
client.read(args, function (err, response) {
if (err) {
next(handleError, err);
} else {
if (response.readReturn.success === 'true') {
if (response.readReturn.asset.folder.children.child) {
if (!Array.isArray(response.readReturn.asset.folder.children.child)) {
response.readReturn.asset.folder.children.child = [response.readReturn.asset.folder.children.child];
}
// sort the array with folders first - results are often in correct order from server, but lets make sure
response.readReturn.asset.folder.children.child.sort(function (a, b) {
var swap,
aIsFolder = a.type === 'folder',
bIsFolder = b.type === 'folder',
aName = a.path.path.toLowerCase(),
bName = b.path.path.toLowerCase();
if (aIsFolder && bIsFolder) {
swap = (aName > bName) ? 1 : -1;
} else if (aIsFolder || bIsFolder) {
swap = (bIsFolder) ? 1 : -1;
} else {
swap = (aName > bName) ? 1 : -1;
}
return swap;
});
depth++;
response.readReturn.asset.folder.children.child.forEach(function (item) {
// avoiding reading files, but then we don't get the file metadata.
// have to read folders to traverse them, not heavy cost
// have to read pages to get contentType
// could have a read all flag that gets set manually, or with the rule file
calls[i++] = [readAccess, args, parentACL, item.path.path, item.type, depth];
});
next(calls);
} else {
next(report, 'empty folder');
}
} else {
next(report, response.readReturn.message);
}
}
});
}
// read Access
function readAccess(args, parentACL, path, type, depth) {
var thisACL; // shortcut from response
args.identifier.path.path = path;
args.identifier.type = type;
client.readAccessRights(args, function (err, response) {
if (err) {
next(handleError, err);
} else {
if (response.readAccessRightsReturn.success.toString() === 'true') {
thisACL = response.readAccessRightsReturn.accessRightsInformation;
// normalize thisACL
if (!thisACL.aclEntries) {
thisACL.aclEntries = {};
}
// normalize thisACL entries
if (!thisACL.aclEntries.aclEntry) {
thisACL.aclEntries.aclEntry = [];
} else {
// normalize thisACL entries as an array
if (!Array.isArray(thisACL.aclEntries.aclEntry)) {
thisACL.aclEntries.aclEntry = [thisACL.aclEntries.aclEntry];
}
}
// normalize parentACL
if (!parentACL.aclEntries) {
parentACL.aclEntries = {};
}
// normalize parentACL entries
if (!parentACL.aclEntries.aclEntry) {
parentACL.aclEntries.aclEntry = [];
}
console.log(repeat(' ', depth * 2) + type + ':' + path);
//check the all level
if (thisACL.allLevel !== parentACL.allLevel) {
console.log(repeat(' ', depth * 2) + 'the all level changed from '.red + parentACL.allLevel + ' to ' + thisACL.allLevel);
}
if (thisACL.aclEntries.aclEntry) {
// check existing acls for missing in new
thisACL.aclEntries.aclEntry.forEach(function (acl) {
if (!findByMatchingProperties(parentACL.aclEntries.aclEntry, acl)[0]) {
console.log(repeat(' ', depth * 2) + acl.level + ' access added for '.green + acl.type + ' ' + acl.name);
}
});
}
if (parentACL.aclEntries.aclEntry) {
// check new for additions to existing
parentACL.aclEntries.aclEntry.forEach(function (acl) {
if (!findByMatchingProperties(thisACL.aclEntries.aclEntry, acl)[0]) {
console.log(repeat(' ', depth * 2) + acl.level + ' access removed for '.red + acl.type + ' ' + acl.name);
}
});
}
if (type === 'folder') {
// go read in more
next(readFolder, args, thisACL, path, type, depth);
} else {
next();
}
} else {
next(report, response.readAccessRightsReturn.message);
}
}
});
}
function createClient(url, startPath) {
var wsPath = '/ws/services/AssetOperationService?wsdl';
soap.createClient(url + wsPath, function (err, newClient) {
if (err) {
next(handleError, err);
} else {
client = newClient;
next(readAccess, soapArgs, {}, startPath, 'folder', 0); // begin the call chain
}
});
}
// Main
console.log('Cascade Server Access Report'.cyan);
console.log('by Jason Aller');
prompt.message = '';
prompt.delimiter = '';
prompt.start();
prompt.get(authenticationPrompt, function (err, result) {
if (!err) {
soapArgs.authentication.username = result.username;
soapArgs.authentication.password = result.password;
soapArgs.identifier.path.siteName = result.site;
next([
[report, 'start'],
[createClient, result.server, result.rootPath],
[report, 'end']
]);
} else {
return 'canceled by user';
}
});