Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose more user properties #275

Merged
merged 3 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 41 additions & 35 deletions lib/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ var libOrg = require('./org');

const API_BASE = '/dw/rest/v1';
const USER_LIST_PAGE_SIZE = 25;
const USER_ALLOWED_READ_PROPERTIES = [ 'id', 'userState', 'roles', 'roleTenantFilter', 'preferredLocale',
'preferredlocale', 'primaryOrganization', 'mail', 'firstName', 'lastName', 'displayName', 'organizations' ];
const ROLE_NAMES_MAP = { 'bm-admin' : 'ECOM_ADMIN', 'bm-user' : 'ECOM_USER' };
const ROLE_NAMES_MAP_REVERSE = { 'ECOM_ADMIN' : 'bm-admin', 'ECOM_USER' : 'bm-user' };

Expand Down Expand Up @@ -130,48 +128,54 @@ function getUser(login, token, callback) {

// do the callback with the body
// filter some properties before returning
callback(undefined, filterUser(body));
callback(undefined, toExternalUser(body));
});
}

/**
* Filters properties of the passed user object and returns a reduced object containing only
* an allowed list of properties.
* Transforms the API user representation to an external format. Certain properties are
* transformed into an object representation.
*
* See function toInternalUser for the inverse operation.
*
* @param {Object} user the original user object
* @return {Object} the filtered user
* @return {Object} the transformed user object
*/
function filterUser(user) {
for (var prop in user) {
if (user.hasOwnProperty(prop) && USER_ALLOWED_READ_PROPERTIES.indexOf(prop) === -1) {
// delete the property if not allowed to read
delete user[prop];
} else if ( prop === 'roleTenantFilter' && user[prop] !== null ) {
// transform to object form
var scopeFilters = {};
var groups = user[prop].split(';');
for (var i=0; i<groups.length; i++) {
var role = groups[i].split(':');
if (role[0] === '') {
continue;
}
// map to consistent role ID
var roleID = mapFromInternalRole(role[0]);

scopeFilters[roleID] = null;
if (typeof(role[1]) !== 'undefined') {
scopeFilters[roleID] = role[1].split(',');
}
function toExternalUser(user) {
// map role tenant filter property to an object structure
if ( user['roleTenantFilter'] !== undefined ) {
// transform to object form
var scopeFilters = {};
var groups = user['roleTenantFilter'].split(';');
for (var i=0; i<groups.length; i++) {
var role = groups[i].split(':');
if (role[0] === '') {
continue;
}
// map to consistent role ID
var roleID = mapFromInternalRole(role[0]);

scopeFilters[roleID] = null;
if (typeof(role[1]) !== 'undefined') {
scopeFilters[roleID] = role[1].split(',');
}
user[prop] = scopeFilters;
}
user['roleTenantFilter'] = scopeFilters;
}

// always delete some properties
delete user['links'];
delete user['externalNames'];
delete user['roleTenantFilterMap']

return user;
}

/**
* Transforms the passed user object to an internal format accepted by the API.
*
* See function toExternalUser for the inverse operation.
*
* @param {Object} user the filtered user object
* @return {Object} the internal user object
*/
Expand Down Expand Up @@ -290,7 +294,7 @@ function createUser(orgID, user, token, callback) {
}
// do the callback with the body
// filter some properties before returning
callback(errback, filterUser(body));
callback(errback, toExternalUser(body));
});
}

Expand Down Expand Up @@ -339,7 +343,7 @@ function updateUser(user, changes, token, callback) {

// do the callback with the body
// filter some properties before returning
callback(errback, filterUser(body));
callback(errback, toExternalUser(body));
});
}

Expand Down Expand Up @@ -419,7 +423,7 @@ function deleteUser(user, purge, token, callback) {

// do the callback with the body
// filter some properties before returning
callback(errback, filterUser(body));
callback(errback, toExternalUser(body));
});
}
}
Expand Down Expand Up @@ -810,7 +814,7 @@ function grantRole(user, role, scope, token, callback) {

// do the callback with the body
// filter some properties before returning
callback(errback, filterUser(body));
callback(errback, toExternalUser(body));
});
}

Expand Down Expand Up @@ -889,7 +893,7 @@ function revokeRole(user, role, scope, token, callback) {

// do the callback with the body
// filter some properties before returning
callback(errback, filterUser(body));
callback(errback, toExternalUser(body));
});
}

Expand Down Expand Up @@ -1073,9 +1077,11 @@ module.exports.cli = {
}

// table fields
var data = [['mail','firstName','lastName','userState']];
var data = [['mail','firstName','lastName','userState','passwordExpired','verifiers','linkedToSfIdentity']];
for (var i of list) {
data.push([i.mail, i.firstName, i.lastName, i.userState]);
var user = toExternalUser(i);
data.push([user.mail, user.firstName, user.lastName, user.userState,
(!!user.passwordExpirationTimestamp), ( user.verifiers.length > 0 ), user.linkedToSfIdentity]);
}

console.table(data);
Expand Down
4 changes: 2 additions & 2 deletions test/unit/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ describe('Tests for lib/user.js', function() {
primaryOrganization : 'doe org',
mail : 'john@doe.org',
organizations : ['doe org','other org'],
unsupported : 'foo',
hobby : 'coding'
externalNames : 'foo',
links : 'coding'
};
var cleanUserObj = {
firstName : 'John',
Expand Down