<commit_before>import React from 'react';
import {connect} from 'react-redux';

import * as tosActions from './actions/tos';
import TOSSignatureContainer from './containers/tos';


export class DefaultUnauthorizedHandler extends React.Component {
  render() {
    return (
      <section>
        <img src="http://i.imgur.com/X9Vgo96.gif"
             alt="Permission denied."/>
      </section>
    );
  }
};


export function loginRequired(Component, LoginHandler, group,
                              UnauthorizedHandler=DefaultUnauthorizedHandler) {
  /* 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 (!!this.props.user.token) {
        const hasPermission = (process.env.NODE_ENV === 'test' ||
                               _checkPermissions(this.props.user, group));
        const hasSignedTOS = ('tos' in this.props.user &&
                              this.props.user.tos.has_signed);

        if (hasPermission && hasSignedTOS) {
          return <Component/>;

        // Redirect to UnauthorizedHandler if logged in but lacking permission.
        } else if (!hasPermission) {
          return <UnauthorizedHandler/>;

        // Redirect to TOSSignatureContainer if the user hasn't signed the TOS.
        } else if (!hasSignedTOS) {
          return <TOSSignatureContainer/>;
        }

      // Redirect to LoginHandler if not logged in.
      } else {
        return <LoginHandler/>
      }
    }
  }
  return AuthenticatedComponent;
}


function _checkPermissions(user, group) {
  if (!user.token) {
    return false;
  }<commit_msg>fix sherlocked<commit_after>import React from 'react';
import {connect} from 'react-redux';

import * as tosActions from './actions/tos';
import TOSSignatureContainer from './containers/tos';


export class DefaultUnauthorizedHandler extends React.Component {
  render() {
    return (
      <section>
        <img src="http://i.imgur.com/X9Vgo96.gif"
             alt="Permission denied."/>
      </section>
    );
  }
};


export function loginRequired(Component, LoginHandler, group,
                              UnauthorizedHandler=DefaultUnauthorizedHandler) {
  /* 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 (!!this.props.user.token) {
        const hasPermission = _checkPermissions(this.props.user, group);
        const hasSignedTOS = ('tos' in this.props.user &&
                              this.props.user.tos.has_signed);

        if (process.env.NODE_ENV === 'test' ||
            (hasPermission && hasSignedTOS)) {
          return <Component/>;

        // Redirect to UnauthorizedHandler if logged in but lacking permission.
        } else if (!hasPermission) {
          return <UnauthorizedHandler/>;

        // Redirect to TOSSignatureContainer if the user hasn't signed the TOS.
        } else if (!hasSignedTOS) {
          return <TOSSignatureContainer/>;
        }

      // Redirect to LoginHandler if not logged in.
      } else {
        return <LoginHandler/>
      }
    }
  }
  return AuthenticatedComponent;
}


function _checkPermissions(user, group) {
  if (!user.token) {
    return false;
  }
{'fraction_non_alphanumeric': 0.10491071428571429, 'fraction_numerical': 0.0016741071428571428, 'mean_word_length': 2.3788878416588126, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2982139', 'n_tokens_mistral': 993, 'n_tokens_neox': 978, 'n_words': 273}