]>
Commit | Line | Data |
---|---|---|
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 { | |
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 route: ActivatedRoute, | |
34 | private router: Router, | |
35 | private notifier: Notifier, | |
36 | private userService: UserService | |
37 | ) { | |
38 | super() | |
39 | ||
40 | this.buildQuotaOptions() | |
41 | } | |
42 | ||
43 | ngOnInit () { | |
44 | super.ngOnInit() | |
45 | ||
46 | const defaultValues = { | |
47 | role: UserRole.USER.toString(), | |
48 | videoQuota: -1, | |
49 | videoQuotaDaily: -1 | |
50 | } | |
51 | ||
52 | this.buildForm({ | |
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, | |
60 | byPassAutoBlock: null | |
61 | }, defaultValues) | |
62 | } | |
63 | ||
64 | formValidated () { | |
65 | this.error = undefined | |
66 | ||
67 | const userCreate: UserCreate = this.form.value | |
68 | ||
69 | userCreate.adminFlags = this.buildAdminFlags(this.form.value) | |
70 | ||
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) | |
74 | ||
75 | this.userService.addUser(userCreate).subscribe( | |
76 | () => { | |
77 | this.notifier.success($localize`User ${userCreate.username} created.`) | |
78 | this.router.navigate([ '/admin/users/list' ]) | |
79 | }, | |
80 | ||
81 | err => this.error = err.message | |
82 | ) | |
83 | } | |
84 | ||
85 | isCreation () { | |
86 | return true | |
87 | } | |
88 | ||
89 | isPasswordOptional () { | |
90 | const serverConfig = this.route.snapshot.data.serverConfig | |
91 | return serverConfig.email.enabled | |
92 | } | |
93 | ||
94 | getFormButtonTitle () { | |
95 | return $localize`Create user` | |
96 | } | |
97 | } |