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
9 * # http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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';
28 selector: 'app-user-management',
29 templateUrl: './user-management.component.html',
30 styleUrls: ['./user-management.component.css']
32 export class UserManagementComponent implements OnInit {
40 editUserFlag: boolean = false;
41 editUserForm: FormGroup;
42 rolesFromBackend = [];
43 selectedRoles : Array<String>= [];
45 constructor(private userService: UserService, private router: Router, private fb: FormBuilder, private authService: AuthService) { }
48 this.userService.getUsers().subscribe((res: User[]) => {
50 this.users.map(user=>{
52 user.roles.map(role=>{
53 tempRoles.push(role.name.substring(5));
55 user.roles = tempRoles;
59 this.editUserForm = this.fb.group({
62 password: [null, [ Validators.minLength(6)]],
63 roles: [this.selectedRoles, [Validators.required]]
65 this.userService.getRoles().subscribe(res=>{
66 Object.values(res).forEach(ele=>{
67 this.rolesFromBackend.push({label:ele.substring(5), value:ele});
72 handleDelete(username) {
73 const result = window.confirm('Are you sure to delete this user?');
75 this.userService.deleteUser(username).subscribe(res=>{
77 this.userService.getUsers().subscribe( r => {
79 this.users.map(user=>{
81 user.roles.map(role=>{
82 tempRoles.push(role.name.substring(5));
84 user.roles = tempRoles;
92 this.selectedRoles = [];
93 this.editUserFlag = true;
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);
102 this.editUserForm.get('roles').setValue(this.selectedRoles);
107 this.editUserForm.reset();
108 this.editUserFlag = false;
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);
120 this.editUserForm.patchValue({password: this.editUserForm.value.password});
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 => {
132 this.users.map(user=>{
134 user.roles.map(role=>{
135 tempRoles.push(role.name.substring(5));
137 user.roles = tempRoles;
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();
145 alert(err.error.message);