Finished up with global user bans.
This commit is contained in:
parent
26df91262f
commit
8fc699924e
13 changed files with 180 additions and 37 deletions
|
|
@ -18,12 +18,11 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
|||
const {mongoose} = require('mongoose');
|
||||
|
||||
//Local Imports
|
||||
const {userSchema} = require('./userSchema');
|
||||
const {userModel, userSchema} = require('./userSchema');
|
||||
|
||||
const userBanSchema = new mongoose.Schema({
|
||||
user: {
|
||||
type: mongoose.SchemaTypes.ObjectID,
|
||||
required: true,
|
||||
ref: "user"
|
||||
},
|
||||
//To be used in future when ip-hashing/better session tracking is implemented
|
||||
|
|
@ -36,6 +35,10 @@ const userBanSchema = new mongoose.Schema({
|
|||
type: [userSchema],
|
||||
required: false
|
||||
},
|
||||
deletedNames: {
|
||||
type: [mongoose.SchemaTypes.String],
|
||||
required: false
|
||||
},
|
||||
banDate: {
|
||||
|
||||
type: mongoose.SchemaTypes.Date,
|
||||
|
|
@ -60,8 +63,10 @@ userBanSchema.statics.checkBanByUserDoc = async function(userDB){
|
|||
var foundBan = null;
|
||||
|
||||
banDB.forEach((ban) => {
|
||||
if(ban.user.toString() == userDB._id.toString()){
|
||||
foundBan = ban;
|
||||
if(ban.user != null){
|
||||
if(ban.user.toString() == userDB._id.toString()){
|
||||
foundBan = ban;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -73,6 +78,27 @@ userBanSchema.statics.checkBan = async function(user){
|
|||
return this.checkBanByUserDoc(userDB);
|
||||
}
|
||||
|
||||
userBanSchema.statics.checkProcessedBans = async function(user){
|
||||
//Pull banlist and create empty variable to hold any found ban
|
||||
const banDB = await this.find({});
|
||||
var foundBan = null;
|
||||
|
||||
//For each ban in list
|
||||
banDB.forEach((ban)=>{
|
||||
//For each deleted account associated with the ban
|
||||
ban.deletedNames.forEach((name)=>{
|
||||
//If the banned name equals the name we're checking against
|
||||
if(name == user){
|
||||
//We've found our ban
|
||||
foundBan = ban;
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
//Return any found associated ban
|
||||
return foundBan;
|
||||
}
|
||||
|
||||
userBanSchema.statics.banByUserDoc = async function(userDB, permanent, expirationDays){
|
||||
//Prevent missing users
|
||||
if(userDB == null){
|
||||
|
|
@ -111,20 +137,40 @@ userBanSchema.statics.unbanByUserDoc = async function(userDB){
|
|||
throw new Error("User not found")
|
||||
}
|
||||
|
||||
const ban = await this.checkBanByUserDoc(userDB);
|
||||
const banDB = await this.checkBanByUserDoc(userDB);
|
||||
|
||||
if(!ban){
|
||||
if(!banDB){
|
||||
throw new Error("User already un-banned");
|
||||
}
|
||||
|
||||
//Use _id in-case mongoose wants to be a cunt
|
||||
var oldBan = await this.deleteOne({_id: ban._id});
|
||||
var oldBan = await this.deleteOne({_id: banDB._id});
|
||||
return oldBan;
|
||||
}
|
||||
|
||||
userBanSchema.statics.unbanDeleted = async function(user){
|
||||
const banDB = await this.checkProcessedBans(user);
|
||||
|
||||
if(!banDB){
|
||||
throw new Error("User already un-banned");
|
||||
}
|
||||
|
||||
const oldBan = await this.deleteOne({_id: banDB._id});
|
||||
return oldBan;
|
||||
}
|
||||
|
||||
userBanSchema.statics.unban = async function(user){
|
||||
//Find user in DB
|
||||
const userDB = await userModel.findOne({user: user.user});
|
||||
return this.unbanByUserDoc(userDB);
|
||||
|
||||
//If user was deleted
|
||||
if(userDB == null){
|
||||
//unban deleted user
|
||||
return await this.unbanDeleted(user.user);
|
||||
}else{
|
||||
//unban by user doc
|
||||
return await this.unbanByUserDoc(userDB);
|
||||
}
|
||||
}
|
||||
|
||||
userBanSchema.statics.getBans = async function(){
|
||||
|
|
@ -136,11 +182,14 @@ userBanSchema.statics.getBans = async function(){
|
|||
var expirationDate = new Date(ban.banDate);
|
||||
expirationDate.setDate(expirationDate.getDate() + ban.expirationDays);
|
||||
|
||||
const userObj = {
|
||||
id: ban.user.id,
|
||||
user: ban.user.user,
|
||||
img: ban.user.img,
|
||||
date: ban.user.date
|
||||
//Make sure we're not about to read the properties of a null object
|
||||
if(ban.user != null){
|
||||
var userObj = {
|
||||
id: ban.user.id,
|
||||
user: ban.user.user,
|
||||
img: ban.user.img,
|
||||
date: ban.user.date
|
||||
}
|
||||
}
|
||||
|
||||
const banObj = {
|
||||
|
|
@ -150,6 +199,7 @@ userBanSchema.statics.getBans = async function(){
|
|||
user: userObj,
|
||||
ips: ban.ips,
|
||||
alts: ban.alts,
|
||||
deletedNames: ban.deletedNames,
|
||||
permanent: ban.permanent
|
||||
}
|
||||
|
||||
|
|
@ -159,6 +209,38 @@ userBanSchema.statics.getBans = async function(){
|
|||
return bans;
|
||||
}
|
||||
|
||||
userBanSchema.statics.processExpiredBans = async function(){
|
||||
const banDB = await this.find({});
|
||||
|
||||
banDB.forEach(async (ban) => {
|
||||
//This ban was already processed, and it's user has been deleted. There is no more to be done...
|
||||
if(ban.user == null){
|
||||
console.log(ban);
|
||||
return;
|
||||
}
|
||||
|
||||
//If the ban hasn't been processed and it's got 0 or less days to go
|
||||
if(ban.getDaysUntilExpiration() <= 0){
|
||||
//If the ban is permanent
|
||||
if(ban.permanent){
|
||||
//Populate the user field
|
||||
await ban.populate('user');
|
||||
//Add the name to our deleted names list
|
||||
ban.deletedNames.push(ban.user.user);
|
||||
//Hey hey hey, goodbye!
|
||||
await userModel.deleteOne({_id: ban.user._id});
|
||||
//Empty out the reference
|
||||
ban.user = null;
|
||||
//Save the ban
|
||||
await ban.save();
|
||||
}else{
|
||||
//Otherwise, delete the ban and let our user back in :P
|
||||
await this.deleteOne({_id: ban._id});
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
//methods
|
||||
userBanSchema.methods.getDaysUntilExpiration = function(){
|
||||
//Get ban date
|
||||
|
|
@ -166,7 +248,7 @@ userBanSchema.methods.getDaysUntilExpiration = function(){
|
|||
//Get expiration days and calculate expiration date
|
||||
expirationDate.setDate(expirationDate.getDate() + this.expirationDays);
|
||||
//Calculate and return days until ban expiration
|
||||
return ((expirationDate - new Date()) / (1000 * 60 * 60 * 24)).toFixed(1);
|
||||
return daysUntilExpiraiton = ((expirationDate - new Date()) / (1000 * 60 * 60 * 24)).toFixed(1);
|
||||
}
|
||||
|
||||
module.exports = mongoose.model("userBan", userBanSchema);
|
||||
|
|
@ -18,10 +18,10 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
|||
const {mongoose} = require('mongoose');
|
||||
|
||||
//local imports
|
||||
const server = require('../server.js');
|
||||
const statModel = require('./statSchema.js');
|
||||
const flairModel = require('./flairSchema.js');
|
||||
const permissionModel = require('./permissionSchema.js');
|
||||
const server = require('../server');
|
||||
const statModel = require('./statSchema');
|
||||
const flairModel = require('./flairSchema');
|
||||
const permissionModel = require('./permissionSchema');
|
||||
const hashUtil = require('../utils/hashUtils');
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue