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

129 lines
3.8 KiB
TypeScript
Raw Normal View History

import { Component, OnDestroy, OnInit } from '@angular/core'
2017-09-05 21:29:39 +02:00
import { ActivatedRoute, Router } from '@angular/router'
2018-05-15 11:55:51 +02:00
import { Subscription } from 'rxjs'
2019-07-30 09:59:19 +02:00
import { AuthService, Notifier } from '@app/core'
import { ServerService } from '../../../core'
2017-09-05 21:29:39 +02:00
import { UserEdit } from './user-edit'
2018-09-26 14:46:54 +02:00
import { User, UserUpdate } from '../../../../../../shared'
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'
2019-04-15 10:49:46 +02:00
import { UserAdminFlag } from '@shared/models/users/user-flag.model'
2017-09-05 21:29:39 +02:00
@Component({
selector: 'my-user-update',
templateUrl: './user-edit.component.html',
styleUrls: [ './user-edit.component.scss' ]
2017-09-05 21:29:39 +02:00
})
export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
error: string
userId: number
userEmail: string
2017-09-05 21:29:39 +02:00
username: string
private paramsSub: Subscription
constructor (
2018-06-05 10:58:45 +02:00
protected formValidatorService: FormValidatorService,
protected serverService: ServerService,
2018-09-26 14:46:54 +02:00
protected configService: ConfigService,
2019-07-30 09:59:19 +02:00
protected auth: AuthService,
2018-06-05 15:01:45 +02:00
private userValidatorsService: UserValidatorsService,
2017-09-05 21:29:39 +02:00
private route: ActivatedRoute,
private router: Router,
private notifier: Notifier,
2018-06-04 16:21:17 +02:00
private userService: UserService,
private i18n: I18n
2017-09-05 21:29:39 +02:00
) {
super()
2018-09-26 14:46:54 +02:00
this.buildQuotaOptions()
2017-09-05 21:29:39 +02:00
}
ngOnInit () {
2019-12-18 15:31:54 +01:00
super.ngOnInit()
const defaultValues = { videoQuota: '-1', videoQuotaDaily: '-1' }
2018-06-05 10:58:45 +02:00
this.buildForm({
2018-06-05 15:01:45 +02:00
email: this.userValidatorsService.USER_EMAIL,
role: this.userValidatorsService.USER_ROLE,
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)
2017-09-05 21:29:39 +02:00
this.paramsSub = this.route.params.subscribe(routeParams => {
const userId = routeParams['id']
this.userService.getUser(userId).subscribe(
user => this.onUserFetched(user),
2017-11-04 18:20:13 +01:00
err => this.error = err.message
2017-09-05 21:29:39 +02:00
)
})
}
ngOnDestroy () {
this.paramsSub.unsubscribe()
}
formValidated () {
this.error = undefined
const userUpdate: UserUpdate = this.form.value
2019-04-15 10:49:46 +02:00
userUpdate.adminFlags = this.buildAdminFlags(this.form.value)
2017-09-05 21:29:39 +02:00
// A select in HTML is always mapped as a string, we convert it to number
userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
2017-09-05 21:29:39 +02:00
this.userService.updateUser(this.userId, userUpdate).subscribe(
() => {
this.notifier.success(this.i18n('User {{username}} updated.', { username: this.username }))
2017-09-05 21:29:39 +02:00
this.router.navigate([ '/admin/users/list' ])
},
2017-11-04 18:20:13 +01:00
err => this.error = err.message
2017-09-05 21:29:39 +02:00
)
}
isCreation () {
return false
}
isPasswordOptional () {
return false
}
2017-09-05 21:29:39 +02:00
getFormButtonTitle () {
2018-06-04 16:21:17 +02:00
return this.i18n('Update user')
2017-09-05 21:29:39 +02:00
}
resetPassword () {
this.userService.askResetPassword(this.userEmail).subscribe(
() => {
this.notifier.success(
this.i18n('An email asking for password reset has been sent to {{username}}.', { username: this.username })
)
},
err => this.error = err.message
)
}
2017-09-05 21:29:39 +02:00
private onUserFetched (userJson: User) {
this.userId = userJson.id
this.username = userJson.username
this.userEmail = userJson.email
2017-09-05 21:29:39 +02:00
this.form.patchValue({
email: userJson.email,
role: userJson.role,
videoQuota: userJson.videoQuota,
2019-04-15 10:49:46 +02:00
videoQuotaDaily: userJson.videoQuotaDaily,
byPassAutoBlacklist: userJson.adminFlags & UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST
2017-09-05 21:29:39 +02:00
})
}
}