Skip to content

Commit

Permalink
fix(admin-ui): Do not load pending search index updates if permission…
Browse files Browse the repository at this point in the history
…s are insufficient (#2460)

Fixes #2456
  • Loading branch information
jnugh authored Oct 17, 2023
1 parent 2f7a8d5 commit 08ad982
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/admin-ui/src/lib/core/src/core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { LocalStorageService } from './providers/local-storage/local-storage.ser
import { NotificationService } from './providers/notification/notification.service';
import { registerDefaultFormInputs } from './shared/dynamic-form-inputs/default-form-inputs';
import { SharedModule } from './shared/shared.module';
import { Permission } from './public_api';

@NgModule({
imports: [
Expand Down Expand Up @@ -107,6 +108,7 @@ export class CoreModule {
private initAlerts() {
this.alertsService.configureAlert({
id: 'pending-search-index-updates',
requiredPermissions: [Permission.ReadCatalog, Permission.ReadProduct],
check: () =>
this.dataService.product
.getPendingSearchIndexUpdates()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { Injectable } from '@angular/core';
import { notNullOrUndefined } from '@vendure/common/lib/shared-utils';
import { BehaviorSubject, combineLatest, interval, isObservable, Observable, Subject, switchMap } from 'rxjs';
import { map, mapTo, startWith, take } from 'rxjs/operators';
import {
BehaviorSubject,
combineLatest,
interval,
isObservable,
Observable,
of,
Subject,
switchMap,
} from 'rxjs';
import { filter, first, map, mapTo, startWith, take } from 'rxjs/operators';
import { DataService, Permission } from '../../public_api';

export interface AlertConfig<T = any> {
id: string;
Expand All @@ -10,6 +20,7 @@ export interface AlertConfig<T = any> {
isAlert: (value: T) => boolean;
action: (data: T) => void;
label: (data: T) => { text: string; translationVars?: { [key: string]: string | number } };
requiredPermissions?: Permission[];
}

export interface ActiveAlert {
Expand Down Expand Up @@ -74,7 +85,7 @@ export class AlertsService {
private alertsMap = new Map<string, Alert<any>>();
private configUpdated = new Subject<void>();

constructor() {
constructor(private dataService: DataService) {
const alerts$ = this.configUpdated.pipe(
mapTo([...this.alertsMap.values()]),
startWith([...this.alertsMap.values()]),
Expand All @@ -91,8 +102,26 @@ export class AlertsService {
}

configureAlert<T>(config: AlertConfig<T>) {
this.alertsMap.set(config.id, new Alert(config));
this.configUpdated.next();
this.hasSufficientPermissions(config.requiredPermissions)
.pipe(first())
.subscribe(hasSufficientPermissions => {
if (hasSufficientPermissions) {
this.alertsMap.set(config.id, new Alert(config));
this.configUpdated.next();
}
});
}

hasSufficientPermissions(permissions?: Permission[]) {
if (!permissions || permissions.length === 0) {
return of(true);
}
return this.dataService.client.userStatus().stream$.pipe(
filter(({ userStatus }) => userStatus.isLoggedIn),
map(({ userStatus }) =>
permissions.some(permission => userStatus.permissions.includes(permission)),
),
);
}

refresh(id?: string) {
Expand Down

0 comments on commit 08ad982

Please sign in to comment.