]>
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 | |
328c78bc RK |
22 | username: string |
23 | showPassword = false | |
24 | ||
2c2baef6 RK |
25 | @Input() userId: number |
26 | ||
328c78bc RK |
27 | private paramsSub: Subscription |
28 | ||
29 | constructor ( | |
30 | protected formValidatorService: FormValidatorService, | |
31 | protected serverService: ServerService, | |
32 | protected configService: ConfigService, | |
33 | private userValidatorsService: UserValidatorsService, | |
34 | private route: ActivatedRoute, | |
35 | private router: Router, | |
36 | private notificationsService: NotificationsService, | |
37 | private userService: UserService, | |
38 | private i18n: I18n | |
39 | ) { | |
40 | super() | |
41 | } | |
42 | ||
43 | ngOnInit () { | |
44 | this.buildForm({ | |
45 | password: this.userValidatorsService.USER_PASSWORD | |
46 | }) | |
328c78bc RK |
47 | } |
48 | ||
49 | ngOnDestroy () { | |
50 | this.paramsSub.unsubscribe() | |
51 | } | |
52 | ||
53 | formValidated () { | |
54 | this.error = undefined | |
55 | ||
56 | const userUpdate: UserUpdate = this.form.value | |
57 | ||
58 | this.userService.updateUser(this.userId, userUpdate).subscribe( | |
59 | () => { | |
60 | this.notificationsService.success( | |
61 | this.i18n('Success'), | |
62 | this.i18n('Password changed for user {{username}}.', { username: this.username }) | |
63 | ) | |
64 | }, | |
65 | ||
66 | err => this.error = err.message | |
67 | ) | |
68 | } | |
69 | ||
70 | generatePassword () { | |
71 | this.form.patchValue({ | |
72 | password: generator.generate({ | |
73 | length: 16, | |
74 | excludeSimilarCharacters: true, | |
75 | strict: true | |
76 | }) | |
77 | }) | |
78 | } | |
79 | ||
80 | togglePasswordVisibility () { | |
81 | this.showPassword = !this.showPassword | |
82 | } | |
83 | ||
84 | getFormButtonTitle () { | |
85 | return this.i18n('Update user password') | |
86 | } | |
87 | ||
88 | private onUserFetched (userJson: User) { | |
89 | this.userId = userJson.id | |
90 | this.username = userJson.username | |
91 | } | |
92 | } |