user-update.component.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { Subscription } from 'rxjs'
  2. import { Component, OnDestroy, OnInit } from '@angular/core'
  3. import { ActivatedRoute, Router } from '@angular/router'
  4. import { ConfigService } from '@app/+admin/config/shared/config.service'
  5. import { AuthService, Notifier, ScreenService, ServerService, User, UserService } from '@app/core'
  6. import {
  7. USER_EMAIL_VALIDATOR,
  8. USER_ROLE_VALIDATOR,
  9. USER_VIDEO_QUOTA_DAILY_VALIDATOR,
  10. USER_VIDEO_QUOTA_VALIDATOR
  11. } from '@app/shared/form-validators/user-validators'
  12. import { FormReactiveService } from '@app/shared/shared-forms'
  13. import { TwoFactorService, UserAdminService } from '@app/shared/shared-users'
  14. import { User as UserType, UserAdminFlag, UserRole, UserUpdate } from '@peertube/peertube-models'
  15. import { UserEdit } from './user-edit'
  16. @Component({
  17. selector: 'my-user-update',
  18. templateUrl: './user-edit.component.html',
  19. styleUrls: [ './user-edit.component.scss' ]
  20. })
  21. export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
  22. error: string
  23. private paramsSub: Subscription
  24. constructor (
  25. protected formReactiveService: FormReactiveService,
  26. protected serverService: ServerService,
  27. protected configService: ConfigService,
  28. protected screenService: ScreenService,
  29. protected auth: AuthService,
  30. private route: ActivatedRoute,
  31. private router: Router,
  32. private notifier: Notifier,
  33. private userService: UserService,
  34. private twoFactorService: TwoFactorService,
  35. private userAdminService: UserAdminService
  36. ) {
  37. super()
  38. this.buildQuotaOptions()
  39. }
  40. ngOnInit () {
  41. super.ngOnInit()
  42. const defaultValues = {
  43. role: UserRole.USER.toString(),
  44. videoQuota: '-1',
  45. videoQuotaDaily: '-1'
  46. }
  47. this.buildForm({
  48. email: USER_EMAIL_VALIDATOR,
  49. role: USER_ROLE_VALIDATOR,
  50. videoQuota: USER_VIDEO_QUOTA_VALIDATOR,
  51. videoQuotaDaily: USER_VIDEO_QUOTA_DAILY_VALIDATOR,
  52. byPassAutoBlock: null,
  53. pluginAuth: null
  54. }, defaultValues)
  55. this.paramsSub = this.route.params.subscribe(routeParams => {
  56. const userId = routeParams['id']
  57. this.userService.getUser(userId, true)
  58. .subscribe({
  59. next: user => this.onUserFetched(user),
  60. error: err => {
  61. this.error = err.message
  62. }
  63. })
  64. })
  65. }
  66. ngOnDestroy () {
  67. this.paramsSub.unsubscribe()
  68. }
  69. formValidated () {
  70. this.error = undefined
  71. const userUpdate: UserUpdate = this.form.value
  72. userUpdate.adminFlags = this.buildAdminFlags(this.form.value)
  73. // A select in HTML is always mapped as a string, we convert it to number
  74. userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
  75. userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
  76. if (userUpdate.pluginAuth === 'null') userUpdate.pluginAuth = null
  77. this.userAdminService.updateUser(this.user.id, userUpdate)
  78. .subscribe({
  79. next: () => {
  80. this.notifier.success($localize`User ${this.user.username} updated.`)
  81. this.router.navigate([ '/admin/users/list' ])
  82. },
  83. error: err => {
  84. this.error = err.message
  85. }
  86. })
  87. }
  88. isCreation () {
  89. return false
  90. }
  91. isPasswordOptional () {
  92. return false
  93. }
  94. getFormButtonTitle () {
  95. return $localize`Update user`
  96. }
  97. resetPassword () {
  98. this.userService.askResetPassword(this.user.email)
  99. .subscribe({
  100. next: () => {
  101. this.notifier.success($localize`An email asking for password reset has been sent to ${this.user.username}.`)
  102. },
  103. error: err => this.notifier.error(err.message)
  104. })
  105. }
  106. disableTwoFactorAuth () {
  107. this.twoFactorService.disableTwoFactor({ userId: this.user.id })
  108. .subscribe({
  109. next: () => {
  110. this.user.twoFactorEnabled = false
  111. this.notifier.success($localize`Two factor authentication of ${this.user.username} disabled.`)
  112. },
  113. error: err => this.notifier.error(err.message)
  114. })
  115. }
  116. private onUserFetched (userJson: UserType) {
  117. this.user = new User(userJson)
  118. this.form.patchValue({
  119. email: userJson.email,
  120. role: userJson.role.id.toString(),
  121. videoQuota: userJson.videoQuota,
  122. videoQuotaDaily: userJson.videoQuotaDaily,
  123. pluginAuth: userJson.pluginAuth,
  124. byPassAutoBlock: userJson.adminFlags & UserAdminFlag.BYPASS_VIDEO_AUTO_BLACKLIST
  125. })
  126. }
  127. }