]>
Commit | Line | Data |
---|---|---|
df98563e | 1 | import { Component, OnInit } from '@angular/core' |
67ed6552 | 2 | import { ActivatedRoute, Router } from '@angular/router' |
3827c3b3 | 3 | import { ConfigService } from '@app/+admin/config/shared/config.service' |
67ed6552 C |
4 | import { AuthService, Notifier, ScreenService, ServerService, UserService } from '@app/core' |
5 | import { FormValidatorService, UserValidatorsService } from '@app/shared/shared-forms' | |
67ed6552 C |
6 | import { UserCreate, UserRole } from '@shared/models' |
7 | import { UserEdit } from './user-edit' | |
7da18e44 C |
8 | |
9 | @Component({ | |
4c200caa | 10 | selector: 'my-user-create', |
6a84aafd C |
11 | templateUrl: './user-edit.component.html', |
12 | styleUrls: [ './user-edit.component.scss' ] | |
7da18e44 | 13 | }) |
4c200caa | 14 | export class UserCreateComponent extends UserEdit implements OnInit { |
8094a898 | 15 | error: string |
7da18e44 | 16 | |
df98563e | 17 | constructor ( |
6a84aafd | 18 | protected serverService: ServerService, |
d18d6478 | 19 | protected formValidatorService: FormValidatorService, |
3827c3b3 | 20 | protected configService: ConfigService, |
76314386 | 21 | protected screenService: ScreenService, |
a95a4cc8 | 22 | protected auth: AuthService, |
e309822b | 23 | private userValidatorsService: UserValidatorsService, |
45f1bd72 | 24 | private route: ActivatedRoute, |
4b2f33f3 | 25 | private router: Router, |
f8b2c1b4 | 26 | private notifier: Notifier, |
66357162 C |
27 | private userService: UserService |
28 | ) { | |
df98563e | 29 | super() |
3827c3b3 C |
30 | |
31 | this.buildQuotaOptions() | |
4b2f33f3 C |
32 | } |
33 | ||
df98563e | 34 | ngOnInit () { |
ba430d75 C |
35 | super.ngOnInit() |
36 | ||
d18d6478 C |
37 | const defaultValues = { |
38 | role: UserRole.USER.toString(), | |
d6ca951b C |
39 | videoQuota: '-1', |
40 | videoQuotaDaily: '-1' | |
d18d6478 C |
41 | } |
42 | ||
43 | this.buildForm({ | |
e309822b | 44 | username: this.userValidatorsService.USER_USERNAME, |
4abe9c59 | 45 | channelName: this.userValidatorsService.USER_CHANNEL_NAME, |
e309822b | 46 | email: this.userValidatorsService.USER_EMAIL, |
45f1bd72 | 47 | password: this.isPasswordOptional() ? this.userValidatorsService.USER_PASSWORD_OPTIONAL : this.userValidatorsService.USER_PASSWORD, |
e309822b | 48 | role: this.userValidatorsService.USER_ROLE, |
d6ca951b | 49 | videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA, |
1eddc9a7 | 50 | videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY, |
3487330d | 51 | byPassAutoBlock: null |
d18d6478 | 52 | }, defaultValues) |
7da18e44 C |
53 | } |
54 | ||
8094a898 C |
55 | formValidated () { |
56 | this.error = undefined | |
7da18e44 | 57 | |
4771e000 | 58 | const userCreate: UserCreate = this.form.value |
4b2f33f3 | 59 | |
1eddc9a7 C |
60 | userCreate.adminFlags = this.buildAdminFlags(this.form.value) |
61 | ||
b0f9f39e C |
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) | |
1eddc9a7 | 64 | userCreate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10) |
b0f9f39e | 65 | |
4771e000 | 66 | this.userService.addUser(userCreate).subscribe( |
7ddd02c9 | 67 | () => { |
66357162 | 68 | this.notifier.success($localize`User ${userCreate.username} created.`) |
df98563e | 69 | this.router.navigate([ '/admin/users/list' ]) |
7ddd02c9 | 70 | }, |
7da18e44 | 71 | |
f7354483 | 72 | err => this.error = err.message |
df98563e | 73 | ) |
7da18e44 | 74 | } |
8094a898 C |
75 | |
76 | isCreation () { | |
77 | return true | |
78 | } | |
79 | ||
45f1bd72 JL |
80 | isPasswordOptional () { |
81 | const serverConfig = this.route.snapshot.data.serverConfig | |
82 | return serverConfig.email.enabled | |
83 | } | |
84 | ||
8094a898 | 85 | getFormButtonTitle () { |
66357162 | 86 | return $localize`Create user` |
8094a898 | 87 | } |
7da18e44 | 88 | } |