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