Fix mod ui build issues
[dcaegen2/platform.git] / mod2 / ui / src / app / register / register.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 } from '@angular/core';
20 import { FormGroup, FormBuilder, Validators } from '@angular/forms';
21 import { AuthService } from '../services/auth.service';
22 import { Router } from '@angular/router';
23 import { User } from '../models/User';
24 import { AuthResponse } from '../models/AuthResponse';
25 import {SelectItem} from 'primeng/api';
26 import { UserService } from '../services/user.service';
27
28 @Component({
29   selector: 'app-register',
30   templateUrl: './register.component.html',
31   styleUrls: ['./register.component.css']
32 })
33 export class RegisterComponent implements OnInit {
34
35   user: User = {
36     username:'',
37     fullName:'',
38     password:'',
39     roles: []
40   };
41   form: FormGroup;
42   roleOptions: SelectItem[];
43   rolesFromBackend = [];
44
45   constructor(private fb: FormBuilder, private authService: AuthService, private router: Router, private userService: UserService) { }
46
47   ngOnInit() {
48     this.form = this.fb.group({
49         username: ['', [Validators.required, Validators.minLength(5), Validators.maxLength(10)]],
50         fullName: ['', [Validators.required]],
51         password: ['', [Validators.required, Validators.minLength(6)]],
52         roles:['', [Validators.required]]
53     });
54     // this.roleOptions = [{label:'Admin', value:{name:'ROLE_ADMIN'}}, {label:'User', value:{name:'ROLE_USER'}}, {label:'ScrumLead', value:{name:'ROLE_SCRUMLEAD'}}, {label:'Developer', value:{name:'ROLE_DEVELOPER'}}, {label:'PST', value:{name:'ROLE_PST'}}, {label:'ETE', value:{name:'ROLE_ETE'}}, {label:'Ops', value:{name:'ROLE_OPS'}}];
55     this.userService.getRoles().subscribe(res=>{
56      Object.values(res).forEach(ele=>{
57       this.rolesFromBackend.push({label:ele.substring(5), value:ele});
58      });
59     });
60   }
61
62   generateNewPassword() {
63     this.form.value.password = '';
64     const chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%&';
65     var array = new Uint32Array(32);
66     window.crypto.getRandomValues(array);
67     for(let i=0;i<32;i++) {
68       const index = Math.floor(array[i] % chars.length);
69       this.form.value.password += chars.charAt(index);
70     }
71     this.form.patchValue({password: this.form.value.password});
72   }
73
74   cancel() {
75     const result = window.confirm("Are you sure to quit registering current user and go back to user management?");
76     if(result === true){
77       this.router.navigate(['/users']);
78     }  
79   }
80
81   submit() {
82     this.user.fullName = this. form.value.fullName;
83     this.user.username = this.form.value.username;
84     this.user.password = this.form.value.password;
85     this.user.roles = this.form.value.roles;
86     console.log(this.user.roles);
87     this.authService.register(this.user) 
88     .subscribe( (res: AuthResponse) => {
89           alert(res.message);
90           this.router.navigate(['/users']);
91       }, (err) => {
92         console.log(err);
93         alert(err.error.message);
94         this.form.reset();
95       }
96   ); 
97   }
98
99 }