Moderators can only manage users
[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 { 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
7 export abstract class UserEdit extends FormReactive {
8   videoQuotaOptions: { value: string, label: string }[] = []
9   videoQuotaDailyOptions: { value: string, label: string }[] = []
10   username: string
11   userId: number
12
13   protected abstract serverService: ServerService
14   protected abstract configService: ConfigService
15   protected abstract auth: AuthService
16   abstract isCreation (): boolean
17   abstract getFormButtonTitle (): string
18
19   getRoles () {
20     const authUser = this.auth.getUser()
21
22     if (authUser.role === UserRole.ADMINISTRATOR) {
23       return Object.keys(USER_ROLE_LABELS)
24             .map(key => ({ value: key.toString(), label: USER_ROLE_LABELS[key] }))
25     }
26
27     return [
28       { value: UserRole.USER.toString(), label: USER_ROLE_LABELS[UserRole.USER] }
29     ]
30   }
31
32   isTranscodingInformationDisplayed () {
33     const formVideoQuota = parseInt(this.form.value['videoQuota'], 10)
34
35     return this.serverService.getConfig().transcoding.enabledResolutions.length !== 0 &&
36            formVideoQuota > 0
37   }
38
39   computeQuotaWithTranscoding () {
40     const transcodingConfig = this.serverService.getConfig().transcoding
41
42     const resolutions = transcodingConfig.enabledResolutions
43     const higherResolution = VideoResolution.H_4K
44     let multiplier = 0
45
46     for (const resolution of resolutions) {
47       multiplier += resolution / higherResolution
48     }
49
50     if (transcodingConfig.hls.enabled) multiplier *= 2
51
52     return multiplier * parseInt(this.form.value['videoQuota'], 10)
53   }
54
55   resetPassword () {
56     return
57   }
58
59   protected buildAdminFlags (formValue: any) {
60     return formValue.byPassAutoBlacklist ? UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST : UserAdminFlag.NONE
61   }
62
63   protected buildQuotaOptions () {
64     // These are used by a HTML select, so convert key into strings
65     this.videoQuotaOptions = this.configService
66                                  .videoQuotaOptions.map(q => ({ value: q.value.toString(), label: q.label }))
67
68     this.videoQuotaDailyOptions = this.configService
69                                       .videoQuotaDailyOptions.map(q => ({ value: q.value.toString(), label: q.label }))
70   }
71 }