]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr/dashboard: Asynchronous unique username validation for User Component
authorNizamudeen A <nia@localhost.localdomain>
Wed, 22 Apr 2020 11:23:41 +0000 (16:53 +0530)
committerNizamudeen A <nia@localhost.localdomain>
Wed, 22 Apr 2020 11:35:25 +0000 (17:05 +0530)
Implements an asynchronous validation for the username field in the Create User form which immediately display an error message if the username already exists.

Fixes: https://tracker.ceph.com/issues/36375
Signed-off-by: Nizamudeen <nia@redhat.com>
src/pybind/mgr/dashboard/frontend/src/app/core/auth/user-form/user-form.component.html
src/pybind/mgr/dashboard/frontend/src/app/core/auth/user-form/user-form.component.ts
src/pybind/mgr/dashboard/frontend/src/app/shared/api/user.service.ts

index 66835259f33c8fe29c91fa22a053518f80739821..e0a27c9a0c233235b9931268d007124f35478c7d 100644 (file)
@@ -29,6 +29,9 @@
             <span class="invalid-feedback"
                   *ngIf="userForm.showError('username', formDir, 'required')"
                   i18n>This field is required.</span>
+            <span class="invalid-feedback"
+                  *ngIf="userForm.showError('username', formDir, 'notUnique')"
+                  i18n>The username already exists.</span>
           </div>
         </div>
 
index 75e14e330361fcee56d3b0ef2aad1a03297723f3..952508ca0d018665b93eb24d4a956cb7c1123735 100644 (file)
@@ -84,7 +84,11 @@ export class UserFormComponent implements OnInit {
     });
     this.userForm = this.formBuilder.group(
       {
-        username: ['', [Validators.required]],
+        username: [
+          '',
+          [Validators.required],
+          [CdValidators.unique(this.userService.validateUserName, this.userService)]
+        ],
         name: [''],
         password: [
           '',
index 05c53de0cca2217b155a50ca61d3c768e2b94dc1..c0b7acdb6de5ffb2da6fa10f7c36559ee89958bc 100644 (file)
@@ -1,5 +1,7 @@
 import { HttpClient, HttpParams } from '@angular/common/http';
 import { Injectable } from '@angular/core';
+import { Observable, of as observableOf } from 'rxjs';
+import { catchError, mapTo } from 'rxjs/operators';
 
 import { UserFormModel } from '../../core/auth/user-form/user-form.model';
 import { ApiModule } from './api.module';
@@ -40,6 +42,16 @@ export class UserService {
     });
   }
 
+  validateUserName(user_name: string): Observable<boolean> {
+    return this.get(user_name).pipe(
+      mapTo(true),
+      catchError((error) => {
+        error.preventDefault();
+        return observableOf(false);
+      })
+    );
+  }
+
   validatePassword(password: string, username: string = null, oldPassword: string = null) {
     let params = new HttpParams();
     params = params.append('password', password);