1 import { Component, EventEmitter, Output } from '@angular/core';
2 import { AbstractControl, ValidationErrors, ValidatorFn, Validators } from '@angular/forms';
4 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
5 import * as _ from 'lodash';
7 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
8 import { CdFormBuilder } from '../../../shared/forms/cd-form-builder';
9 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
10 import { CdValidators, isEmptyInputValue } from '../../../shared/forms/cd-validators';
11 import { RgwUserSubuser } from '../models/rgw-user-subuser';
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 {
20 * The event that is triggered when the 'Add' or 'Update' button
24 submitAction = new EventEmitter();
26 formGroup: CdFormGroup;
28 subusers: RgwUserSubuser[] = [];
33 private formBuilder: CdFormBuilder,
34 public bsModalRef: NgbActiveModal,
35 private actionLabels: ActionLabelsI18n
37 this.resource = $localize`Subuser`;
42 this.formGroup = this.formBuilder.group({
44 subuid: [null, [Validators.required, this.subuserValidator()]],
45 perm: [null, [Validators.required]],
47 generate_secret: [true],
48 secret_key: [null, [CdValidators.requiredIf({ generate_secret: false })]]
53 * Validates whether the subuser already exists.
55 subuserValidator(): ValidatorFn {
57 return (control: AbstractControl): ValidationErrors | null => {
61 if (isEmptyInputValue(control.value)) {
64 const found = self.subusers.some((subuser) => {
65 return _.isEqual(self.getSubuserName(subuser.id), control.value);
67 return found ? { subuserIdExists: true } : null;
72 * Get the subuser name.
74 * 'johndoe' => 'johndoe'
75 * 'janedoe:xyz' => 'xyz'
76 * @param {string} value The value to process.
77 * @returns {string} Returns the user ID.
79 private getSubuserName(value: string) {
80 if (_.isEmpty(value)) {
83 const matches = value.match(/([^:]+)(:(.+))?/);
84 return _.isUndefined(matches[3]) ? matches[1] : matches[3];
88 * Set the 'editing' flag. If set to TRUE, the modal dialog is in 'Edit' mode,
89 * otherwise in 'Add' mode. According to the mode the dialog and its controls
91 * @param {boolean} viewing
93 setEditing(editing: boolean = true) {
94 this.editing = editing;
95 this.action = this.editing ? this.actionLabels.EDIT : this.actionLabels.CREATE;
99 * Set the values displayed in the dialog.
101 setValues(uid: string, subuser: string = '', permissions: string = '') {
102 this.formGroup.setValue({
104 subuid: this.getSubuserName(subuser),
106 generate_secret: true,
112 * Set the current capabilities of the user.
114 setSubusers(subusers: RgwUserSubuser[]) {
115 this.subusers = subusers;
119 // Get the values from the form and create an object that is sent
120 // by the triggered submit action event.
121 const values = this.formGroup.value;
122 const subuser = new RgwUserSubuser();
123 subuser.id = `${values.uid}:${values.subuid}`;
124 subuser.permissions = values.perm;
125 subuser.generate_secret = values.generate_secret;
126 subuser.secret_key = values.secret_key;
127 this.submitAction.emit(subuser);
128 this.bsModalRef.close();