From b138b26f27532e861b4db28fdf46292262e4a327 Mon Sep 17 00:00:00 2001 From: rainbownapkin Date: Sun, 1 Dec 2024 20:48:42 -0500 Subject: [PATCH] Added kickConnections to channelManager --- src/app/channel/channelManager.js | 28 ++++++++++++++++++++-------- src/app/channel/chatHandler.js | 2 +- src/schemas/userBanSchema.js | 6 +++++- src/schemas/userSchema.js | 11 +++++++---- src/utils/sessionUtils.js | 2 +- www/css/global.css | 3 +++ 6 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/app/channel/channelManager.js b/src/app/channel/channelManager.js index 8e03b87..b2e4486 100644 --- a/src/app/channel/channelManager.js +++ b/src/app/channel/channelManager.js @@ -152,21 +152,33 @@ module.exports = class{ return chanList; } - getConnections(socket){ - //Create a list to store our connections - var connections = []; - + crawlConnections(user, cb){ //For each channel this.activeChannels.forEach((channel) => { //Check and see if the user is connected - const foundUser = channel.userList.get(socket.user.user); + const foundUser = channel.userList.get(user); //If we found a user and this channel hasn't been added to the list if(foundUser){ - connections.push(foundUser); + cb(foundUser); } - }); - + }); + } + + getConnections(user){ + //Create a list to store our connections + var connections = []; + + //crawl through connections + //this.crawlConnections(user,(foundUser)=>{connections.push(foundUser)}); + this.crawlConnections(user,(foundUser)=>{connections.push(foundUser)}); + + //return connects return connections; } + + kickConnections(user, reason){ + //crawl through connections and kick user + this.crawlConnections(user,(foundUser)=>{foundUser.disconnect(reason)}); + } } \ No newline at end of file diff --git a/src/app/channel/chatHandler.js b/src/app/channel/chatHandler.js index 64bbb21..a202a58 100644 --- a/src/app/channel/chatHandler.js +++ b/src/app/channel/chatHandler.js @@ -61,7 +61,7 @@ module.exports = class{ userDB.flair = data.flair; userDB = await userDB.save(); - const connections = this.server.getConnections(socket); + const connections = this.server.getConnections(socket.user.user); connections.forEach((conn) => { conn.updateFlair(userDB.flair); diff --git a/src/schemas/userBanSchema.js b/src/schemas/userBanSchema.js index 5e8ea80..31c6909 100644 --- a/src/schemas/userBanSchema.js +++ b/src/schemas/userBanSchema.js @@ -119,7 +119,11 @@ userBanSchema.statics.banByUserDoc = async function(userDB, permanent, expiratio } //Log the user out - await userDB.killAllSessions(); + if(permanent){ + await userDB.killAllSessions(`Your account has been permanently banned, and will be nuked from the database in ${expirationDays} day(s).`); + }else{ + await userDB.killAllSessions(`Your account has been temporarily banned, and will be reinstated in: ${expirationDays} day(s).`); + } //Add the ban to the database return await this.create({user: userDB._id, permanent, expirationDays}); diff --git a/src/schemas/userSchema.js b/src/schemas/userSchema.js index e599256..a602f55 100644 --- a/src/schemas/userSchema.js +++ b/src/schemas/userSchema.js @@ -113,7 +113,7 @@ userSchema.pre('save', async function (next){ } if(this.isModified("rank")){ - await this.killAllSessions(); + await this.killAllSessions("Your site-wide rank has changed. Sign-in required."); } //All is good, continue on saving. @@ -235,7 +235,7 @@ userSchema.statics.getUserList = async function(fullList = false){ } //note: if you gotta call this from a request authenticated by it's user, make sure to kill that session first! -userSchema.methods.killAllSessions = async function(){ +userSchema.methods.killAllSessions = async function(reason = "A full log-out from all devices was requested for your account."){ //get authenticated sessions var sessions = await this.getAuthenticatedSessions(); @@ -243,6 +243,9 @@ userSchema.methods.killAllSessions = async function(){ sessions.forEach((session) => { server.store.destroy(session.seshid); }); + + //Tell the application side of the house to kick the user out as well + server.channelManager.kickConnections(this.user, reason); } userSchema.methods.passwordReset = async function(passChange){ @@ -255,7 +258,7 @@ userSchema.methods.passwordReset = async function(passChange){ await this.save(); //Kill all authed sessions for security purposes - await this.killAllSessions(); + await this.killAllSessions("Your password has been reset."); }else{ //confirmation pass doesn't match throw new Error("Mismatched confirmation password!"); @@ -277,7 +280,7 @@ userSchema.methods.nuke = async function(pass){ var oldUser = await module.exports.userModel.deleteOne(this); if(oldUser){ - await this.killAllSessions(); + await this.killAllSessions("This account has been deleted. So long, and thanks for all the fish! <3"); }else{ throw new Error("Server Error: Unable to delete account! Please report this error to your server administrator, and with timestamp."); } diff --git a/src/utils/sessionUtils.js b/src/utils/sessionUtils.js index 3325fad..b71a3b0 100644 --- a/src/utils/sessionUtils.js +++ b/src/utils/sessionUtils.js @@ -30,7 +30,7 @@ module.exports.authenticateSession = async function(user, pass, req){ //Make the number a little prettier despite the lack of precision since we're not doing calculations here :P const expiration = banDB.getDaysUntilExpiration() < 1 ? 0 : banDB.getDaysUntilExpiration(); if(banDB.permanent){ - throw new Error(`Your account has been banned, and will be permanently deleted in: ${expiration} day(s)`); + throw new Error(`Your account has been permanently banned, and will be nuked from the database in: ${expiration} day(s)`); }else{ throw new Error(`Your account has been temporarily banned, and will be reinstated in: ${expiration} day(s)`); } diff --git a/www/css/global.css b/www/css/global.css index 86f8b18..fccf6eb 100644 --- a/www/css/global.css +++ b/www/css/global.css @@ -56,6 +56,7 @@ p.navbar-item, input.navbar-item{ bottom: 0; right: 0; left: 0; + z-index: 9000; } .popup-div{ @@ -69,6 +70,8 @@ p.navbar-item, input.navbar-item{ left: 0; height: fit-content; width: fit-content; + /*This joke is probably old enough to drink by now*/ + z-index: 9001; } .popup-close-icon{