user-list.component.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { Component, OnInit, ViewChild } from '@angular/core'
  2. import { AuthService, Notifier } from '@app/core'
  3. import { SortMeta } from 'primeng/components/common/sortmeta'
  4. import { ConfirmService, ServerService } from '../../../core'
  5. import { RestPagination, RestTable, UserService } from '../../../shared'
  6. import { I18n } from '@ngx-translate/i18n-polyfill'
  7. import { User } from '../../../../../../shared'
  8. import { UserBanModalComponent } from '@app/shared/moderation'
  9. import { DropdownAction } from '@app/shared/buttons/action-dropdown.component'
  10. @Component({
  11. selector: 'my-user-list',
  12. templateUrl: './user-list.component.html',
  13. styleUrls: [ './user-list.component.scss' ]
  14. })
  15. export class UserListComponent extends RestTable implements OnInit {
  16. @ViewChild('userBanModal', { static: true }) userBanModal: UserBanModalComponent
  17. users: User[] = []
  18. totalRecords = 0
  19. rowsPerPage = 10
  20. sort: SortMeta = { field: 'createdAt', order: 1 }
  21. pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
  22. selectedUsers: User[] = []
  23. bulkUserActions: DropdownAction<User[]>[] = []
  24. constructor (
  25. private notifier: Notifier,
  26. private confirmService: ConfirmService,
  27. private serverService: ServerService,
  28. private userService: UserService,
  29. private auth: AuthService,
  30. private i18n: I18n
  31. ) {
  32. super()
  33. }
  34. get authUser () {
  35. return this.auth.getUser()
  36. }
  37. get requiresEmailVerification () {
  38. return this.serverService.getConfig().signup.requiresEmailVerification
  39. }
  40. ngOnInit () {
  41. this.initialize()
  42. this.bulkUserActions = [
  43. {
  44. label: this.i18n('Delete'),
  45. handler: users => this.removeUsers(users),
  46. isDisplayed: users => users.every(u => this.authUser.canManage(u))
  47. },
  48. {
  49. label: this.i18n('Ban'),
  50. handler: users => this.openBanUserModal(users),
  51. isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === false)
  52. },
  53. {
  54. label: this.i18n('Unban'),
  55. handler: users => this.unbanUsers(users),
  56. isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === true)
  57. },
  58. {
  59. label: this.i18n('Set Email as Verified'),
  60. handler: users => this.setEmailsAsVerified(users),
  61. isDisplayed: users => {
  62. return this.requiresEmailVerification &&
  63. users.every(u => this.authUser.canManage(u) && !u.blocked && u.emailVerified === false)
  64. }
  65. }
  66. ]
  67. }
  68. openBanUserModal (users: User[]) {
  69. for (const user of users) {
  70. if (user.username === 'root') {
  71. this.notifier.error(this.i18n('You cannot ban root.'))
  72. return
  73. }
  74. }
  75. this.userBanModal.openModal(users)
  76. }
  77. onUserChanged () {
  78. this.loadData()
  79. }
  80. async unbanUsers (users: User[]) {
  81. const message = this.i18n('Do you really want to unban {{num}} users?', { num: users.length })
  82. const res = await this.confirmService.confirm(message, this.i18n('Unban'))
  83. if (res === false) return
  84. this.userService.unbanUsers(users)
  85. .subscribe(
  86. () => {
  87. const message = this.i18n('{{num}} users unbanned.', { num: users.length })
  88. this.notifier.success(message)
  89. this.loadData()
  90. },
  91. err => this.notifier.error(err.message)
  92. )
  93. }
  94. async removeUsers (users: User[]) {
  95. for (const user of users) {
  96. if (user.username === 'root') {
  97. this.notifier.error(this.i18n('You cannot delete root.'))
  98. return
  99. }
  100. }
  101. const message = this.i18n('If you remove these users, you will not be able to create others with the same username!')
  102. const res = await this.confirmService.confirm(message, this.i18n('Delete'))
  103. if (res === false) return
  104. this.userService.removeUser(users).subscribe(
  105. () => {
  106. this.notifier.success(this.i18n('{{num}} users deleted.', { num: users.length }))
  107. this.loadData()
  108. },
  109. err => this.notifier.error(err.message)
  110. )
  111. }
  112. async setEmailsAsVerified (users: User[]) {
  113. this.userService.updateUsers(users, { emailVerified: true }).subscribe(
  114. () => {
  115. this.notifier.success(this.i18n('{{num}} users email set as verified.', { num: users.length }))
  116. this.loadData()
  117. },
  118. err => this.notifier.error(err.message)
  119. )
  120. }
  121. isInSelectionMode () {
  122. return this.selectedUsers.length !== 0
  123. }
  124. protected loadData () {
  125. this.selectedUsers = []
  126. this.userService.getUsers(this.pagination, this.sort, this.search)
  127. .subscribe(
  128. resultList => {
  129. this.users = resultList.data
  130. this.totalRecords = resultList.total
  131. },
  132. err => this.notifier.error(err.message)
  133. )
  134. }
  135. }