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