Browse Source

feat(admin-ui): Enable overriding of default dashboard widget permissions

Fixes #1832
Michael Bromley 3 years ago
parent
commit
c946b61b27

+ 28 - 0
docs/content/plugins/extending-the-admin-ui/dashboard-widgets/index.md

@@ -138,3 +138,31 @@ export class MySharedUiExtensionModule {}
 ```
 
 This defines the order of widgets with their default widths. The actual layout in terms of rows and columns will be calculated at run-time based on what will fit on each row.
+
+## Overriding default widgets
+
+The Admin UI comes with a set of default widgets, such as the order summary and latest orders widgets (they can be found in [the default-widgets.ts file](https://github.com/vendure-ecommerce/vendure/blob/master/packages/admin-ui/src/lib/dashboard/src/default-widgets.ts)).
+
+Sometimes you may wish to alter the permissions settings of the default widgets to better control which of your Administrators is able to access it.
+
+For example, the "order summary" widget has a default permission requirement of "ReadOrder". If you want to limit the availability to e.g. the SuperAdmin role, you can do so
+by overriding the definition like this:
+
+```TypeScript
+import { NgModule } from '@angular/core';
+import { registerDashboardWidget } from '@vendure/admin-ui/core';
+import { OrderSummaryWidgetComponent } from '@vendure/admin-ui/dashboard';
+
+@NgModule({
+  imports: [],
+  declarations: [],
+  providers: [
+    registerDashboardWidget('orderSummary', {
+      title: 'dashboard.orders-summary',
+      loadComponent: () => OrderSummaryWidgetComponent,
+      requiresPermissions: ['SuperAdmin'],
+    }),
+  ],
+})
+export class MySharedUiExtensionModule {}
+```

+ 0 - 3
packages/admin-ui/src/lib/core/src/providers/dashboard-widget/dashboard-widget.service.ts

@@ -44,9 +44,6 @@ export class DashboardWidgetService {
     }
 
     getWidgetById(id: string) {
-        if (!this.registry.has(id)) {
-            throw new Error(`No widget was found with the id "${id}"`);
-        }
         return this.registry.get(id);
     }
 

+ 5 - 3
packages/admin-ui/src/lib/dashboard/src/dashboard.module.ts

@@ -13,9 +13,11 @@ import { DEFAULT_DASHBOARD_WIDGET_LAYOUT, DEFAULT_WIDGETS } from './default-widg
 })
 export class DashboardModule {
     constructor(dashboardWidgetService: DashboardWidgetService) {
-        Object.entries(DEFAULT_WIDGETS).map(([id, config]) =>
-            dashboardWidgetService.registerWidget(id, config),
-        );
+        Object.entries(DEFAULT_WIDGETS).map(([id, config]) => {
+            if (!dashboardWidgetService.getWidgetById(id)) {
+                dashboardWidgetService.registerWidget(id, config);
+            }
+        });
         if (dashboardWidgetService.getDefaultLayout().length === 0) {
             dashboardWidgetService.setDefaultLayout(DEFAULT_DASHBOARD_WIDGET_LAYOUT);
         }