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