<commit_before>/*
 * Copyright 2012 <NAME> Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 *      
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 */
 
/**
 * 
 * db: provide way to exchange wih dbms
 * validator: given a type, validate and prepare data from outside before integration  
 * mapping: given an external name, provide the name of an entity type accessible from outside
 * moment: date manipulation utility
 * 
 */  
 
exports.inject = function (db, moment) {
	return {
		build: function (validator, mapping) {
			var memoizer = {}
			
			var internal = function (t) {
				var h = memoizer[t]
				if (typeof h === 'undefined') {
					console.log(Date(),'t:',t)
					var col = db.col(t)
					var val = validator(t)
					h = {
						getById: function(id,cb) {
							if ('getById' in val)
								val.getById(internal, id, cb)
							else {
								col.find({_id: db.objectId(id)},function (err, docs) {
									if (! err) {
										docs = docs || []
										if ('onResult' in val) docs = val.onResult(docs)
									}
									cb(err,docs[0])
								})
							}
						},
						find: function(crit,cb) {col.find(crit,cb)},
						save: function(obj,cb) {
							val.check(obj,h,function (err) {
								if (err) cb(err);
								else val.prepare(obj, function(obj) { col.save(obj,cb) })
							})
						},
						remove: function(id,cb) {col.remove({_id: db.objectId(id)},cb)}
					}
					memoizer[t] = h
				}
				return h
			}

			var biz = {
				login: function (l, cb) {
					internal('user').find({'login':l.l},function(err,docs) {
						if(err) {
							console.error(Date(),'While retrieving user',err)
							setTimeout(function() {cb({status:500})}, 1000)
						} else if (docs.length>0) {
							var v = docs[0]
							if(validator('login').compare(l,v)) {
								console.log(Date(), "Accepted user ", l.l, "creating session")
								internal('session').save({'user':v._id},function(err,session) {
									if (err) {
										console.error(Date(),'While creating session',err)
										setTimeout(function() {cb({status:500})}, 1000)
									}
									else {
										console.log(Date(), "Session created for user ", l.l, ":", session._id)
										cb(undefined,session._id)
									}
								})
							} else {
								console.error(Date(),'refuse invalid password',l)
								setTimeout(function() {cb({status:401})}, 1000)
							}
						} else {
							console.error(Date(),'refuse invalid login',l)
							setTimeout(function() {cb({status:401})}, 1000)		
						}
					})
				},
				auth: function (sid, path, cb) {
					console.log(Date(), 'biz.auth: ', sid, path)
					if (sid) {
						internal('session').getById(sid,function(err,doc) {
							if (err) cb(err)
							else if (doc && (moment().valueOf() < moment(doc.expirationDate).valueOf())) {
								internal('user').getById(doc.user.toString(), function(err,user) {
									if (!err && user) validator('user').isAuthorized(user, path, internal('role'), cb)
									else cb(err)
								})
							} else cb(true)
						})
					} else cb(true)
				},
				on: function (ext_name) {<commit_msg>more async style<commit_after>/*
 * Copyright 2012 <NAME> Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 *      
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 */
 
/**
 * 
 * db: provide way to exchange wih dbms
 * validator: given a type, validate and prepare data from outside before integration  
 * mapping: given an external name, provide the name of an entity type accessible from outside
 * moment: date manipulation utility
 * 
 */  
 
exports.inject = function (db, moment) {
	return {
		build: function (validator, mapping) {
			var memoizer = {}
			
			var internal = function (t) {
				var h = memoizer[t]
				if (typeof h === 'undefined') {
					console.log(Date(),'t:',t)
					var col = db.col(t)
					var val = validator(t)
					h = {
						getById: function(id,cb) {
							if ('getById' in val)
								val.getById(internal, id, cb)
							else {
								col.find({_id: db.objectId(id)},function (err, docs) {
									if (! err) {
										docs = docs || []
										if ('onResult' in val) docs = val.onResult(docs)
									}
									cb(err,docs[0])
								})
							}
						},
						find: function(crit,cb) {col.find(crit,cb)},
						save: function(obj,cb) {
							val.check(obj,h,function (err) {
								if (err) cb(err);
								else val.prepare(obj, function(err, obj) {
									if (err) cb(err)
									else col.save(obj,cb)
								})
							})
						},
						remove: function(id,cb) {col.remove({_id: db.objectId(id)},cb)}
					}
					memoizer[t] = h
				}
				return h
			}

			var biz = {
				login: function (l, cb) {
					internal('user').find({'login':l.l},function(err,docs) {
						if(err) {
							console.error(Date(),'While retrieving user',err)
							setTimeout(function() {cb({status:500})}, 1000)
						} else if (docs.length>0) {
							var v = docs[0]
							validator('login').compare(l, v, function(err, valid) {
								if (err) {
									console.error(Date(),'While creating session',err)
									setTimeout(function() {cb({status:500})}, 1000)
								} else if (valid) {
									console.log(Date(), "Accepted user ", l.l, "creating session")
									internal('session').save({'user':v._id},function(err,session) {
										if (err) {
											console.error(Date(),'While creating session',err)
											setTimeout(function() {cb({status:500})}, 1000)
										}
										else {
											console.log(Date(), "Session created for user ", l.l, ":", session._id)
											cb(undefined,session._id)
										}
									})
								} else {
									console.error(Date(),'refuse invalid password',l)
									setTimeout(function() {cb({status:401})}, 1000)
								}
							})
						} else {
							console.error(Date(),'refuse invalid login',l)
							setTimeout(function() {cb({status:401})}, 1000)		
						}
					})
				},
				auth: function (sid, path, cb) {
					console.log(Date(), 'biz.auth: ', sid, path)
					if (sid) {
						internal('session').getById(sid,function(err,doc) {
							if (err) cb(err)
							else if (doc && (moment().valueOf() < moment(doc.expirationDate).valueOf())) {
								internal('user').getById(doc.user.toString(), function(err,user) {
									if (!err && user) validator('user').isAuthorized(user, path, internal('role'), cb)
									else cb(err)
								})
							} else cb(true)
						})
					} else cb(true)
				},
				on: function (ext_name) {
{'fraction_non_alphanumeric': 0.15086618469513027, 'fraction_numerical': 0.011594598281271313, 'mean_word_length': 2.4406381980290943, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3766838', 'n_tokens_mistral': 3299, 'n_tokens_neox': 2242, 'n_words': 609}