Permissions
This service allows us to manipulate the permissions easily in vanilla JS and React.
Has Permission
By passing in a string of the permission name, we can use hasPermission to check if the user has the permission we're looking for.
Example
import React from 'react'
import { hasPermission } from '@rhapsody/permissions'
export const PrivateRoute: React.FC<> = () => {
const userHasPermission = hasPermission('private_permission')
return <span>{userHasPermission ? 'Welcome back!' : 'Access Denied'}</span>
}
Has Any Permission
By passing in an array of strings with permissions names, we can use hasAnyPermission to check if the user has any of the permissions we're looking for.
Example
import React from 'react'
import { hasAnyPermission } from '@rhapsody/permissions'
export const PrivateRoute: React.FC<> = () => {
const userHasPermission = hasAnyPermission([
'private_permission',
'awesome_permission',
])
return <span>{userHasPermission ? 'Welcome back!' : 'Access Denied'}</span>
}
Testing Permissions
Inside this library you can also find some tools created for testing the permissions
Set Permissions
By passing an array of strings, setPermissions simulates if the user has that permissions for unit tests.
Example
import React from 'react'
import { setPermissions } from '@rhapsody/permissions'
describe('Component Specs', () => {
beforeEach(() => {
setPermissions(['awesome_permission_to_test'])
})
})
Clear Permissions
clearPermissions cleans all the permissions of the user for the unit tests.
Example
import React from 'react'
import { clearPermissions } from '@rhapsody/permissions'
describe('Component Specs', () => {
afterEach(() => {
clearPermissions()
})
})