Summary Table
Categories |
Total Count |
PII |
0 |
URL |
0 |
DNS |
0 |
EKL |
0 |
IP |
0 |
PORT |
0 |
VsID |
0 |
CF |
0 |
AI |
0 |
VPD |
0 |
PL |
0 |
Other |
0 |
File Content
import { FormBuilder, Validators, FormGroup } from '@angular/forms';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs/Subscription';
import {
UserRequestModel,
UserModel,
SearchModel,
PaginationModel
} from './user-admin.model';
import { UserAdminService } from './user-admin.service';
import {
TableColumnModel,
TableSettings,
PaginationSettings
} from '../../shared/table';
import { initialValueChanged } from './modifyUser-initial-valueChanged.directive';
import { AuthenticationService } from '../../auth/auth.service';
import { AppSettingsService } from './../../shared/app-settings/app-settings.service';
import { MenuModel } from './../../shared/menu/menu.component.model';
// function YN(yes: boolean): string {
// return yes ? 'Y' : 'N';
// }
const emailRx = /[a-zA-Z0-9._%+-]+@[a-zA-Z.-]{2,}[.][a-zA-Z]{2,}/;
@Component({
selector: 'app-user-admin',
templateUrl: './user-admin.component.html',
styleUrls: ['./user-admin.component.scss'],
providers: [UserAdminService]
})
export class UserAdminComponent implements OnInit, OnDestroy {
title = 'ARS User Administration';
menu: MenuModel;
deleteEvent;
errorStatus = false;
errorMsg = '';
modalClosed = true;
// deleteModalClosed = true;
showResults = false;
searchUserClicked = false;
createForm: FormGroup;
searchForm: FormGroup;
modifyForm: FormGroup;
requestModel: UserRequestModel = new UserRequestModel();
editRequestModel: UserRequestModel = new UserRequestModel();
paginationModel: PaginationModel = new PaginationModel();
searchModel: SearchModel = new SearchModel();
searchResults: UserModel[] = [];
loading = true;
infoStatus = false;
infoMsg = '';
modalError = false;
successMsg = '';
successStatus = false;
subscription: Subscription = new Subscription();
enableEdit = false;
enableExport = false;
isAdmin = false;
status = true;
modifyEnableEdit = false;
modifyEnableExport = false;
modifyIsAdmin = false;
modifyStatus = true;
userValueBeforeChange: UserRequestModel;
tabIdx = '0';
constructor(
private fb: FormBuilder,
private userAdminService: UserAdminService,
private authenticationService: AuthenticationService,
private appSettingsService: AppSettingsService
) {}
tableColumns: TableColumnModel[] = [
new TableColumnModel('UserName', 'userName'),
new TableColumnModel('Domain ', 'domain'),
new TableColumnModel('Last Name', 'lastName'),
new TableColumnModel('First Name', 'firstName'),
new TableColumnModel('Phone Number', 'phone'),
new TableColumnModel('Email', 'email'),
new TableColumnModel('Status', 'status'),
new TableColumnModel('Export', 'export'),
new TableColumnModel('Edit', 'edit'),
new TableColumnModel('Admin', 'admin')
];
tableSettings: TableSettings = new TableSettings();
paginationSettings: PaginationSettings = new PaginationSettings();
ngOnInit() {
this.appSettingsService
.getMenu('ARS_MENU')
.subscribe(menu => (this.menu = menu));
this.tableSettings.buttonShow = true;
this.tableSettings.buttonNames = ['Modify'];
this.formInit();
}
formInit() {
this.createForm = this.fb.group({
userName: [
this.requestModel.userName,
[
Validators.required,
Validators.pattern('[\x00-\x7F]+'),
Validators.max(30)
]
],
domain: this.requestModel.domain,
export: this.enableExport,
edit: this.enableEdit,
admin: this.isAdmin,
phone: [this.requestModel.phone, Validators.pattern('[0-9]*')],
email: [
this.requestModel.email,
[Validators.required, Validators.pattern(emailRx)]
],
firstName: this.requestModel.firstName,
lastName: this.requestModel.lastName,
status: this.status
});
this.searchForm = this.fb.group({
userName: [
this.searchModel.userName,
[
Validators.required,
Validators.pattern('[\x00-\x7F]+'),
Validators.max(30)
]
],
includeInactive: this.paginationModel.inActiveUsers
});
this.modifyForm = this.fb.group({
editDomain: this.editRequestModel.domain,
editExport: this.modifyEnableExport,
editEdit: this.modifyEnableEdit,
editAdmin: this.modifyIsAdmin,
editPhone: [this.editRequestModel.phone, Validators.pattern('[0-9]*')],
editEmail: [
this.editRequestModel.email,
[Validators.pattern(emailRx), Validators.required]
],
editFirstName: this.editRequestModel.firstName,
editLastName: this.editRequestModel.lastName,
editStatus: this.modifyStatus
});
}
search() {
this.searchUserClicked = true;
this.paginationSettings = new PaginationSettings();
this.fetchData();
}
fetchData() {
this.loading = true;
this.errorMsg = '';
this.errorStatus = false;
this.infoStatus = false;
this.infoMsg = '';
// If search for a particular user is clicked
if (this.searchUserClicked) {
const searchUserSubscription = this.userAdminService
.getUserInfo(this.searchModel.userName.trim())
.subscribe(
data => {
this.showResults = true;
this.setUserRoles(data);
this.tableSettings.pagination = false;
this.errorMsg = '';
this.errorStatus = false;
this.loading = false;
},
error => {
if (error.status === 400) {
this.infoStatus = true;
this.infoMsg = error.error.message;
} else {
this.errorMsg = error.error.message;
this.errorStatus = true;
}
this.showResults = false;
this.loading = false;
}
);
this.subscription.add(searchUserSubscription);
} else {
// List all users is clicked
// Clears search form
this.formInit();
this.searchModel = new SearchModel();
// Sets pagination settings for the table
this.paginationModel.pageSize = this.paginationSettings.pageSize;
this.paginationModel.pageNumber = this.paginationSettings.currentPage;
this.paginationModel.descending = this.paginationSettings.descending;
this.paginationModel.sortColumn = this.paginationSettings.sortColumn;
const allUserSubscription = this.userAdminService
.getAllUsers(this.paginationModel)
.subscribe(
data => {
this.showResults = true;
this.tableSettings.pagination = true;
// this.searchResults = data.response;
this.setUsersRoles(data.response);
this.errorMsg = '';
this.errorStatus = false;
this.loading = false;
this.paginationSettings = {
currentPage: data.pageNumber,
pageSize: data.pageSize,
totalPages: Math.ceil(data.totalNumberOfResults / data.pageSize),
totalResults: data.totalNumberOfResults,
sortColumn: data.sortColumn,
descending: this.paginationModel.descending
};
},
error => {
this.loading = false;
this.errorMsg = error.statusText;
this.errorStatus = true;
}
);
this.subscription.add(allUserSubscription);
}
}
updateTable() {
this.fetchData();
}
listAllUsers() {
this.searchUserClicked = false;
this.clearMsgs();
this.paginationSettings = new PaginationSettings();
this.fetchData();
}
resetSearchForm() {
this.showResults = false;
this.searchUserClicked = false;
this.formInit();
this.clearMsgs();
this.searchModel = new SearchModel();
this.paginationModel = new PaginationModel();
}
clearMsgs() {
this.errorMsg = '';
this.errorStatus = false;
this.successMsg = '';
this.successStatus = false;
this.infoStatus = false;
this.infoMsg = '';
}
resetCreateForm() {
this.clearMsgs();
this.formInit();
this.enableEdit = false;
this.enableExport = false;
this.isAdmin = false;
this.status = true;
this.requestModel = new UserRequestModel();
}
buttonClicked(event) {
// if (event.buttonName === 'Modify') {
this.openEditModal(event);
// }
// else {
// this.openDeleteModal(event);
// // this.delete(event);
// }
}
// openDeleteModal(event) {
// this.deleteModalClosed = false;
// this.deleteEvent = { ...event };
// }
openEditModal(event) {
this.editRequestModel = { ...event.row };
this.userValueBeforeChange = { ...event.row };
// Custom validator to check if initial values in the edit modal changed or not
this.setCustomValidator(this.modifyForm, this.userValueBeforeChange);
this.modalClosed = false;
this.tabIdx = '-1';
this.modifyEnableEdit = this.editRequestModel.userRoles.some(
role => role === 'ARS_EDIT_ATTACHMENT_USER'
);
this.modifyStatus = !this.editRequestModel.userRoles.some(
role => role === 'ARS_INACTIVE'
);
this.modifyIsAdmin = this.editRequestModel.userRoles.some(
role => role === 'ARS_ADMIN'
);
this.modifyEnableExport = this.editRequestModel.userRoles.some(
role => role === 'ARS_EXPORT_ATTACHMENT_USER'
);
}
// delete() {
// const deleteSubscription = this.userAdminService
// .deleteUserInfo(this.deleteEvent.row.userId)
// .subscribe(data => {
// if (data.errorCode) {
// this.errorMsg = 'An error occurred during user deletion.';
// this.errorStatus = true;
// this.successStatus = false;
// this.successMsg = '';
// this.showResults = false;
// } else {
// if (this.searchUserClicked) {
// this.showResults = false;
// } else {
// this.successMsg = 'User was successfully deleted.';
// this.successStatus = true;
// this.errorMsg = '';
// this.errorStatus = false;
// this.showResults = true;
// this.listAllUsers();
// this.onDeleteModalClose();
// }
// }
// });
// this.subscription.add(deleteSubscription);
// }
setCustomValidator(form: FormGroup, initialObject: UserRequestModel) {
form.setValidators([initialValueChanged(initialObject)]);
form.updateValueAndValidity();
}
// Creates user on submitting create user form
createUser() {
this.clearMsgs();
this.setRequestRoles(true);
this.requestModel.createdBy = this.authenticationService.getDecodedUserInfo().userName;
this.requestModel.system = 'ARS';
const createSubscription = this.userAdminService
.createUser(this.requestModel)
.subscribe(
data => {
this.formInit();
this.enableEdit = false;
this.enableExport = false;
this.isAdmin = false;
this.status = true;
this.requestModel = new UserRequestModel();
this.successMsg = 'User was successfully created.';
this.successStatus = true;
this.errorMsg = '';
this.errorStatus = false;
},
error => {
if (error.status = 405) {
this.infoStatus = true;
this.infoMsg = error.error.message;
} else {
this.errorMsg = error.error.message;
this.errorStatus = true;
this.successMsg = '';
this.successStatus = false;
}
}
);
this.subscription.add(createSubscription);
}
setRequestRoles(isCreate: boolean) {
this.requestModel.userRoles = ['ARS_BASE_ATTACHMENT_USER'];
// If setting roles on create request
if (isCreate) {
if (this.enableEdit) {
this.requestModel.userRoles.push('ARS_EDIT_ATTACHMENT_USER');
}
if (this.enableExport) {
this.requestModel.userRoles.push('ARS_EXPORT_ATTACHMENT_USER');
}
if (this.isAdmin) {
this.requestModel.userRoles.push('ARS_ADMIN');
}
if (!this.status) {
this.requestModel.userRoles.push('ARS_INACTIVE');
}
} else {
this.editRequestModel.userRoles = ['ARS_BASE_ATTACHMENT_USER'];
// Setting roles for modify request
if (this.modifyEnableEdit) {
this.editRequestModel.userRoles.push('ARS_EDIT_ATTACHMENT_USER');
}
if (this.modifyEnableExport) {
this.editRequestModel.userRoles.push('ARS_EXPORT_ATTACHMENT_USER');
}
if (this.modifyIsAdmin) {
this.editRequestModel.userRoles.push('ARS_ADMIN');
}
if (!this.modifyStatus) {
this.editRequestModel.userRoles.push('ARS_INACTIVE');
}
}
}
// For List all users functionality
setUsersRoles(usersInfo: UserModel[]) {
this.searchResults = [];
usersInfo.forEach(user => {
user.admin = user.userRoles.some(role => role === 'ARS_ADMIN')
? 'Y'
: 'N';
user.export = user.userRoles.some(
role => role === 'ARS_EXPORT_ATTACHMENT_USER'
)
? 'Y'
: 'N';
user.edit = user.userRoles.some(
role => role === 'ARS_EDIT_ATTACHMENT_USER'
)
? 'Y'
: 'N';
user.status = user.userRoles.some(role => role === 'ARS_INACTIVE')
? 'Inactive'
: 'Active';
});
this.searchResults = usersInfo;
}
// Sets roles for individual user
setUserRoles(user: UserModel) {
this.searchResults = [];
user.admin = user.userRoles.some(role => role === 'ARS_ADMIN') ? 'Y' : 'N';
user.export = user.userRoles.some(
role => role === 'ARS_EXPORT_ATTACHMENT_USER'
)
? 'Y'
: 'N';
user.edit = user.userRoles.some(role => role === 'ARS_EDIT_ATTACHMENT_USER')
? 'Y'
: 'N';
user.status = user.userRoles.some(role => role === 'ARS_INACTIVE')
? 'Inactive'
: 'Active';
this.searchResults.push(user);
}
modifyUser() {
this.modalClosed = true;
this.clearMsgs();
this.tabIdx = '0';
this.setRequestRoles(false);
this.editRequestModel.system = 'ARS';
this.editRequestModel.modifiedBy = this.authenticationService.getDecodedUserInfo().userName;
const modifySubscription = this.userAdminService
.modifyUser(this.editRequestModel)
.subscribe(
data => {
this.successMsg = 'User was successfully modified.';
this.successStatus = true;
this.errorMsg = '';
this.errorStatus = false;
if (!this.searchUserClicked) {
// Reset the search user form
this.formInit();
this.searchModel = new SearchModel();
// Update the search results table
this.paginationSettings = new PaginationSettings();
this.fetchData();
} else {
this.fetchData();
}
},
error => {
this.errorMsg = 'An error occurred during user modification.';
this.errorStatus = true;
this.successMsg = '';
this.successStatus = false;
}
);
this.subscription.add(modifySubscription);
}
onModalClose(event) {
this.modalClosed = true;
this.tabIdx = '0';
}
// onDeleteModalClose() {
// this.deleteModalClosed = true;
// this.deleteEvent = null;
// }
// Resets Edit Modal
resetModal() {
this.editRequestModel = { ...this.userValueBeforeChange };
this.modalClosed = true;
this.tabIdx = '0';
}
// Unsubscribe all subscriptions
ngOnDestroy() {
this.subscription.unsubscribe();
}
}