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