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 } 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';
29 selector: 'app-register',
30 templateUrl: './register.component.html',
31 styleUrls: ['./register.component.css']
33 export class RegisterComponent implements OnInit {
42 roleOptions: SelectItem[];
43 rolesFromBackend = [];
45 constructor(private fb: FormBuilder, private authService: AuthService, private router: Router, private userService: UserService) { }
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]]
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});
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);
71 this.form.patchValue({password: this.form.value.password});
75 const result = window.confirm("Are you sure to quit registering current user and go back to user management?");
77 this.router.navigate(['/users']);
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) => {
90 this.router.navigate(['/users']);
93 alert(err.error.message);