<commit_before>'use strict';
var isEmail = require('isemail');
var async = require('async');
var g = require('strong-globalize')();
var loopback = require('loopback/lib/loopback');
var utils = require('loopback/lib/utils');
var path = require('path');

var DEFAULT_RESET_PW_TTL = 15 * 60; // 15 mins in seconds
var assert = require('assert');

var debug = require('debug')('loopback:user');

module.exports = function(User) {
  delete User.validations.email;

  var Phone = User.registry.createModel(require('./models/phone.json'));
  require('./models/phone.js')(Phone);

  var EmailAddress = User.registry.createModel(require('./models/emailAddress.json'));
  require('./models/emailAddress.js')(EmailAddress);
  User.registry.configureModel(EmailAddress, {
    dataSource: User.getDataSource(),
  });

  User.embedsMany(EmailAddress, {as: 'emails', options: {persistent: true, validate: false}});

  var PhoneNumber = User.registry.createModel(require('./models/phoneNumber.json'));
  require('./models/phoneNumber.js')(PhoneNumber);
  User.registry.configureModel(PhoneNumber, {
    dataSource: User.getDataSource(),
  });

  User.embedsMany(PhoneNumber, {as: 'phones', options: {persistent: true, validate: false}});

  User.setCaseSensitiveEmail = function(value) {
    this.settings.caseSensitiveEmail = value;
    this.relations.emails.modelTo.settings.caseSensitiveEmail = value;
  };

  User.setVerificationRequired = function(value) {
    this.settings.emailVerificationRequired = value;
    this.relations.emails.modelTo.settings.verificationRequired = value;
    this.relations.phones.modelTo.settings.verificationRequired = value;
  };

  function splitPrincipal(name, realmDelimiter) {
    var parts = [null, name];
    if (!realmDelimiter) {
      return parts;
    }
    var index = name.indexOf(realmDelimiter);
    if (index !== -1) {
      parts[0] = name.substring(0, index);
      parts[1] = name.substring(index + realmDelimiter.length);
    }
    return parts;
  }

  /**
   * Normalize the credentials
   * @param {Object} credentials The credential object
   * @param {Boolean} realmRequired
   * @param {String} realmDelimiter The realm delimiter, if not set, no realm is needed
   * @returns {Object} The normalized credential object
   */
  User.normalizeCredentials = function(credentials, realmRequired, realmDelimiter) {
    var query = {};
    credentials = credentials || {};
    if (!realmRequired) {
      if (credentials.email) {
        query.email = credentials.email;
      } else if (credentials.phone) {
        query.phone = credentials.phone;
      } else if (credentials.username) {
        query.username = credentials.username;
      }
    } else {
      if (credentials.realm) {
        query.realm = credentials.realm;
      }
      var parts;
      if (credentials.email) {
        parts = splitPrincipal(credentials.email, realmDelimiter);
        query.email = parts[1];
        if (parts[0]) {
          query.realm = parts[0];
        }
      } else if (credentials.phone) {
        parts = splitPrincipal(credentials.phone, realmDelimiter);
        query.phone = parts[1];
        if (parts[0]) {
          query.realm = parts[0];
        }
      } else if (credentials.username) {
        parts = splitPrincipal(credentials.username, realmDelimiter);
        query.username = parts[1];
        if (parts[0]) {
          query.realm = parts[0];
        }
      }
    }
    return query;
  };

  /**
   * Login a user by with the given `credentials`.
   *
   * ```js
   *    User.login({username: 'foo', password: '<PASSWORD>'}, function (err, token) {
  *      console.log(token.id);
  *    });
   * ```
   *
   * @param {Object} credentials username/password or email/password
   * @param {String[]|String} [include] Optionally set it to "user" to include
   * the user info
   * @callback {Function} callback Callback function
   * @param {Error} err Error object
   * @param {AccessToken} token Access token if login is successful
   * @promise
   */

  User.login = function(credentials, include, fn) {
    var self = this;
    var includeRelations, realmDelimiter;

    if (typeof include === 'function') {
      fn = include;
      include = undefined;
    }

    fn = fn || utils.createPromiseCallback();

    include = (include || '');
    if (Array.isArray(include)) {
      includeRelations = include.filter(function(val) {
        return val.indexOf('user.') !== -1;
      });

      if (includeRelations && includeRelations.length) {
        include = include.filter(function(val) {
          return val.indexOf('user.') === -1;
        });

        includeRelations = includeRelations.map(function(val) {
          return val.replace('user.', '');
        });
      }
    }

    // Check if realm is required
    var realmRequired = !!(self.settings.realmRequired ||
      self.settings.realmDelimiter);
    if (realmRequired) {
      realmDelimiter = self.settings.realmDelimiter;
    }
    var query = self.normalizeCredentials(credentials, realmRequired,
      realmDelimiter);
<commit_msg>fix(user:login): lowercase<commit_after>'use strict';
var isEmail = require('isemail');
var async = require('async');
var g = require('strong-globalize')();
var loopback = require('loopback/lib/loopback');
var utils = require('loopback/lib/utils');
var path = require('path');

var DEFAULT_RESET_PW_TTL = 15 * 60; // 15 mins in seconds
var assert = require('assert');

var debug = require('debug')('loopback:user');

module.exports = function(User) {
  delete User.validations.email;

  var Phone = User.registry.createModel(require('./models/phone.json'));
  require('./models/phone.js')(Phone);

  var EmailAddress = User.registry.createModel(require('./models/emailAddress.json'));
  require('./models/emailAddress.js')(EmailAddress);
  User.registry.configureModel(EmailAddress, {
    dataSource: User.getDataSource(),
  });

  User.embedsMany(EmailAddress, {as: 'emails', options: {persistent: true, validate: false}});

  var PhoneNumber = User.registry.createModel(require('./models/phoneNumber.json'));
  require('./models/phoneNumber.js')(PhoneNumber);
  User.registry.configureModel(PhoneNumber, {
    dataSource: User.getDataSource(),
  });

  User.embedsMany(PhoneNumber, {as: 'phones', options: {persistent: true, validate: false}});

  User.setCaseSensitiveEmail = function(value) {
    this.settings.caseSensitiveEmail = value;
    this.relations.emails.modelTo.settings.caseSensitiveEmail = value;
  };

  User.setVerificationRequired = function(value) {
    this.settings.emailVerificationRequired = value;
    this.relations.emails.modelTo.settings.verificationRequired = value;
    this.relations.phones.modelTo.settings.verificationRequired = value;
  };

  function splitPrincipal(name, realmDelimiter) {
    var parts = [null, name];
    if (!realmDelimiter) {
      return parts;
    }
    var index = name.indexOf(realmDelimiter);
    if (index !== -1) {
      parts[0] = name.substring(0, index);
      parts[1] = name.substring(index + realmDelimiter.length);
    }
    return parts;
  }

  /**
   * Normalize the credentials
   * @param {Object} credentials The credential object
   * @param {Boolean} realmRequired
   * @param {String} realmDelimiter The realm delimiter, if not set, no realm is needed
   * @returns {Object} The normalized credential object
   */
  User.normalizeCredentials = function(credentials, realmRequired, realmDelimiter) {
    var query = {};
    credentials = credentials || {};
    if (!realmRequired) {
      if (credentials.email) {
        query.email = credentials.email;
      } else if (credentials.phone) {
        query.phone = credentials.phone;
      } else if (credentials.username) {
        query.username = credentials.username;
      }
    } else {
      if (credentials.realm) {
        query.realm = credentials.realm;
      }
      var parts;
      if (credentials.email) {
        parts = splitPrincipal(credentials.email, realmDelimiter);
        query.email = parts[1];
        if (parts[0]) {
          query.realm = parts[0];
        }
      } else if (credentials.phone) {
        parts = splitPrincipal(credentials.phone, realmDelimiter);
        query.phone = parts[1];
        if (parts[0]) {
          query.realm = parts[0];
        }
      } else if (credentials.username) {
        parts = splitPrincipal(credentials.username, realmDelimiter);
        query.username = parts[1];
        if (parts[0]) {
          query.realm = parts[0];
        }
      }
    }
    return query;
  };

  /**
   * Login a user by with the given `credentials`.
   *
   * ```js
   *    User.login({username: 'foo', password: '<PASSWORD>'}, function (err, token) {
  *      console.log(token.id);
  *    });
   * ```
   *
   * @param {Object} credentials username/password or email/password
   * @param {String[]|String} [include] Optionally set it to "user" to include
   * the user info
   * @callback {Function} callback Callback function
   * @param {Error} err Error object
   * @param {AccessToken} token Access token if login is successful
   * @promise
   */

  User.login = function(credentials, include, fn) {
    var self = this;
    var includeRelations, realmDelimiter;

    if (typeof include === 'function') {
      fn = include;
      include = undefined;
    }

    fn = fn || utils.createPromiseCallback();

    include = (include || '');
    if (Array.isArray(include)) {
      includeRelations = include.filter(function(val) {
        return val.indexOf('user.') !== -1;
      });

      if (includeRelations && includeRelations.length) {
        include = include.filter(function(val) {
          return val.indexOf('user.') === -1;
        });

        includeRelations = includeRelations.map(function(val) {
          return val.replace('user.', '');
        });
      }

      include = include.map(function(val) {
        return val.toLowerCase();
      });
    } else {
      include = include.toLowerCase();
    }

    // Check if realm is required
    var realmRequired = !!(self.settings.realmRequired ||
      self.settings.realmDelimiter);
    if (realmRequired) {
      realmDelimiter = self.settings.realmDelimiter;
    }
    var query = self.normalizeCredentials(credentials, realmRequired,
      realmDelimiter);

{'fraction_non_alphanumeric': 0.1374075515764889, 'fraction_numerical': 0.004087193460490463, 'mean_word_length': 3.057244374259771, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5971755', 'n_tokens_mistral': 3047, 'n_tokens_neox': 3021, 'n_words': 660}