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

70.97% Statements 44/62
55.56% Branches 15/27
53.85% Functions 7/13
72.73% Lines 40/55

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
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    1452x
    2x

    2x
    2x

    2x
    2x
    2x

    2x
    2x

    2x

    6x

    6x
    6x

    6x
    6x
    6x
    6x

    6x
    6x
    6x

    2x
    6x

    6x

    6x

    2x
    11x
    11x

    19x

    1x

    3x
    5x

    3x

    2x

    5x
    5x

    2x

    2x

    2x

    2x

    2x
     import { Component, EventEmitter, Output } from '@angular/core';
    import { AbstractControl, ValidationErrors, ValidatorFn, 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 { CdValidators, isEmptyInputValue } from '../../../shared/forms/cd-validators';
    import { RgwUserSubuser } from '../models/rgw-user-subuser';

    @Component({
      selector: 'cd-rgw-user-subuser-modal',
      template: require('./rgw-user-subuser-modal.component.html'),
      styles: []
    })
    export class RgwUserSubuserModalComponent {
      /**
       * The event that is triggered when the 'Add' or 'Update' button
       * has been pressed.
       */
      @Output()
      submitAction = new EventEmitter();

      formGroup: CdFormGroup;
      editing = true;
      subusers: RgwUserSubuser[] = [];
      resource: string;
      action: string;

      constructor(
        private formBuilder: CdFormBuilder,
        public bsModalRef: BsModalRef,
        private i18n: I18n,
        private actionLabels: ActionLabelsI18n
      ) {
        this.resource = this.i18n('Subuser');
        this.createForm();
        this.listenToChanges();
      }

      createForm() {
        this.formGroup = this.formBuilder.group({
          uid: [null],
          subuid: [null, [Validators.required, this.subuserValidator()]],
          perm: [null, [Validators.required]],
          // Swift key
          generate_secret: [true],
          secret_key: [null, [CdValidators.requiredIf({ generate_secret: false })]]
        });
      }

      listenToChanges() {
        // Reset the validation status of various controls, especially those that are using
        // the 'requiredIf' validator. This is necessary because the controls itself are not
        // validated again if the status of their prerequisites have been changed.
        this.formGroup.get('generate_secret').valueChanges.subscribe(() => {
          ['secret_key'].forEach((path) => {
            this.formGroup.get(path).updateValueAndValidity({ onlySelf: true });
          });
        });
      }

      /**
       * Validates whether the subuser already exists.
       */
      subuserValidator(): ValidatorFn {
        const self = this;
        return (control: AbstractControl): ValidationErrors | null => {
          if (self.editing) {
            return null;
          }
          if (isEmptyInputValue(control.value)) {
            return null;
          }
          const found = self.subusers.some((subuser) => {
            return _.isEqual(self.getSubuserName(subuser.id), control.value);
          });
          return found ? { subuserIdExists: true } : null;
        };
      }

      /**
       * Get the subuser name.
       * Examples:
       *   'johndoe' => 'johndoe'
       *   'janedoe:xyz' => 'xyz'
       * @param {string} value The value to process.
       * @returns {string} Returns the user ID.
       */
      private getSubuserName(value: string) {
        if (_.isEmpty(value)) {
          return value;
        }
        const matches = value.match(/([^:]+)(:(.+))?/);
        return _.isUndefined(matches[3]) ? matches[1] : matches[3];
      }

      /**
       * 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.CREATE;
      }

      /**
       * Set the values displayed in the dialog.
       */
      setValues(uid: string, subuser: string = '', permissions: string = '') {
        this.formGroup.setValue({
          uid: uid,
          subuid: this.getSubuserName(subuser),
          perm: permissions,
          generate_secret: true,
          secret_key: null
        });
      }

      /**
       * Set the current capabilities of the user.
       */
      setSubusers(subusers: RgwUserSubuser[]) {
        this.subusers = subusers;
      }

      onSubmit() {
        // Get the values from the form and create an object that is sent
        // by the triggered submit action event.
        const values = this.formGroup.value;
        const subuser = new RgwUserSubuser();
        subuser.id = `${values.uid}:${values.subuid}`;
        subuser.permissions = values.perm;
        subuser.generate_secret = values.generate_secret;
        subuser.secret_key = values.secret_key;
        this.submitAction.emit(subuser);
        this.bsModalRef.hide();
      }
    }