Started global user bans (DB schema/admin panel)
This commit is contained in:
parent
5c30508e96
commit
f996018ea5
|
|
@ -16,7 +16,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
|||
|
||||
//Local Imports
|
||||
const channelModel = require('../../schemas/channel/channelSchema');
|
||||
const userModel = require('../../schemas/userSchema');
|
||||
const {userModel} = require('../../schemas/userSchema');
|
||||
const loggerUtils = require('../../utils/loggerUtils');
|
||||
const activeChannel = require('./activeChannel');
|
||||
const chatHandler = require('./chatHandler');
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ const validator = require('validator');//No express here, so regular validator i
|
|||
|
||||
//local imports
|
||||
const loggerUtils = require('../../utils/loggerUtils');
|
||||
const userModel = require('../../schemas/userSchema');
|
||||
const {userModel} = require('../../schemas/userSchema');
|
||||
|
||||
module.exports = class{
|
||||
constructor(server){
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
|||
|
||||
//Config
|
||||
const config = require('../../config.json');
|
||||
const userModel = require('../schemas/userSchema');
|
||||
const {userModel} = require('../schemas/userSchema');
|
||||
const permissionModel = require('../schemas/permissionSchema');
|
||||
const channelModel = require('../schemas/channel/channelSchema');
|
||||
const {exceptionHandler} = require("../utils/loggerUtils");
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
|||
const {validationResult, matchedData} = require('express-validator');
|
||||
|
||||
//local imports
|
||||
const userModel = require('../../../schemas/userSchema');
|
||||
const {userModel} = require('../../../schemas/userSchema');
|
||||
const accountUtils = require('../../../utils/sessionUtils.js');
|
||||
const {exceptionHandler} = require('../../../utils/loggerUtils.js');
|
||||
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
|||
const {validationResult, matchedData} = require('express-validator');
|
||||
|
||||
//local imports
|
||||
const userModel = require('../../../schemas/userSchema');
|
||||
const {userModel} = require('../../../schemas/userSchema');
|
||||
const {exceptionHandler} = require('../../../utils/loggerUtils.js');
|
||||
|
||||
module.exports.post = async function(req, res){
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
|||
const {validationResult, matchedData} = require('express-validator');
|
||||
|
||||
//local imports
|
||||
const userModel = require('../../../schemas/userSchema');
|
||||
const {userModel} = require('../../../schemas/userSchema');
|
||||
const accountUtils = require('../../../utils/sessionUtils.js');
|
||||
const {exceptionHandler} = require('../../../utils/loggerUtils.js');
|
||||
|
||||
|
|
|
|||
59
src/controllers/api/admin/banController.js
Normal file
59
src/controllers/api/admin/banController.js
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
/*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 {validationResult, matchedData} = require('express-validator');
|
||||
|
||||
//local imports
|
||||
const banModel = require('../../../schemas/userBanSchema');
|
||||
const {userModel} = require('../../../schemas/userSchema');
|
||||
const {exceptionHandler} = require('../../../utils/loggerUtils');
|
||||
|
||||
module.exports.get = async function(req, res){
|
||||
try{
|
||||
//get bans
|
||||
const bans = await banModel.getBans();
|
||||
|
||||
//send bans
|
||||
res.status(200);
|
||||
return res.send(bans);
|
||||
}catch(err){
|
||||
return exceptionHandler(res, err);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.post = async function(req, res){
|
||||
try{
|
||||
const validResult = validationResult(req);
|
||||
if(validResult.isEmpty()){
|
||||
const {user} = matchedData(req);
|
||||
const userDB = await userModel.findOne({user});
|
||||
|
||||
if(userDB == null){
|
||||
res.status(400);
|
||||
res.send({errors:[{type: "Bad Query", msg: "User not found.", date: new Date()}]});
|
||||
}
|
||||
|
||||
await banModel.banByUserDoc(userDB);
|
||||
|
||||
}else{
|
||||
res.status(400);
|
||||
return res.send({errors: validResult.array()})
|
||||
}
|
||||
}catch(err){
|
||||
return exceptionHandler(res, err);
|
||||
}
|
||||
}
|
||||
|
|
@ -20,7 +20,7 @@ const {validationResult, matchedData} = require('express-validator');
|
|||
//local imports
|
||||
const {exceptionHandler} = require('../../../utils/loggerUtils');
|
||||
const permissionModel = require('../../../schemas/permissionSchema');
|
||||
const userModel = require('../../../schemas/userSchema');
|
||||
const {userModel} = require('../../../schemas/userSchema');
|
||||
|
||||
//api change rank functions
|
||||
module.exports.post = async function(req, res){
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
|||
|
||||
//local imports
|
||||
const {exceptionHandler} = require('../../../utils/loggerUtils.js');
|
||||
const userModel = require('../../../schemas/userSchema');
|
||||
const {userModel} = require('../../../schemas/userSchema');
|
||||
|
||||
//api list account functions
|
||||
module.exports.get = async function(req, res){
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ const {validationResult, matchedData} = require('express-validator');
|
|||
//local imports
|
||||
const {exceptionHandler} = require('../../../utils/loggerUtils');
|
||||
const permissionModel = require('../../../schemas/permissionSchema');
|
||||
const userModel = require('../../../schemas/userSchema');
|
||||
const {userModel} = require('../../../schemas/userSchema');
|
||||
const channelModel = require('../../../schemas/channel/channelSchema');
|
||||
|
||||
//api channel rank functions
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ const {validationResult, matchedData} = require('express-validator');
|
|||
|
||||
//local imports
|
||||
const {exceptionHandler} = require('../../../utils/loggerUtils.js');
|
||||
const userModel = require('../../../schemas/userSchema.js');
|
||||
const {userModel} = require('../../../schemas/userSchema.js');
|
||||
const channelModel = require('../../../schemas/channel/channelSchema');
|
||||
|
||||
//api account functions
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ 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/>.*/
|
||||
|
||||
//Local Imports
|
||||
const userModel = require('../schemas/userSchema');
|
||||
const {userModel} = require('../schemas/userSchema');
|
||||
const {exceptionHandler} = require('../utils/loggerUtils.js');
|
||||
|
||||
//Config
|
||||
|
|
|
|||
|
|
@ -27,6 +27,7 @@ const listUsersController = require("../../controllers/api/admin/listUsersContro
|
|||
const listChannelsController = require("../../controllers/api/admin/listChannelsController");
|
||||
const changeRankController = require("../../controllers/api/admin/changeRankController");
|
||||
const permissionsController = require("../../controllers/api/admin/permissionsController");
|
||||
const banController = require("../../controllers/api/admin/banController");
|
||||
|
||||
//globals
|
||||
const router = Router();
|
||||
|
|
@ -40,5 +41,7 @@ router.get('/listChannels', listChannelsController.get);
|
|||
router.get('/permissions', permissionsController.get);
|
||||
router.post('/permissions', checkExact([permissionsValidator.permissionsMap(), channelPermissionValidator.channelPermissionsMap()]), permissionsController.post);
|
||||
router.post('/changeRank', accountValidator.user(), accountValidator.rank(), changeRankController.post);
|
||||
router.get('/ban', banController.get);
|
||||
router.post('/ban', accountValidator.user(), banController.post);
|
||||
|
||||
module.exports = router;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
@ -15,7 +15,7 @@ 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/>.*/
|
||||
|
||||
//local imports
|
||||
const userModel = require('../schemas/userSchema.js');
|
||||
const {userModel} = require('../schemas/userSchema.js');
|
||||
|
||||
//this module is good for keeping wrappers for userModel and other shit in that does more session handling than database access/modification.
|
||||
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.-->
|
|||
<%- include('partial/adminPanel/channelList', {chanGuide}) %>
|
||||
<%- include('partial/adminPanel/userList', {user, userList, rankEnum}) %>
|
||||
<%- include('partial/adminPanel/permList', {permList, rankEnum}) %>
|
||||
<%- include('partial/adminPanel/userBanList') %>
|
||||
</body>
|
||||
<footer>
|
||||
<%- include('partial/scripts', {user}); %>
|
||||
|
|
|
|||
43
src/views/partial/adminPanel/userBanList.ejs
Normal file
43
src/views/partial/adminPanel/userBanList.ejs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
<!--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/>.-->
|
||||
<div id="admin-ban-list-div" class="admin-list-div">
|
||||
<h3>User Ban List:</h3>
|
||||
<table id="admin-ban-list-table" class="admin-list-table">
|
||||
<tr id="admin-ban-list-entry-title" class="admin-list-entry-title-row">
|
||||
<td id="admin-ban-list-entry-img-title" class="admin-list-entry admin-list-entry-title admin-list-entry-item admin-list-entry-img-title">
|
||||
<h3>Img</h3>
|
||||
</td>
|
||||
<td id="admin-ban-list-entry-id-title" class="admin-list-entry admin-list-entry-title admin-list-entry-item admin-list-entry-not-first-col">
|
||||
<h3>ID</h3>
|
||||
</td>
|
||||
<td id="admin-ban-list-entry-name-title" class="admin-list-entry admin-list-entry-title admin-list-entry-item admin-list-entry-not-first-col">
|
||||
<h3>Name</h3>
|
||||
</td>
|
||||
<td id="admin-ban-list-entry-date-title" class="admin-list-entry admin-list-entry-title admin-list-entry-item admin-list-entry-not-first-col">
|
||||
<h3>Sign-Up Date</h3>
|
||||
</td>
|
||||
<td id="admin-ban-list-entry-ban-date-title" class="admin-list-entry admin-list-entry-title admin-list-entry-item admin-list-entry-not-first-col">
|
||||
<h3>Ban Date</h3>
|
||||
</td>
|
||||
<td id="admin-ban-list-entry-expiration-date-title" class="admin-list-entry admin-list-entry-title admin-list-entry-item admin-list-entry-not-first-col">
|
||||
<h3>Expiration Date</h3>
|
||||
</td>
|
||||
<td id="admin-ban-list-entry-expiration-type-title" class="admin-list-entry admin-list-entry-title admin-list-entry-item admin-list-entry-not-first-col">
|
||||
<h3>Expiration Action</h3>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
|
|
@ -69,6 +69,35 @@ class canopyAdminUtils{
|
|||
utils.ux.displayResponseError(await response.json());
|
||||
}
|
||||
}
|
||||
|
||||
async getBans(){
|
||||
var response = await fetch(`/api/admin/ban`,{
|
||||
method: "GET"
|
||||
});
|
||||
|
||||
if(response.status == 200){
|
||||
return await response.json();
|
||||
}else{
|
||||
utils.ux.displayResponseError(await response.json());
|
||||
}
|
||||
}
|
||||
|
||||
async banUser(user){
|
||||
var response = await fetch(`/api/admin/ban`,{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
//Unfortunately JSON doesn't natively handle ES6 maps, and god forbid someone update the standard in a way that's backwards compatible...
|
||||
body: JSON.stringify({user})
|
||||
});
|
||||
|
||||
if(response.status == 200){
|
||||
return await response.json();
|
||||
}else{
|
||||
utils.ux.displayResponseError(await response.json());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class adminUserList{
|
||||
|
|
@ -146,6 +175,73 @@ class adminPermissionList{
|
|||
}
|
||||
}
|
||||
|
||||
class adminUserBanList{
|
||||
constructor(){
|
||||
this.table = document.querySelector("#admin-ban-list-table");
|
||||
|
||||
this.getBanList();
|
||||
}
|
||||
|
||||
async getBanList(){
|
||||
this.renderBanList(await adminUtil.getBans());
|
||||
}
|
||||
|
||||
renderBanList(banList){
|
||||
banList.forEach((ban) => {
|
||||
//Create entry row
|
||||
const entryRow = document.createElement('tr');
|
||||
entryRow.classList.add("admin-list-entry");
|
||||
|
||||
//Create IMG node inside of IMG cell
|
||||
const imgNode = document.createElement('img');
|
||||
imgNode.classList.add("admin-list-entry","admin-list-entry-item");
|
||||
imgNode.src = ban.user.img;
|
||||
|
||||
console.log(new Date(ban.user.date).toDateString());
|
||||
|
||||
const expirationDate = new Date(ban.expirationDate);
|
||||
|
||||
const expirationDays = Math.floor((expirationDate - new Date()) / (1000 * 60 * 60 * 24));
|
||||
|
||||
//Append cells to row
|
||||
entryRow.appendChild(newCell(imgNode, true, true));
|
||||
entryRow.appendChild(newCell(ban.user.id));
|
||||
entryRow.appendChild(newCell(ban.user.user));
|
||||
entryRow.appendChild(newCell(new Date(ban.user.date).toDateString()));
|
||||
entryRow.appendChild(newCell(new Date(ban.banDate).toDateString()));
|
||||
entryRow.appendChild(newCell(`${expirationDate.toDateString()} (${expirationDays} days left)`));
|
||||
entryRow.appendChild(newCell(ban.deleteAccountOnExpire ? "Delete" : "Un-Ban"));
|
||||
|
||||
//Append row to table
|
||||
this.table.appendChild(entryRow);
|
||||
});
|
||||
|
||||
function newCell(content, addAsNode = false, firstCol = false){
|
||||
//Create a new 'td' element
|
||||
const cell = document.createElement('td');
|
||||
cell.classList.add("admin-list-entry","admin-list-entry-item");
|
||||
|
||||
//If it's not the first column, mention it!
|
||||
if(!firstCol){
|
||||
cell.classList.add("admin-list-entry-not-first-col");
|
||||
}
|
||||
|
||||
//If we're adding as node
|
||||
if(addAsNode){
|
||||
//append it like it's a node
|
||||
cell.appendChild(content);
|
||||
}else{
|
||||
//otherwise use it as innerHTML
|
||||
cell.innerHTML = content;
|
||||
}
|
||||
|
||||
//return the resulting cell
|
||||
return cell;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const adminUtil = new canopyAdminUtils();
|
||||
const userList = new adminUserList();
|
||||
const permissionList = new adminPermissionList();
|
||||
const userBanList = new adminUserBanList();
|
||||
Loading…
Reference in a new issue