1 import { Component, EventEmitter, Output } from '@angular/core';
2 import { AbstractControl, ValidationErrors, ValidatorFn, Validators } from '@angular/forms';
4 import _ from 'lodash';
6 import { ActionLabelsI18n } from '~/app/shared/constants/app.constants';
7 import { CdFormBuilder } from '~/app/shared/forms/cd-form-builder';
8 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
9 import { CdValidators, isEmptyInputValue } from '~/app/shared/forms/cd-validators';
10 import { RgwUserSubuser } from '../models/rgw-user-subuser';
11 import { BaseModal } from 'carbon-components-angular';
14 selector: 'cd-rgw-user-subuser-modal',
15 templateUrl: './rgw-user-subuser-modal.component.html',
16 styleUrls: ['./rgw-user-subuser-modal.component.scss']
18 export class RgwUserSubuserModalComponent extends BaseModal {
20 * The event that is triggered when the 'Add' or 'Update' button
24 submitAction = new EventEmitter();
26 formGroup: CdFormGroup;
28 subusers: RgwUserSubuser[] = [];
32 constructor(private formBuilder: CdFormBuilder, private actionLabels: ActionLabelsI18n) {
34 this.resource = $localize`Subuser`;
39 this.formGroup = this.formBuilder.group({
41 subuid: [null, [Validators.required, this.subuserValidator()]],
42 perm: ['full-control', [Validators.required]],
44 generate_secret: [true],
45 secret_key: [null, [CdValidators.requiredIf({ generate_secret: false })]]
50 * Validates whether the subuser already exists.
52 subuserValidator(): ValidatorFn {
54 return (control: AbstractControl): ValidationErrors | null => {
58 if (isEmptyInputValue(control.value)) {
61 const found = self.subusers.some((subuser) => {
62 return _.isEqual(self.getSubuserName(subuser.id), control.value);
64 return found ? { subuserIdExists: true } : null;
69 * Get the subuser name.
71 * 'johndoe' => 'johndoe'
72 * 'janedoe:xyz' => 'xyz'
73 * @param {string} value The value to process.
74 * @returns {string} Returns the user ID.
76 private getSubuserName(value: string) {
77 if (_.isEmpty(value)) {
80 const matches = value.match(/([^:]+)(:(.+))?/);
81 return _.isUndefined(matches[3]) ? matches[1] : matches[3];
85 * Set the 'editing' flag. If set to TRUE, the modal dialog is in 'Edit' mode,
86 * otherwise in 'Add' mode. According to the mode the dialog and its controls
88 * @param {boolean} viewing
90 setEditing(editing: boolean = true) {
91 this.editing = editing;
92 this.action = this.editing ? this.actionLabels.EDIT : this.actionLabels.CREATE;
96 * Set the values displayed in the dialog.
98 setValues(uid: string, subuser: string = '', permissions: string = '') {
99 this.formGroup.setValue({
101 subuid: this.getSubuserName(subuser),
103 generate_secret: true,
109 * Set the current capabilities of the user.
111 setSubusers(subusers: RgwUserSubuser[]) {
112 this.subusers = subusers;
116 // Get the values from the form and create an object that is sent
117 // by the triggered submit action event.
118 const values = this.formGroup.value;
119 const subuser = new RgwUserSubuser();
120 subuser.id = `${values.uid}:${values.subuid}`;
121 subuser.permissions = values.perm;
122 subuser.generate_secret = values.generate_secret;
123 subuser.secret_key = values.secret_key;
124 this.submitAction.emit(subuser);