Skip to content

Commit

Permalink
HPCC-31202 File permission dialog fails to open
Browse files Browse the repository at this point in the history
When checking file permissions for file scopes, dialog was not opening.
Dialog now opening for users to check permissions.

Signed-off-by: Kunal Aswani <[email protected]>
  • Loading branch information
kunalaswani committed Feb 7, 2024
1 parent 6cea89d commit 6a2e39c
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 1 deletion.
5 changes: 4 additions & 1 deletion esp/src/src-react/components/Permissions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useConfirm } from "../hooks/confirm";
import { useBuildInfo } from "../hooks/platform";
import { DojoGrid, selector, tree } from "./DojoGrid";
import { AddPermissionForm } from "./forms/AddPermission";
import { CheckPermissionsForm } from "./forms/CheckPermissions";
import { HolyGrail } from "../layouts/HolyGrail";
import { pushUrl } from "../util/history";

Expand All @@ -35,6 +36,7 @@ export const Permissions: React.FunctionComponent<PermissionsProps> = ({
const [selection, setSelection] = React.useState([]);

const [showAddPermission, setShowAddPermission] = React.useState(false);
const [showCheckFilePermissions, setShowCheckFilePermissions] = React.useState(false);
const [scopeScansEnabled, setScopeScansEnabled] = React.useState(false);
const [modulesDn, setModulesDn] = React.useState("");
const [uiState, setUIState] = React.useState({ ...defaultUIState });
Expand Down Expand Up @@ -203,7 +205,7 @@ export const Permissions: React.FunctionComponent<PermissionsProps> = ({
{ key: "fileScopeDefaults", text: nlsHPCC.FileScopeDefaultPermissions, onClick: (evt, item) => pushUrl(`/${opsCategory}/security/permissions/_/File%20Scopes`), disabled: !uiState.fileScope },
{ key: "workunitScopeDefaults", text: nlsHPCC.WorkUnitScopeDefaultPermissions, onClick: (evt, item) => pushUrl(`/${opsCategory}/security/permissions/_/Workunit%20Scopes`), disabled: !uiState.workunitScope },
{ key: "physicalFiles", text: nlsHPCC.PhysicalFiles, onClick: (evt, item) => pushUrl(`/${opsCategory}/security/permissions/file/File%20Scopes`), disabled: !uiState.fileScope },
{ key: "checkFilePermissions", text: nlsHPCC.CheckFilePermissions, disabled: !uiState.fileScope },
{ key: "checkFilePermissions", text: nlsHPCC.CheckFilePermissions, onClick: () => setShowCheckFilePermissions(true), disabled: !uiState.fileScope },
{ key: "codeGenerator", text: nlsHPCC.CodeGenerator, onClick: (evt, item) => pushUrl(`/${opsCategory}/security/permissions/_/${modulesDn}`), disabled: !uiState.repositoryModule },
],
},
Expand All @@ -223,6 +225,7 @@ export const Permissions: React.FunctionComponent<PermissionsProps> = ({
<ClearPermissionsConfirm />
<EnableScopesConfirm />
<DisableScopesConfirm />
<CheckPermissionsForm showForm={showCheckFilePermissions} setShowForm={setShowCheckFilePermissions} refreshGrid={() => refreshTable()} />
<AddPermissionForm showForm={showAddPermission} setShowForm={setShowAddPermission} refreshGrid={refreshTable} />
</>;

Expand Down
140 changes: 140 additions & 0 deletions esp/src/src-react/components/forms/CheckPermissions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import * as React from "react";
import { DefaultButton, Dropdown, IDropdownOption, MessageBar, MessageBarType, PrimaryButton, TextField } from "@fluentui/react";
import { scopedLogger } from "@hpcc-js/util";
import { useForm, Controller } from "react-hook-form";
import nlsHPCC from "src/nlsHPCC";
import { MessageBox } from "../../layouts/MessageBox";
import { Users } from "../Users"
import { Groups } from "../Groups";

const logger = scopedLogger("src-react/components/forms/CheckPermissions.tsx");

interface CheckPermissionsFormValues {
FileName: string;
SelectedType: string;
SelectedValue: string;
}

const defaultValues: CheckPermissionsFormValues = {
FileName: "",
SelectedType: "",
SelectedValue: "",
};

interface CheckPermissionsFormProps {
refreshGrid?: () => void;
showForm: boolean;
setShowForm: (_: boolean) => void;
}

export const CheckPermissionsForm: React.FunctionComponent<CheckPermissionsFormProps> = ({
refreshGrid,
showForm,
setShowForm
}) => {

const { handleSubmit, control, reset, setValue, watch } = useForm<CheckPermissionsFormValues>({ defaultValues });

const [showError, setShowError] = React.useState(false);
const [errorMessage] = React.useState("");
const [userOptions, setUserOptions] = React.useState<IDropdownOption[]>([]);
const [groupOptions, setGroupOptions] = React.useState<IDropdownOption[]>([]);
const selectedType = watch("SelectedType");

React.useEffect(() => {
const fetchUsersAndGroups = async () => {
try {
const users = Users();
const userOptions: IDropdownOption[] = Array.isArray(users) ? users.map(user => ({ key: user.id, text: user.name })) : [];
setUserOptions(userOptions);

const groups = Groups();
const groupOptions: IDropdownOption[] = Array.isArray(groups) ? groups.map(group => ({ key: group.id, text: group.name })) : [];
setGroupOptions(groupOptions);
} catch (error) {
logger.error("Error fetching users and groups:");
}
};

fetchUsersAndGroups();
}, []);

const closeForm = React.useCallback(() => {
setShowForm(false);
}, [setShowForm]);

const handleDropdownChange = (newValue: string, selectedType: string) => {
setValue("SelectedType", selectedType);
setValue("SelectedValue", newValue);
};

const onSubmit = React.useCallback((data) => {
const { FileName, SelectedType, SelectedValue } = data;
logger.info(`Checking permissions for file ${FileName} for ${SelectedType}: ${SelectedValue}`);
}, []);

return (
<MessageBox show={showForm} setShow={closeForm} title={nlsHPCC.CheckFilePermissions} minWidth={400}
footer={<>
<PrimaryButton text={nlsHPCC.Submit} onClick={handleSubmit(onSubmit)} />
<DefaultButton text={nlsHPCC.Cancel} onClick={() => { reset(defaultValues); closeForm(); }} />
</>}>
<Controller
control={control} name="FileName"
render={({
field: { onChange, name: fieldName, value },
fieldState: { error }
}) => <TextField
name={fieldName}
onChange={onChange}
label={nlsHPCC.Name}
value={value}
errorMessage={error && error?.message}
/>}
rules={{
required: nlsHPCC.ValidationErrorRequired
}}
/>
<Dropdown
label={nlsHPCC.Type}
options={[
{ key: "Users", text: nlsHPCC.Users },
{ key: "Groups", text: nlsHPCC.Groups }
]}
selectedKey={selectedType}
onChange={(ev, option) => {
handleDropdownChange("", option.key.toString());
}}
/>
{selectedType === "Users" && (
<Dropdown
label={nlsHPCC.Users}
options={userOptions}
selectedKey={watch("SelectedValue")}
onChange={(ev, option) => {
handleDropdownChange(option?.key.toString() || "", "Users");
}}
/>
)}
{selectedType === "Groups" && (
<Dropdown
label={nlsHPCC.Groups}
options={groupOptions}
selectedKey={watch("SelectedValue")}
onChange={(ev, option) => {
handleDropdownChange(option?.key.toString() || "", "Groups");
}}
/>
)}
{showError &&
<div style={{ marginTop: 16 }}>
<MessageBar
messageBarType={MessageBarType.error} isMultiline={true}
onDismiss={() => setShowError(false)} dismissButtonAriaLabel="Close">
{errorMessage}
</MessageBar>
</div>
}
</MessageBox>
);
};

0 comments on commit 6a2e39c

Please sign in to comment.