-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d4a6a11
commit 9cd424a
Showing
3 changed files
with
46 additions
and
40 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,40 @@ | ||
import { Component, PropTypes } from 'react'; | ||
import Roles from 'roles-client'; | ||
|
||
class RoleCheck extends Component { | ||
static propTypes = { | ||
children: PropTypes.node.isRequired, | ||
roles: PropTypes.oneOfType([ | ||
PropTypes.string, | ||
PropTypes.array, | ||
]), | ||
group: PropTypes.string.isRequired, | ||
} | ||
constructor(props, context) { | ||
super(props, context); | ||
this.state = { | ||
isLoading: true, | ||
hasAccess: false, | ||
}; | ||
} | ||
async componentDidMount() { | ||
const { roles, group } = this.props; | ||
// TODO Catch and handle promise error | ||
let hasAccess = false; | ||
if (roles && group) { | ||
hasAccess = await Roles.userIsInRole(roles, group); | ||
} else if (group) { | ||
hasAccess = await Roles.userIsInGroup(group); | ||
} | ||
// eslint-disable-next-line react/no-did-mount-set-state | ||
this.setState({ | ||
isLoading: false, | ||
hasAccess, | ||
}); | ||
} | ||
render() { | ||
return this.state.hasAccess && this.props.children; | ||
} | ||
} | ||
|
||
export default RoleCheck; |
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 |
---|---|---|
@@ -1,39 +1,3 @@ | ||
// import { Component, PropTypes } from 'react'; | ||
// import Roles from './roles'; | ||
// | ||
// class RoleCheck extends Component { | ||
// static propTypes = { | ||
// children: PropTypes.node.isRequired, | ||
// roles: PropTypes.oneOfType([ | ||
// PropTypes.string, | ||
// PropTypes.array, | ||
// ]), | ||
// group: PropTypes.string.isRequired, | ||
// userId: PropTypes.string.isRequired, | ||
// } | ||
// constructor(props, context) { | ||
// super(props, context); | ||
// this.state = { | ||
// isLoading: true, | ||
// hasAccess: false, | ||
// }; | ||
// } | ||
// componentDidMount() { | ||
// const { roles, group, userId } = this.props; | ||
// // TODO Catch and handle promise error | ||
// const promise = roles | ||
// ? Roles.userIsInRole(userId, roles, group) | ||
// : Roles.userIsInGroup(userId, group); | ||
// promise.then((res) => { | ||
// this.setState({ | ||
// isLoading: false, | ||
// hasAccess: res, | ||
// }); | ||
// }); | ||
// } | ||
// render() { | ||
// return this.state.hasAccess && this.props.children; | ||
// } | ||
// } | ||
// | ||
// export default RoleCheck; | ||
import RoleCheck from './RoleCheck'; | ||
|
||
export default RoleCheck; |