]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/overview/users/user-edit/user-create.component.ts
Increase global font size
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / overview / users / user-edit / user-create.component.ts
1 import { Component, OnInit } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { ConfigService } from '@app/+admin/config/shared/config.service'
4 import { AuthService, Notifier, ScreenService, ServerService } from '@app/core'
5 import {
6 USER_CHANNEL_NAME_VALIDATOR,
7 USER_EMAIL_VALIDATOR,
8 USER_PASSWORD_OPTIONAL_VALIDATOR,
9 USER_PASSWORD_VALIDATOR,
10 USER_ROLE_VALIDATOR,
11 USER_USERNAME_VALIDATOR,
12 USER_VIDEO_QUOTA_DAILY_VALIDATOR,
13 USER_VIDEO_QUOTA_VALIDATOR
14 } from '@app/shared/form-validators/user-validators'
15 import { FormValidatorService } from '@app/shared/shared-forms'
16 import { UserAdminService } from '@app/shared/shared-users'
17 import { UserCreate, UserRole } from '@shared/models'
18 import { UserEdit } from './user-edit'
19
20 @Component({
21 selector: 'my-user-create',
22 templateUrl: './user-edit.component.html',
23 styleUrls: [ './user-edit.component.scss' ]
24 })
25 export class UserCreateComponent extends UserEdit implements OnInit {
26 error: string
27
28 constructor (
29 protected serverService: ServerService,
30 protected formValidatorService: FormValidatorService,
31 protected configService: ConfigService,
32 protected screenService: ScreenService,
33 protected auth: AuthService,
34 private router: Router,
35 private notifier: Notifier,
36 private userAdminService: UserAdminService
37 ) {
38 super()
39
40 this.buildQuotaOptions()
41 }
42
43 ngOnInit () {
44 super.ngOnInit()
45
46 const defaultValues = {
47 role: UserRole.USER.toString(),
48 videoQuota: -1,
49 videoQuotaDaily: -1
50 }
51
52 this.buildForm({
53 username: USER_USERNAME_VALIDATOR,
54 channelName: USER_CHANNEL_NAME_VALIDATOR,
55 email: USER_EMAIL_VALIDATOR,
56 password: this.isPasswordOptional() ? USER_PASSWORD_OPTIONAL_VALIDATOR : USER_PASSWORD_VALIDATOR,
57 role: USER_ROLE_VALIDATOR,
58 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
59 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR,
60 byPassAutoBlock: null
61 }, defaultValues)
62 }
63
64 formValidated () {
65 this.error = undefined
66
67 const userCreate: UserCreate = this.form.value
68
69 userCreate.adminFlags = this.buildAdminFlags(this.form.value)
70
71 // A select in HTML is always mapped as a string, we convert it to number
72 userCreate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
73 userCreate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
74
75 this.userAdminService.addUser(userCreate)
76 .subscribe({
77 next: () => {
78 this.notifier.success($localize`User ${userCreate.username} created.`)
79 this.router.navigate([ '/admin/users/list' ])
80 },
81
82 error: err => {
83 this.error = err.message
84 }
85 })
86 }
87
88 isCreation () {
89 return true
90 }
91
92 isPasswordOptional () {
93 return this.serverConfig.email.enabled
94 }
95
96 getFormButtonTitle () {
97 return $localize`Create user`
98 }
99 }