Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component, OnInit, ViewChild, OnDestroy, Input } from '@angular/core';
- import { ActivatedRoute } from '@angular/router';
- import {
- GrowlBoxComponent, AlertDialogComponent, LoadingLayerService, OverlayDialogComponent
- } from '@aeb/next-ui-components';
- import { NextUserService } from '../service/next-user.service';
- import { NextUserRoleComponent } from '../next-user-role/next-user-role.component';
- import { NextUserHelper } from '../controller/helper/next-user.helper';
- import { Subscription } from 'apollo-client';
- import { NextUser, NextUserDocument } from '../service/types/next-user.type';
- import { NextUserRole, NextUserRoleSimple } from '../service/types/next-user-role.type';
- import { DeleteNextUserInputVariables } from '../service/generated/schema_sapp';
- import { NextUserRoleService } from 'app/service/next-user-role.service';
- import { AllNextUserRolesQueryVariables, GetNextUserQuery } from '../service/generated/schema_modelmanager';
- import * as bcrypt from 'bcryptjs';
- @Component({
- templateUrl: 'next-user.component.html',
- selector: 'app-next-user',
- styleUrls: ['next-user.component.less']
- })
- export class NextUserComponent implements OnInit, OnDestroy {
- unchangableData: NextUser = new NextUser();
- changableData: NextUserDocument = new NextUserDocument();
- nextUserHelper: NextUserHelper = new NextUserHelper();
- userId: string;
- nextUserServiceSub: Subscription;
- nextUserRoleServiceSub: Subscription;
- allNextUserRoles: NextUserRole[] = [];
- unhashedPassword: string;
- @Input() flag: boolean = false;
- descriptions = {
- titleHeader: undefined,
- titleRoles: undefined,
- titleUser: undefined
- };
- variables: AllNextUserRolesQueryVariables = {
- orderBy: null,
- first: null,
- after: null,
- filter: null
- };
- @ViewChild('growl') growl: GrowlBoxComponent;
- @ViewChild('systemDomainDeleteWarning') systemDomainDeleteWarning: AlertDialogComponent;
- @ViewChild('editRoleDialog') editRoleDialog: NextUserRoleComponent;
- @ViewChild('dialog') dialog: AlertDialogComponent;
- @ViewChild('passwordDialog') passwordDialog: OverlayDialogComponent;
- constructor(private route: ActivatedRoute,
- private nextUserService: NextUserService,
- private nextUserRoleService: NextUserRoleService,
- private loadingLayerService: LoadingLayerService) {
- }
- ngOnInit(): void {
- this.route.params.subscribe(params => {
- this.userId = params['id'];
- this.loadingLayerService.getState('detail').activate();
- if (this.userId === '/new') {
- this.initData(null);
- } else if (this.userId) {
- this.destroySub();
- this.nextUserServiceSub = this.nextUserService.watchGetNextUser({id: this.userId}).subscribe(res => {
- this.initData(res.data);
- this.createDescriptions();
- this.loadingLayerService.getState('detail').deactivate();
- }, console.error);
- }
- this.nextUserRoleServiceSub = this.nextUserRoleService.watchAllNextUserRoles(this.variables).subscribe(res => {
- this.allNextUserRoles = res.data.allNextUserRoles;
- }, console.error);
- }, console.error);
- }
- initData(data: GetNextUserQuery) {
- if (data.nextUser) {
- this.unchangableData = data.nextUser;
- this.changableData = this.nextUserHelper.copyObject(data.nextUser);
- } else {
- this.unchangableData = new NextUser();
- this.changableData.identCode = undefined;
- this.changableData.id = undefined;
- this.changableData.password = undefined;
- this.changableData.roles = new Array<string>();
- }
- }
- hashPassword(password: string): string {
- const salt = bcrypt.genSaltSync(10);
- const hashedPassword = bcrypt.hashSync(password, salt);
- return hashedPassword;
- }
- createDescriptions() {
- this.descriptions.titleRoles = 'Rollen (' + this.changableData.roles.length + ')';
- this.descriptions.titleUser = 'Benutzerinfo';
- if (this.changableData.roles.length === 1) {
- this.descriptions.titleHeader = this.changableData.roles.length + ' Rolle';
- } else {
- this.descriptions.titleHeader = this.changableData.roles.length + ' Rollen';
- }
- }
- ngOnDestroy() {
- this.destroySub();
- }
- destroySub() {
- if (this.nextUserServiceSub && this.nextUserRoleServiceSub) {
- this.nextUserServiceSub.unsubscribe();
- this.nextUserRoleServiceSub.unsubscribe();
- this.nextUserServiceSub = undefined;
- this.nextUserRoleServiceSub = undefined;
- }
- }
- getRoleDescription(role: string) {
- for (let cuRole of this.allNextUserRoles) {
- if (cuRole.id.valueOf() === role.valueOf()) {
- return cuRole.identCode;
- }
- }
- return '....';
- }
- createOrUpdateNextUser() {
- if (!this.unchangableData.id) {
- this.createNextUser();
- } else {
- this.updateNextUser();
- }
- }
- updateNextUser() {
- this.nextUserService.updateUser(this.changableData).then(res => {
- if (res.data.updateNextUser.success) {
- this.growl.showSuccess('Benutzer gespeichert');
- } else {
- this.showErrorMessage('Benutzer löschen', res.data.updateNextUser.validationErrors);
- }
- });
- }
- deleteNextUser() {
- this.systemDomainDeleteWarning.open();
- }
- closeSystemDomainDeleteWarning() {
- this.systemDomainDeleteWarning.close();
- }
- confirmSystemDomainDeleteWarning() {
- this.systemDomainDeleteWarning.close();
- this.deleteNextUserBasic();
- }
- deleteNextUserBasic() {
- let input: DeleteNextUserInputVariables = {
- id: undefined
- };
- input.id = this.changableData.id;
- this.nextUserService.deleteUser(input).then(res => {
- if (res.data.deleteNextUser.success) {
- this.growl.showSuccess('Benutzer gelöscht');
- } else {
- this.showErrorMessage('Benutzer löschen', res.data.deleteNextUser.validationErrors);
- }
- });
- }
- removeRoleFromUser(role: string) {
- let i = 0;
- for (let curRole of this.changableData.roles) {
- if (role.valueOf() === curRole.valueOf()) {
- this.changableData.roles.splice(i, 1);
- }
- i++;
- }
- }
- cancelNextUser() {
- if (this.userId === 'new') {
- if (this.changableData.id !== null) {
- this.deleteNextUserBasic();
- }
- this.changableData = new NextUserDocument();
- this.unchangableData = new NextUser();
- this.unchangableData.roles = new Array<NextUserRoleSimple>();
- } else {
- this.nextUserService.getUser(this.changableData).then(res => {
- this.unchangableData = res.data.nextUser;
- this.changableData = this.nextUserHelper.copyObject(res.data.nextUser);
- });
- }
- }
- async openAddRoleForm() {
- let success: boolean = true;
- if (!this.changableData.id) {
- success = await this.createNextUser();
- }
- if (success) {
- this.editRoleDialog.changableData = this.changableData;
- this.editRoleDialog.unchangableData = this.unchangableData;
- this.editRoleDialog.allNextUserRoles = this.allNextUserRoles;
- this.editRoleDialog.title = 'Rolle verwalten';
- this.openRoleForm();
- }
- }
- async openEditRoleForm(roleFromUi: string) {
- let success: boolean = true;
- if (!this.changableData.id) {
- success = await this.createNextUser();
- }
- if (success) {
- this.editRoleDialog.changableData = this.changableData;
- this.editRoleDialog.unchangableData = this.unchangableData;
- this.editRoleDialog.allNextUserRoles = this.allNextUserRoles;
- this.editRoleDialog.title = 'Rolle verwalten';
- this.openRoleForm();
- }
- }
- openEditPassword() {
- console.log('Edit password');
- this.passwordDialog.open();
- }
- passwordDialogOnCancel() {
- this.passwordDialog.close();
- }
- passwordDialogOnOk() {
- console.log(this.unhashedPassword);
- console.log(this.hashPassword(this.unhashedPassword));
- this.changableData.password = this.hashPassword(this.unhashedPassword);
- this.passwordDialog.close();
- }
- private openRoleForm() {
- setTimeout(() => {
- this.editRoleDialog.open();
- }, 10);
- }
- async createNextUser(): Promise<boolean> {
- return this.nextUserService.createUser(this.changableData).then(res => {
- if (res.data.createNextUser.success) {
- this.unchangableData = res.data.createNextUser.user;
- this.changableData = this.nextUserHelper.copyObject(res.data.createNextUser.user);
- return true;
- } else {
- this.showErrorMessage('Benutzer anlegen', res.data.createNextUser.validationErrors);
- return false;
- }
- });
- }
- showErrorMessage(title: string, validationErrors: any[]) {
- let errorMessage = title;
- for (let validationError of validationErrors) {
- errorMessage = errorMessage + validationError.errorMessage + '\n\n';
- }
- this.dialog.open(errorMessage, 'error');
- }
- }
Add Comment
Please, Sign In to add comment