-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
STCOR-910 provide IfAnyPermission, stripes.ifAnyPerm (#1560)
`<IfPermission>` and `stripes.ifPerm()` return `true` when the user has ALL the given permissions. `<IfAnyPermission>` and `stripes.ifAnyPerm()` return `true` when the user has ANY of the given permissions. Refs STCOR-910 (cherry picked from commit 921f0ab)
- Loading branch information
Showing
11 changed files
with
207 additions
and
16 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
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
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,20 @@ | ||
import PropTypes from 'prop-types'; | ||
import { useStripes } from '../../StripesContext'; | ||
|
||
const IfAnyPermission = ({ children, perm }) => { | ||
const stripes = useStripes(); | ||
const hasPermission = stripes.hasAnyPerm(perm); | ||
|
||
if (typeof children === 'function') { | ||
return children({ hasPermission }); | ||
} | ||
|
||
return hasPermission ? children : null; | ||
}; | ||
|
||
IfAnyPermission.propTypes = { | ||
children: PropTypes.oneOfType([PropTypes.node, PropTypes.func]), | ||
perm: PropTypes.string.isRequired | ||
}; | ||
|
||
export default IfAnyPermission; |
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,33 @@ | ||
import { render, screen } from '@folio/jest-config-stripes/testing-library/react'; | ||
|
||
import { useStripes } from '../../StripesContext'; | ||
import Stripes from '../../Stripes'; | ||
import IfAnyPermission from './IfAnyPermission'; | ||
|
||
jest.mock('../../StripesContext'); | ||
const stripes = new Stripes({ | ||
user: { | ||
perms: { | ||
john: true, | ||
george: true, | ||
ringo: true, | ||
} | ||
}, | ||
logger: { | ||
log: jest.fn(), | ||
} | ||
}); | ||
|
||
describe('IfAnyPermission', () => { | ||
it('returns true if any permission matches', () => { | ||
useStripes.mockReturnValue(stripes); | ||
render(<IfAnyPermission perm="john,paul">monkey</IfAnyPermission>); | ||
expect(screen.queryByText(/monkey/)).toBeTruthy(); | ||
}); | ||
|
||
it('returns false if no permissions match', () => { | ||
useStripes.mockReturnValue(stripes); | ||
render(<IfAnyPermission perm="paul,is,dead">monkey</IfAnyPermission>); | ||
expect(screen.queryByText(/monkey/)).toBeFalsy(); | ||
}); | ||
}); |
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 @@ | ||
export { default } from './IfAnyPermission'; |
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,34 @@ | ||
# IfAnyPermission | ||
|
||
A wrapper component that facilitates conditional rendering based on | ||
whether the currently authentiated user has _any_ of the permissions | ||
named in the given comma-delimited string. | ||
|
||
Supports children in the form of React nodes or as a render-prop function. | ||
|
||
## Usage (children as nodes) | ||
|
||
``` | ||
<IfAnyPermission perm="users.edit,users.manage"> | ||
<button onClick={this.onClickEditUser}>Edit</button> | ||
</IfAnyPermission> | ||
``` | ||
|
||
## Usage (children as function) | ||
|
||
``` | ||
<IfAnyPermission perm="users.edit,users.manage"> | ||
{({ hasPermission }) => hasPermission ? | ||
<button onClick={this.onClickEditUser}>Edit</button> | ||
: | ||
<span>You do not have permission to edit this user!</span> | ||
} | ||
</IfAnyPermission> | ||
``` | ||
|
||
## Properties | ||
|
||
A single property is supported: | ||
|
||
* `perm`: a comma-delimited string of permissions to check. | ||
|
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,33 @@ | ||
import { render, screen } from '@folio/jest-config-stripes/testing-library/react'; | ||
|
||
import { useStripes } from '../../StripesContext'; | ||
import Stripes from '../../Stripes'; | ||
import IfPermission from './IfPermission'; | ||
|
||
jest.mock('../../StripesContext'); | ||
const stripes = new Stripes({ | ||
user: { | ||
perms: { | ||
john: true, | ||
george: true, | ||
ringo: true, | ||
} | ||
}, | ||
logger: { | ||
log: jest.fn(), | ||
} | ||
}); | ||
|
||
describe('IfPermission', () => { | ||
it('returns true if all permissions match', () => { | ||
useStripes.mockReturnValue(stripes); | ||
render(<IfPermission perm="john,george">monkey</IfPermission>); | ||
expect(screen.queryByText(/monkey/)).toBeTruthy(); | ||
}); | ||
|
||
it('returns false unless all permissions match', () => { | ||
useStripes.mockReturnValue(stripes); | ||
render(<IfPermission perm="john,paul">monkey</IfPermission>); | ||
expect(screen.queryByText(/monkey/)).toBeFalsy(); | ||
}); | ||
}); |
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