1 import { SortMeta } from 'primeng/api'
2 import { Component, OnInit, ViewChild } from '@angular/core'
3 import { ActivatedRoute, Router } from '@angular/router'
4 import { AuthService, ConfirmService, Notifier, RestPagination, RestTable, ServerService, UserService } from '@app/core'
5 import { AdvancedInputFilter } from '@app/shared/shared-forms'
6 import { DropdownAction } from '@app/shared/shared-main'
7 import { UserBanModalComponent } from '@app/shared/shared-moderation'
8 import { User, UserRole } from '@shared/models'
10 type UserForList = User & {
12 rawVideoQuotaUsed: number
13 rawVideoQuotaDaily: number
14 rawVideoQuotaUsedDaily: number
18 selector: 'my-user-list',
19 templateUrl: './user-list.component.html',
20 styleUrls: [ './user-list.component.scss' ]
22 export class UserListComponent extends RestTable implements OnInit {
23 @ViewChild('userBanModal', { static: true }) userBanModal: UserBanModalComponent
28 sort: SortMeta = { field: 'createdAt', order: 1 }
29 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
31 highlightBannedUsers = false
33 selectedUsers: User[] = []
34 bulkUserActions: DropdownAction<User[]>[][] = []
35 columns: { id: string, label: string }[]
37 inputFilters: AdvancedInputFilter[] = [
39 queryParams: { 'search': 'banned:true' },
40 label: $localize`Banned users`
44 requiresEmailVerification = false
46 private _selectedColumns: string[]
49 protected route: ActivatedRoute,
50 protected router: Router,
51 private notifier: Notifier,
52 private confirmService: ConfirmService,
53 private serverService: ServerService,
54 private auth: AuthService,
55 private userService: UserService
61 return this.auth.getUser()
64 get selectedColumns () {
65 return this._selectedColumns
68 set selectedColumns (val: string[]) {
69 this._selectedColumns = val
73 this.serverService.getConfig()
74 .subscribe(config => this.requiresEmailVerification = config.signup.requiresEmailVerification)
78 this.bulkUserActions = [
81 label: $localize`Delete`,
82 description: $localize`Videos will be deleted, comments will be tombstoned.`,
83 handler: users => this.removeUsers(users),
84 isDisplayed: users => users.every(u => this.authUser.canManage(u))
87 label: $localize`Ban`,
88 description: $localize`User won't be able to login anymore, but videos and comments will be kept as is.`,
89 handler: users => this.openBanUserModal(users),
90 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === false)
93 label: $localize`Unban`,
94 handler: users => this.unbanUsers(users),
95 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === true)
100 label: $localize`Set Email as Verified`,
101 handler: users => this.setEmailsAsVerified(users),
102 isDisplayed: users => {
103 return this.requiresEmailVerification &&
104 users.every(u => this.authUser.canManage(u) && !u.blocked && u.emailVerified === false)
111 { id: 'username', label: 'Username' },
112 { id: 'email', label: 'Email' },
113 { id: 'quota', label: 'Video quota' },
114 { id: 'role', label: 'Role' },
115 { id: 'createdAt', label: 'Created' }
118 this.selectedColumns = this.columns.map(c => c.id)
120 this.columns.push({ id: 'quotaDaily', label: 'Daily quota' })
121 this.columns.push({ id: 'pluginAuth', label: 'Auth plugin' })
122 this.columns.push({ id: 'lastLoginDate', label: 'Last login' })
126 return 'UserListComponent'
129 getRoleClass (role: UserRole) {
131 case UserRole.ADMINISTRATOR:
132 return 'badge-purple'
133 case UserRole.MODERATOR:
136 return 'badge-yellow'
140 isSelected (id: string) {
141 return this.selectedColumns.find(c => c === id)
144 getColumn (id: string) {
145 return this.columns.find(c => c.id === id)
148 getUserVideoQuotaPercentage (user: UserForList) {
149 return user.rawVideoQuotaUsed * 100 / user.rawVideoQuota
152 getUserVideoQuotaDailyPercentage (user: UserForList) {
153 return user.rawVideoQuotaUsedDaily * 100 / user.rawVideoQuotaDaily
156 openBanUserModal (users: User[]) {
157 for (const user of users) {
158 if (user.username === 'root') {
159 this.notifier.error($localize`You cannot ban root.`)
164 this.userBanModal.openModal(users)
171 async unbanUsers (users: User[]) {
172 const res = await this.confirmService.confirm($localize`Do you really want to unban ${users.length} users?`, $localize`Unban`)
173 if (res === false) return
175 this.userService.unbanUsers(users)
178 this.notifier.success($localize`${users.length} users unbanned.`)
182 err => this.notifier.error(err.message)
186 async removeUsers (users: User[]) {
187 for (const user of users) {
188 if (user.username === 'root') {
189 this.notifier.error($localize`You cannot delete root.`)
194 const message = $localize`If you remove these users, you will not be able to create others with the same username!`
195 const res = await this.confirmService.confirm(message, $localize`Delete`)
196 if (res === false) return
198 this.userService.removeUser(users).subscribe(
200 this.notifier.success($localize`${users.length} users deleted.`)
204 err => this.notifier.error(err.message)
208 async setEmailsAsVerified (users: User[]) {
209 this.userService.updateUsers(users, { emailVerified: true }).subscribe(
211 this.notifier.success($localize`${users.length} users email set as verified.`)
215 err => this.notifier.error(err.message)
219 isInSelectionMode () {
220 return this.selectedUsers.length !== 0
223 protected reloadData () {
224 this.selectedUsers = []
226 this.userService.getUsers({
227 pagination: this.pagination,
232 this.users = resultList.data
233 this.totalRecords = resultList.total
236 err => this.notifier.error(err.message)