]> source.dussan.org Git - archiva.git/blob
288b5522ba1788fa95ee32a216b398b02d1139ce
[archiva.git] /
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an
13  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14  * KIND, either express or implied.  See the License for the
15  * specific language governing permissions and limitations
16  * under the License.
17  */
18
19 import {Component, OnInit} from '@angular/core';
20 import {ActivatedRoute} from '@angular/router';
21 import {UserService} from "../../../../services/user.service";
22 import {FormBuilder, FormControl} from "@angular/forms";
23 import {catchError, map, switchMap, tap} from 'rxjs/operators';
24 import {ManageUsersBaseComponent} from "../manage-users-base.component";
25 import {ErrorResult} from "../../../../model/error-result";
26
27 @Component({
28     selector: 'app-manage-users-edit',
29     templateUrl: './manage-users-edit.component.html',
30     styleUrls: ['./manage-users-edit.component.scss']
31 })
32 export class ManageUsersEditComponent extends ManageUsersBaseComponent implements OnInit {
33
34     editProperties = ['user_id', 'full_name', 'email', 'locked', 'password_change_required',
35         'password', 'confirm_password', 'validated'];
36     editUser;
37     originUser;
38     editMode: boolean;
39     minUserIdSize = 0;
40
41     constructor(private route: ActivatedRoute, public userService: UserService, public fb: FormBuilder) {
42         super(userService, fb);
43         this.editMode=false;
44         this.route.queryParams.subscribe((params)=>{
45           if (params.editmode) {
46             this.editMode=true;
47           }
48         })
49         this.editUser = this.route.params.pipe(
50             map(params => params.userid), switchMap(userid => userService.getUser(userid))).subscribe(user => {
51             this.editUser = user;
52             this.originUser = user;
53             this.copyToForm(this.editProperties, this.editUser);
54         });
55     }
56
57     ngOnInit(): void {
58       // This resets the validators of the base class
59       this.userForm.get('user_id').clearValidators();
60       this.userForm.get('user_id').clearAsyncValidators();
61     }
62
63     valid(field: string): string[] {
64         if (this.editMode) {
65             let classArr = super.valid(field);
66             return classArr.concat('form-control')
67         } else {
68             return ['form-control-plaintext'];
69         }
70     }
71
72
73     onSubmit() {
74         let user = this.copyFromForm(this.editProperties);
75         this.userService.updateUser(user).pipe(
76             catchError((err: ErrorResult) => {
77                 this.error = true;
78                 this.success = false;
79                 this.errorResult = err;
80                 return [];
81             })
82         ).subscribe(userInfo=>{
83             this.error=false;
84             this.success=true;
85             this.errorResult=null;
86             this.result = userInfo;
87             this.editMode = false;
88         });
89
90     }
91
92
93 }