user-update.component.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { Component, OnDestroy, OnInit } from '@angular/core'
  2. import { ActivatedRoute, Router } from '@angular/router'
  3. import { Subscription } from 'rxjs'
  4. import { AuthService, Notifier } from '@app/core'
  5. import { ServerService } from '../../../core'
  6. import { UserEdit } from './user-edit'
  7. import { User, UserUpdate } from '../../../../../../shared'
  8. import { I18n } from '@ngx-translate/i18n-polyfill'
  9. import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
  10. import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service'
  11. import { ConfigService } from '@app/+admin/config/shared/config.service'
  12. import { UserService } from '@app/shared'
  13. import { UserAdminFlag } from '@shared/models/users/user-flag.model'
  14. @Component({
  15. selector: 'my-user-update',
  16. templateUrl: './user-edit.component.html',
  17. styleUrls: [ './user-edit.component.scss' ]
  18. })
  19. export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy {
  20. error: string
  21. userId: number
  22. userEmail: string
  23. username: string
  24. private paramsSub: Subscription
  25. constructor (
  26. protected formValidatorService: FormValidatorService,
  27. protected serverService: ServerService,
  28. protected configService: ConfigService,
  29. protected auth: AuthService,
  30. private userValidatorsService: UserValidatorsService,
  31. private route: ActivatedRoute,
  32. private router: Router,
  33. private notifier: Notifier,
  34. private userService: UserService,
  35. private i18n: I18n
  36. ) {
  37. super()
  38. this.buildQuotaOptions()
  39. }
  40. ngOnInit () {
  41. const defaultValues = { videoQuota: '-1', videoQuotaDaily: '-1' }
  42. this.buildForm({
  43. email: this.userValidatorsService.USER_EMAIL,
  44. role: this.userValidatorsService.USER_ROLE,
  45. videoQuota: this.userValidatorsService.USER_VIDEO_QUOTA,
  46. videoQuotaDaily: this.userValidatorsService.USER_VIDEO_QUOTA_DAILY,
  47. byPassAutoBlacklist: null
  48. }, defaultValues)
  49. this.paramsSub = this.route.params.subscribe(routeParams => {
  50. const userId = routeParams['id']
  51. this.userService.getUser(userId).subscribe(
  52. user => this.onUserFetched(user),
  53. err => this.error = err.message
  54. )
  55. })
  56. }
  57. ngOnDestroy () {
  58. this.paramsSub.unsubscribe()
  59. }
  60. formValidated () {
  61. this.error = undefined
  62. const userUpdate: UserUpdate = this.form.value
  63. userUpdate.adminFlags = this.buildAdminFlags(this.form.value)
  64. // A select in HTML is always mapped as a string, we convert it to number
  65. userUpdate.videoQuota = parseInt(this.form.value['videoQuota'], 10)
  66. userUpdate.videoQuotaDaily = parseInt(this.form.value['videoQuotaDaily'], 10)
  67. this.userService.updateUser(this.userId, userUpdate).subscribe(
  68. () => {
  69. this.notifier.success(this.i18n('User {{username}} updated.', { username: this.username }))
  70. this.router.navigate([ '/admin/users/list' ])
  71. },
  72. err => this.error = err.message
  73. )
  74. }
  75. isCreation () {
  76. return false
  77. }
  78. getFormButtonTitle () {
  79. return this.i18n('Update user')
  80. }
  81. resetPassword () {
  82. this.userService.askResetPassword(this.userEmail).subscribe(
  83. () => {
  84. this.notifier.success(
  85. this.i18n('An email asking for password reset has been sent to {{username}}.', { username: this.username })
  86. )
  87. },
  88. err => this.error = err.message
  89. )
  90. }
  91. private onUserFetched (userJson: User) {
  92. this.userId = userJson.id
  93. this.username = userJson.username
  94. this.userEmail = userJson.email
  95. this.form.patchValue({
  96. email: userJson.email,
  97. role: userJson.role,
  98. videoQuota: userJson.videoQuota,
  99. videoQuotaDaily: userJson.videoQuotaDaily,
  100. byPassAutoBlacklist: userJson.adminFlags & UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST
  101. })
  102. }
  103. }