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 { ServerConfig, 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[]>[] = []
28 private serverConfig: ServerConfig
31 private notifier: Notifier,
32 private confirmService: ConfirmService,
33 private serverService: ServerService,
34 private userService: UserService,
35 private auth: AuthService,
42 return this.auth.getUser()
45 get requiresEmailVerification () {
46 return this.serverConfig.signup.requiresEmailVerification
50 this.serverConfig = this.serverService.getTmpConfig()
51 this.serverService.getConfig()
52 .subscribe(config => this.serverConfig = config)
56 this.bulkUserActions = [
58 label: this.i18n('Delete'),
59 handler: users => this.removeUsers(users),
60 isDisplayed: users => users.every(u => this.authUser.canManage(u))
63 label: this.i18n('Ban'),
64 handler: users => this.openBanUserModal(users),
65 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === false)
68 label: this.i18n('Unban'),
69 handler: users => this.unbanUsers(users),
70 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === true)
73 label: this.i18n('Set Email as Verified'),
74 handler: users => this.setEmailsAsVerified(users),
75 isDisplayed: users => {
76 return this.requiresEmailVerification &&
77 users.every(u => this.authUser.canManage(u) && !u.blocked && u.emailVerified === false)
83 openBanUserModal (users: User[]) {
84 for (const user of users) {
85 if (user.username === 'root') {
86 this.notifier.error(this.i18n('You cannot ban root.'))
91 this.userBanModal.openModal(users)
98 async unbanUsers (users: User[]) {
99 const message = this.i18n('Do you really want to unban {{num}} users?', { num: users.length })
101 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
102 if (res === false) return
104 this.userService.unbanUsers(users)
107 const message = this.i18n('{{num}} users unbanned.', { num: users.length })
109 this.notifier.success(message)
113 err => this.notifier.error(err.message)
117 async removeUsers (users: User[]) {
118 for (const user of users) {
119 if (user.username === 'root') {
120 this.notifier.error(this.i18n('You cannot delete root.'))
125 const message = this.i18n('If you remove these users, you will not be able to create others with the same username!')
126 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
127 if (res === false) return
129 this.userService.removeUser(users).subscribe(
131 this.notifier.success(this.i18n('{{num}} users deleted.', { num: users.length }))
135 err => this.notifier.error(err.message)
139 async setEmailsAsVerified (users: User[]) {
140 this.userService.updateUsers(users, { emailVerified: true }).subscribe(
142 this.notifier.success(this.i18n('{{num}} users email set as verified.', { num: users.length }))
146 err => this.notifier.error(err.message)
150 isInSelectionMode () {
151 return this.selectedUsers.length !== 0
154 protected loadData () {
155 this.selectedUsers = []
157 this.userService.getUsers(this.pagination, this.sort, this.search)
160 this.users = resultList.data
161 this.totalRecords = resultList.total
164 err => this.notifier.error(err.message)