Skip to content

Chore/ban users #1936

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion server/config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import { BasicStrategy } from 'passport-http';

import User from '../models/user';

const accountSuspensionMessage =
'Account has been suspended. Please contact privacy@p5js.org if you believe this is an error.';

function generateUniqueUsername(username) {
const adj =
friendlyWords.predicates[
Expand Down Expand Up @@ -38,6 +41,9 @@ passport.use(
if (!user) {
done(null, false, { msg: `Email ${email} not found.` });
return;
} else if (user.banned) {
done(null, false, { msg: accountSuspensionMessage });
return;
}
user.comparePassword(password, (innerErr, isMatch) => {
if (isMatch) {
Expand Down Expand Up @@ -65,6 +71,10 @@ passport.use(
done(null, false);
return;
}
if (user.banned) {
done(null, false, { msg: accountSuspensionMessage });
return;
}
user.findMatchingKey(key, (innerErr, isMatch, keyDocument) => {
if (isMatch) {
keyDocument.lastUsedAt = Date.now();
Expand Down Expand Up @@ -117,6 +127,9 @@ passport.use(
new Error('GitHub account is already linked to another account.')
);
return;
} else if (existingUser.banned) {
done(new Error(accountSuspensionMessage));
return;
}
done(null, existingUser);
return;
Expand Down Expand Up @@ -145,6 +158,10 @@ passport.use(
} else {
[existingEmailUser] = existingEmailUsers;
}
if (existingEmailUser.banned) {
done(new Error(accountSuspensionMessage));
return;
}
existingEmailUser.email = existingEmailUser.email || primaryEmail;
existingEmailUser.github = profile.id;
existingEmailUser.username =
Expand Down Expand Up @@ -207,11 +224,13 @@ passport.use(
)
);
return;
} else if (existingUser.banned) {
done(new Error(accountSuspensionMessage));
return;
}
done(null, existingUser);
return;
}

const primaryEmail = profile._json.emails[0].value;

if (req.user) {
Expand All @@ -236,6 +255,10 @@ passport.use(
// what if a username is already taken from the display name too?
// then, append a random friendly word?
if (existingEmailUser) {
if (existingEmailUser.banned) {
done(new Error(accountSuspensionMessage));
return;
}
existingEmailUser.email =
existingEmailUser.email || primaryEmail;
existingEmailUser.google = profile._json.emails[0].value;
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/session.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function createSession(req, res, next) {
}

export function getSession(req, res) {
if (req.user) {
if (req.user && !req.user.banned) {
return res.json(userResponse(req.user));
}
return res.status(404).send({ message: 'Session does not exist' });
Expand Down
3 changes: 2 additions & 1 deletion server/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ const userSchema = new Schema(
type: String,
enum: ['none', 'essential', 'all'],
default: 'none'
}
},
banned: { type: Boolean, default: false }
},
{ timestamps: true, usePushEach: true }
);
Expand Down