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';
7 import { I18n } from '@ngx-translate/i18n-polyfill';
8 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
9 import { CdFormBuilder } from '../../../shared/forms/cd-form-builder';
10 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
11 import { CdValidators, isEmptyInputValue } from '../../../shared/forms/cd-validators';
12 import { RgwUserSubuser } from '../models/rgw-user-subuser';
15 selector: 'cd-rgw-user-subuser-modal',
16 templateUrl: './rgw-user-subuser-modal.component.html',
17 styleUrls: ['./rgw-user-subuser-modal.component.scss']
19 export class RgwUserSubuserModalComponent {
21 * The event that is triggered when the 'Add' or 'Update' button
25 submitAction = new EventEmitter();
27 formGroup: CdFormGroup;
29 subusers: RgwUserSubuser[] = [];
34 private formBuilder: CdFormBuilder,
35 public bsModalRef: BsModalRef,
37 private actionLabels: ActionLabelsI18n
39 this.resource = this.i18n('Subuser');
44 this.formGroup = this.formBuilder.group({
46 subuid: [null, [Validators.required, this.subuserValidator()]],
47 perm: [null, [Validators.required]],
49 generate_secret: [true],
50 secret_key: [null, [CdValidators.requiredIf({ generate_secret: false })]]
55 * Validates whether the subuser already exists.
57 subuserValidator(): ValidatorFn {
59 return (control: AbstractControl): ValidationErrors | null => {
63 if (isEmptyInputValue(control.value)) {
66 const found = self.subusers.some((subuser) => {
67 return _.isEqual(self.getSubuserName(subuser.id), control.value);
69 return found ? { subuserIdExists: true } : null;
74 * Get the subuser name.
76 * 'johndoe' => 'johndoe'
77 * 'janedoe:xyz' => 'xyz'
78 * @param {string} value The value to process.
79 * @returns {string} Returns the user ID.
81 private getSubuserName(value: string) {
82 if (_.isEmpty(value)) {
85 const matches = value.match(/([^:]+)(:(.+))?/);
86 return _.isUndefined(matches[3]) ? matches[1] : matches[3];
90 * Set the 'editing' flag. If set to TRUE, the modal dialog is in 'Edit' mode,
91 * otherwise in 'Add' mode. According to the mode the dialog and its controls
93 * @param {boolean} viewing
95 setEditing(editing: boolean = true) {
96 this.editing = editing;
97 this.action = this.editing ? this.actionLabels.EDIT : this.actionLabels.CREATE;
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();