Created
January 21, 2023 18:18
-
-
Save kmelve/b4fb4c388bed58a0942aae4101f7d7fb to your computer and use it in GitHub Desktop.
Studio v3 version of https://kittygiraudel.com/2022/05/21/simple-access-management-with-sanity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {defineConfig, type Role} from 'sanity' | |
import {deskTool} from 'sanity/desk' | |
import {visionTool} from '@sanity/vision' | |
import {schemaTypes} from './schemas' | |
export const EDITOR_TYPES = ['post'] | |
const isAdmin = (roles: Role[]) => !roles?.find(({name = ''}) => name === 'administrator') | |
export default defineConfig({ | |
name: 'default', | |
title: 'test', | |
projectId: 'kh2fp3g7', | |
dataset: 'production', | |
plugins: [ | |
deskTool({ | |
name: 'default', | |
title: 'test', | |
structure: (S, context) => { | |
const roles = context.currentUser?.roles as Role[] | |
return S.list() | |
.title(isAdmin(roles) ? 'Content' : 'Editorial content') | |
.items( | |
S.documentTypeListItems().filter( | |
(item) => isAdmin(roles) || EDITOR_TYPES.includes(item.getId() || '') | |
) | |
) | |
}, | |
}), | |
visionTool(), | |
], | |
document: { | |
actions: (prev, {currentUser}) => { | |
if (isAdmin(currentUser?.roles as Role[])) { | |
return prev | |
} | |
return [] | |
}, | |
newDocumentOptions: (prev, {currentUser, creationContext}) => { | |
if (isAdmin(currentUser?.roles as Role[])) { | |
return prev | |
} | |
return [ | |
{ | |
id: 'post', | |
templateId: 'post', | |
type: 'post', | |
}, | |
] | |
}, | |
}, | |
studio: { | |
components: { | |
// We don't get the current user in context here, so a hack might be to set it to a global variable on the window object and then read it here | |
toolMenu: (props) => { | |
/* const roles = props.currentUser?.roles as Role[] | |
if (isAdmin(roles)) { | |
} */ | |
return props.renderDefault(props) | |
}, | |
}, | |
}, | |
schema: { | |
// We don't get the current user in context here, so a hack might be to set it to a global variable on the window object and then read it here | |
types: schemaTypes.map((type) => { | |
if (type.type === 'document' && !EDITOR_TYPES.includes(type.name)) { | |
return {...type, __experimental_omnisearch_visibility: false} | |
} | |
return type | |
}), | |
}, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment