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

186 lines
5.8 KiB
TypeScript
Raw Normal View History

2020-06-23 14:10:17 +02:00
import { Subscription } from 'rxjs'
import { Component, OnDestroy, OnInit } from '@angular/core'
2024-03-04 10:01:52 +01:00
import { ActivatedRoute, Router, RouterLink } from '@angular/router'
2018-09-26 14:46:54 +02:00
import { ConfigService } from '@app/+admin/config/shared/config.service'
2020-06-23 14:10:17 +02:00
import { AuthService, Notifier, ScreenService, ServerService, User, UserService } from '@app/core'
import {
USER_EMAIL_VALIDATOR,
USER_ROLE_VALIDATOR,
USER_VIDEO_QUOTA_DAILY_VALIDATOR,
USER_VIDEO_QUOTA_VALIDATOR
} from '@app/shared/form-validators/user-validators'
import { FormReactiveService } from '@app/shared/shared-forms/form-reactive.service'
import { User as UserType, UserAdminFlag, UserRole, UserUpdate } from '@peertube/peertube-models'
2020-06-23 14:10:17 +02:00
import { UserEdit } from './user-edit'
2024-03-04 10:01:52 +01:00
import { BytesPipe } from '../../../../shared/shared-main/angular/bytes.pipe'
import { UserPasswordComponent } from './user-password.component'
import { PeertubeCheckboxComponent } from '../../../../shared/shared-forms/peertube-checkbox.component'
import { UserRealQuotaInfoComponent } from '../../../shared/user-real-quota-info.component'
import { SelectCustomValueComponent } from '../../../../shared/shared-forms/select/select-custom-value.component'
import { InputTextComponent } from '../../../../shared/shared-forms/input-text.component'
import { PeerTubeTemplateDirective } from '../../../../shared/shared-main/angular/peertube-template.directive'
import { HelpComponent } from '../../../../shared/shared-main/misc/help.component'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
import { ActorAvatarEditComponent } from '../../../../shared/shared-actor-image-edit/actor-avatar-edit.component'
import { NgIf, NgTemplateOutlet, NgClass, NgFor } from '@angular/common'
import { UserAdminService } from '@app/shared/shared-users/user-admin.service'
import { TwoFactorService } from '@app/shared/shared-users/two-factor.service'
2017-09-05 21:29:39 +02:00
@Component({
selector: 'my-user-update',
templateUrl: './user-edit.component.html',
2024-03-04 10:01:52 +01:00
styleUrls: [ './user-edit.component.scss' ],
standalone: true,
imports: [
RouterLink,
NgIf,
NgTemplateOutlet,
ActorAvatarEditComponent,
FormsModule,
ReactiveFormsModule,
NgClass,
HelpComponent,
PeerTubeTemplateDirective,
InputTextComponent,
NgFor,
SelectCustomValueComponent,
UserRealQuotaInfoComponent,
PeertubeCheckboxComponent,
UserPasswordComponent,
BytesPipe
]
2017-09-05 21:29:39 +02:00
})
export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
error: string
private paramsSub: Subscription
constructor (
2022-10-07 15:26:53 +02:00
protected formReactiveService: FormReactiveService,
protected serverService: ServerService,
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,
2017-09-05 21:29:39 +02:00
private route: ActivatedRoute,
private router: Router,
private notifier: Notifier,
2022-01-21 11:03:25 +01:00
private userService: UserService,
private twoFactorService: TwoFactorService,
2022-01-21 11:03:25 +01:00
private userAdminService: UserAdminService
2021-08-17 14:42:53 +02:00
) {
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 = {
role: UserRole.USER.toString(),
videoQuota: '-1',
videoQuotaDaily: '-1'
}
2018-06-05 10:58:45 +02:00
this.buildForm({
email: USER_EMAIL_VALIDATOR,
role: USER_ROLE_VALIDATOR,
videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR,
2021-02-01 15:39:13 +01:00
byPassAutoBlock: null,
pluginAuth: 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']
2021-08-17 11:27:47 +02:00
this.userService.getUser(userId, true)
.subscribe({
next: user => this.onUserFetched(user),
2017-09-05 21:29:39 +02:00
2021-08-17 14:42:53 +02:00
error: err => {
this.error = err.message
}
2021-08-17 11:27:47 +02:00
})
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
2021-05-25 13:49:52 +02:00
if (userUpdate.pluginAuth === 'null') userUpdate.pluginAuth = null
2022-01-21 11:03:25 +01:00
this.userAdminService.updateUser(this.user.id, userUpdate)
2021-08-17 11:27:47 +02:00
.subscribe({
next: () => {
this.notifier.success($localize`User ${this.user.username} updated.`)
this.router.navigate([ '/admin/users/list' ])
},
2017-09-05 21:29:39 +02:00
2021-08-17 14:42:53 +02:00
error: err => {
this.error = err.message
}
2021-08-17 11:27:47 +02:00
})
2017-09-05 21:29:39 +02:00
}
isCreation () {
return false
}
isPasswordOptional () {
return false
}
2017-09-05 21:29:39 +02:00
getFormButtonTitle () {
return $localize`Update user`
2017-09-05 21:29:39 +02:00
}
resetPassword () {
2021-08-17 11:27:47 +02:00
this.userService.askResetPassword(this.user.email)
.subscribe({
next: () => {
this.notifier.success($localize`An email asking for password reset has been sent to ${this.user.username}.`)
},
error: err => this.notifier.error(err.message)
})
}
disableTwoFactorAuth () {
this.twoFactorService.disableTwoFactor({ userId: this.user.id })
.subscribe({
next: () => {
this.user.twoFactorEnabled = false
this.notifier.success($localize`Two factor authentication of ${this.user.username} disabled.`)
},
error: err => this.notifier.error(err.message)
2021-08-17 11:27:47 +02:00
})
}
private onUserFetched (userJson: UserType) {
this.user = new User(userJson)
2017-09-05 21:29:39 +02:00
this.form.patchValue({
email: userJson.email,
role: userJson.role.id.toString(),
videoQuota: userJson.videoQuota,
2019-04-15 10:49:46 +02:00
videoQuotaDaily: userJson.videoQuotaDaily,
2021-02-01 15:39:13 +01:00
pluginAuth: userJson.pluginAuth,
byPassAutoBlock: userJson.adminFlags & UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST
2017-09-05 21:29:39 +02:00
})
}
}