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