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 { ServerConfig, 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 private _selectedColumns: string[]
45 private serverConfig: ServerConfig
48 protected route: ActivatedRoute,
49 protected router: Router,
50 private notifier: Notifier,
51 private confirmService: ConfirmService,
52 private serverService: ServerService,
53 private auth: AuthService,
54 private userService: UserService
60 return this.auth.getUser()
63 get requiresEmailVerification () {
64 return this.serverConfig.signup.requiresEmailVerification
67 get selectedColumns () {
68 return this._selectedColumns
71 set selectedColumns (val: string[]) {
72 this._selectedColumns = val
76 this.serverConfig = this.serverService.getTmpConfig()
77 this.serverService.getConfig()
78 .subscribe(config => this.serverConfig = config)
82 this.bulkUserActions = [
85 label: $localize`Delete`,
86 description: $localize`Videos will be deleted, comments will be tombstoned.`,
87 handler: users => this.removeUsers(users),
88 isDisplayed: users => users.every(u => this.authUser.canManage(u))
91 label: $localize`Ban`,
92 description: $localize`User won't be able to login anymore, but videos and comments will be kept as is.`,
93 handler: users => this.openBanUserModal(users),
94 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === false)
97 label: $localize`Unban`,
98 handler: users => this.unbanUsers(users),
99 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === true)
104 label: $localize`Set Email as Verified`,
105 handler: users => this.setEmailsAsVerified(users),
106 isDisplayed: users => {
107 return this.requiresEmailVerification &&
108 users.every(u => this.authUser.canManage(u) && !u.blocked && u.emailVerified === false)
115 { id: 'username', label: 'Username' },
116 { id: 'email', label: 'Email' },
117 { id: 'quota', label: 'Video quota' },
118 { id: 'role', label: 'Role' },
119 { id: 'createdAt', label: 'Created' }
122 this.selectedColumns = this.columns.map(c => c.id)
124 this.columns.push({ id: 'quotaDaily', label: 'Daily quota' })
125 this.columns.push({ id: 'pluginAuth', label: 'Auth plugin' })
126 this.columns.push({ id: 'lastLoginDate', label: 'Last login' })
130 return 'UserListComponent'
133 getRoleClass (role: UserRole) {
135 case UserRole.ADMINISTRATOR:
136 return 'badge-purple'
137 case UserRole.MODERATOR:
140 return 'badge-yellow'
144 isSelected (id: string) {
145 return this.selectedColumns.find(c => c === id)
148 getColumn (id: string) {
149 return this.columns.find(c => c.id === id)
152 getUserVideoQuotaPercentage (user: UserForList) {
153 return user.rawVideoQuotaUsed * 100 / user.rawVideoQuota
156 getUserVideoQuotaDailyPercentage (user: UserForList) {
157 return user.rawVideoQuotaUsedDaily * 100 / user.rawVideoQuotaDaily
160 openBanUserModal (users: User[]) {
161 for (const user of users) {
162 if (user.username === 'root') {
163 this.notifier.error($localize`You cannot ban root.`)
168 this.userBanModal.openModal(users)
175 async unbanUsers (users: User[]) {
176 const res = await this.confirmService.confirm($localize`Do you really want to unban ${users.length} users?`, $localize`Unban`)
177 if (res === false) return
179 this.userService.unbanUsers(users)
182 this.notifier.success($localize`${users.length} users unbanned.`)
186 err => this.notifier.error(err.message)
190 async removeUsers (users: User[]) {
191 for (const user of users) {
192 if (user.username === 'root') {
193 this.notifier.error($localize`You cannot delete root.`)
198 const message = $localize`If you remove these users, you will not be able to create others with the same username!`
199 const res = await this.confirmService.confirm(message, $localize`Delete`)
200 if (res === false) return
202 this.userService.removeUser(users).subscribe(
204 this.notifier.success($localize`${users.length} users deleted.`)
208 err => this.notifier.error(err.message)
212 async setEmailsAsVerified (users: User[]) {
213 this.userService.updateUsers(users, { emailVerified: true }).subscribe(
215 this.notifier.success($localize`${users.length} users email set as verified.`)
219 err => this.notifier.error(err.message)
223 isInSelectionMode () {
224 return this.selectedUsers.length !== 0
227 protected reloadData () {
228 this.selectedUsers = []
230 this.userService.getUsers({
231 pagination: this.pagination,
236 this.users = resultList.data
237 this.totalRecords = resultList.total
240 err => this.notifier.error(err.message)