Skip to content

Commit

Permalink
Merge pull request #5941 from ashanthamara/maintenance
Browse files Browse the repository at this point in the history
Add missing alphanumeric special character validation in add user wizard
  • Loading branch information
sumedhe authored Apr 11, 2024
2 parents cfdc841 + f939a3b commit e31b68d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/fifty-ligers-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wso2is/features": patch
---

Add missing validation for alphanumeric special charactor username type
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ import {
PageLayout,
Text
} from "@wso2is/react-components";
import { updateValidationConfigData, useValidationConfigData } from "../../../../admin.validation.v1/api";
import {
ValidationConfInterface,
ValidationDataInterface,
ValidationFormInterface,
ValidationPropertyInterface
} from "../../../../admin.validation.v1/models";
import { AxiosError } from "axios";
import React, {
FunctionComponent,
Expand All @@ -48,6 +41,13 @@ import { Dispatch } from "redux";
import { Grid, Ref } from "semantic-ui-react";
import { ApplicationManagementConstants } from "../../../../admin.applications.v1/constants";
import { AppConstants, history } from "../../../../admin.core.v1";
import { updateValidationConfigData, useValidationConfigData } from "../../../../admin.validation.v1/api";
import {
ValidationConfInterface,
ValidationDataInterface,
ValidationFormInterface,
ValidationPropertyInterface
} from "../../../../admin.validation.v1/models";
import { UsernameValidationConstants } from "../constants/username-validation-constants";
import { UsernameTypes } from "../models";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import {
Grid,
Message
} from "semantic-ui-react";
import { userstoresConfig } from "../../../../admin.extensions.v1/configs/userstores";
import { AppState } from "../../../../admin.core.v1";
import { SharedUserStoreUtils } from "../../../../admin.core.v1/utils";
import { userstoresConfig } from "../../../../admin.extensions.v1/configs/userstores";
import { getUsersList } from "../../../../admin.users.v1/api/users";
import { UserListInterface } from "../../../../admin.users.v1/models/user";
import { getConfiguration, getUsernameConfiguration } from "../../../../admin.users.v1/utils";
Expand Down Expand Up @@ -91,6 +91,8 @@ export const AddConsumerUser: React.FunctionComponent<AddConsumerUserProps> = (
const USERNAME_JAVA_REGEX: string = "UsernameJavaRegEx";
const USERNAME_HAS_INVALID_SYMBOLS_ERROR_MESSAGE: string = t("extensions:manage.features.user.addUser.validation." +
"usernameSymbols");
const USERNAME_HAS_INVALID_SPECIAL_SYMBOLS_ERROR_MESSAGE: string = t("extensions:manage.features.user.addUser." +
"validation.usernameSpecialCharSymbols");
const USERNAME_HAS_INVALID_LENGTH_ERROR_MESSAGE: string =
t("extensions:manage.features.user.addUser.validation.usernameLength", {
maxLength: usernameConfig?.maxLength,
Expand Down Expand Up @@ -471,9 +473,16 @@ export const AddConsumerUser: React.FunctionComponent<AddConsumerUserProps> = (
"validations.empty"
) }
validation={ async (value: string, validation: Validation) => {
// Regular expression to validate having alphanumeric characters.
const regExpInvalidUsername: RegExp
= new RegExp(UserManagementConstants.USERNAME_VALIDATION_REGEX);
// Regular expression to validate having alphanumeric characters.
let regExpInvalidUsername: RegExp = new RegExp(
UserManagementConstants.USERNAME_VALIDATION_REGEX);

// Check if special characters enabled for username.
if (!usernameConfig?.isAlphanumericOnly) {
regExpInvalidUsername = new RegExp(
UserManagementConstants.
USERNAME_VALIDATION_REGEX_WITH_SPECIAL_CHARS);
}

// Check username length validations.
if (value.length < Number(usernameConfig.minLength)
Expand All @@ -484,8 +493,13 @@ export const AddConsumerUser: React.FunctionComponent<AddConsumerUserProps> = (
// Check username validity against userstore regex.
} else if (!regExpInvalidUsername.test(value)) {
validation.isValid = false;
validation.errorMessages.push(
USERNAME_HAS_INVALID_SYMBOLS_ERROR_MESSAGE);
if (usernameConfig?.isAlphanumericOnly) {
validation.errorMessages.push(
USERNAME_HAS_INVALID_SYMBOLS_ERROR_MESSAGE);
} else {
validation.errorMessages.push(
USERNAME_HAS_INVALID_SPECIAL_SYMBOLS_ERROR_MESSAGE);
}
}
} }
type="text"
Expand All @@ -494,7 +508,12 @@ export const AddConsumerUser: React.FunctionComponent<AddConsumerUserProps> = (
maxLength={ 60 }
/>
<Hint>
{ t("extensions:manage.features.user.addUser.validation.usernameHint", {
{ usernameConfig?.isAlphanumericOnly ? t("extensions:manage.features." +
"user.addUser.validation.usernameHint", {
maxLength: usernameConfig?.maxLength,
minLength: usernameConfig?.minLength
}) : t("extensions:manage.features.user.addUser.validation" +
".usernameSpecialCharHint", {
maxLength: usernameConfig?.maxLength,
minLength: usernameConfig?.minLength
}) }
Expand Down
30 changes: 24 additions & 6 deletions features/admin.users.v1/components/wizard/steps/add-user-basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* under the License.
*/

import useUIConfig from "../../../../admin.core.v1/hooks/use-ui-configs";
import { Field, FormValue, Forms, Validation } from "@wso2is/forms";
import { Button, Hint, Link, PasswordValidation, Popup } from "@wso2is/react-components";
import { FormValidation } from "@wso2is/validation";
Expand All @@ -25,6 +24,7 @@ import { Trans, useTranslation } from "react-i18next";
import { Dropdown, DropdownItemProps, DropdownProps, Form, Grid, Menu, Message, Radio } from "semantic-ui-react";
import { AppConstants } from "../../../../admin.core.v1/constants";
import { history } from "../../../../admin.core.v1/helpers/history";
import useUIConfig from "../../../../admin.core.v1/hooks/use-ui-configs";
import { EventPublisher } from "../../../../admin.core.v1/utils/event-publisher";
import { SharedUserStoreUtils } from "../../../../admin.core.v1/utils/user-store-utils";
import { userConfig } from "../../../../admin.extensions.v1/configs/user";
Expand Down Expand Up @@ -138,6 +138,8 @@ export const AddUserUpdated: React.FunctionComponent<AddUserProps> = (
const USERNAME_HAS_INVALID_SYMBOLS_ERROR_MESSAGE: string =
t("extensions:manage.features.user.addUser.validation." +
"usernameSymbols");
const USERNAME_HAS_INVALID_SPECIAL_SYMBOLS_ERROR_MESSAGE: string = t("extensions:manage.features.user.addUser." +
"validation.usernameSpecialCharSymbols");
const USERNAME_HAS_INVALID_LENGTH_ERROR_MESSAGE: string =
t("extensions:manage.features.user.addUser.validation.usernameLength", {
maxLength: usernameConfig?.maxLength,
Expand Down Expand Up @@ -745,8 +747,14 @@ export const AddUserUpdated: React.FunctionComponent<AddUserProps> = (
"validations.empty"
) }
validation={ async (value: string, validation: Validation) => {
const regExpInvalidUsername: RegExp
= new RegExp(UserManagementConstants.USERNAME_VALIDATION_REGEX);
let regExpInvalidUsername: RegExp = new RegExp(
UserManagementConstants.USERNAME_VALIDATION_REGEX);

// Check if special characters enabled for username.
if (!usernameConfig?.isAlphanumericOnly) {
regExpInvalidUsername = new RegExp(
UserManagementConstants.USERNAME_VALIDATION_REGEX_WITH_SPECIAL_CHARS);
}

// Check username length validations.
if (value.length < Number(usernameConfig.minLength)
Expand All @@ -757,8 +765,13 @@ export const AddUserUpdated: React.FunctionComponent<AddUserProps> = (
// Check username validity against userstore regex.
} else if (!regExpInvalidUsername.test(value)) {
validation.isValid = false;
validation.errorMessages.push(
USERNAME_HAS_INVALID_SYMBOLS_ERROR_MESSAGE);
if (usernameConfig?.isAlphanumericOnly) {
validation.errorMessages.push(
USERNAME_HAS_INVALID_SYMBOLS_ERROR_MESSAGE);
} else {
validation.errorMessages.push(
USERNAME_HAS_INVALID_SPECIAL_SYMBOLS_ERROR_MESSAGE);
}
}

try {
Expand Down Expand Up @@ -805,7 +818,12 @@ export const AddUserUpdated: React.FunctionComponent<AddUserProps> = (
maxLength={ 60 }
/>
<Hint>
{ t("extensions:manage.features.user.addUser.validation.usernameHint", {
{ usernameConfig?.isAlphanumericOnly ? t("extensions:manage.features." +
"user.addUser.validation.usernameHint", {
maxLength: usernameConfig?.maxLength,
minLength: usernameConfig?.minLength
}) : t("extensions:manage.features.user.addUser.validation" +
".usernameSpecialCharHint", {
maxLength: usernameConfig?.maxLength,
minLength: usernameConfig?.minLength
}) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ export class UserManagementConstants {

// Regular expression to validate having alphanumeric characters.
public static readonly USERNAME_VALIDATION_REGEX: string = "^(?=.*[a-zA-Z])[a-zA-Z0-9]+$";
// Regular expression to validate having alphanumeric with special characters.
public static readonly USERNAME_VALIDATION_REGEX_WITH_SPECIAL_CHARS: string =
"^(?=.*[a-zA-Z])[a-zA-Z0-9!@#$&'+\\\\=^_.{|}~-]+$";

// Error message when API call returns a status code !== 200
public static readonly INVALID_STATUS_CODE_ERROR: string = "Invalid Status Code. Expected Code 200.";
// Error message text for resources not found.
Expand Down

0 comments on commit e31b68d

Please sign in to comment.