1 import { SortMeta } from 'primeng/api'
2 import { Component, OnInit, ViewChild } from '@angular/core'
3 import { ActivatedRoute, Params, Router } from '@angular/router'
4 import { AuthService, ConfirmService, Notifier, RestPagination, RestTable, ServerService, UserService } from '@app/core'
5 import { Actor, DropdownAction } from '@app/shared/shared-main'
6 import { UserBanModalComponent } from '@app/shared/shared-moderation'
7 import { ServerConfig, User, UserRole } from '@shared/models'
9 type UserForList = User & {
11 rawVideoQuotaUsed: number
12 rawVideoQuotaDaily: number
13 rawVideoQuotaUsedDaily: number
17 selector: 'my-user-list',
18 templateUrl: './user-list.component.html',
19 styleUrls: [ './user-list.component.scss' ]
21 export class UserListComponent extends RestTable implements OnInit {
22 @ViewChild('userBanModal', { static: true }) userBanModal: UserBanModalComponent
26 sort: SortMeta = { field: 'createdAt', order: 1 }
27 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
28 highlightBannedUsers = false
30 selectedUsers: User[] = []
31 bulkUserActions: DropdownAction<User[]>[][] = []
32 columns: { id: string, label: string }[]
34 private _selectedColumns: string[]
35 private serverConfig: ServerConfig
38 private notifier: Notifier,
39 private confirmService: ConfirmService,
40 private serverService: ServerService,
41 private userService: UserService,
42 private auth: AuthService,
43 private route: ActivatedRoute,
44 private router: Router
50 return this.auth.getUser()
53 get requiresEmailVerification () {
54 return this.serverConfig.signup.requiresEmailVerification
57 get selectedColumns () {
58 return this._selectedColumns
61 set selectedColumns (val: string[]) {
62 this._selectedColumns = val
66 this.serverConfig = this.serverService.getTmpConfig()
67 this.serverService.getConfig()
68 .subscribe(config => this.serverConfig = config)
72 this.route.queryParams
73 .subscribe(params => {
74 this.search = params.search || ''
76 this.setTableFilter(this.search)
80 this.bulkUserActions = [
83 label: $localize`Delete`,
84 description: $localize`Videos will be deleted, comments will be tombstoned.`,
85 handler: users => this.removeUsers(users),
86 isDisplayed: users => users.every(u => this.authUser.canManage(u))
89 label: $localize`Ban`,
90 description: $localize`User won't be able to login anymore, but videos and comments will be kept as is.`,
91 handler: users => this.openBanUserModal(users),
92 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === false)
95 label: $localize`Unban`,
96 handler: users => this.unbanUsers(users),
97 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === true)
102 label: $localize`Set Email as Verified`,
103 handler: users => this.setEmailsAsVerified(users),
104 isDisplayed: users => {
105 return this.requiresEmailVerification &&
106 users.every(u => this.authUser.canManage(u) && !u.blocked && u.emailVerified === false)
113 { id: 'username', label: 'Username' },
114 { id: 'email', label: 'Email' },
115 { id: 'quota', label: 'Video quota' },
116 { id: 'role', label: 'Role' },
117 { id: 'createdAt', label: 'Created' }
120 this.selectedColumns = this.columns.map(c => c.id)
122 this.columns.push({ id: 'quotaDaily', label: 'Daily quota' })
123 this.columns.push({ id: 'pluginAuth', label: 'Auth plugin' })
124 this.columns.push({ id: 'lastLoginDate', label: 'Last login' })
128 return 'UserListComponent'
131 getRoleClass (role: UserRole) {
133 case UserRole.ADMINISTRATOR:
134 return 'badge-purple'
135 case UserRole.MODERATOR:
138 return 'badge-yellow'
142 isSelected (id: string) {
143 return this.selectedColumns.find(c => c === id)
146 getColumn (id: string) {
147 return this.columns.find(c => c.id === id)
150 getUserVideoQuotaPercentage (user: UserForList) {
151 return user.rawVideoQuotaUsed * 100 / user.rawVideoQuota
154 getUserVideoQuotaDailyPercentage (user: UserForList) {
155 return user.rawVideoQuotaUsedDaily * 100 / user.rawVideoQuotaDaily
158 openBanUserModal (users: User[]) {
159 for (const user of users) {
160 if (user.username === 'root') {
161 this.notifier.error($localize`You cannot ban root.`)
166 this.userBanModal.openModal(users)
173 /* Table filter functions */
174 onUserSearch (event: Event) {
176 this.setQueryParams((event.target as HTMLInputElement).value)
179 setQueryParams (search: string) {
180 const queryParams: Params = {}
181 if (search) Object.assign(queryParams, { search })
183 this.router.navigate([ '/admin/users/list' ], { queryParams })
186 resetTableFilter () {
187 this.setTableFilter('')
188 this.setQueryParams('')
191 /* END Table filter functions */
193 switchToDefaultAvatar ($event: Event) {
194 ($event.target as HTMLImageElement).src = Actor.GET_DEFAULT_AVATAR_URL()
197 async unbanUsers (users: User[]) {
198 const res = await this.confirmService.confirm($localize`Do you really want to unban ${users.length} users?`, $localize`Unban`)
199 if (res === false) return
201 this.userService.unbanUsers(users)
204 this.notifier.success($localize`${users.length} users unbanned.`)
208 err => this.notifier.error(err.message)
212 async removeUsers (users: User[]) {
213 for (const user of users) {
214 if (user.username === 'root') {
215 this.notifier.error($localize`You cannot delete root.`)
220 const message = $localize`If you remove these users, you will not be able to create others with the same username!`
221 const res = await this.confirmService.confirm(message, $localize`Delete`)
222 if (res === false) return
224 this.userService.removeUser(users).subscribe(
226 this.notifier.success($localize`${users.length} users deleted.`)
230 err => this.notifier.error(err.message)
234 async setEmailsAsVerified (users: User[]) {
235 this.userService.updateUsers(users, { emailVerified: true }).subscribe(
237 this.notifier.success($localize`${users.length} users email set as verified.`)
241 err => this.notifier.error(err.message)
245 isInSelectionMode () {
246 return this.selectedUsers.length !== 0
249 protected loadData () {
250 this.selectedUsers = []
252 this.userService.getUsers({
253 pagination: this.pagination,
258 this.users = resultList.data
259 this.totalRecords = resultList.total
262 err => this.notifier.error(err.message)