Map two arrays and delete item not found

Asked

Viewed 57 times

0

I have an api on Node and I have the following situation:

I am the master user and create a usuário A to any employee. That usuário A has the permissions

Permissions user A [criar_usuario,editar_usuario,receber_contas,editar_contas,ver_clientes]

So this usuário A creates another usuário B in the system with permissions

Permissions user B [criar_usuario,deletar_usuario,deletar_contas,enviar_sms,excluir_registro]

what I want is to map the permissions of the usuário A and, if it creates or edits the usuário B, the usuário B may have the same permissions as usuário A or less, but never have permissions that user A does not have.

In the example above, the usuário B may have:

[criar_usuario,editar_usuario,ver_clientes]

But never:

[deletar_usuario,receber_contas,editar_clientes,ver_clientes]

I tried that

let p = usuarioLogado.permissions; // permissões do usuário logado
let up = req.body.permissoes; // permissões do usuário que será criado ou editado

up.map((o, index) => {
     if (p.indexOf(o) <= -1) {
         up.splice(index, 1)
     }
})

but it doesn’t work, there are traces of the permissions that cannot be applied

3 answers

3


The method Array.map() apply a function to an array and return a new array where each element of this new array is the result of the function applied to the elements of the previous array.

What you seem to be wanting is to use the method Array.filter(), that will return a new array containing only the elements that pass the "test function", that is, only the elements whose function return was true will be in the result array.

So to ensure that all user permissions B are contained in user permissions A, you can use the method Array.includes() to test whether permission B is contained in permissions A.

Example:

let perm_A = [
    "criar_usuario", 
    "editar_usuario", 
    "receber_contas", 
    "editar_contas", 
    "ver_clientes"
];

let perm_B_OK = [
    "criar_usuario",
    "editar_usuario",
    "ver_clientes"
];

let perm_B_Errado = [
    "deletar_usuario",
    "receber_contas",
    "editar_clientes",  // Essa permissão não pode
    "ver_clientes"
]


let allowed_perms_OK = perm_B_OK.filter(perm => perm_A.includes(perm));
console.log(allowed_perms_OK);

let allowed_perms_Errado = perm_B_OK.filter(perm => perm_A.includes(perm));
console.log(allowed_perms_Errado);


If for some reason someone is using these methods in the browser, it is worth remembering that the method Array.includes() is relatively recent (ie problems with IE). Check here compatibility before using in your project and consider using Array.indexOf() to ensure browser compatibility.

  • fernandosavio, was exactly that. Mto thank you!!!

2

You can use Array.filter and Array.indexOf together to check the permissions of the logged-in user p is already in up.

Thus:

let p = usuarioLogado.permissions; // permissões do usuário logado
let up = req.body.permissoes; // permissões do usuário que será criado ou editado

let permissoes_up = p.filter((permicao) => (up.indexOf(permicao) !== -1))

console.log(permissoes_up)

1

The error in your code is in the comparison, I believe that if you change the <= for >= will work, the indexOf returns the position of the element in the array when it exists and returns -1 when it does not, so when it exists it will always be a value greater than -1.

I did it this way

const usuarioA = ['criar_usuario', 'editar_usuario', 'receber_contas', 'editar_contas', 'ver_clientes']
let usuarioB = ['criar_usuario', 'deletar_usuario', 'deletar_contas', 'enviar_sms', 'excluir_registro']

usuarioB = usuarioB.filter(permissao => usuarioA.indexOf(permissao) !== -1)

Then just adapt to your reality.

Note: I gave a small refatora based on the other boy’s answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.