]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/users/user-edit/user-create.component.ts
Merge branch 'release/3.2.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / users / user-edit / user-create.component.ts
CommitLineData
df98563e 1import { Component, OnInit } from '@angular/core'
67ed6552 2import { ActivatedRoute, 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,
45f1bd72 33 private route: ActivatedRoute,
4b2f33f3 34 private router: Router,
f8b2c1b4 35 private notifier: Notifier,
66357162
C
36 private userService: UserService
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
4771e000 75 this.userService.addUser(userCreate).subscribe(
7ddd02c9 76 () => {
66357162 77 this.notifier.success($localize`User ${userCreate.username} created.`)
df98563e 78 this.router.navigate([ '/admin/users/list' ])
7ddd02c9 79 },
7da18e44 80
f7354483 81 err => this.error = err.message
df98563e 82 )
7da18e44 83 }
8094a898
C
84
85 isCreation () {
86 return true
87 }
88
45f1bd72
JL
89 isPasswordOptional () {
90 const serverConfig = this.route.snapshot.data.serverConfig
91 return serverConfig.email.enabled
92 }
93
8094a898 94 getFormButtonTitle () {
66357162 95 return $localize`Create user`
8094a898 96 }
7da18e44 97}