Map two arrays and delete the item not found

0

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

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

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

Then, this usuário A creates another usuário B on the system with permissions

User permissions B [criar_usuario,deletar_usuario,deletar_contas,enviar_sms,excluir_registro]

What I want is to map the permissions of usuário A , and if I create or edit usuário B , usuário B can have the same permissions as usuário A or less, but never have permissions that% user A does not have.

In the example above,% w / w% may have:

usuário B

But never:

[criar_usuario,editar_usuario,ver_clientes]

I tried this

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 does not work, there are remnants of permissions that can not be applied

    
asked by anonymous 07.11.2018 / 17:03

3 answers

3

The Array.map() method applies a function in an array and returns 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 Array.filter() , which will return a new array containing only the elements that pass in the "test function", ie only elements whose function return was true will be in the result array.

So for you to ensure that all permissions of user B are contained in the permissions of user A, you can use the 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's worth remembering that the Array.includes() " is relatively recent (ie problems with IE). Check here for compatibility before using in your project and consider using Array.indexOf() to ensure browser compatibility.

    
07.11.2018 / 17:47
2

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

So:

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)
    
07.11.2018 / 17:19
1

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

I did this as follows

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)

That's just adapting to your reality.

Note: I gave a small refactor based on the response of the other boy.

    
07.11.2018 / 17:40