Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add
userHasRole
utility method
- Loading branch information
Showing
3 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
cd0819f
There was a problem hiding this comment.
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