Change user password integration

pull/3/head
Alvaro Puente Mella 2024-06-13 12:55:47 +02:00
parent c3027e3f6e
commit 1c6b558844
4 changed files with 132 additions and 0 deletions

View File

@ -0,0 +1,15 @@
.user-form .form-field {
display: block;
margin-bottom: 10px;
}
.checkbox-group label {
display: block;
margin-bottom: 8px;
}
.error-message {
color: red;
margin-top: 10px;
}

View File

@ -0,0 +1,23 @@
<h1 mat-dialog-title>Editar Usuario</h1>
<div mat-dialog-content>
<form [formGroup]="userForm" class="user-form">
<mat-form-field class="form-field">
<mat-label>Nombre de usuario</mat-label>
<input matInput formControlName="username">
</mat-form-field>
<mat-form-field class="form-field">
<mat-label>Contraseña</mat-label>
<input matInput formControlName="password" type="password">
</mat-form-field>
<mat-form-field class="form-field">
<mat-label>Repite la contraseña</mat-label>
<input matInput formControlName="confirmPassword" type="password">
</mat-form-field>
<div *ngIf="passwordMismatch" class="error-message">Las contraseñas no coinciden</div>
<div *ngIf="updateError" class="error-message">Ha habido un error, inténtelo de nuevo</div>
</form>
</div>
<div mat-dialog-actions>
<button mat-button (click)="onNoClick()">Cancelar</button>
<button mat-button (click)="onSubmit()">Editar</button>
</div>

View File

@ -0,0 +1,23 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ChangePasswordModalComponent } from './change-password-modal.component';
describe('ChangePasswordModalComponent', () => {
let component: ChangePasswordModalComponent;
let fixture: ComponentFixture<ChangePasswordModalComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [ChangePasswordModalComponent]
})
.compileComponents();
fixture = TestBed.createComponent(ChangePasswordModalComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});

View File

@ -0,0 +1,71 @@
import { Component, EventEmitter, Inject, Output } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { EditUserModalComponent } from '../edit-user-modal/edit-user-modal.component';
import { UserService } from '../users.service';
@Component({
selector: 'app-change-password-modal',
templateUrl: './change-password-modal.component.html',
styleUrls: ['./change-password-modal.component.css']
})
export class ChangePasswordModalComponent {
@Output() userEdited = new EventEmitter<void>();
userForm: FormGroup;
passwordMismatch: boolean = false;
updateError: boolean = false;
constructor(
public dialogRef: MatDialogRef<EditUserModalComponent>,
@Inject(MAT_DIALOG_DATA) public data: any,
private fb: FormBuilder,
private userService: UserService
) {
this.userForm = this.fb.group({
username: [this.data.user, Validators.required],
password: ['', Validators.required],
confirmPassword: ['', Validators.required]
}, { validators: this.passwordMatchValidator });
}
ngOnInit(): void {}
onNoClick(): void {
this.dialogRef.close();
}
onSubmit(): void {
if (this.userForm.valid) {
this.passwordMismatch = false;
this.updateError = false;
const userPayload = {
username: this.userForm.value.username,
allowedOrganizationalUnits: this.data.user.allowedOrganizationalUnits,
password: this.userForm.value.password,
enabled: true,
userGroups: this.data.user.userGroups
};
this.userService.updateUser(this.data.uuid, userPayload).subscribe(
response => {
console.log('User updated successfully:', response);
this.userEdited.emit();
this.dialogRef.close(this.userForm.value);
},
error => {
console.error('Error updating user:', error);
this.updateError = true;
}
);
} else {
console.error('Form is invalid');
this.passwordMismatch = this.userForm.hasError('mismatch');
this.updateError = true;
}
}
private passwordMatchValidator(form: FormGroup): { [key: string]: boolean } | null {
return form.get('password')?.value === form.get('confirmPassword')?.value ? null : { mismatch: true };
}
}