Skip to content
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

[IMPROVE] AvatarBlockUnauthenticatedAccess do not call user.find if you dont have to #15355

Merged
merged 1 commit into from
Sep 13, 2019
Merged
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
13 changes: 6 additions & 7 deletions server/routes/avatar/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,26 @@ function isUserAuthenticated({ headers, query }) {
return false;
}

const userFound = Users.findOneByIdAndLoginToken(rc_uid, rc_token, { fields: { _id: 1 } });
const userFound = Users.findOneByIdAndLoginToken(rc_uid, rc_token, { fields: { _id: 1 } }); // TODO memoize find

return !!rc_uid && !!rc_token && !!userFound;
return !!userFound;
}

const warnUnauthenticatedAccess = throttle(() => {
console.warn('The server detected an unauthenticated access to an user avatar. This type of request will soon be blocked by default.');
}, 60000 * 30); // 30 minutes

export function userCanAccessAvatar({ headers = {}, query = {} }) {
const isAuthenticated = isUserAuthenticated({ headers, query });

if (settings.get('Accounts_AvatarBlockUnauthenticatedAccess') === true) {
return isAuthenticated;
if (!settings.get('Accounts_AvatarBlockUnauthenticatedAccess')) {
return true;
}

const isAuthenticated = isUserAuthenticated({ headers, query });
if (!isAuthenticated) {
warnUnauthenticatedAccess();
}

return true;
return isAuthenticated;
}

const getFirstLetter = (name) => name.replace(/[^A-Za-z0-9]/g, '').substr(0, 1).toUpperCase();
Expand Down