Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Discussion on hiding views and fields based on group permissions in Sanity.io

16 replies
Last updated: Apr 15, 2020
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
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_HIDE
on a field to at leat be able to build a studio for an editor with less visible fields
Apr 8, 2020, 7:23 PM
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.

{
  name: 'fieldForHighPayGrade',
  type: 'string',
  title: 'C-suite level field',
  hidden: !!process.env.SANITY_STUDIO_HIDE
}
This field will be hidden if you run

SANITY_STUDIO_HIDE=true sanity start
or

SANITY_STUDIO_HIDE=true sanity build
or has the env set in a dotenv file.
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
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
ok cool thanks I think I have all I need then, appreciate the code snippets and alternatives
Apr 8, 2020, 7:32 PM
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
Probably not this quarter 🙂 But very probably this year
Apr 8, 2020, 7:34 PM
ok thanks!
Apr 8, 2020, 7:34 PM
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
That was… unexpected. I'll check in with the team what's going on.
Apr 15, 2020, 7:01 AM

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.

Was this answer helpful?