]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
8f5b427bd78f722866853ef737cea33e01ab1030
[ceph.git] /
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/bs-modal-ref.service';
6
7 import { CdFormBuilder } from '../../../shared/forms/cd-form-builder';
8 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
9 import { CdValidators, isEmptyInputValue } from '../../../shared/forms/cd-validators';
10 import { RgwUserSubuser } from '../models/rgw-user-subuser';
11
12 @Component({
13   selector: 'cd-rgw-user-subuser-modal',
14   templateUrl: './rgw-user-subuser-modal.component.html',
15   styleUrls: ['./rgw-user-subuser-modal.component.scss']
16 })
17 export class RgwUserSubuserModalComponent {
18   /**
19    * The event that is triggered when the 'Add' or 'Update' button
20    * has been pressed.
21    */
22   @Output() submitAction = new EventEmitter();
23
24   formGroup: CdFormGroup;
25   editing = true;
26   subusers: RgwUserSubuser[] = [];
27
28   constructor(private formBuilder: CdFormBuilder, public bsModalRef: BsModalRef) {
29     this.createForm();
30     this.listenToChanges();
31   }
32
33   createForm() {
34     this.formGroup = this.formBuilder.group({
35       uid: [null],
36       subuid: [null, [Validators.required, this.subuserValidator()]],
37       perm: [null, [Validators.required]],
38       // Swift key
39       generate_secret: [true],
40       secret_key: [null, [CdValidators.requiredIf({ generate_secret: false })]]
41     });
42   }
43
44   listenToChanges() {
45     // Reset the validation status of various controls, especially those that are using
46     // the 'requiredIf' validator. This is necessary because the controls itself are not
47     // validated again if the status of their prerequisites have been changed.
48     this.formGroup.get('generate_secret').valueChanges.subscribe(() => {
49       ['secret_key'].forEach((path) => {
50         this.formGroup.get(path).updateValueAndValidity({ onlySelf: true });
51       });
52     });
53   }
54
55   /**
56    * Validates whether the subuser already exists.
57    */
58   subuserValidator(): ValidatorFn {
59     const self = this;
60     return (control: AbstractControl): ValidationErrors | null => {
61       if (self.editing) {
62         return null;
63       }
64       if (isEmptyInputValue(control.value)) {
65         return null;
66       }
67       const found = self.subusers.some((subuser) => {
68         return _.isEqual(self.getSubuserName(subuser.id), control.value);
69       });
70       return found ? { subuserIdExists: true } : null;
71     };
72   }
73
74   /**
75    * Get the subuser name.
76    * Examples:
77    *   'johndoe' => 'johndoe'
78    *   'janedoe:xyz' => 'xyz'
79    * @param {string} value The value to process.
80    * @returns {string} Returns the user ID.
81    */
82   private getSubuserName(value: string) {
83     if (_.isEmpty(value)) {
84       return value;
85     }
86     const matches = value.match(/([^:]+)(:(.+))?/);
87     return _.isUndefined(matches[3]) ? matches[1] : matches[3];
88   }
89
90   /**
91    * Set the 'editing' flag. If set to TRUE, the modal dialog is in 'Edit' mode,
92    * otherwise in 'Add' mode. According to the mode the dialog and its controls
93    * behave different.
94    * @param {boolean} viewing
95    */
96   setEditing(editing: boolean = true) {
97     this.editing = editing;
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 }