How to hide structure elements based on user role in Sanity v3.
Yes, this is absolutely possible in Sanity Studio v3! You can access the current user's information, including their roles, through the context object in your structure configuration.
In Studio v3, when you define your structure using the structure tool, you receive a context object that includes currentUser. Here's how you can use it:
// sanity.config.ts
import {defineConfig} from 'sanity'
import {structureTool} from 'sanity/structure'
export default defineConfig({
// ... other config
plugins: [
structureTool({
structure: (S, context) => {
const {currentUser} = context
// Check if user has admin role
const isAdmin = currentUser?.roles.some(
role => role.name === 'administrator'
)
return S.list()
.title('Content')
.items([
S.documentTypeListItem('post').title('Posts'),
// Only show this item to administrators
...(isAdmin ? [
S.documentTypeListItem('settings').title('Settings')
] : []),
// ... other items
])
}
})
]
})The currentUser.roles array contains role objects with properties like name, title, and other role information. Common built-in role names include:
administratoreditorviewerdevelopercontributor
You can also hide specific tools (like the Vision plugin or Media Library) based on user roles by conditionally including them in your plugins array:
plugins: [
structureTool({/* ... */}),
// Only include Vision plugin for administrators
...(currentUser?.roles.some(r => r.name === 'administrator')
? [visionTool()]
: [])
]I completely agree the documentation could be clearer on this - there's an example in the migration cheat sheet, but you'd pretty much already have to know where it is to find it. The pattern is the same as v2, just with the updated import paths and configuration structure for v3.
For more advanced access control scenarios (like restricting access to specific documents or fields based on roles), you might want to look into Custom Roles, which are available on Enterprise plans and allow you to define granular permissions using GROQ filters to control who can read, update, create, or publish specific content resources.
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.