From 805387b3da71b447db6e40fa20d0ecf7d7278ad8 Mon Sep 17 00:00:00 2001 From: rainbow napkin Date: Sun, 15 Dec 2024 21:41:59 -0500 Subject: [PATCH] Fixed bug in perm check middleware caused by last refactor --- src/app/channel/channelManager.js | 2 +- src/app/channel/chatHandler.js | 2 +- src/app/channel/tokebot.js | 2 +- src/controllers/adminPanelController.js | 2 +- .../api/account/deleteController.js | 2 +- .../api/account/registerController.js | 2 +- .../api/account/updateController.js | 2 +- src/controllers/api/admin/banController.js | 2 +- .../api/admin/changeRankController.js | 2 +- .../api/admin/listUsersController.js | 2 +- src/controllers/api/channel/banController.js | 2 +- src/controllers/api/channel/rankController.js | 2 +- .../api/channel/registerController.js | 2 +- src/controllers/profileController.js | 2 +- src/schemas/channel/channelSchema.js | 2 +- src/schemas/permissionSchema.js | 20 +++++++++++-------- src/schemas/userBanSchema.js | 2 +- src/schemas/userSchema.js | 2 +- src/utils/sessionUtils.js | 2 +- 19 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/app/channel/channelManager.js b/src/app/channel/channelManager.js index d915aa3..d786e50 100644 --- a/src/app/channel/channelManager.js +++ b/src/app/channel/channelManager.js @@ -16,7 +16,7 @@ along with this program. If not, see .*/ //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'); diff --git a/src/app/channel/chatHandler.js b/src/app/channel/chatHandler.js index 1b1a6f4..688ee15 100644 --- a/src/app/channel/chatHandler.js +++ b/src/app/channel/chatHandler.js @@ -17,7 +17,7 @@ along with this program. If not, see .*/ //local imports const commandPreprocessor = require('./commandPreprocessor'); const loggerUtils = require('../../utils/loggerUtils'); -const userModel = require('../../schemas/userSchema'); +const {userModel} = require('../../schemas/userSchema'); module.exports = class{ constructor(server){ diff --git a/src/app/channel/tokebot.js b/src/app/channel/tokebot.js index c441908..a48b335 100644 --- a/src/app/channel/tokebot.js +++ b/src/app/channel/tokebot.js @@ -16,7 +16,7 @@ along with this program. If not, see .*/ //Local Imports const tokeCommandModel = require('../../schemas/tokebot/tokeCommandSchema'); -const userModel = require('../../schemas/userSchema'); +const {userModel} = require('../../schemas/userSchema'); const statModel = require('../../schemas/statSchema'); const statSchema = require('../../schemas/statSchema'); diff --git a/src/controllers/adminPanelController.js b/src/controllers/adminPanelController.js index 3842eab..d126eba 100644 --- a/src/controllers/adminPanelController.js +++ b/src/controllers/adminPanelController.js @@ -16,7 +16,7 @@ along with this program. If not, see .*/ //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, errorHandler} = require("../utils/loggerUtils"); diff --git a/src/controllers/api/account/deleteController.js b/src/controllers/api/account/deleteController.js index c88178a..e71ce3a 100644 --- a/src/controllers/api/account/deleteController.js +++ b/src/controllers/api/account/deleteController.js @@ -18,7 +18,7 @@ along with this program. If not, see .*/ const {validationResult, matchedData} = require('express-validator'); //local imports -const userModel = require('../../../schemas/userSchema'); +const {userModel} = require('../../../schemas/userSchema'); const accountUtils = require('../../../utils/sessionUtils'); const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils'); diff --git a/src/controllers/api/account/registerController.js b/src/controllers/api/account/registerController.js index c273c27..1ea0d2c 100644 --- a/src/controllers/api/account/registerController.js +++ b/src/controllers/api/account/registerController.js @@ -18,7 +18,7 @@ along with this program. If not, see .*/ const {validationResult, matchedData} = require('express-validator'); //local imports -const userModel = require('../../../schemas/userSchema'); +const {userModel} = require('../../../schemas/userSchema'); const userBanModel = require('../../../schemas/userBanSchema'); const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils'); diff --git a/src/controllers/api/account/updateController.js b/src/controllers/api/account/updateController.js index 3e67b04..de7bb3e 100644 --- a/src/controllers/api/account/updateController.js +++ b/src/controllers/api/account/updateController.js @@ -18,7 +18,7 @@ along with this program. If not, see .*/ const {validationResult, matchedData} = require('express-validator'); //local imports -const userModel = require('../../../schemas/userSchema'); +const {userModel} = require('../../../schemas/userSchema'); const accountUtils = require('../../../utils/sessionUtils'); const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils'); diff --git a/src/controllers/api/admin/banController.js b/src/controllers/api/admin/banController.js index 9160009..bd5d85b 100644 --- a/src/controllers/api/admin/banController.js +++ b/src/controllers/api/admin/banController.js @@ -20,7 +20,7 @@ const {validationResult, matchedData} = require('express-validator'); //local imports const banModel = require('../../../schemas/userBanSchema'); const permissionModel = require('../../../schemas/permissionSchema'); -const userModel = require('../../../schemas/userSchema'); +const {userModel} = require('../../../schemas/userSchema'); const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils'); module.exports.get = async function(req, res){ diff --git a/src/controllers/api/admin/changeRankController.js b/src/controllers/api/admin/changeRankController.js index aa6a72e..503ec89 100644 --- a/src/controllers/api/admin/changeRankController.js +++ b/src/controllers/api/admin/changeRankController.js @@ -20,7 +20,7 @@ const {validationResult, matchedData} = require('express-validator'); //local imports const {exceptionHandler, errorHandler} = 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){ diff --git a/src/controllers/api/admin/listUsersController.js b/src/controllers/api/admin/listUsersController.js index 4fad97a..be58b48 100644 --- a/src/controllers/api/admin/listUsersController.js +++ b/src/controllers/api/admin/listUsersController.js @@ -16,7 +16,7 @@ along with this program. If not, see .*/ //local imports const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils'); -const userModel = require('../../../schemas/userSchema'); +const {userModel} = require('../../../schemas/userSchema'); //api list account functions module.exports.get = async function(req, res){ diff --git a/src/controllers/api/channel/banController.js b/src/controllers/api/channel/banController.js index 2de6a89..1bcda0c 100644 --- a/src/controllers/api/channel/banController.js +++ b/src/controllers/api/channel/banController.js @@ -19,7 +19,7 @@ const {validationResult, matchedData} = require('express-validator'); //local imports const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils'); -const userModel = require('../../../schemas/userSchema'); +const {userModel} = require('../../../schemas/userSchema'); const channelModel = require('../../../schemas/channel/channelSchema'); const permissionModel = require('../../../schemas/permissionSchema') diff --git a/src/controllers/api/channel/rankController.js b/src/controllers/api/channel/rankController.js index f8fc0e5..14ed882 100644 --- a/src/controllers/api/channel/rankController.js +++ b/src/controllers/api/channel/rankController.js @@ -20,7 +20,7 @@ const {validationResult, matchedData} = require('express-validator'); //local imports const {exceptionHandler, errorHandler} = 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 diff --git a/src/controllers/api/channel/registerController.js b/src/controllers/api/channel/registerController.js index 664da8f..977e7ec 100644 --- a/src/controllers/api/channel/registerController.js +++ b/src/controllers/api/channel/registerController.js @@ -19,7 +19,7 @@ const {validationResult, matchedData} = require('express-validator'); //local imports const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils'); -const userModel = require('../../../schemas/userSchema'); +const {userModel} = require('../../../schemas/userSchema'); const channelModel = require('../../../schemas/channel/channelSchema'); //api account functions diff --git a/src/controllers/profileController.js b/src/controllers/profileController.js index d3eb8de..4097154 100644 --- a/src/controllers/profileController.js +++ b/src/controllers/profileController.js @@ -15,7 +15,7 @@ You should have received a copy of the GNU Affero General Public License along with this program. If not, see .*/ //Local Imports -const userModel = require('../schemas/userSchema'); +const {userModel} = require('../schemas/userSchema'); const {exceptionHandler, errorHandler} = require('../utils/loggerUtils'); //Config diff --git a/src/schemas/channel/channelSchema.js b/src/schemas/channel/channelSchema.js index 1852ecc..4b21de6 100644 --- a/src/schemas/channel/channelSchema.js +++ b/src/schemas/channel/channelSchema.js @@ -21,7 +21,7 @@ const {validationResult, matchedData} = require('express-validator'); //Local Imports const server = require('../../server'); const statModel = require('../statSchema'); -const userModel = require('../userSchema'); +const {userModel} = require('../userSchema'); const permissionModel = require('../permissionSchema'); const channelPermissionSchema = require('./channelPermissionSchema'); const channelBanSchema = require('./channelBanSchema'); diff --git a/src/schemas/permissionSchema.js b/src/schemas/permissionSchema.js index 8607e4e..7068518 100644 --- a/src/schemas/permissionSchema.js +++ b/src/schemas/permissionSchema.js @@ -119,8 +119,11 @@ permissionSchema.statics.rankToNum = function(rank){ } permissionSchema.statics.permCheck = async function(user, perm){ + //Check if the user is null if(user != null){ - const userDB = await userModel.findOne({user: user.user}); + //This specific call is why we export the userModel the way we do + //Someone will yell at me for circular dependencies but the fucking interpreter isn't :P + const userDB = await userModel.userModel.findOne({user: user.user}); return await this.permCheckByUserDoc(userDB, perm); }else{ return await this.permCheckByUserDoc(null, perm); @@ -154,13 +157,14 @@ permissionSchema.statics.permCheckByUserDoc = async function(user, perm){ //Middleware for rank checks permissionSchema.statics.reqPermCheck = function(perm){ - return async (req, res, next)=>{ - - if(await permissionSchema.statics.permCheck(req.session.user, perm)){ - next(); - }else{ - return errorHandler(res, "You do not have a high enough rank to access this resource.", 'Unauthorized', 401); - } + return (req, res, next)=>{ + permissionSchema.statics.permCheck(req.session.user, perm).then((access) => { + if(access){ + next(); + }else{ + return errorHandler(res, "You do not have a high enough rank to access this resource.", 'Unauthorized', 401); + } + }); } } diff --git a/src/schemas/userBanSchema.js b/src/schemas/userBanSchema.js index 7d7279b..7326c34 100644 --- a/src/schemas/userBanSchema.js +++ b/src/schemas/userBanSchema.js @@ -18,7 +18,7 @@ along with this program. If not, see .*/ const {mongoose} = require('mongoose'); //Local Imports -const userModel = require('./userSchema'); +const {userModel} = require('./userSchema'); const userBanSchema = new mongoose.Schema({ user: { diff --git a/src/schemas/userSchema.js b/src/schemas/userSchema.js index b2a7346..5c8f834 100644 --- a/src/schemas/userSchema.js +++ b/src/schemas/userSchema.js @@ -402,4 +402,4 @@ userSchema.methods.nuke = async function(pass){ } } -module.exports = mongoose.model("user", userSchema); \ No newline at end of file +module.exports.userModel = mongoose.model("user", userSchema); \ No newline at end of file diff --git a/src/utils/sessionUtils.js b/src/utils/sessionUtils.js index ea67ceb..b71a3b0 100644 --- a/src/utils/sessionUtils.js +++ b/src/utils/sessionUtils.js @@ -15,7 +15,7 @@ You should have received a copy of the GNU Affero General Public License along with this program. If not, see .*/ //local imports -const userModel = require('../schemas/userSchema'); +const {userModel} = require('../schemas/userSchema'); const userBanModel = require('../schemas/userBanSchema') //this module is good for keeping wrappers for userModel and other shit in that does more session handling than database access/modification.