This repository was archived by the owner on Apr 2, 2025. It is now read-only.
forked from launchdarkly/node-server-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_filter.js
More file actions
55 lines (49 loc) · 1.88 KB
/
Copy pathuser_filter.js
File metadata and controls
55 lines (49 loc) · 1.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
/**
* The UserFilter object transforms user objects into objects suitable to be sent as JSON to
* the server, hiding any private user attributes.
*
* @param {Object} the LaunchDarkly client configuration object
**/
function UserFilter(config) {
const filter = {};
const allAttributesPrivate = config.allAttributesPrivate;
const privateAttributeNames = config.privateAttributeNames || [];
const ignoreAttrs = { key: true, custom: true, anonymous: true };
const allowedTopLevelAttrs = { key: true, secondary: true, ip: true, country: true, email: true,
firstName: true, lastName: true, avatar: true, name: true, anonymous: true, custom: true };
filter.filterUser = user => {
const userPrivateAttrs = user.privateAttributeNames || [];
const isPrivateAttr = name =>
!ignoreAttrs[name] && (
allAttributesPrivate || userPrivateAttrs.indexOf(name) !== -1 ||
privateAttributeNames.indexOf(name) !== -1);
const filterAttrs = (props, isAttributeAllowed) =>
Object.keys(props).reduce((acc, name) => {
if (isAttributeAllowed(name)) {
if (isPrivateAttr(name)) {
// add to hidden list
acc[1][name] = true;
} else {
acc[0][name] = props[name];
}
}
return acc;
}, [{}, {}]);
const result = filterAttrs(user, key => allowedTopLevelAttrs[key]);
const filteredProps = result[0];
const removedAttrs = result[1];
if (user.custom) {
const customResult = filterAttrs(user.custom, () => true);
filteredProps.custom = customResult[0];
Object.assign(removedAttrs, customResult[1]);
}
const removedAttrNames = Object.keys(removedAttrs);
if (removedAttrNames.length) {
removedAttrNames.sort();
filteredProps.privateAttrs = removedAttrNames;
}
return filteredProps;
};
return filter;
}
module.exports = UserFilter;