<commit_before>const express = require("express");
const session = require("express-session");
const path = require("path");
const connect = require("../database.js");

// Middleware to make sure user is logged in
var restrict = function(req, res, next){
	if (req.session.user) {
		next();
	} else {
		req.session.error = "Please login to continue.";
		res.redirect("/admin/login");
	}
};

// Restrict route to administrators only
restrict.toAdministrator = function(req, res, next){
	connect.then(function(db){
		db.collection("_users_auth").findOne({username: req.session.user.username}, function(err, user){
			if(err) throw err;

			if(user.role == "administrator"){
				next();
			} else{
				res.status(403);
				res.json({
					status: "error",
					message: "Not allowed"
				});
			}
		});
	});
};

// Restrict route to administrators and editors only
restrict.toEditor = function(req, res, next){
	connect.then(function(db){
		db.collection("_users_auth").findOne({username: req.session.user.username}, function(err, user){
			if(err) throw err;

			if(user.role == "administrator" || user.role == "editor"){
				next();
			} else{
				res.status(403);
				res.json({
					status: "error",
					message: "Not allowed"
				});
			}
		});
	});
};

// Restrict route to administrators, editors and authors only
restrict.toAuthor = function(req, res, next){
	connect.then(function(db){
		db.collection("_users_auth").findOne({username: req.session.user.username}, function(err, user){
			if(err) throw err;

			if(user.role == "administrator" || user.role == "editor" || user.role == "author"){
				next();
			} else{
				res.status(403);
				res.json({
					status: "error",
					message: "Not allowed"
				});
			}
		});
	});
};

module.exports = restrict;<commit_msg>Rolling pin<commit_after>const express = require("express");
const session = require("express-session");
const path = require("path");
const connect = require("../database.js");

// Middleware to make sure user is logged in
var restrict = function(req, res, next){
	if (req.session.user) {
		next();
	} else {
		req.session.error = "Please login to continue.";
		res.redirect("/admin/login");
	}
};

// Restrict route to administrators only
restrict.toAdministrator = function(req, res, next){
	connect.then(function(db){
		return db.collection("_users_auth").findOne({username: req.session.user.username});
	}).then(function(user){
		if(user.role == "administrator"){
			next();
		} else{
			res.status(403);
			res.json({
				status: "error",
				message: "Not allowed"
			});
		}
	}).catch(function(err){
		next(err);
	});
};

// Restrict route to administrators and editors only
restrict.toEditor = function(req, res, next){
	connect.then(function(db){
		return db.collection("_users_auth").findOne({username: req.session.user.username});
	}).then(function(user){
		if(user.role == "administrator" || user.role == "editor"){
			next();
		} else{
			res.status(403);
			res.json({
				status: "error",
				message: "Not allowed"
			});
		}
	}).catch(function(err){
		next(err);
	});
};

// Restrict route to administrators, editors and authors only
restrict.toAuthor = function(req, res, next){
	connect.then(function(db){
		return db.collection("_users_auth").findOne({username: req.session.user.username});
	}).then(function(user){
		if(user.role == "administrator" || user.role == "editor" || user.role == "author"){
			next();
		} else{
			res.status(403);
			res.json({
				status: "error",
				message: "Not allowed"
			});
		}
	}).catch(function(err){
		next(err);
	});
};

module.exports = restrict;
{'fraction_non_alphanumeric': 0.18237590630228667, 'fraction_numerical': 0.0050195203569436695, 'mean_word_length': 3.9069767441860463, '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': '4913287', 'n_tokens_mistral': 1344, 'n_tokens_neox': 1156, 'n_words': 261}