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'
16 selector: 'my-user-password',
17 templateUrl: './user-password.component.html',
18 styleUrls: [ './user-password.component.scss' ]
20 export class UserPasswordComponent extends FormReactive implements OnInit, OnDestroy {
26 private paramsSub: Subscription
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,
44 password: this.userValidatorsService.USER_PASSWORD
47 this.paramsSub = this.route.params.subscribe(routeParams => {
48 const userId = routeParams['id']
49 this.userService.getUser(userId).subscribe(
50 user => this.onUserFetched(user),
52 err => this.error = err.message
58 this.paramsSub.unsubscribe()
62 this.error = undefined
64 const userUpdate: UserUpdate = this.form.value
66 this.userService.updateUser(this.userId, userUpdate).subscribe(
68 this.notificationsService.success(
70 this.i18n('Password changed for user {{username}}.', { username: this.username })
74 err => this.error = err.message
79 this.form.patchValue({
80 password: generator.generate({
82 excludeSimilarCharacters: true,
88 togglePasswordVisibility () {
89 this.showPassword = !this.showPassword
92 getFormButtonTitle () {
93 return this.i18n('Update user password')
96 private onUserFetched (userJson: User) {
97 this.userId = userJson.id
98 this.username = userJson.username