]>
Commit | Line | Data |
---|---|---|
328c78bc RK |
1 | import { Component, OnDestroy, OnInit, Input } from '@angular/core' |
2 | import { ActivatedRoute, Router } from '@angular/router' | |
3 | import { Subscription } from 'rxjs' | |
4 | import * as generator from 'generate-password-browser' | |
5 | import { NotificationsService } from 'angular2-notifications' | |
6 | import { UserService } from '@app/shared/users/user.service' | |
7 | import { ServerService } from '../../../core' | |
8 | import { User, UserUpdate } from '../../../../../../shared' | |
9 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
10 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' | |
11 | import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service' | |
12 | import { ConfigService } from '@app/+admin/config/shared/config.service' | |
13 | import { FormReactive } from '../../../shared' | |
14 | ||
15 | @Component({ | |
16 | selector: 'my-user-password', | |
17 | templateUrl: './user-password.component.html', | |
18 | styleUrls: [ './user-password.component.scss' ] | |
19 | }) | |
20 | export class UserPasswordComponent extends FormReactive implements OnInit, OnDestroy { | |
21 | error: string | |
22 | userId: number | |
23 | username: string | |
24 | showPassword = false | |
25 | ||
26 | private paramsSub: Subscription | |
27 | ||
28 | constructor ( | |
29 | protected formValidatorService: FormValidatorService, | |
30 | protected serverService: ServerService, | |
31 | protected configService: ConfigService, | |
32 | private userValidatorsService: UserValidatorsService, | |
33 | private route: ActivatedRoute, | |
34 | private router: Router, | |
35 | private notificationsService: NotificationsService, | |
36 | private userService: UserService, | |
37 | private i18n: I18n | |
38 | ) { | |
39 | super() | |
40 | } | |
41 | ||
42 | ngOnInit () { | |
43 | this.buildForm({ | |
44 | password: this.userValidatorsService.USER_PASSWORD | |
45 | }) | |
46 | ||
47 | this.paramsSub = this.route.params.subscribe(routeParams => { | |
48 | const userId = routeParams['id'] | |
49 | this.userService.getUser(userId).subscribe( | |
50 | user => this.onUserFetched(user), | |
51 | ||
52 | err => this.error = err.message | |
53 | ) | |
54 | }) | |
55 | } | |
56 | ||
57 | ngOnDestroy () { | |
58 | this.paramsSub.unsubscribe() | |
59 | } | |
60 | ||
61 | formValidated () { | |
62 | this.error = undefined | |
63 | ||
64 | const userUpdate: UserUpdate = this.form.value | |
65 | ||
66 | this.userService.updateUser(this.userId, userUpdate).subscribe( | |
67 | () => { | |
68 | this.notificationsService.success( | |
69 | this.i18n('Success'), | |
70 | this.i18n('Password changed for user {{username}}.', { username: this.username }) | |
71 | ) | |
72 | }, | |
73 | ||
74 | err => this.error = err.message | |
75 | ) | |
76 | } | |
77 | ||
78 | generatePassword () { | |
79 | this.form.patchValue({ | |
80 | password: generator.generate({ | |
81 | length: 16, | |
82 | excludeSimilarCharacters: true, | |
83 | strict: true | |
84 | }) | |
85 | }) | |
86 | } | |
87 | ||
88 | togglePasswordVisibility () { | |
89 | this.showPassword = !this.showPassword | |
90 | } | |
91 | ||
92 | getFormButtonTitle () { | |
93 | return this.i18n('Update user password') | |
94 | } | |
95 | ||
96 | private onUserFetched (userJson: User) { | |
97 | this.userId = userJson.id | |
98 | this.username = userJson.username | |
99 | } | |
100 | } |