]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/overview/users/user-edit/user-create.component.ts
Merge branch 'release/4.2.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / overview / users / user-edit / user-create.component.ts
CommitLineData
df98563e 1import { Component, OnInit } from '@angular/core'
f4e5ac1f 2import { Router } from '@angular/router'
3827c3b3 3import { ConfigService } from '@app/+admin/config/shared/config.service'
d92d070c 4import { AuthService, Notifier, ScreenService, ServerService } from '@app/core'
7ed1edbb
C
5import {
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'
15import { FormValidatorService } from '@app/shared/shared-forms'
d92d070c 16import { UserAdminService } from '@app/shared/shared-users'
67ed6552
C
17import { UserCreate, UserRole } from '@shared/models'
18import { UserEdit } from './user-edit'
7da18e44
C
19
20@Component({
4c200caa 21 selector: 'my-user-create',
6a84aafd
C
22 templateUrl: './user-edit.component.html',
23 styleUrls: [ './user-edit.component.scss' ]
7da18e44 24})
4c200caa 25export class UserCreateComponent extends UserEdit implements OnInit {
8094a898 26 error: string
7da18e44 27
df98563e 28 constructor (
6a84aafd 29 protected serverService: ServerService,
d18d6478 30 protected formValidatorService: FormValidatorService,
3827c3b3 31 protected configService: ConfigService,
76314386 32 protected screenService: ScreenService,
a95a4cc8 33 protected auth: AuthService,
4b2f33f3 34 private router: Router,
f8b2c1b4 35 private notifier: Notifier,
d92d070c 36 private userAdminService: UserAdminService
9df52d66 37 ) {
df98563e 38 super()
3827c3b3
C
39
40 this.buildQuotaOptions()
4b2f33f3
C
41 }
42
df98563e 43 ngOnInit () {
ba430d75
C
44 super.ngOnInit()
45
d18d6478
C
46 const defaultValues = {
47 role: UserRole.USER.toString(),
21e493d4
C
48 videoQuota: -1,
49 videoQuotaDaily: -1
d18d6478
C
50 }
51
52 this.buildForm({
7ed1edbb
C
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,
3487330d 60 byPassAutoBlock: null
d18d6478 61 }, defaultValues)
7da18e44
C
62 }
63
8094a898
C
64 formValidated () {
65 this.error = undefined
7da18e44 66
4771e000 67 const userCreate: UserCreate = this.form.value
4b2f33f3 68
1eddc9a7
C
69 userCreate.adminFlags = this.buildAdminFlags(this.form.value)
70
b0f9f39e
C
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)
1eddc9a7 73 userCreate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
b0f9f39e 74
d92d070c 75 this.userAdminService.addUser(userCreate)
1378c0d3
C
76 .subscribe({
77 next: () => {
78 this.notifier.success($localize`User ${userCreate.username} created.`)
79 this.router.navigate([ '/admin/users/list' ])
80 },
7da18e44 81
9df52d66
C
82 error: err => {
83 this.error = err.message
84 }
1378c0d3 85 })
7da18e44 86 }
8094a898
C
87
88 isCreation () {
89 return true
90 }
91
45f1bd72 92 isPasswordOptional () {
f4e5ac1f 93 return this.serverConfig.email.enabled
45f1bd72
JL
94 }
95
8094a898 96 getFormButtonTitle () {
66357162 97 return $localize`Create user`
8094a898 98 }
7da18e44 99}