#Filtering Users by Supervisor Role in Collections

32 messages · Page 1 of 1 (latest)

outer driftBOT
#

Happy Friday. I was wondering if anyone could assist with a filter in one of our collections. We have a Users collection that has fields for Roles, which is a relationship to the Roles collection, and a supervisor field, which is a relationship to Users. What we need to do is add a filter so the supervisor field returns all users that have the "Supervisor" role assigned.

steep flumeBOT
#

Original message from @rare zenith - Moved from #general message

covert chasm
keen sparrow
#

Example

#

doesn't console log anything.

rare zenith
#

Yes, I've tried the filterOptions as demonstrated in the documentation, however I would need to know the id of the specific role since roles is a relationship field to the Roles collection. That's fine for a single deployment, but we deploy to multiple environments, so the id of that role will be different for each deployment. Thus we can't hard code it.

keen sparrow
#

Im facing the same problem.

#

Your jwt doesn't have the roles?

rare zenith
#

It does, and that's what we've been using up to this point, however our customer wants us to get away from using Keycloak/jwt tokens since we plan on implementing some very complex RBAC features and we are also planning to integrate with different sources for authentication.

#

We have to use Payload

keen sparrow
#

by now try to acess the user in filter options

rare zenith
#

Yes, I can access user in the filter options. Your sample shows all users in the console. Roles is shown as an array. I need to filter for just the users with "Supervisor" in that array.

rare zenith
#

Actually since the filterOptions is just for user, it lists only the current user.

covert chasm
#

currently it doesnt provide req for you, you could open a feature request for this

you have two other options right now:

  1. a custom component for this so you control exactly which relationships are fetched

  2. thinking a bit outside the box, you can provide a custom id to your roles collection, allowing you to keep IDs consistent...if theyre string your ids could be statically set like admin

#

any field with its name id will be used as a custom id, the type here will depend on your database configuration as well

keen sparrow
rare zenith
#

Could you provide an example of how to provide a custom id? That sounds like it might be the way for us to go.

covert chasm
#

put it at the top to make it easier

rare zenith
#

I just found that. Cool I will try it out.

covert chasm
#

might need a data migration for existing data to be moved over

rare zenith
#

Didn't have but a few roles, so I was able to clear it and re-add. Here's what I've got for the filterOptions, but not sure yet is this is correct. I'm doing a build now to test it. The custom id for the Supervisor role is 24001. {
name: "supervisor",
type: "relationship",
relationTo: "users",
filterOptions: ({ relationTo, user }) => {
return {
roles: { contains: 24001}
}
}
},

#

That wasn't correct. Trying filterOptions: ({ relationTo, id })

#

Nope. Not correct either.

#

Tried relationTo, siblingData as well

rare zenith
#

I've tried a number of combinations and can't figure this out.

rare zenith
#

Finally figured it out. I set the custom id as text instead of number and set the id for Supervisor to 'supervisor'. Then the following filterOptions worked: {
name: "supervisor",
type: "relationship",
relationTo: "users",
filterOptions: () => {
return {
roles: { in: 'supervisor' }
}
}
},

keen sparrow
#

Thanks for share @rare zenith

rare zenith
#

Bah, forgot how to mark this as solved.