]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+admin/users/user-list/user-list.component.ts
Serve audit logs to client
[github/Chocobozzz/PeerTube.git] / client / src / app / +admin / users / user-list / user-list.component.ts
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 { User } from '../../../../../../shared'
8 import { UserBanModalComponent } from '@app/shared/moderation'
9 import { DropdownAction } from '@app/shared/buttons/action-dropdown.component'
10
11 @Component({
12 selector: 'my-user-list',
13 templateUrl: './user-list.component.html',
14 styleUrls: [ './user-list.component.scss' ]
15 })
16 export class UserListComponent extends RestTable implements OnInit {
17 @ViewChild('userBanModal', { static: true }) userBanModal: UserBanModalComponent
18
19 users: User[] = []
20 totalRecords = 0
21 rowsPerPage = 10
22 sort: SortMeta = { field: 'createdAt', order: 1 }
23 pagination: RestPagination = { count: this.rowsPerPage, start: 0 }
24
25 selectedUsers: User[] = []
26 bulkUserActions: DropdownAction<User[]>[] = []
27
28 constructor (
29 private notifier: Notifier,
30 private confirmService: ConfirmService,
31 private serverService: ServerService,
32 private userService: UserService,
33 private auth: AuthService,
34 private i18n: I18n
35 ) {
36 super()
37 }
38
39 get authUser () {
40 return this.auth.getUser()
41 }
42
43 get requiresEmailVerification () {
44 return this.serverService.getConfig().signup.requiresEmailVerification
45 }
46
47 ngOnInit () {
48 this.initialize()
49
50 this.bulkUserActions = [
51 {
52 label: this.i18n('Delete'),
53 handler: users => this.removeUsers(users),
54 isDisplayed: users => users.every(u => this.authUser.canManage(u))
55 },
56 {
57 label: this.i18n('Ban'),
58 handler: users => this.openBanUserModal(users),
59 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === false)
60 },
61 {
62 label: this.i18n('Unban'),
63 handler: users => this.unbanUsers(users),
64 isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === true)
65 },
66 {
67 label: this.i18n('Set Email as Verified'),
68 handler: users => this.setEmailsAsVerified(users),
69 isDisplayed: users => {
70 return this.requiresEmailVerification &&
71 users.every(u => this.authUser.canManage(u) && !u.blocked && u.emailVerified === false)
72 }
73 }
74 ]
75 }
76
77 openBanUserModal (users: User[]) {
78 for (const user of users) {
79 if (user.username === 'root') {
80 this.notifier.error(this.i18n('You cannot ban root.'))
81 return
82 }
83 }
84
85 this.userBanModal.openModal(users)
86 }
87
88 onUserChanged () {
89 this.loadData()
90 }
91
92 async unbanUsers (users: User[]) {
93 const message = this.i18n('Do you really want to unban {{num}} users?', { num: users.length })
94
95 const res = await this.confirmService.confirm(message, this.i18n('Unban'))
96 if (res === false) return
97
98 this.userService.unbanUsers(users)
99 .subscribe(
100 () => {
101 const message = this.i18n('{{num}} users unbanned.', { num: users.length })
102
103 this.notifier.success(message)
104 this.loadData()
105 },
106
107 err => this.notifier.error(err.message)
108 )
109 }
110
111 async removeUsers (users: User[]) {
112 for (const user of users) {
113 if (user.username === 'root') {
114 this.notifier.error(this.i18n('You cannot delete root.'))
115 return
116 }
117 }
118
119 const message = this.i18n('If you remove these users, you will not be able to create others with the same username!')
120 const res = await this.confirmService.confirm(message, this.i18n('Delete'))
121 if (res === false) return
122
123 this.userService.removeUser(users).subscribe(
124 () => {
125 this.notifier.success(this.i18n('{{num}} users deleted.', { num: users.length }))
126 this.loadData()
127 },
128
129 err => this.notifier.error(err.message)
130 )
131 }
132
133 async setEmailsAsVerified (users: User[]) {
134 this.userService.updateUsers(users, { emailVerified: true }).subscribe(
135 () => {
136 this.notifier.success(this.i18n('{{num}} users email set as verified.', { num: users.length }))
137 this.loadData()
138 },
139
140 err => this.notifier.error(err.message)
141 )
142 }
143
144 isInSelectionMode () {
145 return this.selectedUsers.length !== 0
146 }
147
148 protected loadData () {
149 this.selectedUsers = []
150
151 this.userService.getUsers(this.pagination, this.sort, this.search)
152 .subscribe(
153 resultList => {
154 this.users = resultList.data
155 this.totalRecords = resultList.total
156 },
157
158 err => this.notifier.error(err.message)
159 )
160 }
161 }