<commit_before>'use strict';

var WebError = require('web-error'),
	tv4 = require('tv4');

exports.isOwner = function(req, res, next) {
	if(!req.user) {
		return next(new WebError(401));
	}

	if(!req.objects.user) {
		return next(new WebError(404));
	}

	if(!req.user.equals(req.objects.user)) {
		return next(new WebError(401));
	}

	next();
};

exports.user = function(req, res, next, id) {
	var User = req.models.User;

	if(!id) {
		return next(new WebError(400));
	}

	User.findByID(id, function(err, user) {
		if(err) {
			return next(err);
		}

		if(!user) {
			return next(new WebError(404));
		}

		req.objects.user = user;
		next();
	});
};

exports.permalink = function(req, res, next, id) {
	var User = req.models.User;

	if(!id) {
		return next(new WebError(400));
	}

	User.findByPermalink(id, function(err, user) {
		if(err) {
			return next(err);
		}

		if(!user) {
			return next(new WebError(404));
		}

		req.objects.user = user;
		next();
	});
};

/**
 * Create user by simple registraion
 */
exports.create = function(req, res, next) {
	var User = req.models.User;
	var rbac = req.server.rbac;

	var valid = tv4.validate(req.body, createSchema);
	if(!valid) {
		return next(new WebError(400, tv4.error));
	}

	User.create(req.body, function(err, user) {
		if(err) {
			return next(err);
		}

		if(!user) {
			return next(new Error('User is undefined'));
		}

		user.getPrivateJSON(rbac, function(err, json) {
			if(err) {
				return next(err);
			}

			res.jsonp({
				user: json
			});
		});
	});
};

exports.delete = function(req, res, next) {
	var user = req.objects.user;

	if(!user) {
		return next(new WebError(404));
	}

	user.remove(function(err) {
		if(err) {
			return next(err);
		}<commit_msg>added access token to user.create<commit_after>'use strict';

var WebError = require('web-error'),
	tv4 = require('tv4');

exports.isOwner = function(req, res, next) {
	if(!req.user) {
		return next(new WebError(401));
	}

	if(!req.objects.user) {
		return next(new WebError(404));
	}

	if(!req.user.equals(req.objects.user)) {
		return next(new WebError(401));
	}

	next();
};

exports.user = function(req, res, next, id) {
	var User = req.models.User;

	if(!id) {
		return next(new WebError(400));
	}

	User.findByID(id, function(err, user) {
		if(err) {
			return next(err);
		}

		if(!user) {
			return next(new WebError(404));
		}

		req.objects.user = user;
		next();
	});
};

exports.permalink = function(req, res, next, id) {
	var User = req.models.User;

	if(!id) {
		return next(new WebError(400));
	}

	User.findByPermalink(id, function(err, user) {
		if(err) {
			return next(err);
		}

		if(!user) {
			return next(new WebError(404));
		}

		req.objects.user = user;
		next();
	});
};

/**
 * Create user by simple registraion
 */
exports.create = function(req, res, next) {
	var User = req.models.User,
		rbac = req.server.rbac,
		config = req.server.config;

	var valid = tv4.validate(req.body, createSchema);
	if(!valid) {
		return next(new WebError(400, tv4.error));
	}

	User.create(req.body, function(err, user) {
		if(err) {
			return next(err);
		}

		if(!user) {
			return next(new Error('User is undefined'));
		}

		user.getPrivateJSON(rbac, function(err, json) {
			if(err) {
				return next(err);
			}

			var accessToken = user.generateAccessToken(config.token.secret, config.token.expiration);

			res.jsonp({
				token: {
					type: 'Bearer',
					access_token: accessToken
				},
				user: json
			});
		});
	});
};

exports.delete = function(req, res, next) {
	var user = req.objects.user;

	if(!user) {
		return next(new WebError(404));
	}

	user.remove(function(err) {
		if(err) {
			return next(err);
		}
{'fraction_non_alphanumeric': 0.17854217854217855, 'fraction_numerical': 0.016926016926016925, 'mean_word_length': 3.414457831325301, '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': '4496575', 'n_tokens_mistral': 1605, 'n_tokens_neox': 1399, 'n_words': 282}