Skip to content
This repository has been archived by the owner on Jul 5, 2023. It is now read-only.

Commit

Permalink
Functionality: ban moderation, offline bans, ban check if the user is…
Browse files Browse the repository at this point in the history
… already banned + eslint

Ban Feature 
#6
  • Loading branch information
LeventHAN authored Nov 4, 2021
2 parents 004d8de + 4162776 commit f633e2b
Show file tree
Hide file tree
Showing 27 changed files with 1,384 additions and 904 deletions.
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Ignore artifacts:
docs/
dashboard/
dashboard/public/
dashboard/views/
assets/
languages/
node_modules/
Expand Down
12 changes: 11 additions & 1 deletion base/Moderation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ const moderationSchema = new mongoose.Schema({
type: String,
default: "12345678911234567",
},
/* admin steamID */
moderatorSteamID: {
type: String,
default: "steamID",
},
/* admin discordName */
moderatorName: {
type: String,
default: "discordName",
},
/* admin discordID */
moderator: {
type: String,
Expand All @@ -28,7 +38,7 @@ const moderationSchema = new mongoose.Schema({
},
/* end date of the action - null if just kick or warn */
endDate: {
type: Date,
type: Number,
default: null,
},
});
Expand Down
54 changes: 37 additions & 17 deletions base/SquadStatsJSv3.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,17 @@ class SquadStatsJSv3 extends Client {

async addModeration({
steamID: steamID,
moderatorSteamID: moderatorSteamID,
moderatorName: moderatorName,
moderator: moderator,
typeModeration: typeModeration,
reason: reason,
endDate: endDate,
}) {
const moderationRow = new this.moderation({
steamID: steamID,
moderatorSteamID: moderatorSteamID,
moderatorName: moderatorName,
moderator: moderator,
typeModeration: typeModeration,
reason: reason,
Expand Down Expand Up @@ -322,6 +326,16 @@ class SquadStatsJSv3 extends Client {
);
}

async getBanlist() {
const bans = await this.moderation.find({});
const onList = [];
bans.forEach((element) => {
if (element.typeModeration == "ban" && element.endDate > Date.now())
return onList.push(element);
});
return onList;
}

// Returns the whitelist roles only (Groups)
async getWhitelistRoles() {
const whitelist = await this.whitelists.findOne({});
Expand All @@ -335,6 +349,14 @@ class SquadStatsJSv3 extends Client {
return whitelist.memberData;
}

async removeUserBanlist(steamID) {
await this.moderation.findOneAndUpdate(
{ steamID: steamID, endDate: { $gt: Date.now() } },
{ $set: { endDate: Date.now() } }
);
return true;
}

// Remove user from whitelist by steamID
async removeUserWhitelist(steamID) {
const whitelist = await this.whitelists.findOne({});
Expand Down Expand Up @@ -482,7 +504,7 @@ class SquadStatsJSv3 extends Client {
}

async getPlayersLength() {
if(!this.socket) return "N/A";
if (!this.socket) return "N/A";
this.players;
const response = new Promise((res) => {
this.socket.emit("players", async (data) => {
Expand Down Expand Up @@ -525,21 +547,18 @@ class SquadStatsJSv3 extends Client {
const roles = [];
// loop trough perms.canSee and put all values to roles
for (const key in perms.canSee) {
if (perms.canSee[key]){
if (perms.canSee[key]) {
// loop trough the roles
for (let i=0; i<perms.canSee[key].length; i++) {
for (let i = 0; i < perms.canSee[key].length; i++) {
roles.push(perms.canSee[key][i]);
}
}
}
// remove duplicates
return [...new Set(roles)];



}

async getAllPagesCanSee(){
async getAllPagesCanSee() {
const perms = await this.permission.findOne({});
if (!perms) return;
// look inside canSee and whoCan and put all diffrent values in an array
Expand All @@ -550,7 +569,7 @@ class SquadStatsJSv3 extends Client {
return pages;
}

async getAllActionsWhoCan(){
async getAllActionsWhoCan() {
const perms = await this.permission.findOne({});
if (!perms) return;
// look inside canSee and whoCan and put all diffrent values in an array
Expand All @@ -570,7 +589,6 @@ class SquadStatsJSv3 extends Client {

// Loop trough perms.canSee and search for route in it
for (const key in perms[0].canSee) {

if (key === route) {
// Loop trough the roles in the array
for (const role of perms[0].canSee[key]) {
Expand Down Expand Up @@ -805,33 +823,35 @@ class SquadStatsJSv3 extends Client {
}

async getShowNotifications() {
const guild = await this.findOrCreateGuild({id: this.config.serverID });
const guild = await this.findOrCreateGuild({ id: this.config.serverID });
return guild.dashboard.showNotifications;
}

async getUpdatePlayersTable() {
const guild = await this.findOrCreateGuild({id: this.config.serverID });
const guild = await this.findOrCreateGuild({ id: this.config.serverID });
return guild.dashboard.updatePlayersTable;
}

async toggleShowNotifications(actionType) {
const guild = await this.findOrCreateGuild({id: this.config.serverID });
const guild = await this.findOrCreateGuild({ id: this.config.serverID });
if (guild) {
for( const action in guild.dashboard.showNotifications) {
for (const action in guild.dashboard.showNotifications) {
if (action === actionType) {
guild.dashboard.showNotifications[action] = !guild.dashboard.showNotifications[action];
guild.dashboard.showNotifications[action] =
!guild.dashboard.showNotifications[action];
}
}
}
await guild.save();
}

async toggleUpdatePlayersTable(actionType) {
const guild = await this.findOrCreateGuild({id: this.config.serverID });
const guild = await this.findOrCreateGuild({ id: this.config.serverID });
if (guild) {
for( const action in guild.dashboard.updatePlayersTable) {
for (const action in guild.dashboard.updatePlayersTable) {
if (action === actionType) {
guild.dashboard.updatePlayersTable[action] = !guild.dashboard.updatePlayersTable[action];
guild.dashboard.updatePlayersTable[action] =
!guild.dashboard.updatePlayersTable[action];
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion base/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const userSchema = new mongoose.Schema({
/* ROLES */
roles: {
type: Array,
default: ["user"]
default: ["user"],
},

whitelist: {
Expand Down
102 changes: 61 additions & 41 deletions dashboard/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports.load = async (client) => {
const limiter = rateLimit({
windowMs: 1000,
max: 10, // limit each IP to 6 requests per windowMs (1 second)
message: "RateLimit reached! Please try again."
message: "RateLimit reached! Please try again.",
});
const express = require("express"),
session = require("express-session"),
Expand All @@ -30,6 +30,7 @@ module.exports.load = async (client) => {
discordAPIRouter = require("./routes/discord"),
apiRouter = require("./routes/api"),
logoutRouter = require("./routes/logout"),
banlistRouter = require("./routes/banlist"),
settingsRouter = require("./routes/settings"),
rolesRouter = require("./routes/roles"),
steamRouter = require("./routes/steam"),
Expand Down Expand Up @@ -64,71 +65,80 @@ module.exports.load = async (client) => {
"PLAYER_WARNED",
"PLAYER_KICKED",
"PLAYER_BANNED",
"SQUAD_CREATED"
"SQUAD_CREATED",
];

passport.serializeUser(function(user, done) {
passport.serializeUser(function (user, done) {
done(null, user);
});

passport.deserializeUser(function(obj, done) {
passport.deserializeUser(function (obj, done) {
done(null, obj);
});

passport.use(new SteamStrategy({
returnURL: config.dashboard.baseURL+"/auth/steam/return",
realm: config.dashboard.baseURL,
apiKey: config.apiKeys.steam
},
function(identifier, profile, done) {
process.nextTick(function () {
profile.identifier = identifier;
return done(null, profile);
});
}
));

passport.use(
new SteamStrategy(
{
returnURL: config.dashboard.baseURL + "/auth/steam/return",
realm: config.dashboard.baseURL,
apiKey: config.apiKeys.steam,
},
function (identifier, profile, done) {
process.nextTick(function () {
profile.identifier = identifier;
return done(null, profile);
});
}
)
);

const whitelist = ["http://localhost:3000", "https://localhost:3000", config.dashboard.baseURL];
const whitelist = [
"http://localhost:3000",
"https://localhost:3000",
config.dashboard.baseURL,
];

if(client.socket){
if (client.socket) {
io.use(async (socket, next) => {
if(!socket.handshake.auth) return next(new Error("No token provided."));
if (!socket.handshake.auth) return next(new Error("No token provided."));
const user = await client.fetchUserByToken(socket.handshake.auth.token);
if(!user) {
console.log("Someone did try to login with wrong credintials.", socket.handshake);
if (!user) {
console.log(
"Someone did try to login with wrong credintials.",
socket.handshake
);
return next(new Error("Invalid token."));
}
socket.user = user;
next();
});



io.on("connection", async (socket) => {
for (const eventToBroadcast of eventsToBroadcast) {
client.socket.on((eventToBroadcast), (...args) => {
client.socket.on(eventToBroadcast, (...args) => {
socket.emit(eventToBroadcast, ...args);
});
}
socket.onAny(async (eventName, ...rawArgs) => {
const args = rawArgs.slice(0, rawArgs.length - 1);
const callback = rawArgs[rawArgs.length - 1];
await client.socket.emit(`${eventName}`, ...args, async (response) => {
await client.socket.emit(`${eventName}`, ...args, async (response) => {
return callback(response);
});
});
});
}


/* App configuration */
app
.use(express.json())
.use(express.urlencoded({ extended: true }))
.use(cors({
origin: "*",
optionsSuccessStatus: 200
}))
.use(
cors({
origin: "*",
optionsSuccessStatus: 200,
})
)
// Set the engine to html (for ejs template)
.engine("html", require("ejs").renderFile)
.set("view engine", "ejs")
Expand All @@ -153,7 +163,7 @@ module.exports.load = async (client) => {
req.user = req.session.user;
req.client = client;
req.locale = "en-US";
if (req.user && req.url !== "/"){
if (req.user && req.url !== "/") {
req.userInfos = await utils.fetchUser(req.user, req.client);
}
if (req.user) {
Expand All @@ -170,6 +180,7 @@ module.exports.load = async (client) => {
.use("/settings", settingsRouter)
.use("/roles", rolesRouter)
.use("/steam", steamRouter)
.use("/bans", banlistRouter)
.use("/", mainRouter)
.use("/players", playersRouter)
.use("/profile", profileRouter)
Expand All @@ -193,17 +204,26 @@ module.exports.load = async (client) => {
});
});



// Listen express server
app.listen(app.get("port"), () => {
client.logger.log(`SquadStatsJS ${version} Dashboard is listening on port ${app.get("port")}`,"READY");
client.logger.log(
`SquadStatsJS ${version} Dashboard is listening on port ${app.get(
"port"
)}`,
"READY"
);
});
// Listen websocket server
server.listen(3000, {
origins: whitelist
}, () => {
client.logger.log(`SquadStatsJS ${version} SocketIO is listening on port 3000`,"READY");
});

server.listen(
3000,
{
origins: whitelist,
},
() => {
client.logger.log(
`SquadStatsJS ${version} SocketIO is listening on port 3000`,
"READY"
);
}
);
};
Loading

0 comments on commit f633e2b

Please sign in to comment.