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