8 } from '@angular/core';
9 import { FormControl, Validators } from '@angular/forms';
10 import { OperatorFunction, Observable, of } from 'rxjs';
11 import { debounceTime, distinctUntilChanged, switchMap, catchError } from 'rxjs/operators';
12 import { CephfsService } from '~/app/shared/api/cephfs.service';
13 import { DirectoryStoreService } from '~/app/shared/api/directory-store.service';
14 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
15 import { Icons } from '~/app/shared/enum/icons.enum';
16 import { CdForm } from '~/app/shared/forms/cd-form';
17 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
18 import { FinishedTask } from '~/app/shared/models/finished-task';
19 import { ModalCdsService } from '~/app/shared/services/modal-cds.service';
20 import { TaskWrapperService } from '~/app/shared/services/task-wrapper.service';
21 import { PERMISSION_NAMES } from '~/app/shared/models/cephfs.model';
23 const DEBOUNCE_TIMER = 300;
26 selector: 'cd-cephfs-auth-modal',
27 templateUrl: './cephfs-auth-modal.component.html',
28 styleUrls: ['./cephfs-auth-modal.component.scss']
30 export class CephfsAuthModalComponent extends CdForm implements OnInit, AfterViewInit {
31 subvolumeGroup: string;
33 isDefaultSubvolumeGroup = false;
39 readonly defaultdir: string = '/';
43 name: PERMISSION_NAMES.READ,
44 description: $localize`Read permission is the minimum givable access`
47 name: PERMISSION_NAMES.WRITE,
48 description: $localize`Permission to set layouts or quotas, write access needed`
51 name: PERMISSION_NAMES.QUOTA,
52 description: $localize`Permission to set layouts or quotas, write access needed`
55 name: PERMISSION_NAMES.SNAPSHOT,
56 description: $localize`Permission to create or delete snapshots, write access needed`
59 name: PERMISSION_NAMES.ROOTSQUASH,
60 description: $localize`Safety measure to prevent scenarios such as accidental sudo rm -rf /path`
65 private actionLabels: ActionLabelsI18n,
66 public directoryStore: DirectoryStoreService,
67 private cephfsService: CephfsService,
68 private taskWrapper: TaskWrapperService,
69 private modalService: ModalCdsService,
70 private changeDetectorRef: ChangeDetectorRef,
72 @Optional() @Inject('fsName') public fsName: string,
73 @Optional() @Inject('id') public id: number
76 this.action = this.actionLabels.UPDATE;
77 this.resource = $localize`access`;
80 ngAfterViewInit(): void {
81 this.changeDetectorRef.detectChanges();
85 this.directoryStore.loadDirectories(this.id, '/', 3);
91 this.form = new CdFormGroup({
92 fsName: new FormControl(
93 { value: this.fsName, disabled: true },
95 validators: [Validators.required]
98 directory: new FormControl(
99 { value: this.defaultdir, disabled: false },
102 validators: [Validators.required]
105 userId: new FormControl(undefined, {
106 validators: [Validators.required]
108 read: new FormControl(
109 { value: true, disabled: true },
111 validators: [Validators.required]
114 write: new FormControl(undefined),
115 snapshot: new FormControl({ value: false, disabled: true }),
116 quota: new FormControl({ value: false, disabled: true }),
117 rootSquash: new FormControl(undefined)
121 search: OperatorFunction<string, readonly string[]> = (input: Observable<string>) =>
123 debounceTime(DEBOUNCE_TIMER),
124 distinctUntilChanged(),
126 this.directoryStore.search(term, this.id).pipe(
135 const clientId: number = this.form.getValue('userId');
136 const caps: string[] = [this.form.getValue('directory'), this.transformPermissions()];
137 const rootSquash: boolean = this.form.getValue(PERMISSION_NAMES.ROOTSQUASH);
139 .wrapTaskAroundCall({
140 task: new FinishedTask('cephfs/auth', {
143 call: this.cephfsService.setAuth(this.fsName, clientId, caps, rootSquash)
146 error: () => this.form.setErrors({ cdSubmitButton: true }),
148 this.modalService.dismissAll();
153 transformPermissions(): string {
154 const write = this.form.getValue(PERMISSION_NAMES.WRITE);
155 const snapshot = this.form.getValue(PERMISSION_NAMES.SNAPSHOT);
156 const quota = this.form.getValue(PERMISSION_NAMES.QUOTA);
157 return `r${write ? 'w' : ''}${quota ? 'p' : ''}${snapshot ? 's' : ''}`;
160 toggleFormControl(_event?: boolean, permisson?: string) {
161 const snapshot = this.form.get(PERMISSION_NAMES.SNAPSHOT);
162 const quota = this.form.get(PERMISSION_NAMES.QUOTA);
163 if (_event && permisson == PERMISSION_NAMES.WRITE) {
164 snapshot.disabled ? snapshot.enable() : snapshot.disable();
165 quota.disabled ? quota.enable() : quota.disable();
166 } else if (!_event && permisson == PERMISSION_NAMES.WRITE) {
167 snapshot.setValue(false);
168 quota.setValue(false);