<commit_before>import { getEntity, read, swap, updateEntity } from '../../store/index';
import {
  enterpriseConnection,
  findADConnectionWithoutDomain,
  findSSOConnection,
  isHRDDomain,
  toggleHRD
} from '../enterprise';
import * as l from '../../core/index';
import * as c from '../../field/index';
import { setUsername } from '../../field/username';
import { emailLocalPart } from '../../field/email';
import webApi from '../../core/web_api';

export function startHRD(id) {
  swap(updateEntity, "lock", id, m => {
    m = toggleHRD(m, true);
    m = setUsername(m, emailLocalPart(c.email(m)));
    return m;
  });
}

export function cancelHRD(id) {
  swap(updateEntity, "lock", id, toggleHRD, false);
}

export function signIn(id) {
  const m = read(getEntity, "lock", id);
  const email = c.email(m);
  const ssoConnection = findSSOConnection(m, email);

  if (ssoConnection && isHRDDomain(m, email)) {
    // TODO: maybe this shouldn't be dispatched from here, maybe the
    // signIn function shouldn't exists at all.
    return logInHRD(id, ssoConnection);
  }

  if (ssoConnection) {
    return logInSSO(id, ssoConnection);
  }

  logInAD(id, findADConnectionWithoutDomain(m));
}

export function logInHRD(id, connection = undefined) {
  swap(updateEntity, "lock", id, lock => {
    if (!connection) connection = enterpriseConnection(lock);

    if (c.isFieldValid(lock, "username") && c.isFieldValid(lock, "password")) {
      return l.setSubmitting(lock, true);
    } else {
      lock = c.setFieldShowInvalid(lock, "username", !c.isFieldValid(lock, "username"));
      lock = c.setFieldShowInvalid(lock, "password", !c.isFieldValid(lock, "password"));
      return lock;
    }
  });

  const lock = read(getEntity, "lock", id);

  if (l.submitting(lock)) {
    // TODO: popup + popup options
    const options = l.withAuthOptions(lock, {
      connection: connection.get("name"),
      username: c.username(lock),
      password: <PASSWORD>(lock),
      login_hint: c.username(lock)
    });

    webApi.signIn(
      id,
      options,
      (error, ...args) => {
        if (error) {
          setTimeout(() => logInError(id, error), 250);
        } else {
          logInSuccess(id, ...args);
        }
      }
    );
  }

}

function logInSSO(id, connection) {
  swap(updateEntity, "lock", id, lock => {
    if (c.isFieldValid(lock, "email")) {
      return l.setSubmitting(lock, true);
    } else {
      lock = c.setFieldShowInvalid(lock, "email", !c.isFieldValid(lock, "email"));
      return lock;
    }
  });

  const lock = read(getEntity, "lock", id);

  if (l.submitting(lock)) {
    // TODO: popup + popup options
    const options = l.withAuthOptions(lock, {
      connection: connection.get("name"),
      username: c.email(lock),
      login_hint: c.email(lock)
    });

    webApi.signIn(
      id,
      options,
      (error, ...args) => {
        if (error) {
          setTimeout(() => logInError(id, error), 250);
        } else {
          logInSuccess(id, ...args);
        }
      }
    );
  }
}

function logInAD(id, connection) {
  swap(updateEntity, "lock", id, lock => {
    if (c.isFieldValid(lock, "email") && c.isFieldValid(lock, "password")) {
      return l.setSubmitting(lock, true);
    } else {
      lock = c.setFieldShowInvalid(lock, "email", !c.isFieldValid(lock, "email"));
      lock = c.setFieldShowInvalid(lock, "password", !c.isFieldValid(lock, "password"));
      return lock;
    }
  });
<commit_msg>Fix single sign on action<commit_after>import { getEntity, read, swap, updateEntity } from '../../store/index';
import {
  enterpriseConnection,
  findADConnectionWithoutDomain,
  findSSOConnection,
  isHRDDomain,
  toggleHRD
} from '../enterprise';
import * as l from '../../core/index';
import * as c from '../../field/index';
import { setUsername } from '../../field/username';
import { emailLocalPart } from '../../field/email';
import webApi from '../../core/web_api';

export function startHRD(id) {
  swap(updateEntity, "lock", id, m => {
    m = toggleHRD(m, true);
    m = setUsername(m, emailLocalPart(c.email(m)));
    return m;
  });
}

export function cancelHRD(id) {
  swap(updateEntity, "lock", id, toggleHRD, false);
}

export function signIn(id) {
  const m = read(getEntity, "lock", id);
  const email = c.email(m);
  const ssoConnection = findSSOConnection(m, email);

  if (ssoConnection && isHRDDomain(m, email)) {
    // TODO: maybe this shouldn't be dispatched from here, maybe the
    // signIn function shouldn't exists at all.
    return logInHRD(id, ssoConnection);
  }

  if (ssoConnection) {
    return logInSSO(id, ssoConnection);
  }

  logInAD(id, findADConnectionWithoutDomain(m));
}

export function logInHRD(id, connection = undefined) {
  swap(updateEntity, "lock", id, lock => {
    if (!connection) connection = enterpriseConnection(lock);

    if (c.isFieldValid(lock, "username") && c.isFieldValid(lock, "password")) {
      return l.setSubmitting(lock, true);
    } else {
      lock = c.setFieldShowInvalid(lock, "username", !c.isFieldValid(lock, "username"));
      lock = c.setFieldShowInvalid(lock, "password", !c.isFieldValid(lock, "password"));
      return lock;
    }
  });

  const lock = read(getEntity, "lock", id);

  if (l.submitting(lock)) {
    // TODO: popup + popup options
    const options = l.withAuthOptions(lock, {
      connection: connection.get("name"),
      username: c.username(lock),
      password: <PASSWORD>(<PASSWORD>),
      login_hint: c.username(lock)
    });

    webApi.signIn(
      id,
      options,
      (error, ...args) => {
        if (error) {
          setTimeout(() => logInError(id, error), 250);
        } else {
          logInSuccess(id, ...args);
        }
      }
    );
  }

}

function logInSSO(id, connection) {
  swap(updateEntity, "lock", id, lock => {
    if (c.isFieldValid(lock, "email")) {
      return l.setSubmitting(lock, true);
    } else {
      lock = c.setFieldShowInvalid(lock, "email", !c.isFieldValid(lock, "email"));
      return lock;
    }
  });

  const lock = read(getEntity, "lock", id);

  if (l.submitting(lock)) {
    // TODO: popup + popup options
    const options = l.withAuthOptions(lock, {
      connection: connection.get("name"),
      login_hint: c.email(lock)
    });

    webApi.signIn(
      id,
      options,
      (error, ...args) => {
        if (error) {
          setTimeout(() => logInError(id, error), 250);
        } else {
          logInSuccess(id, ...args);
        }
      }
    );
  }
}

function logInAD(id, connection) {
  swap(updateEntity, "lock", id, lock => {
    if (c.isFieldValid(lock, "email") && c.isFieldValid(lock, "password")) {
      return l.setSubmitting(lock, true);
    } else {
      lock = c.setFieldShowInvalid(lock, "email", !c.isFieldValid(lock, "email"));
      lock = c.setFieldShowInvalid(lock, "password", !c.isFieldValid(lock, "password"));
      return lock;
    }
  });

{'fraction_non_alphanumeric': 0.1642309361886847, 'fraction_numerical': 0.0017363623209376356, 'mean_word_length': 2.931740614334471, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 22, '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': '6871135', 'n_tokens_mistral': 2300, 'n_tokens_neox': 2259, 'n_words': 518}