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

Update username validation on useradd #7134

Merged
merged 4 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion cypress/e2e/users_spec/user_creation.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("User Creation", () => {
}
return result;
};
const username = makeid(25);
const username = makeid(14);
const alreadylinkedusersviews = [
"devdoctor",
"devstaff2",
Expand Down
2 changes: 1 addition & 1 deletion src/Common/validation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const validateName = (name: string) => {
};

export const validateUsername = (username: string) => {
const pattern = /^[\w.@+-]+[^.@+_-]$/;
const pattern = /^(?!.*[._-]{2})[a-z0-9](?:[a-z0-9._-]{2,14}[a-z0-9])$/s;
return pattern.test(username);
};

Expand Down
30 changes: 18 additions & 12 deletions src/Components/Users/UserAdd.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,7 @@ export const UserAdd = (props: UserProps) => {

useEffect(() => {
setUsernameExists(userExistsEnums.idle);
if (
usernameInput.length > 1 &&
!(state.form.username?.length < 2) &&
/[^.@+_-]/.test(state.form.username[state.form.username?.length - 1])
) {
if (validateUsername(usernameInput)) {
const timeout = setTimeout(() => {
check_username(usernameInput);
}, 500);
Expand Down Expand Up @@ -403,7 +399,7 @@ export const UserAdd = (props: UserProps) => {
invalidForm = true;
} else if (!validateUsername(state.form[field])) {
errors[field] =
"Please enter letters, digits and @ . + - _ only and username should not end with @, ., +, - or _";
"Please enter a 4-16 characters long username with lowercase letters, digits and . _ - only and it should not start or end with . _ -";
invalidForm = true;
} else if (usernameExists !== userExistsEnums.available) {
errors[field] = "This username already exists";
Expand Down Expand Up @@ -757,16 +753,26 @@ export const UserAdd = (props: UserProps) => {
</div>
<div>
{validateRule(
state.form.username?.length >= 2,
"Username should be atleast 2 characters long"
usernameInput.length >= 4 && usernameInput.length <= 16,
"Username should be 4-16 characters long"
)}
</div>
<div>
{validateRule(
/[^.@+_-]/.test(
state.form.username[state.form.username?.length - 1]
),
"Username can't end with ^ . @ + _ -"
/^[a-z0-9._-]*$/.test(usernameInput),
"Username can only contain lowercase letters, numbers, and . _ -"
)}
</div>
<div>
{validateRule(
/^[a-z0-9].*[a-z0-9]$/i.test(usernameInput),
"Username must start and end with a letter or number"
)}
</div>
<div>
{validateRule(
!/(?:[._-]{2,})/.test(usernameInput),
"Username can't contain consecutive special characters . _ -"
)}
</div>
</div>
Expand Down
Loading