1 import { Component, EventEmitter, Output } from '@angular/core';
2 import { Validators } from '@angular/forms';
4 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
5 import * as _ from 'lodash';
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';
14 selector: 'cd-rgw-user-capability-modal',
15 templateUrl: './rgw-user-capability-modal.component.html',
16 styleUrls: ['./rgw-user-capability-modal.component.scss']
18 export class RgwUserCapabilityModalComponent {
20 * The event that is triggered when the 'Add' or 'Update' button
24 submitAction = new EventEmitter();
26 formGroup: CdFormGroup;
33 private formBuilder: CdFormBuilder,
34 public activeModal: NgbActiveModal,
35 public actionLabels: ActionLabelsI18n
37 this.resource = $localize`capability`;
42 this.formGroup = this.formBuilder.group({
43 type: [null, [Validators.required]],
44 perm: [null, [Validators.required]]
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
52 * @param {boolean} viewing
54 setEditing(editing: boolean = true) {
55 this.editing = editing;
56 this.action = this.editing ? this.actionLabels.EDIT : this.actionLabels.ADD;
60 * Set the values displayed in the dialog.
62 setValues(type: string, perm: string) {
63 this.formGroup.setValue({
70 * Set the current capabilities of the user.
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);
80 RgwUserCapabilities.getAll().forEach((type) => {
81 if (_.indexOf(usedTypes, type) === -1) {
82 this.types.push(type);
88 const capability: RgwUserCapability = this.formGroup.value;
89 this.submitAction.emit(capability);
90 this.activeModal.close();