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

100 lines
2.8 KiB
TypeScript
Raw Normal View History

import { Component, OnInit } from '@angular/core'
2021-07-16 16:26:20 +02:00
import { Router } from '@angular/router'
2018-09-26 14:46:54 +02:00
import { ConfigService } from '@app/+admin/config/shared/config.service'
2022-01-21 11:03:25 +01:00
import { AuthService, Notifier, ScreenService, ServerService } from '@app/core'
import {
USER_CHANNEL_NAME_VALIDATOR,
USER_EMAIL_VALIDATOR,
USER_PASSWORD_OPTIONAL_VALIDATOR,
USER_PASSWORD_VALIDATOR,
USER_ROLE_VALIDATOR,
USER_USERNAME_VALIDATOR,
USER_VIDEO_QUOTA_DAILY_VALIDATOR,
USER_VIDEO_QUOTA_VALIDATOR
} from '@app/shared/form-validators/user-validators'
2022-10-07 15:26:53 +02:00
import { FormReactiveService } from '@app/shared/shared-forms'
2022-01-21 11:03:25 +01:00
import { UserAdminService } from '@app/shared/shared-users'
import { UserCreate, UserRole } from '@peertube/peertube-models'
2020-06-23 14:10:17 +02:00
import { UserEdit } from './user-edit'
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,
2022-10-07 15:26:53 +02:00
protected formReactiveService: FormReactiveService,
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,
2016-09-09 22:16:51 +02:00
private router: Router,
private notifier: Notifier,
2022-01-21 11:03:25 +01:00
private userAdminService: UserAdminService
2021-08-17 14:42:53 +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(),
2021-02-10 09:05:29 +01:00
videoQuota: -1,
videoQuotaDaily: -1
2018-06-05 10:58:45 +02:00
}
this.buildForm({
username: USER_USERNAME_VALIDATOR,
channelName: USER_CHANNEL_NAME_VALIDATOR,
email: USER_EMAIL_VALIDATOR,
password: this.isPasswordOptional() ? USER_PASSWORD_OPTIONAL_VALIDATOR : USER_PASSWORD_VALIDATOR,
role: USER_ROLE_VALIDATOR,
videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR,
byPassAutoBlock: 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
2022-01-21 11:03:25 +01:00
this.userAdminService.addUser(userCreate)
2021-08-17 11:27:47 +02:00
.subscribe({
next: () => {
this.notifier.success($localize`User ${userCreate.username} created.`)
this.router.navigate([ '/admin/users/list' ])
},
2016-08-09 21:45:21 +02:00
2021-08-17 14:42:53 +02:00
error: err => {
this.error = err.message
}
2021-08-17 11:27:47 +02:00
})
2016-08-09 21:45:21 +02:00
}
2017-09-05 21:29:39 +02:00
isCreation () {
return true
}
isPasswordOptional () {
2021-07-16 16:26:20 +02:00
return this.serverConfig.email.enabled
}
2017-09-05 21:29:39 +02:00
getFormButtonTitle () {
return $localize`Create user`
2017-09-05 21:29:39 +02:00
}
2016-08-09 21:45:21 +02:00
}