Skip to content

Commit

Permalink
feat(core): add userHasRole utility method
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Dec 16, 2022
1 parent 743244b commit cd0819f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/sanity/src/core/util/index.ts
Expand Up @@ -15,3 +15,4 @@ export * from './uncaughtErrorHandler'
export * from './useLoadable'
export * from './useThrottledCallback'
export * from './useUnique'
export * from './userHasRole'
45 changes: 45 additions & 0 deletions packages/sanity/src/core/util/userHasRole.test.ts
@@ -0,0 +1,45 @@
import type {CurrentUser} from '@sanity/types'
import {userHasRole} from './userHasRole'

const roleLessUser: CurrentUser = {
id: 'pabc123',
email: 'some@user.com',
name: 'Some User',
role: '',
roles: [],
}

const adminUser: CurrentUser = {
...roleLessUser,
role: 'administrator', // Legacy
roles: [{name: 'administrator', title: 'Administrator'}],
}

const multiRoleUser: CurrentUser = {
...adminUser,
role: 'editor', // Legacy
roles: [
{name: 'translator', title: 'Translator'},
{name: 'editor', title: 'Editor'},
],
}

test('userHasRole(): no roles', () => {
expect(userHasRole(roleLessUser, 'administrator')).toBe(false)
})

test('userHasRole(): no match', () => {
expect(userHasRole(adminUser, 'dogwalker')).toBe(false)
})

test('userHasRole(): match (single role)', () => {
expect(userHasRole(adminUser, 'administrator')).toBe(true)
})

test('userHasRole(): match (multiple roles)', () => {
expect(userHasRole(multiRoleUser, 'editor')).toBe(true)
})

test('userHasRole(): no match (multiple roles)', () => {
expect(userHasRole(multiRoleUser, 'administrator')).toBe(false)
})
24 changes: 24 additions & 0 deletions packages/sanity/src/core/util/userHasRole.ts
@@ -0,0 +1,24 @@
import type {CurrentUser} from '@sanity/types'

/**
* Checks whether or not the given user has the role with the given ID
*
* @param user - The user to check (currently only the current user is supported)
* @param roleId - The ID of the role to check for
* @returns True if the user has the role, false otherwise
* @example
* Fetch the current user and check if they have the role "administrator":
* ```
* import {userHasRole, useCurrentUser} from 'sanity'
*
* export function MyComponent() {
* const user = useCurrentUser()
* const hasAdminRole = userHasRole(user, 'administrator')
* return <div>Is administrator: {hasAdminRole ? 'Yes' : 'No'}</div>
* }
* ```
* @public
*/
export function userHasRole(user: CurrentUser, roleId: string): boolean {
return user.roles.some((role) => role.name === roleId)
}

1 comment on commit cd0819f

@vercel
Copy link

@vercel vercel bot commented on cd0819f Dec 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

test-studio – ./

test-studio.sanity.build
test-studio-git-next.sanity.build

Please sign in to comment.