All files / src/app/ceph/rgw/rgw-user-capability-modal rgw-user-capability-modal.component.ts

64.29% Statements 27/42
54.55% Branches 12/22
33.33% Functions 3/9
64.86% Lines 24/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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