mirror of
https://github.com/inventree/InvenTree.git
synced 2025-08-02 10:01:32 +00:00
Check user permissions for dashboard items (#10047)
Ref: https://github.com/inventree/InvenTree/discussions/10046
This commit is contained in:
@@ -4,6 +4,7 @@ import { IconX } from '@tabler/icons-react';
|
|||||||
|
|
||||||
import { Boundary } from '../Boundary';
|
import { Boundary } from '../Boundary';
|
||||||
|
|
||||||
|
import type { ModelType } from '@lib/index';
|
||||||
import type { JSX } from 'react';
|
import type { JSX } from 'react';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -20,6 +21,7 @@ export interface DashboardWidgetProps {
|
|||||||
enabled?: boolean;
|
enabled?: boolean;
|
||||||
minWidth?: number;
|
minWidth?: number;
|
||||||
minHeight?: number;
|
minHeight?: number;
|
||||||
|
modelType?: ModelType;
|
||||||
render: () => JSX.Element;
|
render: () => JSX.Element;
|
||||||
visible?: () => boolean;
|
visible?: () => boolean;
|
||||||
}
|
}
|
||||||
|
@@ -2,6 +2,7 @@ import { t } from '@lingui/core/macro';
|
|||||||
|
|
||||||
import { ModelType } from '@lib/enums/ModelType';
|
import { ModelType } from '@lib/enums/ModelType';
|
||||||
import { useGlobalSettingsState } from '../../states/SettingsStates';
|
import { useGlobalSettingsState } from '../../states/SettingsStates';
|
||||||
|
import { useUserState } from '../../states/UserState';
|
||||||
import type { DashboardWidgetProps } from './DashboardWidget';
|
import type { DashboardWidgetProps } from './DashboardWidget';
|
||||||
import ColorToggleDashboardWidget from './widgets/ColorToggleWidget';
|
import ColorToggleDashboardWidget from './widgets/ColorToggleWidget';
|
||||||
import GetStartedWidget from './widgets/GetStartedWidget';
|
import GetStartedWidget from './widgets/GetStartedWidget';
|
||||||
@@ -14,9 +15,10 @@ import QueryCountDashboardWidget from './widgets/QueryCountDashboardWidget';
|
|||||||
* @returns A list of built-in dashboard widgets which display the number of results for a particular query
|
* @returns A list of built-in dashboard widgets which display the number of results for a particular query
|
||||||
*/
|
*/
|
||||||
export function BuiltinQueryCountWidgets(): DashboardWidgetProps[] {
|
export function BuiltinQueryCountWidgets(): DashboardWidgetProps[] {
|
||||||
|
const user = useUserState.getState();
|
||||||
const globalSettings = useGlobalSettingsState.getState();
|
const globalSettings = useGlobalSettingsState.getState();
|
||||||
|
|
||||||
return [
|
const widgets: DashboardWidgetProps[] = [
|
||||||
QueryCountDashboardWidget({
|
QueryCountDashboardWidget({
|
||||||
label: 'sub-prt',
|
label: 'sub-prt',
|
||||||
title: t`Subscribed Parts`,
|
title: t`Subscribed Parts`,
|
||||||
@@ -149,6 +151,15 @@ export function BuiltinQueryCountWidgets(): DashboardWidgetProps[] {
|
|||||||
params: { assigned_to_me: true, outstanding: true }
|
params: { assigned_to_me: true, outstanding: true }
|
||||||
})
|
})
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// Filter widgets based on user permissions (if a modelType is defined)
|
||||||
|
return widgets.filter((widget: DashboardWidgetProps) => {
|
||||||
|
if (widget.modelType) {
|
||||||
|
return user.hasViewPermission(widget.modelType);
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function BuiltinGettingStartedWidgets(): DashboardWidgetProps[] {
|
export function BuiltinGettingStartedWidgets(): DashboardWidgetProps[] {
|
||||||
|
@@ -140,6 +140,7 @@ export default function QueryCountDashboardWidget({
|
|||||||
title: title,
|
title: title,
|
||||||
description: description,
|
description: description,
|
||||||
enabled: enabled,
|
enabled: enabled,
|
||||||
|
modelType: modelType,
|
||||||
minWidth: 2,
|
minWidth: 2,
|
||||||
minHeight: 1,
|
minHeight: 1,
|
||||||
render: () => (
|
render: () => (
|
||||||
|
Reference in New Issue
Block a user