1 import { SortMeta } from 'primeng/api'
2 import { Component, OnInit, ViewChild } from '@angular/core'
3 import { AuthService, ConfirmService, Notifier, RestPagination, RestTable, ServerService, UserService } from '@app/core'
4 import { Actor, DropdownAction } from '@app/shared/shared-main'
5 import { UserBanModalComponent } from '@app/shared/shared-moderation'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7 import { ServerConfig, User, UserRole } from '@shared/models'
8 import { Params, Router, ActivatedRoute } from '@angular/router'
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
27 sort: SortMeta = { field: 'createdAt', order: 1 }
28 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
29 highlightBannedUsers = false
31 selectedUsers: User[] = []
32 bulkUserActions: DropdownAction<User[]>[][] = []
33 columns: { id: string, label: string }[]
35 private _selectedColumns: string[]
36 private serverConfig: ServerConfig
39 private notifier: Notifier,
40 private confirmService: ConfirmService,
41 private serverService: ServerService,
42 private userService: UserService,
43 private auth: AuthService,
44 private route: ActivatedRoute,
45 private router: Router,
52 return this.auth.getUser()
55 get requiresEmailVerification () {
56 return this.serverConfig.signup.requiresEmailVerification
59 get selectedColumns () {
60 return this._selectedColumns
63 set selectedColumns (val: string[]) {
64 this._selectedColumns = val
68 this.serverConfig = this.serverService.getTmpConfig()
69 this.serverService.getConfig()
70 .subscribe(config => this.serverConfig = config)
74 this.route.queryParams
75 .subscribe(params => {
76 this.search = params.search || ''
78 this.setTableFilter(this.search)
82 this.bulkUserActions = [
85 label: this.i18n('Delete'),
86 description: this.i18n('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: this.i18n('Ban'),
92 description: this.i18n('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: this.i18n('Unban'),
98 handler: users => this.unbanUsers(users),
99 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === true)
104 label: this.i18n('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(this.i18n('You cannot ban root.'))
168 this.userBanModal.openModal(users)
175 /* Table filter functions */
176 onUserSearch (event: Event) {
178 this.setQueryParams((event.target as HTMLInputElement).value)
181 setQueryParams (search: string) {
182 const queryParams: Params = {}
183 if (search) Object.assign(queryParams, { search })
185 this.router.navigate([ '/admin/users/list' ], { queryParams })
188 resetTableFilter () {
189 this.setTableFilter('')
190 this.setQueryParams('')
193 /* END Table filter functions */
195 switchToDefaultAvatar ($event: Event) {
196 ($event.target as HTMLImageElement).src = Actor.GET_DEFAULT_AVATAR_URL()
199 async unbanUsers (users: User[]) {
200 const message = this.i18n('Do you really want to unban {{num}} users?', { num: users.length })
202 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
203 if (res === false) return
205 this.userService.unbanUsers(users)
208 const message = this.i18n('{{num}} users unbanned.', { num: users.length })
210 this.notifier.success(message)
214 err => this.notifier.error(err.message)
218 async removeUsers (users: User[]) {
219 for (const user of users) {
220 if (user.username === 'root') {
221 this.notifier.error(this.i18n('You cannot delete root.'))
226 const message = this.i18n('If you remove these users, you will not be able to create others with the same username!')
227 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
228 if (res === false) return
230 this.userService.removeUser(users).subscribe(
232 this.notifier.success(this.i18n('{{num}} users deleted.', { num: users.length }))
236 err => this.notifier.error(err.message)
240 async setEmailsAsVerified (users: User[]) {
241 this.userService.updateUsers(users, { emailVerified: true }).subscribe(
243 this.notifier.success(this.i18n('{{num}} users email set as verified.', { num: users.length }))
247 err => this.notifier.error(err.message)
251 isInSelectionMode () {
252 return this.selectedUsers.length !== 0
255 protected loadData () {
256 this.selectedUsers = []
258 this.userService.getUsers({
259 pagination: this.pagination,
264 this.users = resultList.data
265 this.totalRecords = resultList.total
268 err => this.notifier.error(err.message)