Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Filtering Users by Supervisor Role in Collections

default discord avatar
paulpopus2 years ago
17
  • default discord avatar
    whysocket2 years ago

    The documentation is confusing.


    The user is 'null' when the admin ui is rendering.



    Example



    doesn't console log anything.

  • default discord avatar
    thorsun_999852 years ago

    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.

  • default discord avatar
    whysocket2 years ago

    Im facing the same problem.



    Your jwt doesn't have the roles?

  • default discord avatar
    thorsun_999852 years ago

    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

  • default discord avatar
    whysocket2 years ago

    by now try to acess the user in filter options

  • default discord avatar
    thorsun_999852 years ago

    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.

  • default discord avatar
    paulpopus2 years ago

    oic

  • default discord avatar
    thorsun_999852 years ago

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

  • default discord avatar
    paulpopus2 years ago

    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

  • default discord avatar
    whysocket2 years ago
    @858693520012476436

    im facing a similar problem, can u help me with that please?



    https://discord.com/channels/967097582721572934/1235981447211515998
  • default discord avatar
    thorsun_999852 years ago

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

  • default discord avatar
    paulpopus2 years ago

    just name a field

    id

    with a text type



    put it at the top to make it easier

  • default discord avatar
    thorsun_999852 years ago

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

  • default discord avatar
    paulpopus2 years ago

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

  • default discord avatar
    thorsun_999852 years ago

    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



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



    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' }


    }


    }


    },

  • default discord avatar
    whysocket2 years ago

    Thanks for share

    @1149767980356468886
  • default discord avatar
    thorsun_999852 years ago

    Bah, forgot how to mark this as solved.

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.