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'
10 selector: 'my-user-create',
11 templateUrl: './user-edit.component.html',
12 styleUrls: [ './user-edit.component.scss' ]
14 export class UserCreateComponent extends UserEdit implements OnInit {
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
31 this.buildQuotaOptions()
37 const defaultValues = {
38 role: UserRole.USER.toString(),
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,
56 this.error = undefined
58 const userCreate: UserCreate = this.form.value
60 userCreate.adminFlags = this.buildAdminFlags(this.form.value)
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)
66 this.userService.addUser(userCreate).subscribe(
68 this.notifier.success($localize`User ${userCreate.username} created.`)
69 this.router.navigate([ '/admin/users/list' ])
72 err => this.error = err.message
80 isPasswordOptional () {
81 const serverConfig = this.route.snapshot.data.serverConfig
82 return serverConfig.email.enabled
85 getFormButtonTitle () {
86 return $localize`Create user`