fix: add isCreator role by default when creating new role
This commit is contained in:
@@ -25,7 +25,6 @@ function PermissionSettings(props) {
|
||||
subject,
|
||||
actions,
|
||||
conditions,
|
||||
defaultChecked,
|
||||
} = props;
|
||||
const formatMessage = useFormatMessage();
|
||||
const { getValues, resetField } = useFormContext();
|
||||
@@ -106,7 +105,6 @@ function PermissionSettings(props) {
|
||||
dataTest={`${
|
||||
condition.key
|
||||
}-${action.key.toLowerCase()}-checkbox`}
|
||||
defaultValue={defaultChecked}
|
||||
disabled={
|
||||
getValues(
|
||||
`${fieldPrefix}.${action.key}.value`,
|
||||
@@ -144,7 +142,6 @@ PermissionSettings.propTypes = {
|
||||
fieldPrefix: PropTypes.string.isRequired,
|
||||
subject: PropTypes.string.isRequired,
|
||||
open: PropTypes.bool,
|
||||
defaultChecked: PropTypes.bool,
|
||||
actions: PropTypes.arrayOf(
|
||||
PropTypes.shape({
|
||||
label: PropTypes.string,
|
||||
|
@@ -17,11 +17,7 @@ import usePermissionCatalog from 'hooks/usePermissionCatalog.ee';
|
||||
import PermissionSettings from './PermissionSettings.ee';
|
||||
import PermissionCatalogFieldLoader from './PermissionCatalogFieldLoader';
|
||||
|
||||
const PermissionCatalogField = ({
|
||||
name = 'permissions',
|
||||
disabled = false,
|
||||
defaultChecked = false,
|
||||
}) => {
|
||||
const PermissionCatalogField = ({ name = 'permissions', disabled = false }) => {
|
||||
const { data, isLoading: isPermissionCatalogLoading } =
|
||||
usePermissionCatalog();
|
||||
const permissionCatalog = data?.data;
|
||||
@@ -100,7 +96,6 @@ const PermissionCatalogField = ({
|
||||
subject={subject.key}
|
||||
actions={permissionCatalog?.actions}
|
||||
conditions={permissionCatalog?.conditions}
|
||||
defaultChecked={defaultChecked}
|
||||
/>
|
||||
</Stack>
|
||||
</TableCell>
|
||||
@@ -114,7 +109,6 @@ const PermissionCatalogField = ({
|
||||
PermissionCatalogField.propTypes = {
|
||||
name: PropTypes.string,
|
||||
disabled: PropTypes.bool,
|
||||
defaultChecked: PropTypes.bool,
|
||||
};
|
||||
|
||||
export default PermissionCatalogField;
|
||||
|
@@ -45,3 +45,36 @@ export function getPermissions(computedPermissions) {
|
||||
[],
|
||||
);
|
||||
}
|
||||
|
||||
export const getComputedPermissionsDefaultValues = (
|
||||
data,
|
||||
conditionsInitialValues,
|
||||
) => {
|
||||
if (!data) return {};
|
||||
|
||||
const conditions = {};
|
||||
data.conditions.forEach((condition) => {
|
||||
conditions[condition.key] =
|
||||
conditionsInitialValues?.[condition.key] || false;
|
||||
});
|
||||
|
||||
const result = {};
|
||||
|
||||
data.subjects.forEach((subject) => {
|
||||
const subjectKey = subject.key;
|
||||
result[subjectKey] = {};
|
||||
|
||||
data.actions.forEach((action) => {
|
||||
const actionKey = action.key;
|
||||
|
||||
if (action.subjects.includes(subjectKey)) {
|
||||
result[subjectKey][actionKey] = {
|
||||
value: false,
|
||||
conditions: { ...conditions },
|
||||
};
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return result;
|
||||
};
|
||||
|
@@ -11,9 +11,13 @@ import Form from 'components/Form';
|
||||
import PageTitle from 'components/PageTitle';
|
||||
import TextField from 'components/TextField';
|
||||
import * as URLS from 'config/urls';
|
||||
import { getPermissions } from 'helpers/computePermissions.ee';
|
||||
import {
|
||||
getComputedPermissionsDefaultValues,
|
||||
getPermissions,
|
||||
} from 'helpers/computePermissions.ee';
|
||||
import useFormatMessage from 'hooks/useFormatMessage';
|
||||
import useAdminCreateRole from 'hooks/useAdminCreateRole';
|
||||
import usePermissionCatalog from 'hooks/usePermissionCatalog.ee';
|
||||
|
||||
export default function CreateRole() {
|
||||
const navigate = useNavigate();
|
||||
@@ -21,6 +25,21 @@ export default function CreateRole() {
|
||||
const enqueueSnackbar = useEnqueueSnackbar();
|
||||
const { mutateAsync: createRole, isPending: isCreateRolePending } =
|
||||
useAdminCreateRole();
|
||||
const { data: permissionCatalogData } = usePermissionCatalog();
|
||||
|
||||
const defaultValues = React.useMemo(
|
||||
() => ({
|
||||
name: '',
|
||||
description: '',
|
||||
computedPermissions: getComputedPermissionsDefaultValues(
|
||||
permissionCatalogData?.data,
|
||||
{
|
||||
isCreator: true,
|
||||
},
|
||||
),
|
||||
}),
|
||||
[permissionCatalogData],
|
||||
);
|
||||
|
||||
const handleRoleCreation = async (roleData) => {
|
||||
try {
|
||||
@@ -64,7 +83,7 @@ export default function CreateRole() {
|
||||
</Grid>
|
||||
|
||||
<Grid item xs={12} justifyContent="flex-end" sx={{ pt: 5 }}>
|
||||
<Form onSubmit={handleRoleCreation}>
|
||||
<Form onSubmit={handleRoleCreation} defaultValues={defaultValues}>
|
||||
<Stack direction="column" gap={2}>
|
||||
<TextField
|
||||
required={true}
|
||||
@@ -81,10 +100,7 @@ export default function CreateRole() {
|
||||
data-test="description-input"
|
||||
/>
|
||||
|
||||
<PermissionCatalogField
|
||||
name="computedPermissions"
|
||||
defaultChecked={true}
|
||||
/>
|
||||
<PermissionCatalogField name="computedPermissions" />
|
||||
|
||||
<LoadingButton
|
||||
type="submit"
|
||||
|
@@ -5,6 +5,7 @@ import Stack from '@mui/material/Stack';
|
||||
import useEnqueueSnackbar from 'hooks/useEnqueueSnackbar';
|
||||
import * as React from 'react';
|
||||
import { useNavigate, useParams } from 'react-router-dom';
|
||||
import { merge } from 'lodash';
|
||||
|
||||
import Container from 'components/Container';
|
||||
import Form from 'components/Form';
|
||||
@@ -13,20 +14,23 @@ import PermissionCatalogField from 'components/PermissionCatalogField/index.ee';
|
||||
import TextField from 'components/TextField';
|
||||
import * as URLS from 'config/urls';
|
||||
import {
|
||||
getComputedPermissionsDefaultValues,
|
||||
getPermissions,
|
||||
getRoleWithComputedPermissions,
|
||||
} from 'helpers/computePermissions.ee';
|
||||
import useFormatMessage from 'hooks/useFormatMessage';
|
||||
import useAdminUpdateRole from 'hooks/useAdminUpdateRole';
|
||||
import useRole from 'hooks/useRole.ee';
|
||||
import usePermissionCatalog from 'hooks/usePermissionCatalog.ee';
|
||||
|
||||
export default function EditRole() {
|
||||
const formatMessage = useFormatMessage();
|
||||
const navigate = useNavigate();
|
||||
const { roleId } = useParams();
|
||||
const { data, loading: isRoleLoading } = useRole({ roleId });
|
||||
const { data, isLoading: isRoleLoading } = useRole({ roleId });
|
||||
const { mutateAsync: updateRole, isPending: isUpdateRolePending } =
|
||||
useAdminUpdateRole(roleId);
|
||||
const { data: permissionCatalogData } = usePermissionCatalog();
|
||||
const role = data?.data;
|
||||
const enqueueSnackbar = useEnqueueSnackbar();
|
||||
|
||||
@@ -54,6 +58,23 @@ export default function EditRole() {
|
||||
|
||||
const roleWithComputedPermissions = getRoleWithComputedPermissions(role);
|
||||
|
||||
const computedPermissionsDefaultValues = React.useMemo(
|
||||
() => getComputedPermissionsDefaultValues(permissionCatalogData?.data),
|
||||
[permissionCatalogData],
|
||||
);
|
||||
|
||||
const defaultValues = React.useMemo(
|
||||
() => ({
|
||||
...roleWithComputedPermissions,
|
||||
computedPermissions: merge(
|
||||
{},
|
||||
computedPermissionsDefaultValues,
|
||||
roleWithComputedPermissions.computedPermissions,
|
||||
),
|
||||
}),
|
||||
[roleWithComputedPermissions, computedPermissionsDefaultValues],
|
||||
);
|
||||
|
||||
return (
|
||||
<Container sx={{ py: 3, display: 'flex', justifyContent: 'center' }}>
|
||||
<Grid container item xs={12} sm={10} md={9}>
|
||||
@@ -64,10 +85,7 @@ export default function EditRole() {
|
||||
</Grid>
|
||||
|
||||
<Grid item xs={12} justifyContent="flex-end" sx={{ pt: 5 }}>
|
||||
<Form
|
||||
defaultValues={roleWithComputedPermissions}
|
||||
onSubmit={handleRoleUpdate}
|
||||
>
|
||||
<Form defaultValues={defaultValues} onSubmit={handleRoleUpdate}>
|
||||
<Stack direction="column" gap={2}>
|
||||
{isRoleLoading && (
|
||||
<>
|
||||
|
Reference in New Issue
Block a user