mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
feat: expose username validation logic to user lib, new hook filter:username.check
This commit is contained in:
@@ -106,10 +106,14 @@ module.exports = function (User) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
data.username = data.username.trim();
|
data.username = data.username.trim();
|
||||||
const userData = await User.getUserFields(uid, ['username', 'userslug']);
|
|
||||||
|
let userData;
|
||||||
|
if (uid) {
|
||||||
|
userData = await User.getUserFields(uid, ['username', 'userslug']);
|
||||||
if (userData.username === data.username) {
|
if (userData.username === data.username) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (data.username.length < meta.config.minimumUsernameLength) {
|
if (data.username.length < meta.config.minimumUsernameLength) {
|
||||||
throw new Error('[[error:username-too-short]]');
|
throw new Error('[[error:username-too-short]]');
|
||||||
@@ -124,14 +128,23 @@ module.exports = function (User) {
|
|||||||
throw new Error('[[error:invalid-username]]');
|
throw new Error('[[error:invalid-username]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (userslug === userData.userslug) {
|
if (uid && userslug === userData.userslug) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const exists = await User.existsBySlug(userslug);
|
const exists = await User.existsBySlug(userslug);
|
||||||
if (exists) {
|
if (exists) {
|
||||||
throw new Error('[[error:username-taken]]');
|
throw new Error('[[error:username-taken]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { error } = await plugins.hooks.fire('filter:username.check', {
|
||||||
|
username: data.username,
|
||||||
|
error: undefined,
|
||||||
|
});
|
||||||
|
if (error) {
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
User.checkUsername = async username => isUsernameAvailable({ username });
|
||||||
|
|
||||||
async function isWebsiteValid(callerUid, data) {
|
async function isWebsiteValid(callerUid, data) {
|
||||||
if (!data.website) {
|
if (!data.website) {
|
||||||
|
|||||||
Reference in New Issue
Block a user