Merge branch 'release/2.1.0' into develop
[oweals/peertube.git] / client / src / app / +admin / users / user-edit / user-update.component.ts
1 import { Component, OnDestroy, OnInit } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { Subscription } from 'rxjs'
4 import { AuthService, Notifier } from '@app/core'
5 import { ServerService } from '../../../core'
6 import { UserEdit } from './user-edit'
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 { UserService } from '@app/shared'
13 import { UserAdminFlag } from '@shared/models/users/user-flag.model'
14
15 @Component({
16   selector: 'my-user-update',
17   templateUrl: './user-edit.component.html',
18   styleUrls: [ './user-edit.component.scss' ]
19 })
20 export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
21   error: string
22   userId: number
23   userEmail: string
24   username: string
25
26   private paramsSub: Subscription
27
28   constructor (
29     protected formValidatorService: FormValidatorService,
30     protected serverService: ServerService,
31     protected configService: ConfigService,
32     protected auth: AuthService,
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     super.ngOnInit()
47
48     const defaultValues = { videoQuota: '-1', videoQuotaDaily: '-1' }
49     this.buildForm({
50       email: this.userValidatorsService.USER_EMAIL,
51       role: this.userValidatorsService.USER_ROLE,
52       videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
53       videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY,
54       byPassAutoBlacklist: null
55     }, defaultValues)
56
57     this.paramsSub = this.route.params.subscribe(routeParams => {
58       const userId = routeParams['id']
59       this.userService.getUser(userId).subscribe(
60         user => this.onUserFetched(user),
61
62         err => this.error = err.message
63       )
64     })
65   }
66
67   ngOnDestroy () {
68     this.paramsSub.unsubscribe()
69   }
70
71   formValidated () {
72     this.error = undefined
73
74     const userUpdate: UserUpdate = this.form.value
75     userUpdate.adminFlags = this.buildAdminFlags(this.form.value)
76
77     // A select in HTML is always mapped as a string, we convert it to number
78     userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
79     userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
80
81     this.userService.updateUser(this.userId, userUpdate).subscribe(
82       () => {
83         this.notifier.success(this.i18n('User {{username}} updated.', { username: this.username }))
84         this.router.navigate([ '/admin/users/list' ])
85       },
86
87       err => this.error = err.message
88     )
89   }
90
91   isCreation () {
92     return false
93   }
94
95   isPasswordOptional () {
96     return false
97   }
98
99   getFormButtonTitle () {
100     return this.i18n('Update user')
101   }
102
103   resetPassword () {
104     this.userService.askResetPassword(this.userEmail).subscribe(
105       () => {
106         this.notifier.success(
107           this.i18n('An email asking for password reset has been sent to {{username}}.', { username: this.username })
108         )
109       },
110
111       err => this.error = err.message
112     )
113   }
114
115   private onUserFetched (userJson: User) {
116     this.userId = userJson.id
117     this.username = userJson.username
118     this.userEmail = userJson.email
119
120     this.form.patchValue({
121       email: userJson.email,
122       role: userJson.role,
123       videoQuota: userJson.videoQuota,
124       videoQuotaDaily: userJson.videoQuotaDaily,
125       byPassAutoBlacklist: userJson.adminFlags & UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST
126     })
127   }
128 }