]> git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/frontend/src/app/ceph/rgw/rgw-user-subuser-modal/rgw-user-subuser-modal.component.ts
import ceph nautilus 14.2.2
[ceph.git] / ceph / src / pybind / mgr / dashboard / frontend / src / app / ceph / rgw / rgw-user-subuser-modal / rgw-user-subuser-modal.component.ts
1 import { Component, EventEmitter, Output } from '@angular/core';
2 import { AbstractControl, ValidationErrors, ValidatorFn, Validators } from '@angular/forms';
3
4 import * as _ from 'lodash';
5 import { BsModalRef } from 'ngx-bootstrap/modal';
6
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';
13
14 @Component({
15 selector: 'cd-rgw-user-subuser-modal',
16 templateUrl: './rgw-user-subuser-modal.component.html',
17 styleUrls: ['./rgw-user-subuser-modal.component.scss']
18 })
19 export class RgwUserSubuserModalComponent {
20 /**
21 * The event that is triggered when the 'Add' or 'Update' button
22 * has been pressed.
23 */
24 @Output()
25 submitAction = new EventEmitter();
26
27 formGroup: CdFormGroup;
28 editing = true;
29 subusers: RgwUserSubuser[] = [];
30 resource: string;
31 action: string;
32
33 constructor(
34 private formBuilder: CdFormBuilder,
35 public bsModalRef: BsModalRef,
36 private i18n: I18n,
37 private actionLabels: ActionLabelsI18n
38 ) {
39 this.resource = this.i18n('Subuser');
40 this.createForm();
41 }
42
43 createForm() {
44 this.formGroup = this.formBuilder.group({
45 uid: [null],
46 subuid: [null, [Validators.required, this.subuserValidator()]],
47 perm: [null, [Validators.required]],
48 // Swift key
49 generate_secret: [true],
50 secret_key: [null, [CdValidators.requiredIf({ generate_secret: false })]]
51 });
52 }
53
54 /**
55 * Validates whether the subuser already exists.
56 */
57 subuserValidator(): ValidatorFn {
58 const self = this;
59 return (control: AbstractControl): ValidationErrors | null => {
60 if (self.editing) {
61 return null;
62 }
63 if (isEmptyInputValue(control.value)) {
64 return null;
65 }
66 const found = self.subusers.some((subuser) => {
67 return _.isEqual(self.getSubuserName(subuser.id), control.value);
68 });
69 return found ? { subuserIdExists: true } : null;
70 };
71 }
72
73 /**
74 * Get the subuser name.
75 * Examples:
76 * 'johndoe' => 'johndoe'
77 * 'janedoe:xyz' => 'xyz'
78 * @param {string} value The value to process.
79 * @returns {string} Returns the user ID.
80 */
81 private getSubuserName(value: string) {
82 if (_.isEmpty(value)) {
83 return value;
84 }
85 const matches = value.match(/([^:]+)(:(.+))?/);
86 return _.isUndefined(matches[3]) ? matches[1] : matches[3];
87 }
88
89 /**
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
92 * behave different.
93 * @param {boolean} viewing
94 */
95 setEditing(editing: boolean = true) {
96 this.editing = editing;
97 this.action = this.editing ? this.actionLabels.EDIT : this.actionLabels.CREATE;
98 }
99
100 /**
101 * Set the values displayed in the dialog.
102 */
103 setValues(uid: string, subuser: string = '', permissions: string = '') {
104 this.formGroup.setValue({
105 uid: uid,
106 subuid: this.getSubuserName(subuser),
107 perm: permissions,
108 generate_secret: true,
109 secret_key: null
110 });
111 }
112
113 /**
114 * Set the current capabilities of the user.
115 */
116 setSubusers(subusers: RgwUserSubuser[]) {
117 this.subusers = subusers;
118 }
119
120 onSubmit() {
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();
131 }
132 }