Discussion on hiding views and fields based on group permissions in Sanity.io
16 replies
Last updated: Apr 15, 2020
M
Anybody have any solutions for this question? Need to figure out if I can hide views and fields based on the 2 built in group permissions available on non enterprise. https://sanity-io-land.slack.com/archives/C9Z7RC3V1/p1586321578058400
Apr 8, 2020, 6:21 PM
Here's a super minimal example on a desk structure that's based on roles. It's not exactly what you want perhaps, but it's the way to at least hide document types:
// deskStructure import S from '@sanity/base/structure-builder' import userStore from 'part:@sanity/base/user' import { map } from 'rxjs/operators' export default () => { return userStore.currentUser.pipe( map(({user}) => { const {role} = user if (role === 'administrator') { return S.list().title('Admin structure') } return S.list().title('Editor structure') }) ) }
Apr 8, 2020, 7:16 PM
M
thanks
Knut Melvær
so can I do something similar with displaying fields like in the conditional input field?Apr 8, 2020, 7:18 PM
I'm testing if you can set
hide: process.env.SANITY_STUDIO_HIDEon a field to at leat be able to build a studio for an editor with less visible fields
Apr 8, 2020, 7:23 PM
M
oh sweet yeah that would be all I need, just want to clean up the interface for non admins to only what they should see
Apr 8, 2020, 7:23 PM
Aight, so that actually works.
This field will be hidden if you run
or
or has the env set in a dotenv file.
{ name: 'fieldForHighPayGrade', type: 'string', title: 'C-suite level field', hidden: !!process.env.SANITY_STUDIO_HIDE }
SANITY_STUDIO_HIDE=true sanity start
SANITY_STUDIO_HIDE=true sanity build
Apr 8, 2020, 7:29 PM
This means that you can for example build two studios on Netlify for example, one for admins and one for others. Using the same codebase, but adding the env variable for the one.
Apr 8, 2020, 7:30 PM
Apr 8, 2020, 7:30 PM
M
ah ok gotcha that works but would rather check the user dynamically at runtime and have one studio, would something similar to the original code work in a conditional field?
Apr 8, 2020, 7:31 PM
Then you need to use custom input fields that uses the userStore I used in the structure example. It's on our roadmap to give you more and easier control over field display though.
Apr 8, 2020, 7:31 PM
you could e.g. build on this https://github.com/sanity-io/sanity-recipes/blob/master/snippets/conditionalFieldsCustomInputComponent.js
Apr 8, 2020, 7:32 PM
M
ok cool thanks I think I have all I need then, appreciate the code snippets and alternatives
Apr 8, 2020, 7:32 PM
M
also when you say on our roadmap is it like this quarter or later this year or not sure yet 🙂
Apr 8, 2020, 7:33 PM
M
ok thanks!
Apr 8, 2020, 7:34 PM
M
Knut Melvær
when you're back online, no rush that snippet for checking the user is admin or not only seems to work in some places. If I call it for instance in a workflow action it gets in a refresh/login screen loop even though the user is logged in. Just wondering why it would matter where you call it and is there some way around that so I can use it everywhere?Apr 12, 2020, 9:55 PM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.