<commit_before>var express = require('express');
var router = express.Router({strict:true,mergeParams:true});
var defer = require("promised-io/promise").defer;
var when = require("promised-io/promise").when;
var config = require("../config");
var SolrQueryParser = require("../middleware/SolrQueryParser");
var RQLQueryParser = require("../middleware/RQLQueryParser");
var authMiddleware = require("../middleware/auth");
var solrjs = require("solrjs");
var media = require("../middleware/media");
var httpParams = require("../middleware/http-params");
var SOLR_URL=config.get("solr").url;
var bodyParser = require("body-parser");
var rql = require("solrjs/rql");
var debug = require('debug')('p3api-server:dataroute');
var Expander= require("../ExpandingQuery");



var publicFree=['enzyme_class_ref', 'gene_ontology_ref', 'id_ref', 'misc_niaid_sgc', 'pathway_ref', 'ppi', 'protein_family_ref', 'sp_gene_evidence', 'sp_gene_ref', 'taxonomy', 'transcriptomics_experiment', 'transcriptomics_gene', 'transcriptomics_sample',"model_reaction","model_complex_role","model_compound","model_template_biomass","model_template_reaction"];

var rqlToSolr = function(req, res, next) {
	debug("RQLQueryParser", req.queryType);
	if (req.queryType=="rql"){
		req.call_params[0] = req.call_params[0] || "";
		when(Expander.ResolveQuery(req.call_params[0],{req:req,res:res}), function(q){
			debug("Resolved Query: ", q);
			if (q=="()") { q = ""; }
			req.call_params[0] = rql(q).toSolr({maxRequestLimit: 25000, defaultLimit: 25}) 
			debug("Converted Solr Query: ", req.call_params[0]);
			req.queryType="solr";
			next();
		});
	}else{
		next();
	}
}

var querySOLR = function(req, res, next) {
		if (req.call_method!="query"){ next(); }

		var query = req.call_params[0];
		debug("querySOLR() req.params", req.call_params);
		var solr = new solrjs(SOLR_URL + "/" + req.call_collection);

		when(solr.query(query), function(results) {
			if (!results || !results.response){
				res.results=[];
				res.set("Content-Range", "items 0-0/0");
			}else{
				res.results = results;

				res.set("Content-Range", "items " + (results.response.start || 0) + "-" + ((results.response.start||0)+results.response.docs.length) + "/" + results.response.numFound);
			}
//			debug("res headers: ", res);
			next();
		}, function(err){
			debug("Error Querying SOLR: ", err);
			next(err);
		})
}
var getSOLR = function(req, res, next) {
	var solr = new solrjs(SOLR_URL + "/" + req.call_collection);
	when(solr.get(req.call_params[0]), function(sresults) {
		if (sresults && sresults.doc) {
			var results = sresults.doc;
//			console.log("results: ", results);
			console.log("results.public: ", results.public);
			console.log("publicFree: ", (publicFree.indexOf(req._call_collection)>=0) );
			console.log("Owner: ", results.owner, req.user);
			console.log("user_read: ", results.user_read, (results.user_read && results.user_read.indexOf(req.user)>=0));
			console.log("req.user: ", req.user);

			if (results.public || (publicFree.indexOf(req.call_collection)>=0) || (results.owner==(req.user)) || (results.user_read && results.user_read.indexOf(req.user)>=0)) {		
				res.results = sresults;
				console.log("Results: ", results);
				next();
			}else{
				if (!req.user){
					console.log("User not logged in, permission denied");
					res.sendStatus(401);
				}else{
					console.log("User forbidden from private data");
					res.sendStatus(403);
				}
			}
		}else{
			next();
		} 
	},function(err){
		console.log("Error in SOLR Get: ", err);
	});
}

