]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+admin/users/user-edit/user-create.component.ts
Merge branch 'release/2.1.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'
45f1bd72 2import { Router, ActivatedRoute } from '@angular/router'
a95a4cc8 3import { AuthService, Notifier, ServerService } from '@app/core'
954605a8 4import { UserCreate, UserRole } from '../../../../../../shared'
8094a898 5import { UserEdit } from './user-edit'
b1d40cff 6import { I18n } from '@ngx-translate/i18n-polyfill'
d18d6478 7import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
e309822b 8import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service'
3827c3b3 9import { ConfigService } from '@app/+admin/config/shared/config.service'
e724fa93 10import { UserService } from '@app/shared'
7da18e44
C
11
12@Component({
4c200caa 13 selector: 'my-user-create',
6a84aafd
C
14 templateUrl: './user-edit.component.html',
15 styleUrls: [ './user-edit.component.scss' ]
7da18e44 16})
4c200caa 17export class UserCreateComponent extends UserEdit implements OnInit {
8094a898 18 error: string
7da18e44 19
df98563e 20 constructor (
6a84aafd 21 protected serverService: ServerService,
d18d6478 22 protected formValidatorService: FormValidatorService,
3827c3b3 23 protected configService: ConfigService,
a95a4cc8 24 protected auth: AuthService,
e309822b 25 private userValidatorsService: UserValidatorsService,
45f1bd72 26 private route: ActivatedRoute,
4b2f33f3 27 private router: Router,
f8b2c1b4 28 private notifier: Notifier,
b1d40cff
C
29 private userService: UserService,
30 private i18n: I18n
4b2f33f3 31 ) {
df98563e 32 super()
3827c3b3
C
33
34 this.buildQuotaOptions()
4b2f33f3
C
35 }
36
df98563e 37 ngOnInit () {
ba430d75
C
38 super.ngOnInit()
39
d18d6478
C
40 const defaultValues = {
41 role: UserRole.USER.toString(),
d6ca951b
C
42 videoQuota: '-1',
43 videoQuotaDaily: '-1'
d18d6478
C
44 }
45
46 this.buildForm({
e309822b
C
47 username: this.userValidatorsService.USER_USERNAME,
48 email: this.userValidatorsService.USER_EMAIL,
45f1bd72 49 password: this.isPasswordOptional() ? this.userValidatorsService.USER_PASSWORD_OPTIONAL : this.userValidatorsService.USER_PASSWORD,
e309822b 50 role: this.userValidatorsService.USER_ROLE,
d6ca951b 51 videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
1eddc9a7
C
52 videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY,
53 byPassAutoBlacklist: null
d18d6478 54 }, defaultValues)
7da18e44
C
55 }
56
8094a898
C
57 formValidated () {
58 this.error = undefined
7da18e44 59
4771e000 60 const userCreate: UserCreate = this.form.value
4b2f33f3 61
1eddc9a7
C
62 userCreate.adminFlags = this.buildAdminFlags(this.form.value)
63
b0f9f39e
C
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)
1eddc9a7 66 userCreate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
b0f9f39e 67
4771e000 68 this.userService.addUser(userCreate).subscribe(
7ddd02c9 69 () => {
f8b2c1b4 70 this.notifier.success(this.i18n('User {{username}} created.', { username: userCreate.username }))
df98563e 71 this.router.navigate([ '/admin/users/list' ])
7ddd02c9 72 },
7da18e44 73
f7354483 74 err => this.error = err.message
df98563e 75 )
7da18e44 76 }
8094a898
C
77
78 isCreation () {
79 return true
80 }
81
45f1bd72
JL
82 isPasswordOptional () {
83 const serverConfig = this.route.snapshot.data.serverConfig
84 return serverConfig.email.enabled
85 }
86
8094a898 87 getFormButtonTitle () {
b1d40cff 88 return this.i18n('Create user')
8094a898 89 }
7da18e44 90}