Added more granular permissions.

This commit is contained in:
rainbow napkin 2024-12-02 20:33:18 -05:00
parent ef4894e409
commit 2dbf3b97d5
11 changed files with 179 additions and 75 deletions

View file

@ -28,6 +28,36 @@ const channelPermissionSchema = new mongoose.Schema({
default: "admin",
required: true
},
changeRank: {
type: mongoose.SchemaTypes.String,
enum: rankEnum,
default: "admin",
required: true
},
changePerms: {
type: mongoose.SchemaTypes.String,
enum: rankEnum,
default: "admin",
required: true
},
changeSettings: {
type: mongoose.SchemaTypes.String,
enum: rankEnum,
default: "admin",
required: true
},
kickUser: {
type: mongoose.SchemaTypes.String,
enum: rankEnum,
default: "admin",
required: true
},
banUser: {
type: mongoose.SchemaTypes.String,
enum: rankEnum,
default: "admin",
required: true
},
deleteChannel: {
type: mongoose.SchemaTypes.String,
enum: rankEnum,

View file

@ -84,55 +84,56 @@ channelSchema.pre('save', async function (next){
//Getting the affected user would be a million times easier elsewhere
//But this ensures it happens every time channel rank gets changed no matter what
if(this.isModified('rankList')){
if(this.isModified('rankList') && this.rankList != null){
//Get the rank list before it was modified (gross but works, find a better way if you dont like it :P)
var chanDB = await module.exports.findOne({_id: this._id});
//Create empty variable for the found rank object
var foundRank = null;
//If we're removing one
if(chanDB.rankList.length > this.rankList.length){
//Child/Parent is *WAY* to atomic family for my tastes :P
var top = chanDB;
var bottom = this;
}else{
//otherwise reverse the loops
var top = this;
var bottom = chanDB;
}
//Populate the top doc
await top.populate('rankList.user');
top.rankList.forEach((topObj) => {
//Create empty variable for the matched rank
var matchedRank = null;
//For each rank in the old copy of the rank list
bottom.rankList.forEach((bottomObj) => {
if(topObj.user._id.toString() == bottomObj.user._id.toString()){
matchedRank = bottomObj;
}
});
if(matchedRank == null || matchedRank.rank != topObj.rank){
foundRank = topObj;
if(chanDB != null){
//If we're removing one
if(chanDB.rankList.length > this.rankList.length){
//Child/Parent is *WAY* to atomic family for my tastes :P
var top = chanDB;
var bottom = this;
}else{
//otherwise reverse the loops
var top = this;
var bottom = chanDB;
}
});
//Populate the top doc
await top.populate('rankList.user');
//get relevant active channel
const activeChan = server.channelManager.activeChannels.get(this.name);
//if the channel is online
if(activeChan != null){
//Get the relevant user connection
const userConn = activeChan.userList.get(foundRank.user.user);
//if the user is online
if(userConn != null){
//kick the user
userConn.disconnect("Your channel rank has changed!");
top.rankList.forEach((topObj) => {
//Create empty variable for the matched rank
var matchedRank = null;
//For each rank in the old copy of the rank list
bottom.rankList.forEach((bottomObj) => {
if(topObj.user._id.toString() == bottomObj.user._id.toString()){
matchedRank = bottomObj;
}
});
if(matchedRank == null || matchedRank.rank != topObj.rank){
foundRank = topObj;
}
});
//get relevant active channel
const activeChan = server.channelManager.activeChannels.get(this.name);
//if the channel is online
if(activeChan != null){
//Get the relevant user connection
const userConn = activeChan.userList.get(foundRank.user.user);
//if the user is online
if(userConn != null){
//kick the user
userConn.disconnect("Your channel rank has changed!");
}
}
}
}

View file

@ -58,7 +58,6 @@ flairSchema.statics.loadDefaults = async function(){
}else{
console.log("Error, null flair:");
}
console.log(err);
}
});
}

View file

@ -31,7 +31,31 @@ const permissionSchema = new mongoose.Schema({
default: "admin",
required: true
},
adminAPI: {
changeRank: {
type: mongoose.SchemaTypes.String,
enum: rankEnum,
default: "admin",
required: true
},
changePerms: {
type: mongoose.SchemaTypes.String,
enum: rankEnum,
default: "admin",
required: true
},
banUser: {
type: mongoose.SchemaTypes.String,
enum: rankEnum,
default: "admin",
required: true
},
nukeUser: {
type: mongoose.SchemaTypes.String,
enum: rankEnum,
default: "admin",
required: true
},
genPasswordReset: {
type: mongoose.SchemaTypes.String,
enum: rankEnum,
default: "admin",