<commit_before>/**
 * Middleware that performs custom validation on a user model and can fail the request before going to the database.
 *
 * @param kontx
 * @param next
 */
module.exports = function validate(kontx, next){
    'use strict';

    var _ = require('underscore'),
        q = require('q'),
        db = require('../../db'),
        Strings = require('../../strings'),
        messages = new Strings().group('errors'),
        createError = require('../../utils/error'),
        user = kontx.args[0];


    if ( !identityIsSet() || _.keys(user.identities).length === 0 ) {
        next( createError( 400, messages.missing_identity ) );
    }
    else if ( kontx.route === 'users.update' && _.isUndefined( user._id ) ) {
        next( createError( 404 ) );
    }
    else if( basicIdentityIsSet() ) {
        validateBasicIdentity();
    }
    else {
        next();
    }



    function validateBasicIdentity(){

        if( !loginIsSet() ) {
            next( createError( 400, messages.missing_field_identity_basic_login ) );
        }
        else if ( user.identities.basic.login.length < 4 ) {
            next( createError( 400, messages.login_too_short ) );
        }
        else if ( kontx.route === 'users.insert' && !passwordIsSet() ) {
            next( createError( 400, messages.user_password_too_short ) );
        }
        else {
            loginIsUnique( user._id ).then(
                function () {
                    next();
                },
                function ( err ){
                    next( createError( 400, err.message ) );
                }
            );

        }
    }

    // Function that returns is the user.identites property has been set to a value.
    function identityIsSet() {
        return ( !_.isUndefined( user.identities ) && !_.isNull( user.identities ) );
    }

    // Function that returns is the user.identites.basic property has been set to a value.
    function basicIdentityIsSet(){
        return ( !_.isUndefined( user.identities.basic ) && !_.isNull( user.identities.basic ) );
    }

    // Function that returns is the user.identites.basic.password property has been set to a value.
    function passwordIsSet() {
        return ( !_.isUndefined( user.identities.basic.hash ) && !_.isNull( user.identities.basic.hash ) );
    }

    function loginIsSet() {
        return (
            !_.isUndefined( user.identities.basic.login ) &&
            !_.isNull( user.identities.basic.login ) &&
            user.identities.basic.login !== ''
        );
    }

    /*
     If this is an 'insert' method and basic authentication is set. Then we need to make sure that only 1 login
     is allowed in our system.
     */
    function loginIsUnique ( id ) {
        var deferred = q.defer();

        db.users.query( [ { key: 'identities.basic.login', cmp: '=', value: user.identities.basic.login } ] ,<commit_msg>Cleaned the users validate file. Possibly more clean now.<commit_after>/**
 * Middleware that performs custom validation on a user model and can fail the request before going to the database.
 *
 * @param kontx
 * @param next
 */
module.exports = function validate(kontx, next){
    'use strict';

    var _ = require('underscore'),
        q = require('q'),
        db = require('../../db'),
        Strings = require('../../strings'),
        messages = new Strings().group('errors'),
        createError = require('../../utils/error'),
        user = kontx.args[0];


    if ( kontx.route === 'users.insert' && !identityIsSet()) {
        next( createError( 400, messages.missing_identity ) );
    }
    else if ( kontx.route === 'users.update' && _.isUndefined( user._id ) ) {
        next( createError( 404 ) );
    }
    else if( basicIdentityIsSet() ) {
        validateBasicIdentity();
    }
    else {
        next();
    }

    function validateBasicIdentity(){

        if(kontx.route === 'users.insert') {
            if( !loginIsSet() ) {
                next( createError( 400, messages.missing_field_identity_basic_login ) );
            } else if ( !passwordIsSet() ) {
                next( createError( 400, messages.user_password_too_short ) );
            } else if ( loginIsSet() && user.identities.basic.login.length < 4 ) {
                next( createError( 400, messages.login_too_short ) );
            } else if ( loginIsSet() ) {
                loginIsUnique( user._id )
                    .then(next)
                    .fail(function ( err ){
                        next( createError( 400, err.message ) );
                    })
                    .done();
            } else {
                next();
            }
        } else if (kontx.route === 'users.update') {
            if (_.has(user.identities.basic, 'login') && _.isEmpty(user.identities.basic.login)) { // user is submitting login and it is empty.
                next( createError( 400, messages.password_cannot_be_empty));
            } else if (loginIsSet() && user.identities.basic.login.length < 4 ) {
                next( createError( 400, messages.login_too_short ) );
            } else if ( _.has(user.identities.basic, 'hash') && !passwordIsSet()) {
                next( createError( 400, messages.user_password_too_short ) );
            } else if ( loginIsSet() ) {
                loginIsUnique( user._id )
                    .then(next)
                    .fail(function ( err ){
                        next( createError( 400, err.message ) );
                    })
                    .done();
            } else {
                next();
            }
        } else {
            next();
        }

    }

    // Function that returns is the user.identities property has been set to a value.
    function identityIsSet() {
        return ( _.has(user, 'identities') && !_.isEmpty(user.identities));
    }

    // Function that returns is the user.identities.basic property has been set to a value.
    function basicIdentityIsSet(){
        return _.has(user, 'identities') && _.has(user.identities, 'basic') && !_.isEmpty(user.identities.basic);
    }

    // Function that returns is the user.identities.basic.password property has been set to a value.
    function passwordIsSet() {
        return !_.isEmpty(user.identities.basic.hash );
    }

    function loginIsSet() {
        return !_.isEmpty( user.identities.basic.login );
    }

    /*
     If this is an 'insert' method and basic authentication is set. Then we need to make sure that only 1 login
     is allowed in our system.
     */
    function loginIsUnique ( id ) {
        var deferred = q.defer();

        db.users.query( [ { key: 'identities.basic.login', cmp: '=', value: user.identities.basic.login } ] ,
{'fraction_non_alphanumeric': 0.13131162234837168, 'fraction_numerical': 0.008365700627427548, 'mean_word_length': 1.9146713104048758, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, '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': '7464820', 'n_tokens_mistral': 1929, 'n_tokens_neox': 1814, 'n_words': 483}