]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
ac3597e40ac00ac628c5e1757f19b2fd601d13e5
[ceph-ci.git] /
1 import { Component, EventEmitter, Output } from '@angular/core';
2 import {
3   AbstractControl,
4   FormBuilder,
5   FormGroup,
6   ValidationErrors,
7   ValidatorFn,
8   Validators
9 } from '@angular/forms';
10
11 import * as _ from 'lodash';
12 import { BsModalRef } from 'ngx-bootstrap/modal/bs-modal-ref.service';
13
14 import { CdValidators, isEmptyInputValue } from '../../../shared/validators/cd-validators';
15 import { RgwUserSubuser } from '../models/rgw-user-subuser';
16
17 @Component({
18   selector: 'cd-rgw-user-subuser-modal',
19   templateUrl: './rgw-user-subuser-modal.component.html',
20   styleUrls: ['./rgw-user-subuser-modal.component.scss']
21 })
22 export class RgwUserSubuserModalComponent {
23
24   /**
25    * The event that is triggered when the 'Add' or 'Update' button
26    * has been pressed.
27    */
28   @Output() submitAction = new EventEmitter();
29
30   formGroup: FormGroup;
31   editing = true;
32   subusers: RgwUserSubuser[] = [];
33
34   constructor(private formBuilder: FormBuilder,
35               public bsModalRef: BsModalRef) {
36     this.createForm();
37     this.listenToChanges();
38   }
39
40   createForm() {
41     this.formGroup = this.formBuilder.group({
42       'uid': [
43         null
44       ],
45       'subuid': [
46         null,
47         [
48           Validators.required,
49           this.subuserValidator()
50         ]
51       ],
52       'perm': [
53         null,
54         [Validators.required]
55       ],
56       // Swift key
57       'generate_secret': [
58         true
59       ],
60       'secret_key': [
61         null,
62         [CdValidators.requiredIf({'generate_secret': false})]
63       ]
64     });
65   }
66
67   listenToChanges() {
68     // Reset the validation status of various controls, especially those that are using
69     // the 'requiredIf' validator. This is necessary because the controls itself are not
70     // validated again if the status of their prerequisites have been changed.
71     this.formGroup.get('generate_secret').valueChanges.subscribe(() => {
72       ['secret_key'].forEach((path) => {
73         this.formGroup.get(path).updateValueAndValidity({onlySelf: true});
74       });
75     });
76   }
77
78   /**
79    * Validates whether the subuser already exists.
80    */
81   subuserValidator(): ValidatorFn {
82     const self = this;
83     return (control: AbstractControl): ValidationErrors | null => {
84       if (self.editing) {
85         return null;
86       }
87       if (isEmptyInputValue(control.value)) {
88         return null;
89       }
90       const found = self.subusers.some((subuser) => {
91         return _.isEqual(self.getSubuserName(subuser.id), control.value);
92       });
93       return found ? {'subuserIdExists': true} : null;
94     };
95   }
96
97   /**
98    * Get the subuser name.
99    * Examples:
100    *   'johndoe' => 'johndoe'
101    *   'janedoe:xyz' => 'xyz'
102    * @param {string} value The value to process.
103    * @returns {string} Returns the user ID.
104    */
105   private getSubuserName(value: string) {
106     if (_.isEmpty(value)) {
107       return value;
108     }
109     const matches = value.match(/([^:]+)(:(.+))?/);
110     return _.isUndefined(matches[3]) ? matches[1] : matches[3];
111   }
112
113   /**
114    * Set the 'editing' flag. If set to TRUE, the modal dialog is in 'Edit' mode,
115    * otherwise in 'Add' mode. According to the mode the dialog and its controls
116    * behave different.
117    * @param {boolean} viewing
118    */
119   setEditing(editing: boolean = true) {
120     this.editing = editing;
121   }
122
123   /**
124    * Set the values displayed in the dialog.
125    */
126   setValues(uid: string, subuser: string = '', permissions: string = '') {
127     this.formGroup.setValue({
128       'uid': uid,
129       'subuid': this.getSubuserName(subuser),
130       'perm': permissions,
131       'generate_secret': true,
132       'secret_key': null
133     });
134   }
135
136   /**
137    * Set the current capabilities of the user.
138    */
139   setSubusers(subusers: RgwUserSubuser[]) {
140     this.subusers = subusers;
141   }
142
143   onSubmit() {
144     // Get the values from the form and create an object that is sent
145     // by the triggered submit action event.
146     const values = this.formGroup.value;
147     const subuser = new RgwUserSubuser;
148     subuser.id = `${values.uid}:${values.subuid}`;
149     subuser.permissions = values.perm;
150     subuser.generate_secret = values.generate_secret;
151     subuser.secret_key = values.secret_key;
152     this.submitAction.emit(subuser);
153     this.bsModalRef.hide();
154   }
155 }