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