From cbeca9d0ca95d6d2e5c245694790780dc6e55933 Mon Sep 17 00:00:00 2001 From: Lebeau Elise Date: Tue, 14 Dec 2021 14:22:35 +0000 Subject: [PATCH] user management, create edit user detail dialog --- src/app/app.module.ts | 2 +- .../add-user-dialog.component.html | 2 +- .../add-user-dialog.component.ts | 1 - .../userEmailAsyncValidator.ts | 4 +- .../add-user-dialog/userNameAsyncValidator.ts | 4 +- .../edit-user-dialog.component.html | 42 ++++++++++ .../edit-user-dialog.component.scss | 8 ++ .../edit-user-dialog.component.spec.ts | 25 ++++++ .../edit-user-dialog.component.ts | 81 +++++++++++++++++++ .../user-management.component.html | 2 +- .../user-management.component.ts | 10 +++ src/app/services/user.service.ts | 12 ++- 12 files changed, 178 insertions(+), 15 deletions(-) create mode 100644 src/app/components/user-management/edit-user-dialog/edit-user-dialog.component.html create mode 100644 src/app/components/user-management/edit-user-dialog/edit-user-dialog.component.scss create mode 100644 src/app/components/user-management/edit-user-dialog/edit-user-dialog.component.spec.ts create mode 100644 src/app/components/user-management/edit-user-dialog/edit-user-dialog.component.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index d5b43685..11aff1fc 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -474,7 +474,7 @@ import { DeleteUserDialogComponent } from './components/user-management/delete-u AddUserDialogComponent, UserFilterPipe, DeleteGroupDialogComponent, - DeleteUserDialogComponent + DeleteUserDialogComponent, ], imports: [ BrowserModule, diff --git a/src/app/components/user-management/add-user-dialog/add-user-dialog.component.html b/src/app/components/user-management/add-user-dialog/add-user-dialog.component.html index 2f565edb..c94ac91c 100644 --- a/src/app/components/user-management/add-user-dialog/add-user-dialog.component.html +++ b/src/app/components/user-management/add-user-dialog/add-user-dialog.component.html @@ -22,7 +22,7 @@ - + A password between 6 and 100 characters is required. diff --git a/src/app/components/user-management/add-user-dialog/add-user-dialog.component.ts b/src/app/components/user-management/add-user-dialog/add-user-dialog.component.ts index 2bc64b1b..1a94a1a9 100644 --- a/src/app/components/user-management/add-user-dialog/add-user-dialog.component.ts +++ b/src/app/components/user-management/add-user-dialog/add-user-dialog.component.ts @@ -65,7 +65,6 @@ export class AddUserDialogComponent implements OnInit { const newUser = this.addUserForm.value; this.userService.add(this.server, newUser) .subscribe((user: User) => { - console.log("Done ", user) this.toasterService.success(`User ${user.username} added`); this.dialogRef.close(); }, diff --git a/src/app/components/user-management/add-user-dialog/userEmailAsyncValidator.ts b/src/app/components/user-management/add-user-dialog/userEmailAsyncValidator.ts index 41f4aedd..302febae 100644 --- a/src/app/components/user-management/add-user-dialog/userEmailAsyncValidator.ts +++ b/src/app/components/user-management/add-user-dialog/userEmailAsyncValidator.ts @@ -16,12 +16,12 @@ import {FormControl} from "@angular/forms"; import {timer} from "rxjs"; import {map, switchMap} from "rxjs/operators"; -export const userEmailAsyncValidator = (server: Server, userService: UserService) => { +export const userEmailAsyncValidator = (server: Server, userService: UserService, except: string = '') => { return (control: FormControl) => { return timer(500).pipe( switchMap(() => userService.list(server)), map((response) => { - return (response.find((n) => n.email === control.value) ? { emailExists: true } : null); + return (response.find((n) => n.email === control.value && control.value !== except) ? { emailExists: true } : null); }) ); }; diff --git a/src/app/components/user-management/add-user-dialog/userNameAsyncValidator.ts b/src/app/components/user-management/add-user-dialog/userNameAsyncValidator.ts index 52503e8a..fb2dd5bb 100644 --- a/src/app/components/user-management/add-user-dialog/userNameAsyncValidator.ts +++ b/src/app/components/user-management/add-user-dialog/userNameAsyncValidator.ts @@ -16,12 +16,12 @@ import {timer} from "rxjs"; import {map, switchMap} from "rxjs/operators"; import {UserService} from "../../../services/user.service"; -export const userNameAsyncValidator = (server: Server, userService: UserService) => { +export const userNameAsyncValidator = (server: Server, userService: UserService, except: string = '') => { return (control: FormControl) => { return timer(500).pipe( switchMap(() => userService.list(server)), map((response) => { - return (response.find((n) => n.username === control.value) ? { userExists: true } : null); + return (response.find((n) => n.username === control.value && control.value !== except) ? { userExists: true } : null); }) ); }; diff --git a/src/app/components/user-management/edit-user-dialog/edit-user-dialog.component.html b/src/app/components/user-management/edit-user-dialog/edit-user-dialog.component.html new file mode 100644 index 00000000..8b1e6e1c --- /dev/null +++ b/src/app/components/user-management/edit-user-dialog/edit-user-dialog.component.html @@ -0,0 +1,42 @@ +

