]>
Commit | Line | Data |
---|---|---|
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, UserService } from '@app/core' | |
5 | import { | |
6 | USER_CHANNEL_NAME_VALIDATOR, | |
7 | USER_EMAIL_VALIDATOR, | |
8 | USER_PASSWORD_OPTIONAL_VALIDATOR, | |
9 | USER_PASSWORD_VALIDATOR, | |
10 | USER_ROLE_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' | |
18 | ||
19 | @Component({ | |
20 | selector: 'my-user-create', | |
21 | templateUrl: './user-edit.component.html', | |
22 | styleUrls: [ './user-edit.component.scss' ] | |
23 | }) | |
24 | export class UserCreateComponent extends UserEdit implements OnInit { | |
25 | error: string | |
26 | ||
27 | constructor ( | |
28 | protected serverService: ServerService, | |
29 | protected formValidatorService: FormValidatorService, | |
30 | protected configService: ConfigService, | |
31 | protected screenService: ScreenService, | |
32 | protected auth: AuthService, | |
33 | private router: Router, | |
34 | private notifier: Notifier, | |
35 | private userService: UserService | |
36 | ) { | |
37 | super() | |
38 | ||
39 | this.buildQuotaOptions() | |
40 | } | |
41 | ||
42 | ngOnInit () { | |
43 | super.ngOnInit() | |
44 | ||
45 | const defaultValues = { | |
46 | role: UserRole.USER.toString(), | |
47 | videoQuota: -1, | |
48 | videoQuotaDaily: -1 | |
49 | } | |
50 | ||
51 | this.buildForm({ | |
52 | username: USER_USERNAME_VALIDATOR, | |
53 | channelName: USER_CHANNEL_NAME_VALIDATOR, | |
54 | email: USER_EMAIL_VALIDATOR, | |
55 | password: this.isPasswordOptional() ? USER_PASSWORD_OPTIONAL_VALIDATOR : USER_PASSWORD_VALIDATOR, | |
56 | role: USER_ROLE_VALIDATOR, | |
57 | videoQuota: USER_VIDEO_QUOTA_VALIDATOR, | |
58 | videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR, | |
59 | byPassAutoBlock: null | |
60 | }, defaultValues) | |
61 | } | |
62 | ||
63 | formValidated () { | |
64 | this.error = undefined | |
65 | ||
66 | const userCreate: UserCreate = this.form.value | |
67 | ||
68 | userCreate.adminFlags = this.buildAdminFlags(this.form.value) | |
69 | ||
70 | // A select in HTML is always mapped as a string, we convert it to number | |
71 | userCreate.videoQuota = parseInt(this.form.value['videoQuota'], 10) | |
72 | userCreate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10) | |
73 | ||
74 | this.userService.addUser(userCreate) | |
75 | .subscribe({ | |
76 | next: () => { | |
77 | this.notifier.success($localize`User ${userCreate.username} created.`) | |
78 | this.router.navigate([ '/admin/users/list' ]) | |
79 | }, | |
80 | ||
81 | error: err => { | |
82 | this.error = err.message | |
83 | } | |
84 | }) | |
85 | } | |
86 | ||
87 | isCreation () { | |
88 | return true | |
89 | } | |
90 | ||
91 | isPasswordOptional () { | |
92 | return this.serverConfig.email.enabled | |
93 | } | |
94 | ||
95 | getFormButtonTitle () { | |
96 | return $localize`Create user` | |
97 | } | |
98 | } |