1 import { Component, EventEmitter, Output } from '@angular/core';
2 import { Validators } from '@angular/forms';
4 import * as _ from 'lodash';
5 import { BsModalRef } from 'ngx-bootstrap/modal';
7 import { I18n } from '@ngx-translate/i18n-polyfill';
8 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
9 import { CdFormBuilder } from '../../../shared/forms/cd-form-builder';
10 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
11 import { RgwUserCapabilities } from '../models/rgw-user-capabilities';
12 import { RgwUserCapability } from '../models/rgw-user-capability';
15 selector: 'cd-rgw-user-capability-modal',
16 templateUrl: './rgw-user-capability-modal.component.html',
17 styleUrls: ['./rgw-user-capability-modal.component.scss']
19 export class RgwUserCapabilityModalComponent {
21 * The event that is triggered when the 'Add' or 'Update' button
25 submitAction = new EventEmitter();
27 formGroup: CdFormGroup;
34 private formBuilder: CdFormBuilder,
35 public bsModalRef: BsModalRef,
37 public actionLabels: ActionLabelsI18n
39 this.resource = this.i18n('capability');
44 this.formGroup = this.formBuilder.group({
45 type: [null, [Validators.required]],
46 perm: [null, [Validators.required]]
51 * Set the 'editing' flag. If set to TRUE, the modal dialog is in 'Edit' mode,
52 * otherwise in 'Add' mode. According to the mode the dialog and its controls
54 * @param {boolean} viewing
56 setEditing(editing: boolean = true) {
57 this.editing = editing;
58 this.action = this.editing ? this.actionLabels.EDIT : this.actionLabels.ADD;
62 * Set the values displayed in the dialog.
64 setValues(type: string, perm: string) {
65 this.formGroup.setValue({
72 * Set the current capabilities of the user.
74 setCapabilities(capabilities: RgwUserCapability[]) {
75 // Parse the configured capabilities to get a list of types that
76 // should be displayed.
77 const usedTypes: string[] = [];
78 capabilities.forEach((capability) => {
79 usedTypes.push(capability.type);
82 RgwUserCapabilities.getAll().forEach((type) => {
83 if (_.indexOf(usedTypes, type) === -1) {
84 this.types.push(type);
90 const capability: RgwUserCapability = this.formGroup.value;
91 this.submitAction.emit(capability);
92 this.bsModalRef.hide();