import React from 'react';
import {connect} from 'react-redux';


export function loginRequired(Component, LoginHandler, group) {
  /* Wraps a handler component to require login.
     Subscribes to the user store such that it will automatically
     "redirect" to the page after successful login. */
  @connect(
    state => ({user: state.user})
  )
  class AuthenticatedComponent extends React.Component {
    render() {
      if (process.env.NODE_ENV === 'test' || _checkPermissions(user, group)) {
        return <Component/>
      } else {
        // Redirect to LoginHandler if not logged in or no permission.
        return <LoginHandler/>
      }
    }
  }
  return AuthenticatedComponent;
}


function _checkPermissions(user, group) {
  if (!user.token) {
    return false;
  }
  if (!group) {
    // If no group is specified, then simply return true.
    return true;
  }
  if (group.constructor === String && user.permissions[group]) {
    // Check if the user has the group permission.
    return true;
  } else if (group.constructor === Array) {
    // Check if the user has ANY of the group permissions.
    for (let i = 0; i < group.length; i++) {
      if (user.permissions[group[i]]) {
        return true;
      }
    }
  }
}

@@ -459,16 +459,26 @@
 test' %7C%7C
+%0A         
  _checkP
@@ -480,32 +480,43 @@
 heckPermissions(
+this.props.
 user, group)) %7B%0A

fix loginrequired