0

I am trying to test if the logged in user has the appropriate role to see certain items in the dashboard.

I have an array of objects. These are the items that the user may or may not see:

items: [
    { title: 'Guide', icon: '$guide', component: 'Guide', claims: '', size: '', roles: ['superAdmin', 'admin', 'pastor'] },
    { title: 'Courses', icon: '$courses', component: 'Course', claims: '', size: '', roles: ['superAdmin', 'admin', 'pastor'] },
    { title: 'Sections', icon: '$sections', component: 'Sections', claims: '', size: '', roles: ['superAdmin', 'admin'] },
    { title: 'Units', icon: '$units', component: 'Units', claims: '', size: '', roles: ['superAdmin', 'admin'] },
    { title: 'Groups', icon: '$groups', component: 'Groups', claims: '', size: '', roles: ['superAdmin', 'admin', 'pastor'] },
    { title: 'Users', icon: '$users', component: 'Users', claims: '', size: '', roles: ['superAdmin', 'admin', 'pastor'] },
    { title: 'FAQ', icon: '$faq', component: 'FAG', claims: '', size: '', roles: ['superAdmin', 'admin', 'pastor'] },
]

and an admin object. This is the user and their permission roles:

cid: (...)
email: (...)
emailVerified: (...)
fullPath: (...)
id: undefined
roles: Array(2)
  0: "member"
  1: "pastor"

Here is my code:

const hasRole = this.items.filter(val => this.admin.roles.includes(val.roles))
return hasRole

This code no longer works because the items.roles used to only be a string but I have now made it an array of roles.

I have tried multiple combinations but am struggling to figure this out.

5
  • I suppose your way forward would include writing a "determine if all/some members of subject array are present in an array"-procedure? Show us what you have tried so far. It is also not clear what the roles object in the list of items means -- does a user have to play all the roles listed by roles, or any of these, in order to be authorized for access to the realm the item describes? Commented Jun 25, 2020 at 14:42
  • Not sure I understand your question. This is for a menu system. The code should return true if the current user has the same permission (role) as the menu item Commented Jun 25, 2020 at 14:43
  • Does this answer your question? Check if array contains all elements of another array Commented Jun 25, 2020 at 14:43
  • Funny typo in your items array? Should "FAG" be "FAQ"? Church script... :) :) :) Commented Jun 25, 2020 at 14:52
  • Ok, from what I intuitively understand, a procedure named allowedItems that returns an array of items an admin (user) is authorized to access, can be defined as follows: const allowedItems = (user, items) => items.filter(item => user.roles.some(role => item.roles.includes(role)));. Commented Jun 25, 2020 at 14:53

2 Answers 2

2

Use Array.some() to test if any element in an array satisfies a condition. In this case, the condition would be if the element is found in another array.

const hasRole = this.items.filter(val => 
    this.admin.roles.some(role => val.roles.includes(role))
)
return hasRole

Change to Array.every() instead if all roles in the this.admin object should be included to result in a positive result.

Sign up to request clarification or add additional context in comments.

Comments

0

I see there is already a solution, anyway I post also mine. If you want to filter the array and get only items with specified role then you can try this:

let items= [
            { title: 'Guide', icon: '$guide', component: 'Guide', claims: '', size: '', roles: ['superAdmin', 'admin', 'pastor'] },
            { title: 'Courses', icon: '$courses', component: 'Course', claims: '', size: '', roles: ['superAdmin', 'admin', 'pastor'] },
            { title: 'Sections', icon: '$sections', component: 'Sections', claims: '', size: '', roles: ['superAdmin', 'admin'] },
            { title: 'Units', icon: '$units', component: 'Units', claims: '', size: '', roles: ['superAdmin', 'admin'] },
            { title: 'Groups', icon: '$groups', component: 'Groups', claims: '', size: '', roles: ['superAdmin', 'admin', 'pastor'] },
            { title: 'Users', icon: '$users', component: 'Users', claims: '', size: '', roles: ['superAdmin', 'admin', 'pastor'] },
            { title: 'FAQ', icon: '$faq', component: 'FAG', claims: '', size: '', roles: ['superAdmin', 'admin', 'pastor'] },
        ]


let admin = {
  roles: {
    0: "member",
    1: "pastor"
  }
}
let filtered = {}
for(role in admin.roles){
  itemsWithSpecificRole = items.filter(val => val.roles.includes(admin.roles[role]))
  Object.assign(filtered, itemsWithSpecificRole)
}

console.log(filtered)

1 Comment

Hmm, so you are creating an "array", but it's not really an array, just an object with properties like '0', '1', etc?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.