👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Hiding dashboard and menu items based on user roles in Sanity.io

4 replies
Last updated: Jul 22, 2021
Btw, can I hide some dashboard, menu items, schema pages from users with a “editor” role or based on a e-mail, username?
Jul 21, 2021, 5:38 PM
Ahhh just found it let’s see. But he’s using groupNames ‘wizards’ where do you create such groups?
Jul 21, 2021, 5:51 PM
Haha, I think those are just examples. I mean, if you have an enterprise plan you could create a ‘wizards’ role if you were so inclined.
Jul 21, 2021, 5:52 PM
lol wizards Cool so I only need to check if editors else show admin roles.
So basically I just need to move my current fields inside checks right


import S from '@sanity/desk-tool/structure-builder'

import {
   FiPaperclip,
   FiUser,
   FiUsers,
   FiEdit,
   FiSettings,
   FiMessageSquare,
   FiHeart,
   FiInfo,
   FiNavigation,
   FiFileText,
   FiFile,
   FiTag,
   FiCheckSquare
} from 'react-icons/fi'

import PagePreview from '../studio/src/components/preview/page/PagePreview'

const hiddenDocTypes = (listItem) =>
   ![
      'author',
      'post',
      'page',
      'category',
      'theme',
      'comment',
      'friends',
      'route',
      'navigation',
      'general',
      'featured',
      'cookie'
   ].includes(listItem.getId())

const siteMenu = () =>
   S.list()
      .title('Website')
      .items([
         S.listItem()
            .title('Posts')
            .child(S.documentTypeList('post').title('Posts overview'))
            .schemaType('post')
            .icon(FiEdit),

         S.divider(),

         S.listItem()
            .title('Pages')
            .child(S.documentTypeList('page').title('Pages overview'))
            .schemaType('page')
            .icon(FiFile),

         S.divider(),

         S.listItem()
            .title('Authors')
            .child(S.documentTypeList('author').title('Authors overview'))
            .schemaType('author')
            .icon(FiUser),

         S.divider(),

         S.listItem()
            .title('Themes')
            .child(S.documentTypeList('theme').title('Themes overview'))
            .schemaType('theme')
            .icon(FiHeart),

         S.divider(),

         S.listItem()
            .title('Categories')
            .child(S.documentTypeList('category').title('Categories overview'))
            .schemaType('category')
            .icon(FiTag),

         S.divider(),

         S.listItem()
            .title('Comments')
            .child(S.documentTypeList('comment').title('Comments overview'))
            .schemaType('comment')
            .icon(FiMessageSquare),

         S.divider(),

         S.listItem()
            .title('Routes')
            .child(S.documentTypeList('route').title('Routes'))
            .schemaType('route')
            .icon(FiPaperclip),

         S.divider(),

         // Settings menu with sub-menu's.
         S.listItem()
            .title('Settings')
            .child(
               S.list()
                  // Sets a title for our new list
                  .title('Settings')
                  // Add items to the array
                  // Each will pull one of our new singletons
                  .items([
                     S.listItem()
                        .title('General')
                        .icon(FiInfo)
                        .child(
                           S.document()
                              .schemaType('general')
                              .documentId('generalSettings')
                              .title('General Settings')
                        ),

                     S.divider(),

                     S.listItem()
                        .title('Navigation')
                        .icon(FiNavigation)
                        .child(
                           S.document()
                              .schemaType('navigation')
                              .documentId('navigationSettings')
                              .title('Navigation Settings')
                        ),

                     S.divider(),

                     S.listItem()
                        .title('Featured articles')
                        .icon(FiFileText)
                        .child(
                           S.document()
                              .schemaType('featured')
                              .documentId('featuredArticlesSettings')
                              .title('Featured Articles Settings')
                        ),

                     S.divider(),

                     S.listItem()
                        .title('Cookie Consent')
                        .icon(FiCheckSquare)
                        .child(
                           S.document()
                              .schemaType('cookie')
                              .documentId('cookieSettings')
                              .title('Cookie Consent Settings')
                        ),

                     S.divider(),

                     S.listItem()
                        .title('Friends team')
                        .icon(FiUsers)
                        .child(
                           S.document()
                              .schemaType('friends')
                              .documentId('settingsFriends')
                              .title('Friends Settings')
                        )
                  ])
            )
            .icon(FiSettings),

         // `S.documentTypeListItems()` returns an array of all the document types
         // defined in schema.js. We filter out those that we have
         // defined the structure above.
         ...S.documentTypeListItems().filter(hiddenDocTypes)
      ])

export default siteMenu

export const getDefaultDocumentNode = (props) => {
   /**
    * Here you can define fallback views for document types without
    * a structure definition for the document node. If you want different
    * fallbacks for different types, or document values (e.g. if there is a slug present)
    * you can set up that logic in here too.
    * <https://www.sanity.io/docs/structure-builder-reference#getdefaultdocumentnode-97e44ce262c9>
    */
   const { schemaType } = props
   if (schemaType === 'page') {
      return S.document().views([
         S.view.form(),
         S.view.component(PagePreview).title('Preview Page')
      ])
   }

   return S.document().views([S.view.form()])
}

Jul 21, 2021, 5:55 PM
Exactly.
Jul 22, 2021, 4:25 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.

Was this answer helpful?