From 732c8e007f943fce12d0889ce8c41b5ec5b08cd9 Mon Sep 17 00:00:00 2001 From: Jorge Miguel Lobo Escalona Date: Thu, 13 Oct 2022 13:56:18 +0200 Subject: [PATCH] M #~: disable inputs restricted attributes (#2305) (cherry picked from commit 66442e00a00a85f3d9ad74b083fb37ae100afcbf) --- .../Forms/Vm/AttachDiskForm/CommonFields.js | 20 +++++++++++++++++++ .../Steps/AdvancedOptions/schema.js | 9 +++++++++ .../client/components/HOC/AsyncLoadForm.js | 11 ++-------- .../src/client/features/Auth/hooks.js | 20 +++++++++++++++++++ 4 files changed, 51 insertions(+), 9 deletions(-) diff --git a/src/fireedge/src/client/components/Forms/Vm/AttachDiskForm/CommonFields.js b/src/fireedge/src/client/components/Forms/Vm/AttachDiskForm/CommonFields.js index 2bfa0a5ed3..78664e2db0 100644 --- a/src/fireedge/src/client/components/Forms/Vm/AttachDiskForm/CommonFields.js +++ b/src/fireedge/src/client/components/Forms/Vm/AttachDiskForm/CommonFields.js @@ -15,6 +15,7 @@ * ------------------------------------------------------------------------- */ import { string, number } from 'yup' import { INPUT_TYPES, T, HYPERVISORS, Field } from 'client/constants' +import { useDisableInputByUserAndConfig } from 'client/features/Auth' const { vcenter, firecracker, lxc } = HYPERVISORS @@ -216,6 +217,8 @@ export const THROTTLING_BYTES_FIELDS = [ type: INPUT_TYPES.TEXT, htmlType: 'number', notOnHypervisors: [lxc, firecracker, vcenter], + fieldProps: () => + useDisableInputByUserAndConfig('DISK/READ_BYTES_SEC_MAX_LENGTH'), validation: number() .min(0) .notRequired() @@ -227,6 +230,7 @@ export const THROTTLING_BYTES_FIELDS = [ type: INPUT_TYPES.TEXT, htmlType: 'number', notOnHypervisors: [lxc, firecracker, vcenter], + fieldProps: () => useDisableInputByUserAndConfig('DISK/WRITE_BYTES_SEC'), validation: number() .min(0) .notRequired() @@ -238,6 +242,8 @@ export const THROTTLING_BYTES_FIELDS = [ type: INPUT_TYPES.TEXT, htmlType: 'number', notOnHypervisors: [lxc, firecracker, vcenter], + fieldProps: () => + useDisableInputByUserAndConfig('DISK/WRITE_BYTES_SEC_MAX'), validation: number() .min(0) .notRequired() @@ -249,6 +255,8 @@ export const THROTTLING_BYTES_FIELDS = [ type: INPUT_TYPES.TEXT, htmlType: 'number', notOnHypervisors: [lxc, firecracker, vcenter], + fieldProps: () => + useDisableInputByUserAndConfig('DISK/WRITE_BYTES_SEC_MAX_LENGTH'), validation: number() .min(0) .notRequired() @@ -264,6 +272,7 @@ export const THROTTLING_IOPS_FIELDS = [ type: INPUT_TYPES.TEXT, htmlType: 'number', notOnHypervisors: [lxc, firecracker, vcenter], + fieldProps: () => useDisableInputByUserAndConfig('DISK/TOTAL_IOPS_SEC'), validation: number() .min(0) .notRequired() @@ -275,6 +284,7 @@ export const THROTTLING_IOPS_FIELDS = [ type: INPUT_TYPES.TEXT, htmlType: 'number', notOnHypervisors: [lxc, firecracker, vcenter], + fieldProps: () => useDisableInputByUserAndConfig('DISK/TOTAL_IOPS_SEC_MAX'), validation: number() .min(0) .notRequired() @@ -286,6 +296,8 @@ export const THROTTLING_IOPS_FIELDS = [ type: INPUT_TYPES.TEXT, htmlType: 'number', notOnHypervisors: [lxc, firecracker, vcenter], + fieldProps: () => + useDisableInputByUserAndConfig('DISK/TOTAL_IOPS_SEC_MAX_LENGTH'), validation: number() .min(0) .notRequired() @@ -297,6 +309,7 @@ export const THROTTLING_IOPS_FIELDS = [ type: INPUT_TYPES.TEXT, htmlType: 'number', notOnHypervisors: [lxc, firecracker, vcenter], + fieldProps: () => useDisableInputByUserAndConfig('DISK/READ_IOPS_SEC'), validation: number() .min(0) .notRequired() @@ -308,6 +321,7 @@ export const THROTTLING_IOPS_FIELDS = [ type: INPUT_TYPES.TEXT, htmlType: 'number', notOnHypervisors: [lxc, firecracker, vcenter], + fieldProps: () => useDisableInputByUserAndConfig('DISK/READ_IOPS_SEC_MAX'), validation: number() .min(0) .notRequired() @@ -319,6 +333,8 @@ export const THROTTLING_IOPS_FIELDS = [ type: INPUT_TYPES.TEXT, htmlType: 'number', notOnHypervisors: [lxc, firecracker, vcenter], + fieldProps: () => + useDisableInputByUserAndConfig('DISK/READ_IOPS_SEC_MAX_LENGTH'), validation: number() .min(0) .notRequired() @@ -330,6 +346,7 @@ export const THROTTLING_IOPS_FIELDS = [ type: INPUT_TYPES.TEXT, htmlType: 'number', notOnHypervisors: [lxc, firecracker, vcenter], + fieldProps: () => useDisableInputByUserAndConfig('DISK/WRITE_IOPS_SEC'), validation: number() .min(0) .notRequired() @@ -341,6 +358,7 @@ export const THROTTLING_IOPS_FIELDS = [ type: INPUT_TYPES.TEXT, htmlType: 'number', notOnHypervisors: [lxc, firecracker, vcenter], + fieldProps: () => useDisableInputByUserAndConfig('DISK/WRITE_IOPS_SEC_MAX'), validation: number() .min(0) .notRequired() @@ -352,6 +370,8 @@ export const THROTTLING_IOPS_FIELDS = [ type: INPUT_TYPES.TEXT, htmlType: 'number', notOnHypervisors: [lxc, firecracker, vcenter], + fieldProps: () => + useDisableInputByUserAndConfig('DISK/WRITE_IOPS_SEC_MAX_LENGTH'), validation: number() .min(0) .notRequired() diff --git a/src/fireedge/src/client/components/Forms/Vm/AttachNicForm/Steps/AdvancedOptions/schema.js b/src/fireedge/src/client/components/Forms/Vm/AttachNicForm/Steps/AdvancedOptions/schema.js index 3810edda60..b52fdc3947 100644 --- a/src/fireedge/src/client/components/Forms/Vm/AttachNicForm/Steps/AdvancedOptions/schema.js +++ b/src/fireedge/src/client/components/Forms/Vm/AttachNicForm/Steps/AdvancedOptions/schema.js @@ -24,6 +24,8 @@ import { } from 'client/utils' import { T, INPUT_TYPES, HYPERVISORS, VN_DRIVERS, Nic } from 'client/constants' +import { useDisableInputByUserAndConfig } from 'client/features/Auth' + const { firecracker } = HYPERVISORS const { ovswitch, vcenter } = VN_DRIVERS @@ -248,6 +250,7 @@ const OVERRIDE_IPV4_FIELDS = [ label: T.MAC, tooltip: T.MACConcept, type: INPUT_TYPES.TEXT, + fieldProps: () => useDisableInputByUserAndConfig('NIC/MAC'), validation: string() .trim() .notRequired() @@ -353,6 +356,7 @@ const OVERRIDE_IN_QOS_FIELDS = [ type: INPUT_TYPES.TEXT, notOnHypervisors: [firecracker], htmlType: 'number', + fieldProps: () => useDisableInputByUserAndConfig('NIC/INBOUND_AVG_BW'), validation: number() .notRequired() .default(() => undefined), @@ -364,6 +368,7 @@ const OVERRIDE_IN_QOS_FIELDS = [ type: INPUT_TYPES.TEXT, notOnHypervisors: [firecracker], htmlType: 'number', + fieldProps: () => useDisableInputByUserAndConfig('NIC/INBOUND_PEAK_BW'), validation: number() .notRequired() .default(() => undefined), @@ -376,6 +381,7 @@ const OVERRIDE_IN_QOS_FIELDS = [ notOnHypervisors: [firecracker], notOnDrivers: [vcenter], htmlType: 'number', + fieldProps: () => useDisableInputByUserAndConfig('NIC/INBOUND_PEAK_KB'), validation: number() .notRequired() .default(() => undefined), @@ -392,6 +398,7 @@ const OVERRIDE_OUT_QOS_FIELDS = [ notOnHypervisors: [firecracker], notOnDrivers: [ovswitch], htmlType: 'number', + fieldProps: () => useDisableInputByUserAndConfig('NIC/OUTBOUND_AVG_BW'), validation: number() .notRequired() .default(() => undefined), @@ -404,6 +411,7 @@ const OVERRIDE_OUT_QOS_FIELDS = [ notOnHypervisors: [firecracker], notOnDrivers: [ovswitch], htmlType: 'number', + fieldProps: () => useDisableInputByUserAndConfig('NIC/OUTBOUND_PEAK_BW'), validation: number() .notRequired() .default(() => undefined), @@ -416,6 +424,7 @@ const OVERRIDE_OUT_QOS_FIELDS = [ notOnHypervisors: [firecracker], notOnDrivers: [ovswitch, vcenter], htmlType: 'number', + fieldProps: () => useDisableInputByUserAndConfig('NIC/OUTBOUND_PEAK_KB'), validation: number() .notRequired() .default(() => undefined), diff --git a/src/fireedge/src/client/components/HOC/AsyncLoadForm.js b/src/fireedge/src/client/components/HOC/AsyncLoadForm.js index 2fbbce926c..438d71021c 100644 --- a/src/fireedge/src/client/components/HOC/AsyncLoadForm.js +++ b/src/fireedge/src/client/components/HOC/AsyncLoadForm.js @@ -17,8 +17,7 @@ import { useMemo, useCallback, ReactElement } from 'react' import PropTypes from 'prop-types' import loadable, { LoadableLibrary } from '@loadable/component' import { Backdrop, CircularProgress } from '@mui/material' -import { useAuth } from 'client/features/Auth' -import { useGetOneConfigQuery } from 'client/features/OneApi/system' +import { useSystemData } from 'client/features/Auth' import { CreateFormCallback, CreateStepsCallback } from 'client/utils/schema' /** @@ -98,13 +97,7 @@ const MemoizedForm = ({ [] ) - const { data: oneConfig = {} } = useGetOneConfigQuery() - - const { user } = useAuth() - const userGroup = Array.isArray(user?.GROUPS?.ID) - ? user?.GROUPS?.ID - : [user?.GROUPS?.ID] - const adminGroup = userGroup?.includes?.('0') + const { oneConfig, adminGroup } = useSystemData() const handleTriggerSubmit = useCallback( (data) => diff --git a/src/fireedge/src/client/features/Auth/hooks.js b/src/fireedge/src/client/features/Auth/hooks.js index cd2b53cb9a..8988d8768d 100644 --- a/src/fireedge/src/client/features/Auth/hooks.js +++ b/src/fireedge/src/client/features/Auth/hooks.js @@ -97,6 +97,26 @@ export const useAuth = () => { ) } +export const useSystemData = () => { + const { data: oneConfig = {} } = systemApi.useGetOneConfigQuery() + + const { user } = useAuth() + const userGroup = Array.isArray(user?.GROUPS?.ID) + ? user?.GROUPS?.ID + : [user?.GROUPS?.ID] + const adminGroup = userGroup?.includes?.('0') + + return { oneConfig, adminGroup } +} + +export const useDisableInputByUserAndConfig = (input = '') => { + const { adminGroup, oneConfig } = useSystemData() + + return { + disabled: !adminGroup && oneConfig.VM_RESTRICTED_ATTR?.includes?.(input), + } +} + export const useAuthApi = () => { const dispatch = useDispatch()