1 import { Component, OnInit } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { ConfigService } from '@app/+admin/config/shared/config.service'
4 import { AuthService, Notifier, ScreenService, ServerService } 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 { FormReactiveService } from '@app/shared/shared-forms'
16 import { UserAdminService } from '@app/shared/shared-users'
17 import { UserCreate, UserRole } from '@shared/models'
18 import { UserEdit } from './user-edit'
21 selector: 'my-user-create',
22 templateUrl: './user-edit.component.html',
23 styleUrls: [ './user-edit.component.scss' ]
25 export class UserCreateComponent extends UserEdit implements OnInit {
29 protected serverService: ServerService,
30 protected formReactiveService: FormReactiveService,
31 protected configService: ConfigService,
32 protected screenService: ScreenService,
33 protected auth: AuthService,
34 private router: Router,
35 private notifier: Notifier,
36 private userAdminService: UserAdminService
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.userAdminService.addUser(userCreate)
78 this.notifier.success($localize`User ${userCreate.username} created.`)
79 this.router.navigate([ '/admin/users/list' ])
83 this.error = err.message
92 isPasswordOptional () {
93 return this.serverConfig.email.enabled
96 getFormButtonTitle () {
97 return $localize`Create user`