]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/overview/users/user-edit/user-edit.ts
9547da2d1012703f4214c7697eeba8dcd7887090
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / overview / users / user-edit / user-edit.ts
1 import { Directive, OnInit } from '@angular/core'
2 import { ConfigService } from '@app/+admin/config/shared/config.service'
3 import { AuthService, ScreenService, ServerService, User } from '@app/core'
4 import { FormReactive } from '@app/shared/shared-forms'
5 import { peertubeTranslate } from '@shared/core-utils'
6 import { USER_ROLE_LABELS } from '@shared/core-utils/users'
7 import { HTMLServerConfig, UserAdminFlag, UserRole } from '@shared/models'
8 import { SelectOptionsItem } from '../../../../../types/select-options-item.model'
9
10 @Directive()
11 // eslint-disable-next-line @angular-eslint/directive-class-suffix
12 export abstract class UserEdit extends FormReactive implements OnInit {
13 videoQuotaOptions: SelectOptionsItem[] = []
14 videoQuotaDailyOptions: SelectOptionsItem[] = []
15 username: string
16 user: User
17
18 roles: { value: string, label: string }[] = []
19
20 protected serverConfig: HTMLServerConfig
21
22 protected abstract serverService: ServerService
23 protected abstract configService: ConfigService
24 protected abstract screenService: ScreenService
25 protected abstract auth: AuthService
26 abstract isCreation (): boolean
27 abstract getFormButtonTitle (): string
28
29 ngOnInit () {
30 this.serverConfig = this.serverService.getHTMLConfig()
31
32 this.buildRoles()
33 }
34
35 get subscribersCount () {
36 const forAccount = this.user
37 ? this.user.account.followersCount
38 : 0
39 const forChannels = this.user
40 ? this.user.videoChannels.map(c => c.followersCount).reduce((a, b) => a + b, 0)
41 : 0
42 return forAccount + forChannels
43 }
44
45 getAuthPlugins () {
46 return this.serverConfig.plugin.registeredIdAndPassAuths.map(p => p.npmName)
47 .concat(this.serverConfig.plugin.registeredExternalAuths.map(p => p.npmName))
48 }
49
50 buildRoles () {
51 const authUser = this.auth.getUser()
52
53 this.serverService.getServerLocale()
54 .subscribe(translations => {
55 if (authUser.role.id === UserRole.ADMINISTRATOR) {
56 this.roles = Object.entries(USER_ROLE_LABELS)
57 .map(([ key, value ]) => ({ value: key.toString(), label: peertubeTranslate(value, translations) }))
58 return
59 }
60
61 this.roles = [
62 { value: UserRole.USER.toString(), label: peertubeTranslate(USER_ROLE_LABELS[UserRole.USER], translations) }
63 ]
64 })
65 }
66
67 displayDangerZone () {
68 if (this.isCreation()) return false
69 if (!this.user) return false
70 if (this.user.pluginAuth) return false
71 if (this.auth.getUser().id === this.user.id) return false
72
73 return true
74 }
75
76 resetPassword () {
77 return
78 }
79
80 disableTwoFactorAuth () {
81 return
82 }
83
84 getUserVideoQuota () {
85 return this.form.value['videoQuota']
86 }
87
88 protected buildAdminFlags (formValue: any) {
89 return formValue.byPassAutoBlock ? UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST : UserAdminFlag.NONE
90 }
91
92 protected buildQuotaOptions () {
93 this.videoQuotaOptions = this.configService.videoQuotaOptions
94 this.videoQuotaDailyOptions = this.configService.videoQuotaDailyOptions
95 }
96 }