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'
6 USER_CHANNEL_NAME_VALIDATOR,
8 USER_PASSWORD_OPTIONAL_VALIDATOR,
9 USER_PASSWORD_VALIDATOR,
11 USER_USERNAME_VALIDATOR,
12 USER_VIDEO_QUOTA_DAILY_VALIDATOR,
13 USER_VIDEO_QUOTA_VALIDATOR
14 } from '@app/shared/form-validators/user-validators'
15 import { FormValidatorService } from '@app/shared/shared-forms'
16 import { UserCreate, UserRole } from '@shared/models'
17 import { UserEdit } from './user-edit'
20 selector: 'my-user-create',
21 templateUrl: './user-edit.component.html',
22 styleUrls: [ './user-edit.component.scss' ]
24 export class UserCreateComponent extends UserEdit implements OnInit {
28 protected serverService: ServerService,
29 protected formValidatorService: FormValidatorService,
30 protected configService: ConfigService,
31 protected screenService: ScreenService,
32 protected auth: AuthService,
33 private route: ActivatedRoute,
34 private router: Router,
35 private notifier: Notifier,
36 private userService: UserService
40 this.buildQuotaOptions()
46 const defaultValues = {
47 role: UserRole.USER.toString(),
53 username: USER_USERNAME_VALIDATOR,
54 channelName: USER_CHANNEL_NAME_VALIDATOR,
55 email: USER_EMAIL_VALIDATOR,
56 password: this.isPasswordOptional() ? USER_PASSWORD_OPTIONAL_VALIDATOR : USER_PASSWORD_VALIDATOR,
57 role: USER_ROLE_VALIDATOR,
58 videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
59 videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR,
65 this.error = undefined
67 const userCreate: UserCreate = this.form.value
69 userCreate.adminFlags = this.buildAdminFlags(this.form.value)
71 // A select in HTML is always mapped as a string, we convert it to number
72 userCreate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
73 userCreate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
75 this.userService.addUser(userCreate).subscribe(
77 this.notifier.success($localize`User ${userCreate.username} created.`)
78 this.router.navigate([ '/admin/users/list' ])
81 err => this.error = err.message
89 isPasswordOptional () {
90 const serverConfig = this.route.snapshot.data.serverConfig
91 return serverConfig.email.enabled
94 getFormButtonTitle () {
95 return $localize`Create user`