1 import { Component, EventEmitter, Output } from '@angular/core';
2 import { AbstractControl, ValidationErrors, ValidatorFn, Validators } from '@angular/forms';
4 import * as _ from 'lodash';
5 import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
7 import { CdFormBuilder } from '../../../shared/forms/cd-form-builder';
8 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
9 import { CdValidators, isEmptyInputValue } from '../../../shared/forms/cd-validators';
10 import { RgwUserSubuser } from '../models/rgw-user-subuser';
13 selector: 'cd-rgw-user-subuser-modal',
14 templateUrl: './rgw-user-subuser-modal.component.html',
15 styleUrls: ['./rgw-user-subuser-modal.component.scss']
17 export class RgwUserSubuserModalComponent {
19 * The event that is triggered when the 'Add' or 'Update' button
22 @Output() submitAction = new EventEmitter();
24 formGroup: CdFormGroup;
26 subusers: RgwUserSubuser[] = [];
28 constructor(private formBuilder: CdFormBuilder, public bsModalRef: BsModalRef) {
30 this.listenToChanges();
34 this.formGroup = this.formBuilder.group({
36 subuid: [null, [Validators.required, this.subuserValidator()]],
37 perm: [null, [Validators.required]],
39 generate_secret: [true],
40 secret_key: [null, [CdValidators.requiredIf({ generate_secret: false })]]
45 // Reset the validation status of various controls, especially those that are using
46 // the 'requiredIf' validator. This is necessary because the controls itself are not
47 // validated again if the status of their prerequisites have been changed.
48 this.formGroup.get('generate_secret').valueChanges.subscribe(() => {
49 ['secret_key'].forEach((path) => {
50 this.formGroup.get(path).updateValueAndValidity({ onlySelf: true });
56 * Validates whether the subuser already exists.
58 subuserValidator(): ValidatorFn {
60 return (control: AbstractControl): ValidationErrors | null => {
64 if (isEmptyInputValue(control.value)) {
67 const found = self.subusers.some((subuser) => {
68 return _.isEqual(self.getSubuserName(subuser.id), control.value);
70 return found ? { subuserIdExists: true } : null;
75 * Get the subuser name.
77 * 'johndoe' => 'johndoe'
78 * 'janedoe:xyz' => 'xyz'
79 * @param {string} value The value to process.
80 * @returns {string} Returns the user ID.
82 private getSubuserName(value: string) {
83 if (_.isEmpty(value)) {
86 const matches = value.match(/([^:]+)(:(.+))?/);
87 return _.isUndefined(matches[3]) ? matches[1] : matches[3];
91 * Set the 'editing' flag. If set to TRUE, the modal dialog is in 'Edit' mode,
92 * otherwise in 'Add' mode. According to the mode the dialog and its controls
94 * @param {boolean} viewing
96 setEditing(editing: boolean = true) {
97 this.editing = editing;
101 * Set the values displayed in the dialog.
103 setValues(uid: string, subuser: string = '', permissions: string = '') {
104 this.formGroup.setValue({
106 subuid: this.getSubuserName(subuser),
108 generate_secret: true,
114 * Set the current capabilities of the user.
116 setSubusers(subusers: RgwUserSubuser[]) {
117 this.subusers = subusers;
121 // Get the values from the form and create an object that is sent
122 // by the triggered submit action event.
123 const values = this.formGroup.value;
124 const subuser = new RgwUserSubuser();
125 subuser.id = `${values.uid}:${values.subuid}`;
126 subuser.permissions = values.perm;
127 subuser.generate_secret = values.generate_secret;
128 subuser.secret_key = values.secret_key;
129 this.submitAction.emit(subuser);
130 this.bsModalRef.hide();