Guest User

Untitled

a guest
Jul 4th, 2017
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit, ViewChild, OnDestroy, Input } from '@angular/core';
  2. import { ActivatedRoute } from '@angular/router';
  3. import {
  4.   GrowlBoxComponent, AlertDialogComponent, LoadingLayerService, OverlayDialogComponent
  5. } from '@aeb/next-ui-components';
  6. import { NextUserService } from '../service/next-user.service';
  7. import { NextUserRoleComponent } from '../next-user-role/next-user-role.component';
  8. import { NextUserHelper } from '../controller/helper/next-user.helper';
  9. import { Subscription } from 'apollo-client';
  10. import { NextUser, NextUserDocument } from '../service/types/next-user.type';
  11. import { NextUserRole, NextUserRoleSimple } from '../service/types/next-user-role.type';
  12. import { DeleteNextUserInputVariables } from '../service/generated/schema_sapp';
  13. import { NextUserRoleService } from 'app/service/next-user-role.service';
  14. import { AllNextUserRolesQueryVariables, GetNextUserQuery } from '../service/generated/schema_modelmanager';
  15. import * as bcrypt from 'bcryptjs';
  16.  
  17. @Component({
  18.   templateUrl: 'next-user.component.html',
  19.   selector: 'app-next-user',
  20.   styleUrls: ['next-user.component.less']
  21. })
  22. export class NextUserComponent implements OnInit, OnDestroy {
  23.  
  24.   unchangableData: NextUser = new NextUser();
  25.   changableData: NextUserDocument = new NextUserDocument();
  26.   nextUserHelper: NextUserHelper = new NextUserHelper();
  27.   userId: string;
  28.   nextUserServiceSub: Subscription;
  29.   nextUserRoleServiceSub: Subscription;
  30.   allNextUserRoles: NextUserRole[] = [];
  31.   unhashedPassword: string;
  32.   @Input() flag: boolean = false;
  33.  
  34.   descriptions = {
  35.     titleHeader: undefined,
  36.     titleRoles: undefined,
  37.     titleUser: undefined
  38.   };
  39.  
  40.   variables: AllNextUserRolesQueryVariables = {
  41.     orderBy: null,
  42.     first: null,
  43.     after: null,
  44.     filter: null
  45.   };
  46.  
  47.   @ViewChild('growl') growl: GrowlBoxComponent;
  48.   @ViewChild('systemDomainDeleteWarning') systemDomainDeleteWarning: AlertDialogComponent;
  49.   @ViewChild('editRoleDialog') editRoleDialog: NextUserRoleComponent;
  50.   @ViewChild('dialog') dialog: AlertDialogComponent;
  51.   @ViewChild('passwordDialog') passwordDialog: OverlayDialogComponent;
  52.  
  53.  
  54.   constructor(private route: ActivatedRoute,
  55.               private nextUserService: NextUserService,
  56.               private nextUserRoleService: NextUserRoleService,
  57.               private loadingLayerService: LoadingLayerService) {
  58.   }
  59.  
  60.   ngOnInit(): void {
  61.     this.route.params.subscribe(params => {
  62.       this.userId = params['id'];
  63.       this.loadingLayerService.getState('detail').activate();
  64.       if (this.userId === '/new') {
  65.         this.initData(null);
  66.       } else if (this.userId) {
  67.         this.destroySub();
  68.         this.nextUserServiceSub = this.nextUserService.watchGetNextUser({id: this.userId}).subscribe(res => {
  69.           this.initData(res.data);
  70.           this.createDescriptions();
  71.           this.loadingLayerService.getState('detail').deactivate();
  72.         }, console.error);
  73.       }
  74.       this.nextUserRoleServiceSub = this.nextUserRoleService.watchAllNextUserRoles(this.variables).subscribe(res => {
  75.         this.allNextUserRoles = res.data.allNextUserRoles;
  76.       }, console.error);
  77.     }, console.error);
  78.   }
  79.  
  80.   initData(data: GetNextUserQuery) {
  81.     if (data.nextUser) {
  82.       this.unchangableData = data.nextUser;
  83.       this.changableData = this.nextUserHelper.copyObject(data.nextUser);
  84.     } else {
  85.       this.unchangableData = new NextUser();
  86.       this.changableData.identCode = undefined;
  87.       this.changableData.id = undefined;
  88.       this.changableData.password = undefined;
  89.       this.changableData.roles = new Array<string>();
  90.     }
  91.  
  92.   }
  93.  
  94.   hashPassword(password: string): string {
  95.     const salt = bcrypt.genSaltSync(10);
  96.     const hashedPassword = bcrypt.hashSync(password, salt);
  97.  
  98.     return hashedPassword;
  99.   }
  100.  
  101.   createDescriptions() {
  102.     this.descriptions.titleRoles = 'Rollen (' + this.changableData.roles.length + ')';
  103.     this.descriptions.titleUser = 'Benutzerinfo';
  104.  
  105.     if (this.changableData.roles.length === 1) {
  106.       this.descriptions.titleHeader = this.changableData.roles.length + ' Rolle';
  107.     } else {
  108.       this.descriptions.titleHeader = this.changableData.roles.length + ' Rollen';
  109.     }
  110.   }
  111.  
  112.   ngOnDestroy() {
  113.     this.destroySub();
  114.   }
  115.  
  116.   destroySub() {
  117.     if (this.nextUserServiceSub && this.nextUserRoleServiceSub) {
  118.       this.nextUserServiceSub.unsubscribe();
  119.       this.nextUserRoleServiceSub.unsubscribe();
  120.       this.nextUserServiceSub = undefined;
  121.       this.nextUserRoleServiceSub = undefined;
  122.     }
  123.   }
  124.  
  125.   getRoleDescription(role: string) {
  126.     for (let cuRole of this.allNextUserRoles) {
  127.       if (cuRole.id.valueOf() === role.valueOf()) {
  128.         return cuRole.identCode;
  129.       }
  130.     }
  131.     return '....';
  132.   }
  133.  
  134.   createOrUpdateNextUser() {
  135.     if (!this.unchangableData.id) {
  136.       this.createNextUser();
  137.     } else {
  138.       this.updateNextUser();
  139.     }
  140.   }
  141.  
  142.   updateNextUser() {
  143.     this.nextUserService.updateUser(this.changableData).then(res => {
  144.       if (res.data.updateNextUser.success) {
  145.         this.growl.showSuccess('Benutzer gespeichert');
  146.       } else {
  147.         this.showErrorMessage('Benutzer löschen', res.data.updateNextUser.validationErrors);
  148.       }
  149.     });
  150.   }
  151.  
  152.   deleteNextUser() {
  153.     this.systemDomainDeleteWarning.open();
  154.   }
  155.  
  156.   closeSystemDomainDeleteWarning() {
  157.     this.systemDomainDeleteWarning.close();
  158.   }
  159.  
  160.   confirmSystemDomainDeleteWarning() {
  161.     this.systemDomainDeleteWarning.close();
  162.     this.deleteNextUserBasic();
  163.   }
  164.  
  165.   deleteNextUserBasic() {
  166.     let input: DeleteNextUserInputVariables = {
  167.       id: undefined
  168.     };
  169.     input.id = this.changableData.id;
  170.  
  171.     this.nextUserService.deleteUser(input).then(res => {
  172.       if (res.data.deleteNextUser.success) {
  173.         this.growl.showSuccess('Benutzer gelöscht');
  174.       } else {
  175.         this.showErrorMessage('Benutzer löschen', res.data.deleteNextUser.validationErrors);
  176.       }
  177.     });
  178.   }
  179.  
  180.   removeRoleFromUser(role: string) {
  181.     let i = 0;
  182.     for (let curRole of this.changableData.roles) {
  183.       if (role.valueOf() === curRole.valueOf()) {
  184.         this.changableData.roles.splice(i, 1);
  185.       }
  186.       i++;
  187.     }
  188.   }
  189.  
  190.   cancelNextUser() {
  191.     if (this.userId === 'new') {
  192.       if (this.changableData.id !== null) {
  193.         this.deleteNextUserBasic();
  194.       }
  195.       this.changableData = new NextUserDocument();
  196.       this.unchangableData = new NextUser();
  197.       this.unchangableData.roles = new Array<NextUserRoleSimple>();
  198.     } else {
  199.       this.nextUserService.getUser(this.changableData).then(res => {
  200.         this.unchangableData = res.data.nextUser;
  201.         this.changableData = this.nextUserHelper.copyObject(res.data.nextUser);
  202.       });
  203.     }
  204.   }
  205.  
  206.   async openAddRoleForm() {
  207.     let success: boolean = true;
  208.     if (!this.changableData.id) {
  209.       success = await this.createNextUser();
  210.     }
  211.     if (success) {
  212.       this.editRoleDialog.changableData = this.changableData;
  213.       this.editRoleDialog.unchangableData = this.unchangableData;
  214.       this.editRoleDialog.allNextUserRoles = this.allNextUserRoles;
  215.       this.editRoleDialog.title = 'Rolle verwalten';
  216.       this.openRoleForm();
  217.     }
  218.   }
  219.  
  220.   async openEditRoleForm(roleFromUi: string) {
  221.     let success: boolean = true;
  222.     if (!this.changableData.id) {
  223.       success = await this.createNextUser();
  224.     }
  225.     if (success) {
  226.       this.editRoleDialog.changableData = this.changableData;
  227.       this.editRoleDialog.unchangableData = this.unchangableData;
  228.       this.editRoleDialog.allNextUserRoles = this.allNextUserRoles;
  229.       this.editRoleDialog.title = 'Rolle verwalten';
  230.       this.openRoleForm();
  231.     }
  232.   }
  233.  
  234.   openEditPassword() {
  235.     console.log('Edit password');
  236.     this.passwordDialog.open();
  237.   }
  238.  
  239.   passwordDialogOnCancel() {
  240.     this.passwordDialog.close();
  241.   }
  242.  
  243.   passwordDialogOnOk() {
  244.     console.log(this.unhashedPassword);
  245.     console.log(this.hashPassword(this.unhashedPassword));
  246.     this.changableData.password = this.hashPassword(this.unhashedPassword);
  247.     this.passwordDialog.close();
  248.   }
  249.  
  250.   private openRoleForm() {
  251.     setTimeout(() => {
  252.       this.editRoleDialog.open();
  253.     }, 10);
  254.   }
  255.  
  256.   async createNextUser(): Promise<boolean> {
  257.     return this.nextUserService.createUser(this.changableData).then(res => {
  258.       if (res.data.createNextUser.success) {
  259.         this.unchangableData = res.data.createNextUser.user;
  260.         this.changableData = this.nextUserHelper.copyObject(res.data.createNextUser.user);
  261.         return true;
  262.       } else {
  263.         this.showErrorMessage('Benutzer anlegen', res.data.createNextUser.validationErrors);
  264.         return false;
  265.       }
  266.     });
  267.   }
  268.  
  269.   showErrorMessage(title: string, validationErrors: any[]) {
  270.     let errorMessage = title;
  271.     for (let validationError of validationErrors) {
  272.       errorMessage = errorMessage + validationError.errorMessage + '\n\n';
  273.     }
  274.     this.dialog.open(errorMessage, 'error');
  275.   }
  276. }
Add Comment
Please, Sign In to add comment