Skip to content

Instantly share code, notes, and snippets.

@vlas-ilya
Created September 21, 2016 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vlas-ilya/a1ac3a421aa973b3ffac591fd205a053 to your computer and use it in GitHub Desktop.
Save vlas-ilya/a1ac3a421aa973b3ffac591fd205a053 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react'
import { connect } from 'react-redux'
@connect(
state => ({
user: state.home.authentication.user
})
)
export default class Authorized extends Component {
constructor(props, context) {
super(props, context);
let {
user: { authorities = [] },
hasAuthority = [],
hasAnyAuthority = []
} = props;
this.state = {
hasAuthority: hasAuthority.every(auth => authorities.indexOf(auth) != -1) || hasAuthority.length == 0,
hasAnyAuthority: hasAnyAuthority.some(auth => authorities.indexOf(auth) != -1) || hasAnyAuthority.length == 0
};
}
render () {
return (
this.state.hasAuthority && this.state.hasAnyAuthority ?
this.props.children
:
false
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment