feat: add user change password component (#660)

Fixes: #657
This commit is contained in:
Benura Abeywardena
2020-05-16 13:01:27 +05:30
committed by GitHub
parent 0c2af151f3
commit eb56bdc0ad
8 changed files with 172 additions and 2 deletions
@@ -0,0 +1,50 @@
<h1 mat-dialog-title>Change Password</h1>
<div mat-dialog-content>
<form [formGroup]="changePasswordForm">
<div fxLayout="column">
<mat-form-field>
<mat-label>Password</mat-label>
<input matInput formControlName="password" type="password">
<mat-error *ngIf="changePasswordForm.controls.password.hasError('required')">
Password <strong>is required</strong>
</mat-error>
<mat-error *ngIf="changePasswordForm.controls.password.hasError('minlength')">
Password should be at least <strong>8 characters long</strong>
</mat-error>
<mat-error *ngIf="changePasswordForm.controls.password.hasError('maxlength')">
Password should not be more than <strong>50 characters long</strong>
</mat-error>
<mat-error *ngIf="changePasswordForm.controls.password.hasError('pattern')">
Password should include a <strong>numeral</strong> and at <strong>least one uppercase</strong> ,<strong>lowercase and special character</strong>
</mat-error>
</mat-form-field>
</div>
<div fxLayout="column">
<mat-form-field>
<mat-label>Repeat Password</mat-label>
<input matInput formControlName="repeatPassword" type="password">
<mat-error *ngIf="changePasswordForm.controls.repeatPassword.hasError('required')">
Password <strong>required</strong>
</mat-error>
<mat-error *ngIf="changePasswordForm.controls.repeatPassword.hasError('notequal')">
Password <strong>does not match</strong>
</mat-error>
</mat-form-field>
</div>
</form>
</div>
<mat-dialog-actions align="center">
<button mat-raised-button mat-dialog-close>Cancel</button>
<button mat-raised-button color="warn" [disabled]="!changePasswordForm.valid" [mat-dialog-close]="{password:changePasswordForm.get('password').value,repeatPassword:changePasswordForm.get('repeatPassword').value}">Confirm</button>
</mat-dialog-actions>
@@ -0,0 +1,25 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ChangePasswordDialogComponent } from './change-password-dialog.component';
describe('ChangePasswordDialogComponent', () => {
let component: ChangePasswordDialogComponent;
let fixture: ComponentFixture<ChangePasswordDialogComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ChangePasswordDialogComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ChangePasswordDialogComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
@@ -0,0 +1,60 @@
/** Angular Imports */
import { Component, OnInit, Inject } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
import { FormBuilder, Validators, ValidatorFn, AbstractControl, ValidationErrors } from '@angular/forms';
import { Subscription } from 'rxjs';
/**
* Change Password Dialog component.
*/
@Component({
selector: 'mifosx-change-password-dialog',
templateUrl: './change-password-dialog.component.html',
styleUrls: ['./change-password-dialog.component.scss']
})
export class ChangePasswordDialogComponent implements OnInit {
/** Change Password Form */
changePasswordForm: any;
/**
* @param {MatDialogRef} dialogRef Component reference to dialog.
* @param {any} data Provides any data.
*/
constructor(public dialogRef: MatDialogRef<ChangePasswordDialogComponent>,
@Inject(MAT_DIALOG_DATA) public data: any,
private formBuilder: FormBuilder) { }
ngOnInit() {
this.createChangePasswordForm();
}
/** Change Password form */
createChangePasswordForm() {
this.changePasswordForm = this.formBuilder.group({
'password': ['', [Validators.required, Validators.pattern('^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&].{8,}$'), Validators.maxLength(50), Validators.minLength(8)]],
'repeatPassword': ['', [Validators.required, this.confirmPassword('password')]]
});
}
/**
* Confirm Change Password of Users
* @param controlNameToCompare Form Control Name to be compared.
*/
confirmPassword(controlNameToCompare: string): ValidatorFn {
return (c: AbstractControl): ValidationErrors|null => {
if (c.value == null || c.value.length === 0) {
return null;
}
const controlToCompare = c.root.get(controlNameToCompare);
if (controlToCompare) {
const subscription: Subscription = controlToCompare.valueChanges.subscribe(() => {
c.updateValueAndValidity();
subscription.unsubscribe();
});
}
return controlToCompare && controlToCompare.value !== c.value ? {'notequal': true} : null;
};
}
}
+4 -1
View File
@@ -11,6 +11,7 @@ import { FileUploadComponent } from './file-upload/file-upload.component';
import { FooterComponent } from './footer/footer.component';
import { LanguageSelectorComponent } from './language-selector/language-selector.component';
import { ThemePickerComponent } from './theme-picker/theme-picker.component';
import { ChangePasswordDialogComponent } from './change-password-dialog/change-password-dialog.component';
/** Custom Modules */
import { IconsModule } from './icons.module';
@@ -36,6 +37,7 @@ import { MaterialModule } from './material.module';
FooterComponent,
LanguageSelectorComponent,
ThemePickerComponent,
ChangePasswordDialogComponent
],
exports: [
FileUploadComponent,
@@ -50,7 +52,8 @@ import { MaterialModule } from './material.module';
],
entryComponents: [
FormDialogComponent,
DeleteDialogComponent
DeleteDialogComponent,
ChangePasswordDialogComponent
]
})
export class SharedModule { }
+10
View File
@@ -48,6 +48,16 @@ export class UsersService {
return this.http.get(`/users/${userId}`);
}
/**
* Change User Password
* @param userId User Id of users
* @param password New Password of the user
* @returns {Observable<any>}
*/
changePassword(userId: string, passwordObj: any) {
return this.http.put(`/users/${userId}`, passwordObj);
}
/**
* @param {string} userId user ID of user.
* @returns {Observable<any>}
@@ -7,7 +7,7 @@
<fa-icon icon="trash"></fa-icon>&nbsp;&nbsp;
Delete
</button>
<button mat-raised-button color="primary">
<button mat-raised-button color="primary" (click)="changeUserPassword()">
<fa-icon icon="cog"></fa-icon>&nbsp;&nbsp;
Change Password
</button>
@@ -8,6 +8,7 @@ import { UsersService } from '../users.service';
/** Custom Components */
import { DeleteDialogComponent } from 'app/shared/delete-dialog/delete-dialog.component';
import { ChangePasswordDialogComponent } from 'app/shared/change-password-dialog/change-password-dialog.component';
/**
* View user component.
@@ -58,4 +59,25 @@ export class ViewUserComponent implements OnInit {
});
}
/**
* Change Password of the Users.
*/
changeUserPassword() {
const changeUserPasswordDialogRef = this.dialog.open(ChangePasswordDialogComponent, {
width: '400px',
height: '300px'
});
changeUserPasswordDialogRef.afterClosed().subscribe((response: any) => {
if (response.password && response.repeatPassword) {
const password = response.password;
const repeatPassword = response.repeatPassword;
const firstname = this.userData.firstname;
const data = {password: password, repeatPassword: repeatPassword, firstname: firstname};
this.usersService.changePassword(this.userData.id, data).subscribe(() => {
this.router.navigate(['/users']);
});
}
});
}
}