var decorateQuery = function(req, res, next) {
	if (req.call_method !="query"){ return next(); }

	debug("decorateQuery", req.solr_query);
	req.call_params[0] = req.call_params[0] || "&q=*:*";
	if (!req.user) {
		if (publicFree.indexOf(req.call_collection)<0) {
			req.call_params[0] = req.call_params[0] + "&fq=public:true"
		}
	}
	else {
		if (publicFree.indexOf(req.call_collection)<0) {
			req.call_params[0]= req.call_params[0] + ("&fq=(public:true OR owner:" + req.user +" OR user_read:" + req.user +")");<commit_msg>missed next()<commit_after>var express = require('express');
var router = express.Router({strict:true,mergeParams:true});
var defer = require("promised-io/promise").defer;
var when = require("promised-io/promise").when;
var config = require("../config");
var SolrQueryParser = require("../middleware/SolrQueryParser");
var RQLQueryParser = require("../middleware/RQLQueryParser");
var authMiddleware = require("../middleware/auth");
var solrjs = require("solrjs");
var media = require("../middleware/media");
var httpParams = require("../middleware/http-params");
var SOLR_URL=config.get("solr").url;
var bodyParser = require("body-parser");
var rql = require("solrjs/rql");
var debug = require('debug')('p3api-server:dataroute');
var Expander= require("../ExpandingQuery");



var publicFree=['enzyme_class_ref', 'gene_ontology_ref', 'id_ref', 'misc_niaid_sgc', 'pathway_ref', 'ppi', 'protein_family_ref', 'sp_gene_evidence', 'sp_gene_ref', 'taxonomy', 'transcriptomics_experiment', 'transcriptomics_gene', 'transcriptomics_sample',"model_reaction","model_complex_role","model_compound","model_template_biomass","model_template_reaction"];

var rqlToSolr = function(req, res, next) {
	debug("RQLQueryParser", req.queryType);
	if (req.queryType=="rql"){
		req.call_params[0] = req.call_params[0] || "";
		when(Expander.ResolveQuery(req.call_params[0],{req:req,res:res}), function(q){
			debug("Resolved Query: ", q);
			if (q=="()") { q = ""; }
			req.call_params[0] = rql(q).toSolr({maxRequestLimit: 25000, defaultLimit: 25}) 
			debug("Converted Solr Query: ", req.call_params[0]);
			req.queryType="solr";
			next();
		});
	}else{
		next();
	}
}

var querySOLR = function(req, res, next) {
		if (req.call_method!="query"){ next(); }

		var query = req.call_params[0];
		debug("querySOLR() req.params", req.call_params);
		var solr = new solrjs(SOLR_URL + "/" + req.call_collection);

		when(solr.query(query), function(results) {
			if (!results || !results.response){
				res.results=[];
				res.set("Content-Range", "items 0-0/0");
			}else{
				res.results = results;

				res.set("Content-Range", "items " + (results.response.start || 0) + "-" + ((results.response.start||0)+results.response.docs.length) + "/" + results.response.numFound);
			}
//			debug("res headers: ", res);
			next();
		}, function(err){
			debug("Error Querying SOLR: ", err);
			next(err);
		})
}
var getSOLR = function(req, res, next) {
	var solr = new solrjs(SOLR_URL + "/" + req.call_collection);
	when(solr.get(req.call_params[0]), function(sresults) {
		if (sresults && sresults.doc) {
			var results = sresults.doc;
//			console.log("results: ", results);
			console.log("results.public: ", results.public);
			console.log("publicFree: ", (publicFree.indexOf(req._call_collection)>=0) );
			console.log("Owner: ", results.owner, req.user);
			console.log("user_read: ", results.user_read, (results.user_read && results.user_read.indexOf(req.user)>=0));
			console.log("req.user: ", req.user);

			if (results.public || (publicFree.indexOf(req.call_collection)>=0) || (results.owner==(req.user)) || (results.user_read && results.user_read.indexOf(req.user)>=0)) {		
				res.results = sresults;
				console.log("Results: ", results);
				next();
			}else{
				if (!req.user){
					console.log("User not logged in, permission denied");
					res.sendStatus(401);
				}else{
					console.log("User forbidden from private data");
					res.sendStatus(403);
				}
			}
		}else{
			next();
		} 
	},function(err){
		console.log("Error in SOLR Get: ", err);
		next(err);
	});
}

var decorateQuery = function(req, res, next) {
	if (req.call_method !="query"){ return next(); }

	debug("decorateQuery", req.solr_query);
	req.call_params[0] = req.call_params[0] || "&q=*:*";
	if (!req.user) {
		if (publicFree.indexOf(req.call_collection)<0) {
			req.call_params[0] = req.call_params[0] + "&fq=public:true"
		}
	}
	else {
		if (publicFree.indexOf(req.call_collection)<0) {
			req.call_params[0]= req.call_params[0] + ("&fq=(public:true OR owner:" + req.user +" OR user_read:" + req.user +")");
{'fraction_non_alphanumeric': 0.1976357591429627, 'fraction_numerical': 0.009358453392439355, 'mean_word_length': 4.898329702251271, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '5242788', 'n_tokens_mistral': 3197, 'n_tokens_neox': 2857, 'n_words': 488}