S
S
Sashqa2021-06-10 18:33:59
Vue.js
Sashqa, 2021-06-10 18:33:59

Access menu by different roles in VueJs project?

What is the best way to implement such functionality?
Now, when logging in, a back request is sent and the response contains the current user role and the role of his company.

Menu items look something like this

[
    {
        title: 'title',
        href: 'url',
        companyRoles: ['role1', 'role2'],
        userRoles: ['userRole1', 'userRole2'],
    }
]

Method for creating a menu
export default createMenu(menuItems, userProfile) {
    return userProfile ? menuItems.filter(match(userProfile)) : []
}
const match = (userProfile) => (item) => rules.every(f => f(userProfile, item))

const rules = [check1, check2, etc] // передаем проверки


Actually a question how it is possible to make it more flexible and laconic. Are there any examples?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Kulakov, 2021-06-11
@Sashqa

There are ready-made solutions for such cases, such as this: https://github.com/stalniy/casl
But in general, your bike is sometimes not a bad idea - there is no extra dependency, flexibility is up to you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question