PeerTube/client/src/app/+admin/users/user-edit/user-create.component.ts

93 lines
3.0 KiB
TypeScript
Raw Normal View History

import { Component, OnInit } from '@angular/core'
import { Router, ActivatedRoute } from '@angular/router'
2019-07-30 09:59:19 +02:00
import { AuthService, Notifier, ServerService } from '@app/core'
import { UserCreate, UserRole } from '../../../../../../shared'
2017-09-05 21:29:39 +02:00
import { UserEdit } from './user-edit'
2018-06-04 16:21:17 +02:00
import { I18n } from '@ngx-translate/i18n-polyfill'
2018-06-05 10:58:45 +02:00
import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
2018-06-05 15:01:45 +02:00
import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service'
2018-09-26 14:46:54 +02:00
import { ConfigService } from '@app/+admin/config/shared/config.service'
import { UserService } from '@app/shared'
import { ScreenService } from '@app/shared/misc/screen.service'
2016-08-09 21:45:21 +02:00
@Component({
2018-05-09 15:30:37 +02:00
selector: 'my-user-create',
templateUrl: './user-edit.component.html',
styleUrls: [ './user-edit.component.scss' ]
2016-08-09 21:45:21 +02:00
})
2018-05-09 15:30:37 +02:00
export class UserCreateComponent extends UserEdit implements OnInit {
2017-09-05 21:29:39 +02:00
error: string
2016-08-09 21:45:21 +02:00
constructor (
protected serverService: ServerService,
2018-06-05 10:58:45 +02:00
protected formValidatorService: FormValidatorService,
2018-09-26 14:46:54 +02:00
protected configService: ConfigService,
protected screenService: ScreenService,
2019-07-30 09:59:19 +02:00
protected auth: AuthService,
2018-06-05 15:01:45 +02:00
private userValidatorsService: UserValidatorsService,
private route: ActivatedRoute,
2016-09-09 22:16:51 +02:00
private router: Router,
private notifier: Notifier,
2018-06-04 16:21:17 +02:00
private userService: UserService,
private i18n: I18n
2016-09-09 22:16:51 +02:00
) {
super()
2018-09-26 14:46:54 +02:00
this.buildQuotaOptions()
2016-09-09 22:16:51 +02:00
}
ngOnInit () {
2019-12-18 15:31:54 +01:00
super.ngOnInit()
2018-06-05 10:58:45 +02:00
const defaultValues = {
role: UserRole.USER.toString(),
2018-08-31 10:05:54 +02:00
videoQuota: '-1',
videoQuotaDaily: '-1'
2018-06-05 10:58:45 +02:00
}
this.buildForm({
2018-06-05 15:01:45 +02:00
username: this.userValidatorsService.USER_USERNAME,
email: this.userValidatorsService.USER_EMAIL,
password: this.isPasswordOptional() ? this.userValidatorsService.USER_PASSWORD_OPTIONAL : this.userValidatorsService.USER_PASSWORD,
2018-06-05 15:01:45 +02:00
role: this.userValidatorsService.USER_ROLE,
2018-08-31 10:05:54 +02:00
videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
2019-04-15 10:49:46 +02:00
videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY,
byPassAutoBlacklist: null
2018-06-05 10:58:45 +02:00
}, defaultValues)
2016-08-09 21:45:21 +02:00
}
2017-09-05 21:29:39 +02:00
formValidated () {
this.error = undefined
2016-08-09 21:45:21 +02:00
const userCreate: UserCreate = this.form.value
2016-09-09 22:16:51 +02:00
2019-04-15 10:49:46 +02:00
userCreate.adminFlags = this.buildAdminFlags(this.form.value)
2017-09-04 20:07:54 +02:00
// A select in HTML is always mapped as a string, we convert it to number
userCreate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
2019-04-15 10:49:46 +02:00
userCreate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
2017-09-04 20:07:54 +02:00
this.userService.addUser(userCreate).subscribe(
() => {
this.notifier.success(this.i18n('User {{username}} created.', { username: userCreate.username }))
this.router.navigate([ '/admin/users/list' ])
},
2016-08-09 21:45:21 +02:00
2017-11-04 18:20:13 +01:00
err => this.error = err.message
)
2016-08-09 21:45:21 +02:00
}
2017-09-05 21:29:39 +02:00
isCreation () {
return true
}
isPasswordOptional () {
const serverConfig = this.route.snapshot.data.serverConfig
return serverConfig.email.enabled
}
2017-09-05 21:29:39 +02:00
getFormButtonTitle () {
2018-06-04 16:21:17 +02:00
return this.i18n('Create user')
2017-09-05 21:29:39 +02:00
}
2016-08-09 21:45:21 +02:00
}