mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-22 15:50:32 +00:00
feat: add default role permissions in new mode.
This commit is contained in:
@@ -2,6 +2,7 @@ import React from 'react';
|
|||||||
import { useHistory } from 'react-router-dom';
|
import { useHistory } from 'react-router-dom';
|
||||||
import intl from 'react-intl-universal';
|
import intl from 'react-intl-universal';
|
||||||
import { Formik } from 'formik';
|
import { Formik } from 'formik';
|
||||||
|
import { isEmpty } from 'lodash';
|
||||||
|
|
||||||
import 'style/pages/Preferences/Roles/Form.scss';
|
import 'style/pages/Preferences/Roles/Form.scss';
|
||||||
|
|
||||||
@@ -12,7 +13,11 @@ import { AppToaster, FormattedMessage as T } from 'components';
|
|||||||
import { CreateRolesFormSchema, EditRolesFormSchema } from './RolesForm.schema';
|
import { CreateRolesFormSchema, EditRolesFormSchema } from './RolesForm.schema';
|
||||||
|
|
||||||
import { useRolesFormContext } from './RolesFormProvider';
|
import { useRolesFormContext } from './RolesFormProvider';
|
||||||
import { transformToArray } from './utils';
|
import {
|
||||||
|
getNewRoleInitialValues,
|
||||||
|
transformToArray,
|
||||||
|
transformToObject,
|
||||||
|
} from './utils';
|
||||||
|
|
||||||
import RolesFormContent from './RolesFormContent';
|
import RolesFormContent from './RolesFormContent';
|
||||||
import withDashboardActions from 'containers/Dashboard/withDashboardActions';
|
import withDashboardActions from 'containers/Dashboard/withDashboardActions';
|
||||||
@@ -35,24 +40,28 @@ function RolesForm({
|
|||||||
// History context.
|
// History context.
|
||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
|
|
||||||
|
// Role form context.
|
||||||
const {
|
const {
|
||||||
isNewMode,
|
isNewMode,
|
||||||
createRolePermissionMutate,
|
createRolePermissionMutate,
|
||||||
editRolePermissionMutate,
|
editRolePermissionMutate,
|
||||||
permissionSchema,
|
permissionsSchema,
|
||||||
|
role,
|
||||||
roleId,
|
roleId,
|
||||||
} = useRolesFormContext();
|
} = useRolesFormContext();
|
||||||
|
|
||||||
// Initial values.
|
// Initial values.
|
||||||
const initialValues = {
|
const initialValues = {
|
||||||
...defaultValues,
|
...defaultValues,
|
||||||
...transformToForm(permissionSchema, defaultValues),
|
...(!isEmpty(role)
|
||||||
|
? transformToForm(transformToObject(role), defaultValues)
|
||||||
|
: getNewRoleInitialValues(permissionsSchema)),
|
||||||
};
|
};
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
changePreferencesPageTitle(<T id={'roles.label'} />);
|
changePreferencesPageTitle(<T id={'roles.label'} />);
|
||||||
}, [changePreferencesPageTitle]);
|
}, [changePreferencesPageTitle]);
|
||||||
|
|
||||||
|
// Handle the form submit.
|
||||||
const handleFormSubmit = (values, { setSubmitting }) => {
|
const handleFormSubmit = (values, { setSubmitting }) => {
|
||||||
const permission = transformToArray(values);
|
const permission = transformToArray(values);
|
||||||
const form = {
|
const form = {
|
||||||
|
|||||||
@@ -25,27 +25,27 @@ function RolesFormProvider({ roleId, ...props }) {
|
|||||||
const { mutateAsync: editRolePermissionMutate } =
|
const { mutateAsync: editRolePermissionMutate } =
|
||||||
useEditRolePermissionSchema();
|
useEditRolePermissionSchema();
|
||||||
|
|
||||||
|
// Retrieve permissions schema.
|
||||||
const {
|
const {
|
||||||
data: permissionsSchema,
|
data: permissionsSchema,
|
||||||
isLoading: isPermissionsSchemaLoading,
|
isLoading: isPermissionsSchemaLoading,
|
||||||
isFetching: isPermissionsSchemaFetching,
|
isFetching: isPermissionsSchemaFetching,
|
||||||
} = usePermissionsSchema();
|
} = usePermissionsSchema();
|
||||||
|
|
||||||
const { data: permission, isLoading: isPermissionLoading } =
|
const { data: role, isLoading: isPermissionLoading } =
|
||||||
useRolePermission(roleId, {
|
useRolePermission(roleId, {
|
||||||
enabled: !!roleId,
|
enabled: !!roleId,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Detarmines whether the new or edit mode.
|
||||||
const isNewMode = !roleId;
|
const isNewMode = !roleId;
|
||||||
|
|
||||||
const permissionSchema = transformToObject(permission);
|
|
||||||
|
|
||||||
// Provider state.
|
// Provider state.
|
||||||
const provider = {
|
const provider = {
|
||||||
isNewMode,
|
isNewMode,
|
||||||
roleId,
|
roleId,
|
||||||
|
role,
|
||||||
permissionsSchema,
|
permissionsSchema,
|
||||||
permissionSchema,
|
|
||||||
isPermissionsSchemaLoading,
|
isPermissionsSchemaLoading,
|
||||||
isPermissionsSchemaFetching,
|
isPermissionsSchemaFetching,
|
||||||
createRolePermissionMutate,
|
createRolePermissionMutate,
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
import { isEmpty } from 'lodash';
|
|
||||||
|
|
||||||
export const transformToArray = ({ permissions }) => {
|
export const transformToArray = ({ permissions }) => {
|
||||||
return Object.keys(permissions).map((index) => {
|
return Object.keys(permissions).map((index) => {
|
||||||
const [value, key] = index.split('/');
|
const [value, key] = index.split('/');
|
||||||
@@ -12,16 +10,44 @@ export const transformToArray = ({ permissions }) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const transformToObject = ({ name, description, permissions }) => {
|
export const transformPermissionsToObject = (permissions) => {
|
||||||
if (!isEmpty(permissions)) {
|
const output = {};
|
||||||
const output = {};
|
permissions.forEach((item) => {
|
||||||
permissions.forEach((item) => {
|
output[`${item.subject}/${item.ability}`] = !!item.value;
|
||||||
output[`${item.subject}/${item.ability}`] = !!item.value;
|
});
|
||||||
});
|
return output;
|
||||||
return {
|
};
|
||||||
role_name: name,
|
|
||||||
role_description: description,
|
export const transformToObject = (role) => {
|
||||||
permissions: { ...output },
|
return {
|
||||||
};
|
role_name: role.name,
|
||||||
}
|
role_description: role.description,
|
||||||
|
permissions: transformPermissionsToObject(role.permissions),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getDefaultValuesFromSchema = (schema) => {
|
||||||
|
return schema
|
||||||
|
.map((item) => {
|
||||||
|
const abilities = [
|
||||||
|
...(item.abilities || []),
|
||||||
|
...(item.extra_abilities || []),
|
||||||
|
];
|
||||||
|
return abilities
|
||||||
|
.filter((ability) => ability.default)
|
||||||
|
.map((ability) => ({
|
||||||
|
subject: item.subject,
|
||||||
|
ability: ability.key,
|
||||||
|
value: ability.default,
|
||||||
|
}));
|
||||||
|
})
|
||||||
|
.flat();
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getNewRoleInitialValues = (schema) => {
|
||||||
|
return {
|
||||||
|
permissions: transformPermissionsToObject(
|
||||||
|
getDefaultValuesFromSchema(schema),
|
||||||
|
),
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { Intent, Tag } from '@blueprintjs/core';
|
import { Intent } from '@blueprintjs/core';
|
||||||
import { useHistory } from 'react-router-dom';
|
import { useHistory } from 'react-router-dom';
|
||||||
|
|
||||||
import intl from 'react-intl-universal';
|
import intl from 'react-intl-universal';
|
||||||
|
import styled from 'styled-components';
|
||||||
|
|
||||||
import { DataTable, Choose, AppToaster } from 'components';
|
import { DataTable, AppToaster } from 'components';
|
||||||
import TableSkeletonRows from 'components/Datatable/TableSkeletonRows';
|
import TableSkeletonRows from 'components/Datatable/TableSkeletonRows';
|
||||||
|
|
||||||
import { useRolesTableColumns, ActionsMenu } from './components';
|
import { useRolesTableColumns, ActionsMenu } from './components';
|
||||||
@@ -23,10 +23,13 @@ function RolesDataTable({
|
|||||||
// History context.
|
// History context.
|
||||||
const history = useHistory();
|
const history = useHistory();
|
||||||
|
|
||||||
|
// Retrieve roles table columns
|
||||||
const columns = useRolesTableColumns();
|
const columns = useRolesTableColumns();
|
||||||
|
|
||||||
|
// Roles table context.
|
||||||
const { roles, isRolesFetching, isRolesLoading } = useRolesContext();
|
const { roles, isRolesFetching, isRolesLoading } = useRolesContext();
|
||||||
|
|
||||||
|
// handles delete the given role.
|
||||||
const handleDeleteRole = ({ id, predefined }) => {
|
const handleDeleteRole = ({ id, predefined }) => {
|
||||||
if (predefined) {
|
if (predefined) {
|
||||||
AppToaster.show({
|
AppToaster.show({
|
||||||
@@ -37,24 +40,20 @@ function RolesDataTable({
|
|||||||
openAlert('role-delete', { roleId: id });
|
openAlert('role-delete', { roleId: id });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
// Handles the edit of the given role.
|
||||||
const handleEditRole = ({ id, predefined }) => {
|
const handleEditRole = ({ id, predefined }) => {
|
||||||
if (predefined) {
|
if (predefined) {
|
||||||
{
|
AppToaster.show({
|
||||||
AppToaster.show({
|
message: intl.get('roles.error.you_cannot_edit_predefined_roles'),
|
||||||
message: intl.get('roles.error.you_cannot_edit_predefined_roles'),
|
intent: Intent.DANGER,
|
||||||
intent: Intent.DANGER,
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
{
|
history.push(`/preferences/roles/${id}`);
|
||||||
history.push(`/preferences/roles/${id}`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DataTable
|
<RolesTable
|
||||||
columns={columns}
|
columns={columns}
|
||||||
data={roles}
|
data={roles}
|
||||||
loading={isRolesLoading}
|
loading={isRolesLoading}
|
||||||
@@ -70,4 +69,10 @@ function RolesDataTable({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const RolesTable = styled(DataTable)`
|
||||||
|
.table .tr {
|
||||||
|
min-height: 42px;
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
export default compose(withAlertsActions)(RolesDataTable);
|
export default compose(withAlertsActions)(RolesDataTable);
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import intl from 'react-intl-universal';
|
|
||||||
|
|
||||||
import { RolesListProvider } from './RolesListProvider';
|
import { RolesListProvider } from './RolesListProvider';
|
||||||
import RolesDataTable from './RolesDataTable';
|
import RolesDataTable from './RolesDataTable';
|
||||||
|
|||||||
@@ -9,8 +9,7 @@ const RolesListContext = React.createContext();
|
|||||||
* Roles list provider.
|
* Roles list provider.
|
||||||
*/
|
*/
|
||||||
function RolesListProvider({ ...props }) {
|
function RolesListProvider({ ...props }) {
|
||||||
|
// Fetch roles list.
|
||||||
// fetch roles list.
|
|
||||||
const {
|
const {
|
||||||
data: roles,
|
data: roles,
|
||||||
isFetching: isRolesFetching,
|
isFetching: isRolesFetching,
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import intl from 'react-intl-universal';
|
import intl from 'react-intl-universal';
|
||||||
|
|
||||||
import { Intent, Button, Menu, MenuItem, MenuDivider } from '@blueprintjs/core';
|
import { Intent, Menu, MenuItem, MenuDivider } from '@blueprintjs/core';
|
||||||
import { safeCallback } from 'utils';
|
import { safeCallback } from 'utils';
|
||||||
import { Icon, If } from 'components';
|
import { Icon } from 'components';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Context menu of roles.
|
* Context menu of roles.
|
||||||
|
|||||||
@@ -87,9 +87,7 @@ export function useRolePermission(role_id, props, requestProps) {
|
|||||||
{ method: 'get', url: `roles/${role_id}`, ...requestProps },
|
{ method: 'get', url: `roles/${role_id}`, ...requestProps },
|
||||||
{
|
{
|
||||||
select: (res) => res.data.role,
|
select: (res) => res.data.role,
|
||||||
defaultData: {
|
defaultData: {},
|
||||||
permission: [],
|
|
||||||
},
|
|
||||||
...props,
|
...props,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user