1 import { Component, EventEmitter, Output } from '@angular/core';
 
   9 } from '@angular/forms';
 
  11 import * as _ from 'lodash';
 
  12 import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
 
  14 import { CdValidators, isEmptyInputValue } from '../../../shared/validators/cd-validators';
 
  15 import { RgwUserSubuser } from '../models/rgw-user-subuser';
 
  18   selector: 'cd-rgw-user-subuser-modal',
 
  19   templateUrl: './rgw-user-subuser-modal.component.html',
 
  20   styleUrls: ['./rgw-user-subuser-modal.component.scss']
 
  22 export class RgwUserSubuserModalComponent {
 
  25    * The event that is triggered when the 'Add' or 'Update' button
 
  28   @Output() submitAction = new EventEmitter();
 
  32   subusers: RgwUserSubuser[] = [];
 
  34   constructor(private formBuilder: FormBuilder,
 
  35               public bsModalRef: BsModalRef) {
 
  37     this.listenToChanges();
 
  41     this.formGroup = this.formBuilder.group({
 
  49           this.subuserValidator()
 
  62         [CdValidators.requiredIf({'generate_secret': false})]
 
  68     // Reset the validation status of various controls, especially those that are using
 
  69     // the 'requiredIf' validator. This is necessary because the controls itself are not
 
  70     // validated again if the status of their prerequisites have been changed.
 
  71     this.formGroup.get('generate_secret').valueChanges.subscribe(() => {
 
  72       ['secret_key'].forEach((path) => {
 
  73         this.formGroup.get(path).updateValueAndValidity({onlySelf: true});
 
  79    * Validates whether the subuser already exists.
 
  81   subuserValidator(): ValidatorFn {
 
  83     return (control: AbstractControl): ValidationErrors | null => {
 
  87       if (isEmptyInputValue(control.value)) {
 
  90       const found = self.subusers.some((subuser) => {
 
  91         return _.isEqual(self.getSubuserName(subuser.id), control.value);
 
  93       return found ? {'subuserIdExists': true} : null;
 
  98    * Get the subuser name.
 
 100    *   'johndoe' => 'johndoe'
 
 101    *   'janedoe:xyz' => 'xyz'
 
 102    * @param {string} value The value to process.
 
 103    * @returns {string} Returns the user ID.
 
 105   private getSubuserName(value: string) {
 
 106     if (_.isEmpty(value)) {
 
 109     const matches = value.match(/([^:]+)(:(.+))?/);
 
 110     return _.isUndefined(matches[3]) ? matches[1] : matches[3];
 
 114    * Set the 'editing' flag. If set to TRUE, the modal dialog is in 'Edit' mode,
 
 115    * otherwise in 'Add' mode. According to the mode the dialog and its controls
 
 117    * @param {boolean} viewing
 
 119   setEditing(editing: boolean = true) {
 
 120     this.editing = editing;
 
 124    * Set the values displayed in the dialog.
 
 126   setValues(uid: string, subuser: string = '', permissions: string = '') {
 
 127     this.formGroup.setValue({
 
 129       'subuid': this.getSubuserName(subuser),
 
 131       'generate_secret': true,
 
 137    * Set the current capabilities of the user.
 
 139   setSubusers(subusers: RgwUserSubuser[]) {
 
 140     this.subusers = subusers;
 
 144     // Get the values from the form and create an object that is sent
 
 145     // by the triggered submit action event.
 
 146     const values = this.formGroup.value;
 
 147     const subuser = new RgwUserSubuser;
 
 148     subuser.id = `${values.uid}:${values.subuid}`;
 
 149     subuser.permissions = values.perm;
 
 150     subuser.generate_secret = values.generate_secret;
 
 151     subuser.secret_key = values.secret_key;
 
 152     this.submitAction.emit(subuser);
 
 153     this.bsModalRef.hide();