Update user

+
+ + + Username is required + Username is incorrect + User with this username exists + + + + + + + Email is required + Email is invalid + User with this email exists + + + + + + Password must be between 6 and 100 characters + + + + Is active + +
+ + +
+
diff --git a/src/app/components/user-management/edit-user-dialog/edit-user-dialog.component.scss b/src/app/components/user-management/edit-user-dialog/edit-user-dialog.component.scss new file mode 100644 index 00000000..469c3dda --- /dev/null +++ b/src/app/components/user-management/edit-user-dialog/edit-user-dialog.component.scss @@ -0,0 +1,8 @@ +.input-field { + width: 100%; +} + + +.button-div { + float: right; +} diff --git a/src/app/components/user-management/edit-user-dialog/edit-user-dialog.component.spec.ts b/src/app/components/user-management/edit-user-dialog/edit-user-dialog.component.spec.ts new file mode 100644 index 00000000..7c6e36da --- /dev/null +++ b/src/app/components/user-management/edit-user-dialog/edit-user-dialog.component.spec.ts @@ -0,0 +1,25 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { EditUserDialogComponent } from './edit-user-dialog.component'; + +describe('EditUserDialogComponent', () => { + let component: EditUserDialogComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + declarations: [ EditUserDialogComponent ] + }) + .compileComponents(); + }); + + beforeEach(() => { + fixture = TestBed.createComponent(EditUserDialogComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/components/user-management/edit-user-dialog/edit-user-dialog.component.ts b/src/app/components/user-management/edit-user-dialog/edit-user-dialog.component.ts new file mode 100644 index 00000000..0a5129d1 --- /dev/null +++ b/src/app/components/user-management/edit-user-dialog/edit-user-dialog.component.ts @@ -0,0 +1,81 @@ +/* +* Software Name : GNS3 Web UI +* Version: 3 +* SPDX-FileCopyrightText: Copyright (c) 2022 Orange Business Services +* SPDX-License-Identifier: GPL-3.0-or-later +* +* This software is distributed under the GPL-3.0 or any later version, +* the text of which is available at https://www.gnu.org/licenses/gpl-3.0.txt +* or see the "LICENSE" file for more details. +* +* Author: Sylvain MATHIEU, Elise LEBEAU +*/ +import {Component, Inject, OnInit} from '@angular/core'; +import {FormControl, FormGroup, Validators} from "@angular/forms"; +import {Server} from "@models/server"; +import {MAT_DIALOG_DATA, MatDialogRef} from "@angular/material/dialog"; +import {UserService} from "@services/user.service"; +import {ToasterService} from "@services/toaster.service"; +import {userNameAsyncValidator} from "@components/user-management/add-user-dialog/userNameAsyncValidator"; +import {userEmailAsyncValidator} from "@components/user-management/add-user-dialog/userEmailAsyncValidator"; +import {User} from "@models/users/user"; + +@Component({ + selector: 'app-edit-user-dialog', + templateUrl: './edit-user-dialog.component.html', + styleUrls: ['./edit-user-dialog.component.scss'] +}) +export class EditUserDialogComponent implements OnInit { + + editUserForm: FormGroup; + + constructor(public dialogRef: MatDialogRef, + public userService: UserService, + private toasterService: ToasterService, + @Inject(MAT_DIALOG_DATA) public data: { user: User, server: Server }) {} + + ngOnInit(): void { + this.editUserForm = new FormGroup({ + username: new FormControl(this.data.user.username, [ + Validators.required, + Validators.minLength(3), + Validators.pattern("[a-zA-Z0-9_-]+$")], + [userNameAsyncValidator(this.data.server, this.userService, this.data.user.username)]), + full_name: new FormControl(this.data.user.full_name), + email: new FormControl(this.data.user.email, + [Validators.email, Validators.required], + [userEmailAsyncValidator(this.data.server, this.userService, this.data.user.email)]), + password: new FormControl(null, + [Validators.minLength(6), Validators.maxLength(100)]), + is_active: new FormControl(this.data.user.is_active) + }); + } + + get form() { + return this.editUserForm.controls; + } + + onCancelClick() { + this.dialogRef.close(); + } + + onEditClick() { + if (!this.editUserForm.valid) { + return; + } + const updatedUser = this.editUserForm.value; + + updatedUser.user_id = this.data.user.user_id; + console.log(updatedUser) + this.userService.update(this.data.server, updatedUser) + .subscribe((user: User) => { + console.log("Done ", user) + this.toasterService.success(`User ${user.username} updated`); + this.dialogRef.close(); + }, + (error) => { + this.toasterService.error('Cannot update user : ' + error); + }) + } + +} diff --git a/src/app/components/user-management/user-management.component.html b/src/app/components/user-management/user-management.component.html index a155cdf0..72cc8dbb 100644 --- a/src/app/components/user-management/user-management.component.html +++ b/src/app/components/user-management/user-management.component.html @@ -38,7 +38,7 @@ Username - {{ row.username }} + {{ row.username }} diff --git a/src/app/components/user-management/user-management.component.ts b/src/app/components/user-management/user-management.component.ts index 83798abb..98631aaa 100644 --- a/src/app/components/user-management/user-management.component.ts +++ b/src/app/components/user-management/user-management.component.ts @@ -26,6 +26,7 @@ import {DeleteUserDialogComponent} from "@components/user-management/delete-user import {ToasterService} from "@services/toaster.service"; import {MatPaginator} from "@angular/material/paginator"; import {MatTableDataSource} from "@angular/material/table"; +import {EditUserDialogComponent} from "@components/user-management/edit-user-dialog/edit-user-dialog.component"; @Component({ selector: 'app-user-management', @@ -143,4 +144,13 @@ export class UserManagementComponent implements OnInit { }); } + + onEdit(user: User) { + this.dialog + .open(EditUserDialogComponent, {width: '500px', data: {user: user, server: this.server}}) + .afterClosed() + .subscribe(() => { + this.refresh(); + }); + } } diff --git a/src/app/services/user.service.ts b/src/app/services/user.service.ts index 8cd4c0b4..72ca9ab3 100644 --- a/src/app/services/user.service.ts +++ b/src/app/services/user.service.ts @@ -20,16 +20,14 @@ export class UserService { } add(server: Server, user: any): Observable { - return this.httpServer.post(server, `/users`, { - username: user.username, - is_active: user.is_active, - email: user.email, - full_name: user.full_name, - password: user.password - }); + return this.httpServer.post(server, `/users`, user); } delete(server: Server, user_id: string) { return this.httpServer.delete(server, `/users/${user_id}`); } + + update(server: Server, user: User): Observable { + return this.httpServer.put(server, `/users/${user.user_id}`, user); + } }