<commit_before>'use strict';

var log = require('./logging.js').getLogger('securityController.js');
var extend = require('extend');

var universalDaoModule = require('./UniversalDao.js');

var DEFAULT_CFG = {
    userCollection : 'people',
    groupCollection : 'groups',
    schemas : [  '/shared/schemas/permissions.json', '/shared/schemas/login.json', '/shared/schemas/systemCredentials.json' ]
};

var SchemaToolsModule = require('./SchemaTools.js');

var fs = require('fs');

var SecurityController = function(mongoDriver,schemaRegistry, options) {

	var cfg = extend(true, {}, DEFAULT_CFG, options);


	var userDao = new universalDaoModule.UniversalDao(mongoDriver, {
		collectionName : cfg.userCollection
	});

	
	var groupDao = new universalDaoModule.UniversalDao(mongoDriver, {
		collectionName : cfg.groupCollection
	});
	
	this.getPermissions = function(req, resp) {
		var defaultObj = schemaRegistry.createDefaultObject('uri://registries/security#permissions');

		var result = [];

		for ( var pro in defaultObj) {
			result.push(pro);
		}

		resp.send(200, result);
	};

	this.getUserPermissions = function(req, resp) {

		var userId = req.url.substring(18, req.url.lenght);

		userDao.get(userId, function(err, user) {

			if (err) {
				resp.send(500, err);
			} else {
				var userRes = {};
				userRes.loginName = user.systemCredentials.loginName;
				var perrmissions = [];

				for ( var per in user.systemCredentials.permissions) {
					if (user.systemCredentials.permissions[per]) {
						perrmissions.push(per);
					}
				}
				userRes.permissions = perrmissions;
				resp.send(200, userRes);
			}

		});

	};

	this.getUserGroups = function(req, resp) {

		var userId = req.url.substring(18, req.url.lenght);

		userDao.get(userId, function(err, user) {

			if (err) {
				resp.send(500, err);
			} else {
				var userRes = {};
				userRes.loginName = user.systemCredentials.loginName;
				var groups = [];

				for ( var gr in user.systemCredentials.groups) {
					groups.push(gr);
				}
				userRes.groups = perrmissions;
				resp.send(200, userRes);
			}

		});

	};
	
	this.updateUserGroups = function(req, resp) {

		var userId = req.url.substring(18, req.url.lenght);

		userDao.get(userId, function(err, user) {

			if (err) {
				resp.send(500, err);
			} else {
				var userRes = {};
				userRes.loginName = user.systemCredentials.loginName;
				var groups = [];

				for ( var gr in user.systemCredentials.groups) {
					groups.push(gr);
				}
				userRes.groups = perrmissions;
				resp.send(200, userRes);
			}

		});

	};
	

	var hasPermission = function(coll, perm) {

		for (var i = coll.length; i--;) {
			if (coll[i] === perm) {
				return true;
			}
		}
		return false;
	}

	this.updateUserPermissions = function(req, resp) {

		var userId = req.body.userId;
		userDao.get(userId, function(err, user) {

			if (err) {
				resp.send(500, err);
			} else {

				var defaultObj = schemaTools.createDefaultObject('uri://registries/security#permissions');

				var result = [];

				if (!user.systemCredentials) {
					user.systemCredentials = {};
				}
				if (!user.systemCredentials.permissions) {
					user.systemCredentials.permissions = {};
				}
				for ( var per in defaultObj) {
					result.push(per);
					user.systemCredentials.permissions[per] = hasPermission(req.body.permissions, per);
				}
				userDao.update(user, function(err) {
					if (err) {
						resp.send(500, err);
					} else {
						resp.send(200, result);
					}

				});

			}

		});

	};
	
	
	
	this.updateUserSecurity = function(req, resp) {

		var userId = req.body.userId;
		userDao.get(userId, function(err, user) {

			if (err) {
				resp.send(500, err);
			} else {

				var defaultObj = schemaTools.createDefaultObject('uri://registries/security#permissions');

				log.silly(req.body);

				if (!user.systemCredentials) {
					user.systemCredentials = {};
				}
				if (!user.systemCredentials.permissions) {
					user.systemCredentials.permissions = {};
				}
				
				delete user.systemCredentials.groups;
				
				if (!user.systemCredentials.groups) {
					user.systemCredentials.groups = [];
				}
				
				for ( var per in defaultObj) {
					user.systemCredentials.permissions[per] = hasPermission(req.body.permissions, per);
				}
				
				req.body.groups.map(function(group){<commit_msg>Fix. user security save schematools replaced by schemaregistry.<commit_after>'use strict';

var log = require('./logging.js').getLogger('securityController.js');
var extend = require('extend');

var universalDaoModule = require('./UniversalDao.js');

var DEFAULT_CFG = {
    userCollection : 'people',
    groupCollection : 'groups',
    schemas : [  '/shared/schemas/permissions.json', '/shared/schemas/login.json', '/shared/schemas/systemCredentials.json' ]
};

var SchemaToolsModule = require('./SchemaTools.js');

var fs = require('fs');

var SecurityController = function(mongoDriver,schemaRegistry, options) {

	var cfg = extend(true, {}, DEFAULT_CFG, options);


	var userDao = new universalDaoModule.UniversalDao(mongoDriver, {
		collectionName : cfg.userCollection
	});

	
	var groupDao = new universalDaoModule.UniversalDao(mongoDriver, {
		collectionName : cfg.groupCollection
	});
	
	this.getPermissions = function(req, resp) {
		var defaultObj = schemaRegistry.createDefaultObject('uri://registries/security#permissions');

		var result = [];

		for ( var pro in defaultObj) {
			result.push(pro);
		}

		resp.send(200, result);
	};

	this.getUserPermissions = function(req, resp) {

		var userId = req.url.substring(18, req.url.lenght);

		userDao.get(userId, function(err, user) {

			if (err) {
				resp.send(500, err);
			} else {
				var userRes = {};
				userRes.loginName = user.systemCredentials.loginName;
				var perrmissions = [];

				for ( var per in user.systemCredentials.permissions) {
					if (user.systemCredentials.permissions[per]) {
						perrmissions.push(per);
					}
				}
				userRes.permissions = perrmissions;
				resp.send(200, userRes);
			}

		});

	};

	this.getUserGroups = function(req, resp) {

		var userId = req.url.substring(18, req.url.lenght);

		userDao.get(userId, function(err, user) {

			if (err) {
				resp.send(500, err);
			} else {
				var userRes = {};
				userRes.loginName = user.systemCredentials.loginName;
				var groups = [];

				for ( var gr in user.systemCredentials.groups) {
					groups.push(gr);
				}
				userRes.groups = perrmissions;
				resp.send(200, userRes);
			}

		});

	};
	
	this.updateUserGroups = function(req, resp) {

		var userId = req.url.substring(18, req.url.lenght);

		userDao.get(userId, function(err, user) {

			if (err) {
				resp.send(500, err);
			} else {
				var userRes = {};
				userRes.loginName = user.systemCredentials.loginName;
				var groups = [];

				for ( var gr in user.systemCredentials.groups) {
					groups.push(gr);
				}
				userRes.groups = perrmissions;
				resp.send(200, userRes);
			}

		});

	};
	

	var hasPermission = function(coll, perm) {

		for (var i = coll.length; i--;) {
			if (coll[i] === perm) {
				return true;
			}
		}
		return false;
	}

	this.updateUserPermissions = function(req, resp) {

		var userId = req.body.userId;
		userDao.get(userId, function(err, user) {

			if (err) {
				resp.send(500, err);
			} else {

				var defaultObj = schemaRegistry.createDefaultObject('uri://registries/security#permissions');

				var result = [];

				if (!user.systemCredentials) {
					user.systemCredentials = {};
				}
				if (!user.systemCredentials.permissions) {
					user.systemCredentials.permissions = {};
				}
				for ( var per in defaultObj) {
					result.push(per);
					user.systemCredentials.permissions[per] = hasPermission(req.body.permissions, per);
				}
				userDao.update(user, function(err) {
					if (err) {
						resp.send(500, err);
					} else {
						resp.send(200, result);
					}

				});

			}

		});

	};
	
	
	
	this.updateUserSecurity = function(req, resp) {

		var userId = req.body.userId;
		userDao.get(userId, function(err, user) {

			if (err) {
				resp.send(500, err);
			} else {

				var defaultObj = schemaRegistry.createDefaultObject('uri://registries/security#permissions');

				log.silly(req.body);

				if (!user.systemCredentials) {
					user.systemCredentials = {};
				}
				if (!user.systemCredentials.permissions) {
					user.systemCredentials.permissions = {};
				}
				
				delete user.systemCredentials.groups;
				
				if (!user.systemCredentials.groups) {
					user.systemCredentials.groups = [];
				}
				
				for ( var per in defaultObj) {
					user.systemCredentials.permissions[per] = hasPermission(req.body.permissions, per);
				}
				
				req.body.groups.map(function(group){
{'fraction_non_alphanumeric': 0.14351425942962281, 'fraction_numerical': 0.008969641214351426, 'mean_word_length': 3.1140018921475874, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3201174', 'n_tokens_mistral': 3514, 'n_tokens_neox': 2849, 'n_words': 518}