summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/form-input-disable.directive.ts
blob: 3e3f83bc56e3f6e63fd81e3793111080ae92b677 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { AfterViewInit, Directive, ElementRef, Optional } from '@angular/core';

import { Permissions } from '../models/permissions';
import { AuthStorageService } from '../services/auth-storage.service';
import { FormScopeDirective } from './form-scope.directive';

@Directive({
  selector:
    'input:not([cdNoFormInputDisable]), select:not([cdNoFormInputDisable]), button:not([cdNoFormInputDisable]), [cdFormInputDisable]'
})
export class FormInputDisableDirective implements AfterViewInit {
  permissions: Permissions;

  constructor(
    @Optional() private formScope: FormScopeDirective,
    private authStorageService: AuthStorageService,
    private elementRef: ElementRef
  ) {}

  ngAfterViewInit() {
    this.permissions = this.authStorageService.getPermissions();
    const service_name = this.formScope?.cdFormScope;
    if (service_name && !this.permissions?.[service_name]?.update) {
      this.elementRef.nativeElement.disabled = true;
    }
  }
}