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'
12 selector: 'my-user-list',
13 templateUrl: './user-list.component.html',
14 styleUrls: [ './user-list.component.scss' ]
16 export class UserListComponent extends RestTable implements OnInit {
17 @ViewChild('userBanModal', { static: true }) userBanModal: UserBanModalComponent
22 sort: SortMeta = { field: 'createdAt', order: 1 }
23 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
25 selectedUsers: User[] = []
26 bulkUserActions: DropdownAction<User[]>[] = []
29 private notifier: Notifier,
30 private confirmService: ConfirmService,
31 private serverService: ServerService,
32 private userService: UserService,
33 private auth: AuthService,
40 return this.auth.getUser()
43 get requiresEmailVerification () {
44 return this.serverService.getConfig().signup.requiresEmailVerification
50 this.bulkUserActions = [
52 label: this.i18n('Delete'),
53 handler: users => this.removeUsers(users),
54 isDisplayed: users => users.every(u => this.authUser.canManage(u))
57 label: this.i18n('Ban'),
58 handler: users => this.openBanUserModal(users),
59 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === false)
62 label: this.i18n('Unban'),
63 handler: users => this.unbanUsers(users),
64 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === true)
67 label: this.i18n('Set Email as Verified'),
68 handler: users => this.setEmailsAsVerified(users),
69 isDisplayed: users => {
70 return this.requiresEmailVerification &&
71 users.every(u => this.authUser.canManage(u) && !u.blocked && u.emailVerified === false)
77 openBanUserModal (users: User[]) {
78 for (const user of users) {
79 if (user.username === 'root') {
80 this.notifier.error(this.i18n('You cannot ban root.'))
85 this.userBanModal.openModal(users)
92 async unbanUsers (users: User[]) {
93 const message = this.i18n('Do you really want to unban {{num}} users?', { num: users.length })
95 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
96 if (res === false) return
98 this.userService.unbanUsers(users)
101 const message = this.i18n('{{num}} users unbanned.', { num: users.length })
103 this.notifier.success(message)
107 err => this.notifier.error(err.message)
111 async removeUsers (users: User[]) {
112 for (const user of users) {
113 if (user.username === 'root') {
114 this.notifier.error(this.i18n('You cannot delete root.'))
119 const message = this.i18n('If you remove these users, you will not be able to create others with the same username!')
120 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
121 if (res === false) return
123 this.userService.removeUser(users).subscribe(
125 this.notifier.success(this.i18n('{{num}} users deleted.', { num: users.length }))
129 err => this.notifier.error(err.message)
133 async setEmailsAsVerified (users: User[]) {
134 this.userService.updateUsers(users, { emailVerified: true }).subscribe(
136 this.notifier.success(this.i18n('{{num}} users email set as verified.', { num: users.length }))
140 err => this.notifier.error(err.message)
144 isInSelectionMode () {
145 return this.selectedUsers.length !== 0
148 protected loadData () {
149 this.selectedUsers = []
151 this.userService.getUsers(this.pagination, this.sort, this.search)
154 this.users = resultList.data
155 this.totalRecords = resultList.total
158 err => this.notifier.error(err.message)