Skip to content
This repository was archived by the owner on Sep 25, 2023. It is now read-only.

Commit 3196e93

Browse files
authored
Merge pull request #237 from Zemanzo/feature/banned-users
Banned users can no longer enter marbles or chat
2 parents 26773d6 + 9587bd2 commit 3196e93

File tree

7 files changed

+190
-93
lines changed

7 files changed

+190
-93
lines changed

public/styles/chat.css

+34-8
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,6 @@ header {
137137
flex: 1;
138138
}
139139

140-
#chatInputContainer.authorized > div {
141-
display: flex;
142-
}
143-
144-
/* Reversed visibility for unauthenticated */
145140
#chatInputContainer > #unauthenticated {
146141
display: flex;
147142
justify-content: space-around;
@@ -181,14 +176,19 @@ header {
181176
cursor: not-allowed;
182177
}
183178

184-
#chatInputContainer.authorized > #unauthenticated {
179+
#chatInputContainer.authorized > #unauthenticated,
180+
#chatInputContainer.banned > #unauthenticated {
185181
display: none;
186182
}
187183

188184
#authenticated {
189-
display: flex;
185+
display: none;
190186
flex-direction: column;
191187
}
188+
#chatInputContainer.authorized > #authenticated {
189+
display: flex;
190+
}
191+
192192
#chatUserAndInputContainer {
193193
display: flex;
194194
margin: 2px 10px;
@@ -269,7 +269,8 @@ header {
269269
margin-right: 4px;
270270
}
271271

