Fix user role edition
[oweals/peertube.git] / client / src / app / +admin / users / user-edit / user-edit.ts
1 import { AuthService, ServerService } from '../../../core'
2 import { FormReactive } from '../../../shared'
3 import { ServerConfig, USER_ROLE_LABELS, UserRole, VideoResolution } from '../../../../../../shared'
4 import { ConfigService } from '@app/+admin/config/shared/config.service'
5 import { UserAdminFlag } from '@shared/models/users/user-flag.model'
6 import { OnInit } from '@angular/core'
7
8 export abstract class UserEdit extends FormReactive implements OnInit {
9   videoQuotaOptions: { value: string, label: string }[] = []
10   videoQuotaDailyOptions: { value: string, label: string }[] = []
11   username: string
12   userId: number
13
14   roles: { value: string, label: string }[] = []
15
16   protected serverConfig: ServerConfig
17
18   protected abstract serverService: ServerService
19   protected abstract configService: ConfigService
20   protected abstract auth: AuthService
21   abstract isCreation (): boolean
22   abstract getFormButtonTitle (): string
23
24   ngOnInit (): void {
25     this.serverConfig = this.serverService.getTmpConfig()
26     this.serverService.getConfig()
27         .subscribe(config => this.serverConfig = config)
28
29     this.buildRoles()
30   }
31
32   buildRoles () {
33     const authUser = this.auth.getUser()
34
35     if (authUser.role === UserRole.ADMINISTRATOR) {
36       this.roles = Object.keys(USER_ROLE_LABELS)
37             .map(key => ({ value: key.toString(), label: USER_ROLE_LABELS[key] }))
38       return
39     }
40
41     this.roles = [
42       { value: UserRole.USER.toString(), label: USER_ROLE_LABELS[UserRole.USER] }
43     ]
44   }
45
46   isTranscodingInformationDisplayed () {
47     const formVideoQuota = parseInt(this.form.value['videoQuota'], 10)
48
49     return this.serverConfig.transcoding.enabledResolutions.length !== 0 &&
50            formVideoQuota > 0
51   }
52
53   computeQuotaWithTranscoding () {
54     const transcodingConfig = this.serverConfig.transcoding
55
56     const resolutions = transcodingConfig.enabledResolutions
57     const higherResolution = VideoResolution.H_4K
58     let multiplier = 0
59
60     for (const resolution of resolutions) {
61       multiplier += resolution / higherResolution
62     }
63
64     if (transcodingConfig.hls.enabled) multiplier *= 2
65
66     return multiplier * parseInt(this.form.value['videoQuota'], 10)
67   }
68
69   resetPassword () {
70     return
71   }
72
73   protected buildAdminFlags (formValue: any) {
74     return formValue.byPassAutoBlacklist ? UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST : UserAdminFlag.NONE
75   }
76
77   protected buildQuotaOptions () {
78     // These are used by a HTML select, so convert key into strings
79     this.videoQuotaOptions = this.configService
80                                  .videoQuotaOptions.map(q => ({ value: q.value.toString(), label: q.label }))
81
82     this.videoQuotaDailyOptions = this.configService
83                                       .videoQuotaDailyOptions.map(q => ({ value: q.value.toString(), label: q.label }))
84   }
85 }