]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
3b1ce3fc81c37a31855840e5696915e1534cf08e
[ceph-ci.git] /
1 import { Component, EventEmitter, Output } from '@angular/core';
2 import { Validators } from '@angular/forms';
3
4 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
5 import * as _ from 'lodash';
6
7 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
8 import { CdFormBuilder } from '../../../shared/forms/cd-form-builder';
9 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
10 import { RgwUserCapabilities } from '../models/rgw-user-capabilities';
11 import { RgwUserCapability } from '../models/rgw-user-capability';
12
13 @Component({
14   selector: 'cd-rgw-user-capability-modal',
15   templateUrl: './rgw-user-capability-modal.component.html',
16   styleUrls: ['./rgw-user-capability-modal.component.scss']
17 })
18 export class RgwUserCapabilityModalComponent {
19   /**
20    * The event that is triggered when the 'Add' or 'Update' button
21    * has been pressed.
22    */
23   @Output()
24   submitAction = new EventEmitter();
25
26   formGroup: CdFormGroup;
27   editing = true;
28   types: string[] = [];
29   resource: string;
30   action: string;
31
32   constructor(
33     private formBuilder: CdFormBuilder,
34     public activeModal: NgbActiveModal,
35     public actionLabels: ActionLabelsI18n
36   ) {
37     this.resource = $localize`capability`;
38     this.createForm();
39   }
40
41   createForm() {
42     this.formGroup = this.formBuilder.group({
43       type: [null, [Validators.required]],
44       perm: [null, [Validators.required]]
45     });
46   }
47
48   /**
49    * Set the 'editing' flag. If set to TRUE, the modal dialog is in 'Edit' mode,
50    * otherwise in 'Add' mode. According to the mode the dialog and its controls
51    * behave different.
52    * @param {boolean} viewing
53    */
54   setEditing(editing: boolean = true) {
55     this.editing = editing;
56     this.action = this.editing ? this.actionLabels.EDIT : this.actionLabels.ADD;
57   }
58
59   /**
60    * Set the values displayed in the dialog.
61    */
62   setValues(type: string, perm: string) {
63     this.formGroup.setValue({
64       type: type,
65       perm: perm
66     });
67   }
68
69   /**
70    * Set the current capabilities of the user.
71    */
72   setCapabilities(capabilities: RgwUserCapability[]) {
73     // Parse the configured capabilities to get a list of types that
74     // should be displayed.
75     const usedTypes: string[] = [];
76     capabilities.forEach((capability) => {
77       usedTypes.push(capability.type);
78     });
79     this.types = [];
80     RgwUserCapabilities.getAll().forEach((type) => {
81       if (_.indexOf(usedTypes, type) === -1) {
82         this.types.push(type);
83       }
84     });
85   }
86
87   onSubmit() {
88     const capability: RgwUserCapability = this.formGroup.value;
89     this.submitAction.emit(capability);
90     this.activeModal.close();
91   }
92 }