]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/users/user-edit/user-create.component.ts
Fix user creation form
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / 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'
67ed6552 4import { AuthService, Notifier, ScreenService, ServerService, UserService } 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'
67ed6552
C
16import { UserCreate, UserRole } from '@shared/models'
17import { UserEdit } from './user-edit'
7da18e44
C
18
19@Component({
4c200caa 20 selector: 'my-user-create',
6a84aafd
C
21 templateUrl: './user-edit.component.html',
22 styleUrls: [ './user-edit.component.scss' ]
7da18e44 23})
4c200caa 24export class UserCreateComponent extends UserEdit implements OnInit {
8094a898 25 error: string
7da18e44 26
df98563e 27 constructor (
6a84aafd 28 protected serverService: ServerService,
d18d6478 29 protected formValidatorService: FormValidatorService,
3827c3b3 30 protected configService: ConfigService,
76314386 31 protected screenService: ScreenService,
a95a4cc8 32 protected auth: AuthService,
4b2f33f3 33 private router: Router,
f8b2c1b4 34 private notifier: Notifier,
66357162
C
35 private userService: UserService
36 ) {
df98563e 37 super()
3827c3b3
C
38
39 this.buildQuotaOptions()
4b2f33f3
C
40 }
41
df98563e 42 ngOnInit () {
ba430d75
C
43 super.ngOnInit()
44
d18d6478
C
45 const defaultValues = {
46 role: UserRole.USER.toString(),
21e493d4
C
47 videoQuota: -1,
48 videoQuotaDaily: -1
d18d6478
C
49 }
50
51 this.buildForm({
7ed1edbb
C
52 username: USER_USERNAME_VALIDATOR,
53 channelName: USER_CHANNEL_NAME_VALIDATOR,
54 email: USER_EMAIL_VALIDATOR,
55 password: this.isPasswordOptional() ? USER_PASSWORD_OPTIONAL_VALIDATOR : USER_PASSWORD_VALIDATOR,
56 role: USER_ROLE_VALIDATOR,
57 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
58 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR,
3487330d 59 byPassAutoBlock: null
d18d6478 60 }, defaultValues)
7da18e44
C
61 }
62
8094a898
C
63 formValidated () {
64 this.error = undefined
7da18e44 65
4771e000 66 const userCreate: UserCreate = this.form.value
4b2f33f3 67
1eddc9a7
C
68 userCreate.adminFlags = this.buildAdminFlags(this.form.value)
69
b0f9f39e
C
70 // A select in HTML is always mapped as a string, we convert it to number
71 userCreate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
1eddc9a7 72 userCreate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
b0f9f39e 73
4771e000 74 this.userService.addUser(userCreate).subscribe(
7ddd02c9 75 () => {
66357162 76 this.notifier.success($localize`User ${userCreate.username} created.`)
df98563e 77 this.router.navigate([ '/admin/users/list' ])
7ddd02c9 78 },
7da18e44 79
f7354483 80 err => this.error = err.message
df98563e 81 )
7da18e44 82 }
8094a898
C
83
84 isCreation () {
85 return true
86 }
87
45f1bd72 88 isPasswordOptional () {
f4e5ac1f 89 return this.serverConfig.email.enabled
45f1bd72
JL
90 }
91
8094a898 92 getFormButtonTitle () {
66357162 93 return $localize`Create user`
8094a898 94 }
7da18e44 95}