Started global user bans (DB schema/admin panel)
This commit is contained in:
parent
5c30508e96
commit
f996018ea5
20 changed files with 339 additions and 15 deletions
|
|
@ -20,7 +20,7 @@ const {validationResult, matchedData} = require('express-validator');
|
|||
|
||||
//Local Imports
|
||||
const statModel = require('../statSchema.js');
|
||||
const userModel = require('../userSchema.js');
|
||||
const {userModel} = require('../userSchema.js');
|
||||
const permissionModel = require('../permissionSchema.js');
|
||||
const channelPermissionSchema = require('./channelPermissionSchema.js');
|
||||
const { exceptionHandler } = require('../../utils/loggerUtils.js');
|
||||
|
|
|
|||
121
src/schemas/userBanSchema.js
Normal file
121
src/schemas/userBanSchema.js
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
/*Canopy - The next generation of stoner streaming software
|
||||
Copyright (C) 2024 Rainbownapkin and the TTN Community
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
||||
|
||||
//NPM Imports
|
||||
const {mongoose} = require('mongoose');
|
||||
|
||||
//Local Imports
|
||||
const {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
|
||||
ips: {
|
||||
type: [mongoose.SchemaTypes.String],
|
||||
required: false
|
||||
},
|
||||
//To be used in future when alt-detection has been implemented
|
||||
alts: {
|
||||
type: [userSchema],
|
||||
required: false
|
||||
},
|
||||
banDate: {
|
||||
|
||||
type: mongoose.SchemaTypes.Date,
|
||||
required: true,
|
||||
default: new Date()
|
||||
},
|
||||
expirationDays: {
|
||||
type: mongoose.SchemaTypes.Number,
|
||||
required: true,
|
||||
default: 30
|
||||
},
|
||||
//If true, then expiration date deletes associated accounts instead of deleting the ban record
|
||||
deleteAccountOnExpire: {
|
||||
type: mongoose.SchemaTypes.Boolean,
|
||||
required: true,
|
||||
default: false
|
||||
}
|
||||
});
|
||||
|
||||
userBanSchema.statics.checkBanByUserDoc = async function(userDB){
|
||||
const banDB = await this.find({});
|
||||
var foundBan = null;
|
||||
|
||||
banDB.forEach((ban) => {
|
||||
if(ban.user.toString() == userDB._id.toString()){
|
||||
foundBan = ban;
|
||||
}
|
||||
});
|
||||
|
||||
return foundBan;
|
||||
}
|
||||
|
||||
userBanSchema.statics.checkBan = async function(user){
|
||||
const userDB = await userModel.findOne({user: user.user});
|
||||
return this.checkBanByUserDoc(userDB);
|
||||
}
|
||||
|
||||
userBanSchema.statics.banByUserDoc = async function(userDB){
|
||||
if(await this.checkBanByUserDoc(userDB) != null){
|
||||
throw new Error("User already banned");
|
||||
}
|
||||
|
||||
return await this.create({user: userDB._id});
|
||||
}
|
||||
|
||||
userBanSchema.statics.ban = async function(user){
|
||||
const userDB = await userModel.findOne({user: user.user});
|
||||
return this.banByUserDoc(userDB);
|
||||
}
|
||||
|
||||
userBanSchema.statics.getBans = async function(){
|
||||
const banDB = await this.find({}).populate('user');
|
||||
var bans = [];
|
||||
|
||||
banDB.forEach((ban) => {
|
||||
//Calcualte expiration date
|
||||
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
|
||||
}
|
||||
|
||||
const banObj = {
|
||||
banDate: ban.banDate,
|
||||
expirationDays: ban.expirationDays,
|
||||
expirationDate: expirationDate,
|
||||
user: userObj,
|
||||
ips: ban.ips,
|
||||
alts: ban.alts,
|
||||
deleteAccountOnExpire: ban.deleteAccountOnExpire
|
||||
}
|
||||
|
||||
bans.push(banObj);
|
||||
});
|
||||
|
||||
return bans;
|
||||
}
|
||||
|
||||
module.exports = mongoose.model("userBan", userBanSchema);
|
||||
|
|
@ -282,4 +282,5 @@ userSchema.methods.nuke = async function(pass){
|
|||
}
|
||||
}
|
||||
|
||||
module.exports = mongoose.model("user", userSchema);
|
||||
module.exports.userSchema = userSchema;
|
||||
module.exports.userModel = mongoose.model("user", userSchema);
|
||||
Loading…
Add table
Add a link
Reference in a new issue