diff options
author | Rigel Kent <sendmemail@rigelk.eu> | 2018-10-06 13:54:00 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-02-11 09:26:29 +0100 |
commit | 328c78bc4a570a9aceaaa1a2124bacd4a0e8d295 (patch) | |
tree | 7f07a427828fb5540dfe7e6bed372e846d587b86 /client/src/app/+admin/users/user-edit/user-password.component.ts | |
parent | c7ca4c8be781753f86d806015b182f1ee5443fcf (diff) | |
download | PeerTube-328c78bc4a570a9aceaaa1a2124bacd4a0e8d295.tar.gz PeerTube-328c78bc4a570a9aceaaa1a2124bacd4a0e8d295.tar.zst PeerTube-328c78bc4a570a9aceaaa1a2124bacd4a0e8d295.zip |
allow administration to change/reset a user's password
Diffstat (limited to 'client/src/app/+admin/users/user-edit/user-password.component.ts')
-rw-r--r-- | client/src/app/+admin/users/user-edit/user-password.component.ts | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/client/src/app/+admin/users/user-edit/user-password.component.ts b/client/src/app/+admin/users/user-edit/user-password.component.ts new file mode 100644 index 000000000..1f9ccb4e8 --- /dev/null +++ b/client/src/app/+admin/users/user-edit/user-password.component.ts | |||
@@ -0,0 +1,100 @@ | |||
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 | } | ||