Worked ban frontend and api.

This commit is contained in:
rainbow napkin 2024-11-29 08:00:25 -05:00
parent 5c936462a6
commit c848994c1d
18 changed files with 513 additions and 41 deletions

View file

@ -48,7 +48,7 @@ const userBanSchema = new mongoose.Schema({
default: 30
},
//If true, then expiration date deletes associated accounts instead of deleting the ban record
deleteAccountOnExpire: {
permanent: {
type: mongoose.SchemaTypes.Boolean,
required: true,
default: false
@ -73,17 +73,58 @@ userBanSchema.statics.checkBan = async function(user){
return this.checkBanByUserDoc(userDB);
}
userBanSchema.statics.banByUserDoc = async function(userDB){
userBanSchema.statics.banByUserDoc = async function(userDB, permanent, expirationDays){
//Prevent missing users
if(userDB == null){
throw new Error("User not found")
}
//Ensure the user isn't already banned
if(await this.checkBanByUserDoc(userDB) != null){
throw new Error("User already banned");
}
return await this.create({user: userDB._id});
if(expirationDays < 0){
throw new Error("Expiration Days must be a positive integer!");
}else if(expirationDays < 30 && permanent){
throw new Error("Permanent bans must be given at least 30 days before automatic account deletion!");
}else if(expirationDays > 185){
throw new Error("Expiration/Deletion date cannot be longer than half a year out from the original ban date.");
}
//Log the user out
await userDB.killAllSessions();
//Add the ban to the database
return await this.create({user: userDB._id, permanent, expirationDays});
}
userBanSchema.statics.ban = async function(user){
userBanSchema.statics.ban = async function(user, permanent, expirationDays){
const userDB = await userModel.findOne({user: user.user});
return this.banByUserDoc(userDB);
return this.banByUserDoc(userDB, permanent, expirationDays);
}
userBanSchema.statics.unbanByUserDoc = async function(userDB){
//Prevent missing users
if(userDB == null){
throw new Error("User not found")
}
const ban = await this.checkBanByUserDoc(userDB);
if(!ban){
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});
return oldBan;
}
userBanSchema.statics.unban = async function(user){
const userDB = await userModel.findOne({user: user.user});
return this.unbanByUserDoc(userDB);
}
userBanSchema.statics.getBans = async function(){
@ -109,7 +150,7 @@ userBanSchema.statics.getBans = async function(){
user: userObj,
ips: ban.ips,
alts: ban.alts,
deleteAccountOnExpire: ban.deleteAccountOnExpire
permanent: ban.permanent
}
bans.push(banObj);

View file

@ -211,7 +211,7 @@ userSchema.statics.getUserList = async function(fullList = false){
//create a user object with limited properties (safe for public consumption)
var userObj = {
id: user.id,
name: user.user,
user: user.user,
img: user.img,
date: user.date
}
@ -270,7 +270,7 @@ userSchema.methods.nuke = async function(pass){
if(this.checkPass(pass)){
//Annoyingly there isnt a good way to do this from 'this'
var oldUser = await module.exports.deleteOne(this);
var oldUser = await module.exports.userModel.deleteOne(this);
if(oldUser){
await this.killAllSessions();