]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - client/src/app/+admin/users/user-edit/user-update.component.ts
allow administration to change/reset a user's password
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / users / user-edit / user-update.component.ts
... / ...
CommitLineData
1import { Component, OnDestroy, OnInit, Input } from '@angular/core'
2import { ActivatedRoute, Router } from '@angular/router'
3import { Subscription } from 'rxjs'
4import { Notifier } from '@app/core'
5import { ServerService } from '../../../core'
6import { UserEdit } from './user-edit'
7import { User, UserUpdate } from '../../../../../../shared'
8import { I18n } from '@ngx-translate/i18n-polyfill'
9import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
10import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service'
11import { ConfigService } from '@app/+admin/config/shared/config.service'
12import { UserService } from '@app/shared'
13
14@Component({
15 selector: 'my-user-update',
16 templateUrl: './user-edit.component.html',
17 styleUrls: [ './user-edit.component.scss' ]
18})
19export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
20 error: string
21 userId: number
22 userEmail: string
23 username: string
24 isAdministration = false
25
26 private paramsSub: Subscription
27 private isAdministrationSub: 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 notifier: Notifier,
37 private userService: UserService,
38 private i18n: I18n
39 ) {
40 super()
41
42 this.buildQuotaOptions()
43 }
44
45 ngOnInit () {
46 const defaultValues = { videoQuota: '-1', videoQuotaDaily: '-1' }
47 this.buildForm({
48 email: this.userValidatorsService.USER_EMAIL,
49 role: this.userValidatorsService.USER_ROLE,
50 videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
51 videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY
52 }, defaultValues)
53
54 this.paramsSub = this.route.params.subscribe(routeParams => {
55 const userId = routeParams['id']
56 this.userService.getUser(userId).subscribe(
57 user => this.onUserFetched(user),
58
59 err => this.error = err.message
60 )
61 })
62
63 this.isAdministrationSub = this.route.data.subscribe(data => {
64 if (data.isAdministration) this.isAdministration = data.isAdministration
65 })
66 }
67
68 ngOnDestroy () {
69 this.paramsSub.unsubscribe()
70 this.isAdministrationSub.unsubscribe()
71 }
72
73 formValidated () {
74 this.error = undefined
75
76 const userUpdate: UserUpdate = this.form.value
77
78 // A select in HTML is always mapped as a string, we convert it to number
79 userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
80 userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
81
82 this.userService.updateUser(this.userId, userUpdate).subscribe(
83 () => {
84 this.notifier.success(this.i18n('User {{username}} updated.', { username: this.username }))
85 this.router.navigate([ '/admin/users/list' ])
86 },
87
88 err => this.error = err.message
89 )
90 }
91
92 isCreation () {
93 return false
94 }
95
96 getFormButtonTitle () {
97 return this.i18n('Update user')
98 }
99
100 resetPassword () {
101 this.userService.askResetPassword(this.userEmail).subscribe(
102 () => {
103 this.notificationsService.success(
104 this.i18n('Success'),
105 this.i18n('An email asking for password reset has been sent to {{username}}.', { username: this.username })
106 )
107 },
108
109 err => this.error = err.message
110 )
111 }
112
113 private onUserFetched (userJson: User) {
114 this.userId = userJson.id
115 this.username = userJson.username
116 this.userEmail = userJson.email
117
118 this.form.patchValue({
119 email: userJson.email,
120 role: userJson.role,
121 videoQuota: userJson.videoQuota,
122 videoQuotaDaily: userJson.videoQuotaDaily
123 })
124 }
125}