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