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