]>
Commit | Line | Data |
---|---|---|
f77eb73b | 1 | import { SortMeta } from 'primeng/api' |
2e46eb97 | 2 | import { Component, OnInit, ViewChild } from '@angular/core' |
1fd61899 | 3 | import { ActivatedRoute, Router } from '@angular/router' |
67ed6552 | 4 | import { AuthService, ConfirmService, Notifier, RestPagination, RestTable, ServerService, UserService } from '@app/core' |
1fd61899 C |
5 | import { AdvancedInputFilter } from '@app/shared/shared-forms' |
6 | import { DropdownAction } from '@app/shared/shared-main' | |
67ed6552 | 7 | import { UserBanModalComponent } from '@app/shared/shared-moderation' |
bc99dfe5 | 8 | import { ServerConfig, User, UserRole } from '@shared/models' |
7da18e44 | 9 | |
4f5d0459 RK |
10 | type UserForList = User & { |
11 | rawVideoQuota: number | |
12 | rawVideoQuotaUsed: number | |
13 | rawVideoQuotaDaily: number | |
14 | rawVideoQuotaUsedDaily: number | |
15 | } | |
16 | ||
7da18e44 C |
17 | @Component({ |
18 | selector: 'my-user-list', | |
ec8d8440 C |
19 | templateUrl: './user-list.component.html', |
20 | styleUrls: [ './user-list.component.scss' ] | |
7da18e44 | 21 | }) |
2e46eb97 | 22 | export class UserListComponent extends RestTable implements OnInit { |
f36da21e | 23 | @ViewChild('userBanModal', { static: true }) userBanModal: UserBanModalComponent |
791645e6 | 24 | |
d592e0a9 | 25 | users: User[] = [] |
1fd61899 | 26 | |
d592e0a9 | 27 | totalRecords = 0 |
ab998f7b | 28 | sort: SortMeta = { field: 'createdAt', order: 1 } |
d592e0a9 | 29 | pagination: RestPagination = { count: this.rowsPerPage, start: 0 } |
1fd61899 | 30 | |
bc99dfe5 | 31 | highlightBannedUsers = false |
141b177d | 32 | |
791645e6 | 33 | selectedUsers: User[] = [] |
9b82d49d | 34 | bulkUserActions: DropdownAction<User[]>[][] = [] |
52c4976f | 35 | columns: { id: string, label: string }[] |
791645e6 | 36 | |
1fd61899 C |
37 | inputFilters: AdvancedInputFilter[] = [ |
38 | { | |
39 | queryParams: { 'search': 'banned:true' }, | |
40 | label: $localize`Banned users` | |
41 | } | |
42 | ] | |
43 | ||
52c4976f | 44 | private _selectedColumns: string[] |
ba430d75 C |
45 | private serverConfig: ServerConfig |
46 | ||
df98563e | 47 | constructor ( |
5ed46c1b C |
48 | protected route: ActivatedRoute, |
49 | protected router: Router, | |
f8b2c1b4 | 50 | private notifier: Notifier, |
5769e1db | 51 | private confirmService: ConfirmService, |
fc2ec87a | 52 | private serverService: ServerService, |
a95a4cc8 | 53 | private auth: AuthService, |
5ed46c1b C |
54 | private userService: UserService |
55 | ) { | |
d592e0a9 | 56 | super() |
7da18e44 C |
57 | } |
58 | ||
a95a4cc8 C |
59 | get authUser () { |
60 | return this.auth.getUser() | |
61 | } | |
62 | ||
fc2ec87a | 63 | get requiresEmailVerification () { |
ba430d75 | 64 | return this.serverConfig.signup.requiresEmailVerification |
fc2ec87a JM |
65 | } |
66 | ||
bc99dfe5 RK |
67 | get selectedColumns () { |
68 | return this._selectedColumns | |
69 | } | |
70 | ||
52c4976f | 71 | set selectedColumns (val: string[]) { |
bc99dfe5 RK |
72 | this._selectedColumns = val |
73 | } | |
74 | ||
ab998f7b | 75 | ngOnInit () { |
ba430d75 C |
76 | this.serverConfig = this.serverService.getTmpConfig() |
77 | this.serverService.getConfig() | |
78 | .subscribe(config => this.serverConfig = config) | |
79 | ||
24b9417c | 80 | this.initialize() |
8491293b | 81 | |
791645e6 | 82 | this.bulkUserActions = [ |
9b82d49d RK |
83 | [ |
84 | { | |
66357162 C |
85 | label: $localize`Delete`, |
86 | description: $localize`Videos will be deleted, comments will be tombstoned.`, | |
9b82d49d RK |
87 | handler: users => this.removeUsers(users), |
88 | isDisplayed: users => users.every(u => this.authUser.canManage(u)) | |
89 | }, | |
90 | { | |
66357162 C |
91 | label: $localize`Ban`, |
92 | description: $localize`User won't be able to login anymore, but videos and comments will be kept as is.`, | |
9b82d49d RK |
93 | handler: users => this.openBanUserModal(users), |
94 | isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === false) | |
95 | }, | |
96 | { | |
66357162 | 97 | label: $localize`Unban`, |
9b82d49d RK |
98 | handler: users => this.unbanUsers(users), |
99 | isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === true) | |
a95a4cc8 | 100 | } |
9b82d49d RK |
101 | ], |
102 | [ | |
103 | { | |
66357162 | 104 | label: $localize`Set Email as Verified`, |
9b82d49d RK |
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) | |
109 | } | |
110 | } | |
111 | ] | |
791645e6 | 112 | ] |
bc99dfe5 RK |
113 | |
114 | this.columns = [ | |
52c4976f C |
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' } | |
bc99dfe5 | 120 | ] |
52c4976f C |
121 | |
122 | this.selectedColumns = this.columns.map(c => c.id) | |
123 | ||
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' }) | |
141b177d C |
127 | } |
128 | ||
8e11a1b3 C |
129 | getIdentifier () { |
130 | return 'UserListComponent' | |
131 | } | |
132 | ||
bc99dfe5 RK |
133 | getRoleClass (role: UserRole) { |
134 | switch (role) { | |
135 | case UserRole.ADMINISTRATOR: | |
136 | return 'badge-purple' | |
137 | case UserRole.MODERATOR: | |
138 | return 'badge-blue' | |
139 | default: | |
140 | return 'badge-yellow' | |
141 | } | |
142 | } | |
143 | ||
52c4976f C |
144 | isSelected (id: string) { |
145 | return this.selectedColumns.find(c => c === id) | |
146 | } | |
147 | ||
148 | getColumn (id: string) { | |
149 | return this.columns.find(c => c.id === id) | |
bc99dfe5 RK |
150 | } |
151 | ||
4f5d0459 | 152 | getUserVideoQuotaPercentage (user: UserForList) { |
bc99dfe5 RK |
153 | return user.rawVideoQuotaUsed * 100 / user.rawVideoQuota |
154 | } | |
155 | ||
4f5d0459 | 156 | getUserVideoQuotaDailyPercentage (user: UserForList) { |
bc99dfe5 RK |
157 | return user.rawVideoQuotaUsedDaily * 100 / user.rawVideoQuotaDaily |
158 | } | |
159 | ||
791645e6 C |
160 | openBanUserModal (users: User[]) { |
161 | for (const user of users) { | |
162 | if (user.username === 'root') { | |
66357162 | 163 | this.notifier.error($localize`You cannot ban root.`) |
791645e6 C |
164 | return |
165 | } | |
166 | } | |
167 | ||
168 | this.userBanModal.openModal(users) | |
169 | } | |
170 | ||
2fbe7f19 | 171 | onUserChanged () { |
2e46eb97 | 172 | this.reloadData() |
791645e6 C |
173 | } |
174 | ||
175 | async unbanUsers (users: User[]) { | |
66357162 | 176 | const res = await this.confirmService.confirm($localize`Do you really want to unban ${users.length} users?`, $localize`Unban`) |
791645e6 C |
177 | if (res === false) return |
178 | ||
179 | this.userService.unbanUsers(users) | |
180 | .subscribe( | |
181 | () => { | |
66357162 | 182 | this.notifier.success($localize`${users.length} users unbanned.`) |
2e46eb97 | 183 | this.reloadData() |
791645e6 C |
184 | }, |
185 | ||
f8b2c1b4 | 186 | err => this.notifier.error(err.message) |
791645e6 C |
187 | ) |
188 | } | |
189 | ||
190 | async removeUsers (users: User[]) { | |
191 | for (const user of users) { | |
192 | if (user.username === 'root') { | |
66357162 | 193 | this.notifier.error($localize`You cannot delete root.`) |
791645e6 C |
194 | return |
195 | } | |
196 | } | |
197 | ||
66357162 C |
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`) | |
791645e6 C |
200 | if (res === false) return |
201 | ||
202 | this.userService.removeUser(users).subscribe( | |
203 | () => { | |
66357162 | 204 | this.notifier.success($localize`${users.length} users deleted.`) |
2e46eb97 | 205 | this.reloadData() |
791645e6 C |
206 | }, |
207 | ||
f8b2c1b4 | 208 | err => this.notifier.error(err.message) |
791645e6 C |
209 | ) |
210 | } | |
211 | ||
fc2ec87a JM |
212 | async setEmailsAsVerified (users: User[]) { |
213 | this.userService.updateUsers(users, { emailVerified: true }).subscribe( | |
214 | () => { | |
66357162 | 215 | this.notifier.success($localize`${users.length} users email set as verified.`) |
2e46eb97 | 216 | this.reloadData() |
fc2ec87a JM |
217 | }, |
218 | ||
f8b2c1b4 | 219 | err => this.notifier.error(err.message) |
fc2ec87a JM |
220 | ) |
221 | } | |
222 | ||
791645e6 C |
223 | isInSelectionMode () { |
224 | return this.selectedUsers.length !== 0 | |
225 | } | |
dffd5d12 | 226 | |
2e46eb97 | 227 | protected reloadData () { |
dffd5d12 B |
228 | this.selectedUsers = [] |
229 | ||
8491293b RK |
230 | this.userService.getUsers({ |
231 | pagination: this.pagination, | |
232 | sort: this.sort, | |
233 | search: this.search | |
234 | }).subscribe( | |
235 | resultList => { | |
236 | this.users = resultList.data | |
237 | this.totalRecords = resultList.total | |
238 | }, | |
f8b2c1b4 | 239 | |
8491293b RK |
240 | err => this.notifier.error(err.message) |
241 | ) | |
dffd5d12 | 242 | } |
7da18e44 | 243 | } |