Home > Net >  I need to determine a set of users using X amount of modules
I need to determine a set of users using X amount of modules

Time:04-24

The exercise has two parts, A and B.

A is simple, it requires to check the modules that each user makes use of and return an object with the following format. This part is solved.

{
 'auth_module': {
    'authn.provider_1': ['./u1.json', './u2.json']
    'authn.provider_2': ['./u3.json', './u4.json', './u5.json']
  },
'content_module': {
    'authz.provider_1': ['./u1.json', './u3.json'],
    'authz.provider_2': ['./u2.json', './u4.json'],
    'authz.provider_3': ['./u5.json']
  }
}

Now, in part B I need help to solve this:

"Determine a group of users (taken from part A) that together use all the available modules"

The example output is in this format:

['./u1.json', './u4.json', './u5.json']

I genuinely have no clue on how to solve this, so any kind of help will be really appreciated.

CodePudding user response:

  • Using Object#entries and Array#reduce, iterate over the auth_module pairs while updating a list of target users
    • In each iteration, parse the provider id and get the list of content users for that provider if any
    • Using Array#forEach, iterate over the current auth users and update the target list with common ones between the two
  • In case there were content-only providers only, we need to push the users to the list as well. Using Object#entries and Array#forEach, iterate over the content_module pairs
    • In each iteration, check if the current provider id is not in the auth_module to add its users
  • Finally, return the list of unique users using Set

const _getUsers = modules => {
  const authIdPrefix = 'authn.', contentIdPrefix = 'authz.';
  const { 'auth_module': authModule, 'content_module': contentModule } = modules;
  
  const users = Object.entries(authModule)
    .reduce((list, [authProvider, authUsers]) => {
      const providerId = authProvider.substring(authIdPrefix.length);
      const contentUsers = contentModule[`${contentIdPrefix}${providerId}`] ?? [];
      authUsers.forEach(user => {
        if(contentUsers.includes(user)) {
          list.push(user);
        }
      });
      return list;
    }, []);

  Object.entries(contentModule)
    .forEach(([contentProvider, contentUsers]) => {
      const providerId = contentProvider.substring(contentIdPrefix.length);
      if(!authModule[`${authIdPrefix}${providerId}`]) {
        users.push(...contentUsers);
      }
    });

  return [...new Set(users)];
}

const modules = {
  'auth_module': {
    'authn.provider_1': ['./u1.json', './u2.json'],
    'authn.provider_2': ['./u3.json', './u4.json', './u5.json']
  },
  'content_module': {
    'authz.provider_1': ['./u1.json', './u3.json'],
    'authz.provider_2': ['./u2.json', './u4.json'],
    'authz.provider_3': ['./u5.json']
  }
};
console.log( _getUsers(modules) );

CodePudding user response:

I will assume that the keys in the object of Part A is dynamically created ,

so what you need to do is simply loop through the elements and try to add it in the array and before you add it you need to check if it's in the array or no like this :

const data = []
function getBresult(objectA){
  for(const element in objectA){
    if(Array.isArray(objectA[element])){
      objectA[element].forEach(element => {
        if(data.indexOf(element) === -1) data.push(element)
      })
    }
    
    else getBresult(objectA[element])
  }
}
  • Related