Fix mod ui build issues
[dcaegen2/platform.git] / mod2 / ui / src / app / user-management / user-management.component.ts
1 /* 
2  *  # ============LICENSE_START=======================================================
3  *  # Copyright (c) 2020 AT&T Intellectual Property. All rights reserved.
4  *  # ================================================================================
5  *  # Licensed under the Apache License, Version 2.0 (the "License");
6  *  # you may not use this file except in compliance with the License.
7  *  # You may obtain a copy of the License at
8  *  #
9  *  #      http://www.apache.org/licenses/LICENSE-2.0
10  *  #
11  *  # Unless required by applicable law or agreed to in writing, software
12  *  # distributed under the License is distributed on an "AS IS" BASIS,
13  *  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  # See the License for the specific language governing permissions and
15  *  # limitations under the License.
16  *  # ============LICENSE_END=========================================================
17  */
18
19 import { Component, OnInit, ViewChild, ChangeDetectionStrategy } from '@angular/core';
20 import { User } from '../models/User';
21 import { UserService } from '../services/user.service';
22 import { Router } from '@angular/router';
23 import { FormGroup, FormBuilder, Validators } from '@angular/forms';
24 import { SelectItem } from 'primeng/api';
25 import { AuthService } from '../services/auth.service';
26
27 @Component({
28   selector: 'app-user-management',
29   templateUrl: './user-management.component.html',
30   styleUrls: ['./user-management.component.css']
31 })
32 export class UserManagementComponent implements OnInit {
33
34   users: User[] = [];
35   editUser: User = {
36     username:'',
37     fullName:'',
38     roles: []
39   };
40   editUserFlag: boolean = false;
41   editUserForm: FormGroup;
42   rolesFromBackend = [];
43   selectedRoles : Array<String>= [];
44
45   constructor(private userService: UserService, private router: Router, private fb: FormBuilder, private authService: AuthService) { }
46
47   ngOnInit() {
48       this.userService.getUsers().subscribe((res: User[]) => {
49        this.users = res;
50        this.users.map(user=>{
51          let tempRoles = [];
52          user.roles.map(role=>{
53            tempRoles.push(role.name.substring(5));
54          });
55          user.roles = tempRoles;
56        });
57        
58       });
59       this.editUserForm = this.fb.group({
60         username: '',
61         fullName: '',
62         password: [null, [ Validators.minLength(6)]],
63         roles: [this.selectedRoles, [Validators.required]]
64       });
65       this.userService.getRoles().subscribe(res=>{
66         Object.values(res).forEach(ele=>{
67          this.rolesFromBackend.push({label:ele.substring(5), value:ele});
68         });
69        });
70   }
71
72   handleDelete(username) {
73     const result = window.confirm('Are you sure to delete this user?');
74     if(result === true) {
75       this.userService.deleteUser(username).subscribe(res=>{
76         alert(res.message);
77           this.userService.getUsers().subscribe( r => {
78              this.users = r; 
79              this.users.map(user=>{
80               let tempRoles = [];
81               user.roles.map(role=>{
82                 tempRoles.push(role.name.substring(5));
83               });
84               user.roles = tempRoles;
85             });
86       });
87      });
88     } 
89   }
90
91   handleEdit(user) {
92     this.selectedRoles = [];
93     this.editUserFlag = true;
94     this.editUser = user;
95     this.editUserForm.get('username').setValue(user.username);
96     this.editUserForm.get('fullName').setValue(user.fullName);
97     user.roles.map(ele => {
98        let temp = "ROLE_" + ele;
99        this.selectedRoles.push(temp);
100     })
101
102      this.editUserForm.get('roles').setValue(this.selectedRoles);
103
104   }
105
106   closeEditDialog() {
107     this.editUserForm.reset();
108     this.editUserFlag = false;
109   }
110
111   generateNewPassword() {
112     this.editUserForm.value.password = '';
113     const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%&';
114     var array = new Uint32Array(32);
115     window.crypto.getRandomValues(array);
116     for(let i=0;i<32;i++) {
117       const index = Math.floor(array[i] % chars.length);
118       this.editUserForm.value.password += chars.charAt(index);
119     }
120     this.editUserForm.patchValue({password: this.editUserForm.value.password});
121   }
122
123   submitEdit(user) {
124     this.editUserFlag = false;
125     console.log(this.editUserForm.value.fullName);
126     let tempUser = this.editUserForm.value as User;
127     console.log(tempUser);
128     this.userService.editUser(user.username, this.editUserForm.value as User).subscribe(res=>{
129         alert("User information updated successfully.");
130         this.userService.getUsers().subscribe( r => {
131         this.users = r; 
132         this.users.map(user=>{
133          let tempRoles = [];
134          user.roles.map(role=>{
135            tempRoles.push(role.name.substring(5));
136          });
137          user.roles = tempRoles;
138        });
139        }, (err)=>{
140           // alert(err.error.message);
141           alert("Sorry but your credentials are out of date. Please log in again to resolve this.");
142           this.authService.logout();
143        });   
144     }, (err)=>{
145       alert(err.error.message);
146     });
147   }
148   
149 }