272-
#chatButtons > *.minimal {
272+
#chatButtons > *.minimal,
273+
button.minimal {
273274
margin-left: 0;
274275
color: #999;
275276
font-size:.9em;
@@ -282,6 +283,31 @@ header {
282283
text-decoration: none;
283284
}
284285

286+
#banned {
287+
display: none;
288+
text-align: center;
289+
flex-direction: column;
290+
background: #06060699;
291+
}
292+
#banned a,
293+
#banned a:visited {
294+
color: #09f;
295+
}
296+
297+
#banned a:hover {
298+
color: #3df;
299+
}
300+
301+
#chatInputContainer.banned > #banned {
302+
display: flex;
303+
}
304+
305+
#banTitle {
306+
padding-top: 4px;
307+
font-size: 2em;
308+
color: #e44;
309+
}
310+
285311
input[type=number] {
286312
max-width: 50px;
287313
min-width: 30px;

src/client/chat/chat.js

+70-61
Original file line numberDiff line numberDiff line change
@@ -46,51 +46,7 @@ domReady.then(() => {
4646

4747
// Check for former authentication
4848
cookieData = Cookies.getJSON("user_data");
49-
50-
if (
51-
// If there is former data, check if it is not outdated.
52-
cookieData
53-
54-
// See if current date is later than origin date + expiration period
55-
&& Date.now() < cookieData.access_granted + cookieData.expires_in * 1000
56-
) {
57-
// Request a fresh token
58-
var xhr = new XMLHttpRequest();
59-
xhr.onreadystatechange = function() {
60-
if (this.readyState === 4 && this.status === 200) {
61-
let response = JSON.parse(xhr.responseText);
62-
63-
if (response.authorized && response.refreshed && response.tokenBody) {
64-
response.tokenBody.id = cookieData.id;
65-
response.tokenBody.username = cookieData.username;
66-
response.tokenBody.discriminator = cookieData.discriminator;
67-
response.tokenBody.avatar = cookieData.avatar;
68-
let days = (response.tokenBody.expires_in / 62400) - 0.1; // seconds to days minus some slack
69-
Cookies.set("user_data", response.tokenBody, {
70-
expires: days,
71-
path: "/",
72-
domain: window.location.hostname,
73-
secure: config.ssl
74-
});
75-
cookieData = response.tokenBody;
76-
}
77-
78-
// Redundant check as non-authorized requests are returned as a 400
79-
if (response.authorized) {
80-
onAuthorization(cookieData);
81-
}
82-
}
83-
};
84-
xhr.open("POST", "/chat", true);
85-
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
86-
xhr.send(
87-
JSON.stringify({
88-
"type": "refresh_token",
89-
"id": cookieData.id,
90-
"access_token": cookieData.access_token
91-
})
92-
);
93-
}
49+
checkAuthentication();
9450

9551
let lastMessageSent = Date.now();
9652
let sendMessage = function(message) {
@@ -189,24 +145,25 @@ domReady.then(() => {
189145
}, false);
190146

191147
// Make log out button functional
192-
let chatButtonLogOut = document.getElementById("buttonLogOut");
193-
chatButtonLogOut.addEventListener("click", function() {
194-
if (confirm("Do you really wish to log out?")) {
195-
Cookies.remove("user_data",
196-
{
197-
path: "/",
198-
domain: window.location.hostname
148+
for (let chatButtonLogOut of document.getElementsByClassName("buttonLogOut")) {
149+
chatButtonLogOut.addEventListener("click", function() {
150+
if (confirm("Do you really wish to log out?")) {
151+
Cookies.remove("user_data",
152+
{
153+
path: "/",
154+
domain: window.location.hostname
155+
}
156+
);
157+
158+
// Send to parent if applicable
159+
if (inIframe()) {
160+
window.top.postMessage(userState.AUTH_CHANGED, `${window.location.origin}/client`);
199161
}
200-
);
201162

202-
// Send to parent if applicable
203-
if (inIframe()) {
204-
window.top.postMessage(userState.AUTH_CHANGED, `${window.location.origin}/client`);
163+
window.location.reload(true);
205164
}
206-
207-
window.location.reload(true);
208-
}
209-
}, false);
165+
}, false);
166+
}
210167
});
211168

212169
let authWindow;
@@ -224,9 +181,9 @@ function authenticationWindow() {
224181
window.addEventListener("message", receiveMessage, false);
225182
function receiveMessage(event) {
226183
if (event.data && event.data.success && event.origin === window.location.origin) {
227-
onAuthorization(event.data.response);
228184
cookieData = Cookies.getJSON("user_data");
229185
authWindow.close();
186+
checkAuthentication();
230187

231188
// Send to parent if applicable
232189
if (inIframe()) {
@@ -240,3 +197,55 @@ function onAuthorization(data) {
240197
document.getElementById("userName").innerText = `${data.username}#${data.discriminator}`;
241198
document.getElementById("chatInputContainer").className = "authorized";
242199
}
200+
201+
function onBanned() {
202+
document.getElementById("chatInputContainer").className = "banned";
203+
}
204+
205+
function checkAuthentication() {
206+
if (
207+
// If there is former data, check if it is not outdated.
208+
cookieData
209+
210+
// See if current date is later than origin date + expiration period
211+
&& Date.now() < cookieData.access_granted + cookieData.expires_in * 1000
212+
) {
213+
// Request a fresh token
214+
var xhr = new XMLHttpRequest();
215+
xhr.onreadystatechange = function() {
216+
if (this.readyState === 4 && this.status === 200) {
217+
let response = JSON.parse(xhr.responseText);
218+
219+
if (response.authorized && response.refreshed && response.tokenBody) {
220+
response.tokenBody.id = cookieData.id;
221+
response.tokenBody.username = cookieData.username;
222+
response.tokenBody.discriminator = cookieData.discriminator;
223+
response.tokenBody.avatar = cookieData.avatar;
224+
let days = (response.tokenBody.expires_in / 62400) - 0.1; // seconds to days minus some slack
225+
Cookies.set("user_data", response.tokenBody, {
226+
expires: days,
227+
path: "/",
228+
domain: window.location.hostname,
229+
secure: config.ssl
230+
});
231+
cookieData = response.tokenBody;
232+
}
233+
234+
if (response.authorized) {
235+
onAuthorization(cookieData);
236+
} else if (response.banned) {
237+
onBanned();
238+
}
239+
}
240+
};
241+
xhr.open("POST", "/chat", true);
242+
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
243+
xhr.send(
244+
JSON.stringify({
245+
"type": "refresh_token",
246+
"id": cookieData.id,
247+
"access_token": cookieData.access_token
248+
})
249+
);
250+
}
251+
}

src/server/chat/discord-manager.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ const discordManager = function() {
4949
}, console.error);
5050

5151
this.client.on("error", console.error, console.error);
52+
53+
this.client.on("guildBanAdd", function(guild, user) {
54+
log.info(`DISCORD: ${"Banned user".red} ${user.username}#${user.discriminator} (${user.id})`);
55+
db.user.setBanState(true, user.id);
56+
}, console.error);
57+
58+
this.client.on("guildBanRemove", function(guild, user) {
59+
log.info(`DISCORD: ${"Unbanned user".green} ${user.username}#${user.discriminator} (${user.id})`);
60+
db.user.setBanState(false, user.id);
61+
}, console.error);
62+
63+
5264
this.client.login(config.discord.botToken);
5365

5466
return socketChat;
@@ -151,21 +163,25 @@ const discordManager = function() {
151163
let response = {
152164
authorized: true,
153165
refreshed: true,
166+
banned: false,
154167
tokenBody
155168
};
156169

157170
res.send(response);
158171
},
159172
() => {
160-
res.status(400).send({ authorized: false, refreshed: false });
173+
res.status(400).send({ authorized: false, refreshed: false, banned: false });
161174
});
162175
} else if (this.db.user.idIsAuthenticated(req.body.id, req.body.access_token)) {
163-
res.send({ authorized: true, refreshed: false });
176+
res.send({ authorized: true, refreshed: false, banned: false });
177+
return;
178+
} else if (this.db.user.idIsBanned(req.body.id)) {
179+
res.send({ authorized: false, refreshed: false, banned: true });
164180
return;
165181
}
166182
}
167183

168-
res.status(400).send({ authorized: false, refreshed: false });
184+
res.status(400).send({ authorized: false, refreshed: false, banned: false });
169185
}
170186
};
171187
}();

src/server/database/initializer.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const log = require("../../log");
22

33
module.exports = function(db) {
4-
let _schemaVersion = 3;
4+
let _schemaVersion = 4;
55

66
return {
77
_users: db.prepare(
@@ -15,6 +15,7 @@ module.exports = function(db) {
1515
timestamp_refresh_last INTEGER,
1616
time_refresh_expire INTEGER,
1717
scope TEXT,
18+
is_banned INTEGER,
1819
stat_points_earned INTEGER,
1920
stat_rounds_entered INTEGER,
2021
stat_rounds_finished INTEGER,

0 commit comments

Comments
 (0)