<commit_before>'use strict';

var model = require('../model');

function mtCheck(o, tenantId) {
    if (Array.isArray(o)) {
        for (let e of o) {
            if (!mtCheck(e, tenantId))
                return false;
        }
        return true;
    }
    if (o === null || typeof o !== 'object')
        return true;
    if ('tenantId' in o) {
        if (o.tenantId != tenantId)
            return false;
        o.tenantId = undefined;
    }
    else if ('metadata' in o && 'tenantId' in o.metadata)
    {
        if (o.metadata.tenantId != tenantId)
            return false;
        o.metadata.tenantId = undefined;
    }
    return true;
}

function multi_tenancy (req, res, next) {

    // monkey patch res.send so that non-tenant data is not exposed and 
    // remove the tenant id from the output.
    let send = res.send;
    res.send = function (body) {
        if (!mtCheck(body, req.tenantId))
            return res.sendError(403, 'Access to other tenant data is not allowed');
        return send.call(this, body);
    };
    
    // Talking to ourself is a sign of poor mental health.  But in this
    // case it IS most likely a test being run.
    let isSelf = req.headers.host.startsWith('127.0.0.1');
    if (isSelf) {
        req.tenantId = 42;
        return next();
    }

    // Determine the tenant id from the request's Host header.
    let parts = req.headers.host.split('.');
    let domain = parts[0];
    
    // Find the tenant with the domain.
    let selector = { 
        domain: domain
    };
    model.tenant.db.collection('tenant')
        .findOneAsync(selector)
        .then(function(tenant) {
            if (tenant === null)
                return res.sendError(404, 'Check the host name, especially ' + domain);
                
            req.tenantId = tenant.tenantId;
        })
        .then(next);
}

module.exports = multi_tenancy;<commit_msg>returns 400 Bad Request with unknown tenant<commit_after>'use strict';

var model = require('../model');

function mtCheck(o, tenantId) {
    if (Array.isArray(o)) {
        for (let e of o) {
            if (!mtCheck(e, tenantId))
                return false;
        }
        return true;
    }
    if (o === null || typeof o !== 'object')
        return true;
    if ('tenantId' in o) {
        if (o.tenantId != tenantId)
            return false;
        o.tenantId = undefined;
    }
    else if ('metadata' in o && 'tenantId' in o.metadata)
    {
        if (o.metadata.tenantId != tenantId)
            return false;
        o.metadata.tenantId = undefined;
    }
    return true;
}

function multi_tenancy (req, res, next) {

    // monkey patch res.send so that non-tenant data is not exposed and 
    // remove the tenant id from the output.
    let send = res.send;
    res.send = function (body) {
        if (!mtCheck(body, req.tenantId))
            return res.sendError(403, 'Access to other tenant data is not allowed');
        return send.call(this, body);
    };
    
    // Talking to ourself is a sign of poor mental health.  But in this
    // case it IS most likely a test being run.
    let isSelf = req.headers.host.startsWith('127.0.0.1');
    if (isSelf) {
        req.tenantId = 42;
        return next();
    }

    // Determine the tenant id from the request's Host header.
    let parts = req.headers.host.split('.');
    let domain = parts[0];
    
    // Find the tenant with the domain.
    let selector = { 
        domain: domain
    };
    model.tenant.db.collection('tenant')
        .findOneAsync(selector)
        .then(function(tenant) {
            if (tenant === null)
                return res.sendError(400, 'Check the host name, especially ' + domain);
                
            req.tenantId = tenant.tenantId;
            next();
        });
}

module.exports = multi_tenancy;
{'fraction_non_alphanumeric': 0.11906005221932114, 'fraction_numerical': 0.008616187989556136, 'mean_word_length': 1.8804511278195488, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '7029430', 'n_tokens_mistral': 1157, 'n_tokens_neox': 1154, 'n_words': 361}