]>
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' |
2989628b | 8 | import { 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 | { | |
9df52d66 | 39 | queryParams: { search: 'banned:true' }, |
1fd61899 C |
40 | label: $localize`Banned users` |
41 | } | |
42 | ] | |
43 | ||
2989628b C |
44 | requiresEmailVerification = false |
45 | ||
52c4976f | 46 | private _selectedColumns: string[] |
ba430d75 | 47 | |
df98563e | 48 | constructor ( |
5ed46c1b C |
49 | protected route: ActivatedRoute, |
50 | protected router: Router, | |
f8b2c1b4 | 51 | private notifier: Notifier, |
5769e1db | 52 | private confirmService: ConfirmService, |
fc2ec87a | 53 | private serverService: ServerService, |
a95a4cc8 | 54 | private auth: AuthService, |
5ed46c1b C |
55 | private userService: UserService |
56 | ) { | |
d592e0a9 | 57 | super() |
7da18e44 C |
58 | } |
59 | ||
a95a4cc8 C |
60 | get authUser () { |
61 | return this.auth.getUser() | |
62 | } | |
63 | ||
bc99dfe5 RK |
64 | get selectedColumns () { |
65 | return this._selectedColumns | |
66 | } | |
67 | ||
52c4976f | 68 | set selectedColumns (val: string[]) { |
bc99dfe5 RK |
69 | this._selectedColumns = val |
70 | } | |
71 | ||
ab998f7b | 72 | ngOnInit () { |
ba430d75 | 73 | this.serverService.getConfig() |
2989628b | 74 | .subscribe(config => this.requiresEmailVerification = config.signup.requiresEmailVerification) |
ba430d75 | 75 | |
24b9417c | 76 | this.initialize() |
8491293b | 77 | |
791645e6 | 78 | this.bulkUserActions = [ |
9b82d49d RK |
79 | [ |
80 | { | |
66357162 C |
81 | label: $localize`Delete`, |
82 | description: $localize`Videos will be deleted, comments will be tombstoned.`, | |
9b82d49d RK |
83 | handler: users => this.removeUsers(users), |
84 | isDisplayed: users => users.every(u => this.authUser.canManage(u)) | |
85 | }, | |
86 | { | |
66357162 C |
87 | label: $localize`Ban`, |
88 | description: $localize`User won't be able to login anymore, but videos and comments will be kept as is.`, | |
9b82d49d RK |
89 | handler: users => this.openBanUserModal(users), |
90 | isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === false) | |
91 | }, | |
92 | { | |
66357162 | 93 | label: $localize`Unban`, |
9b82d49d RK |
94 | handler: users => this.unbanUsers(users), |
95 | isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === true) | |
a95a4cc8 | 96 | } |
9b82d49d RK |
97 | ], |
98 | [ | |
99 | { | |
66357162 | 100 | label: $localize`Set Email as Verified`, |
9b82d49d RK |
101 | handler: users => this.setEmailsAsVerified(users), |
102 | isDisplayed: users => { | |
103 | return this.requiresEmailVerification && | |
104 | users.every(u => this.authUser.canManage(u) && !u.blocked && u.emailVerified === false) | |
105 | } | |
106 | } | |
107 | ] | |
791645e6 | 108 | ] |
bc99dfe5 RK |
109 | |
110 | this.columns = [ | |
c3185413 C |
111 | { id: 'username', label: $localize`Username` }, |
112 | { id: 'email', label: $localize`Email` }, | |
113 | { id: 'quota', label: $localize`Video quota` }, | |
114 | { id: 'role', label: $localize`Role` }, | |
115 | { id: 'createdAt', label: $localize`Created` } | |
bc99dfe5 | 116 | ] |
52c4976f C |
117 | |
118 | this.selectedColumns = this.columns.map(c => c.id) | |
119 | ||
c3185413 C |
120 | this.columns.push({ id: 'quotaDaily', label: $localize`Daily quota` }) |
121 | this.columns.push({ id: 'pluginAuth', label: $localize`Auth plugin` }) | |
122 | this.columns.push({ id: 'lastLoginDate', label: $localize`Last login` }) | |
141b177d C |
123 | } |
124 | ||
8e11a1b3 C |
125 | getIdentifier () { |
126 | return 'UserListComponent' | |
127 | } | |
128 | ||
bc99dfe5 RK |
129 | getRoleClass (role: UserRole) { |
130 | switch (role) { | |
131 | case UserRole.ADMINISTRATOR: | |
132 | return 'badge-purple' | |
133 | case UserRole.MODERATOR: | |
134 | return 'badge-blue' | |
135 | default: | |
136 | return 'badge-yellow' | |
137 | } | |
138 | } | |
139 | ||
52c4976f C |
140 | isSelected (id: string) { |
141 | return this.selectedColumns.find(c => c === id) | |
142 | } | |
143 | ||
144 | getColumn (id: string) { | |
145 | return this.columns.find(c => c.id === id) | |
bc99dfe5 RK |
146 | } |
147 | ||
4f5d0459 | 148 | getUserVideoQuotaPercentage (user: UserForList) { |
bc99dfe5 RK |
149 | return user.rawVideoQuotaUsed * 100 / user.rawVideoQuota |
150 | } | |
151 | ||
4f5d0459 | 152 | getUserVideoQuotaDailyPercentage (user: UserForList) { |
bc99dfe5 RK |
153 | return user.rawVideoQuotaUsedDaily * 100 / user.rawVideoQuotaDaily |
154 | } | |
155 | ||
791645e6 C |
156 | openBanUserModal (users: User[]) { |
157 | for (const user of users) { | |
158 | if (user.username === 'root') { | |
66357162 | 159 | this.notifier.error($localize`You cannot ban root.`) |
791645e6 C |
160 | return |
161 | } | |
162 | } | |
163 | ||
164 | this.userBanModal.openModal(users) | |
165 | } | |
166 | ||
2fbe7f19 | 167 | onUserChanged () { |
2e46eb97 | 168 | this.reloadData() |
791645e6 C |
169 | } |
170 | ||
171 | async unbanUsers (users: User[]) { | |
66357162 | 172 | const res = await this.confirmService.confirm($localize`Do you really want to unban ${users.length} users?`, $localize`Unban`) |
791645e6 C |
173 | if (res === false) return |
174 | ||
175 | this.userService.unbanUsers(users) | |
1378c0d3 C |
176 | .subscribe({ |
177 | next: () => { | |
66357162 | 178 | this.notifier.success($localize`${users.length} users unbanned.`) |
2e46eb97 | 179 | this.reloadData() |
791645e6 C |
180 | }, |
181 | ||
1378c0d3 C |
182 | error: err => this.notifier.error(err.message) |
183 | }) | |
791645e6 C |
184 | } |
185 | ||
186 | async removeUsers (users: User[]) { | |
187 | for (const user of users) { | |
188 | if (user.username === 'root') { | |
66357162 | 189 | this.notifier.error($localize`You cannot delete root.`) |
791645e6 C |
190 | return |
191 | } | |
192 | } | |
193 | ||
66357162 C |
194 | const message = $localize`If you remove these users, you will not be able to create others with the same username!` |
195 | const res = await this.confirmService.confirm(message, $localize`Delete`) | |
791645e6 C |
196 | if (res === false) return |
197 | ||
1378c0d3 C |
198 | this.userService.removeUser(users) |
199 | .subscribe({ | |
200 | next: () => { | |
201 | this.notifier.success($localize`${users.length} users deleted.`) | |
202 | this.reloadData() | |
203 | }, | |
791645e6 | 204 | |
1378c0d3 C |
205 | error: err => this.notifier.error(err.message) |
206 | }) | |
791645e6 C |
207 | } |
208 | ||
fc2ec87a | 209 | async setEmailsAsVerified (users: User[]) { |
1378c0d3 C |
210 | this.userService.updateUsers(users, { emailVerified: true }) |
211 | .subscribe({ | |
212 | next: () => { | |
213 | this.notifier.success($localize`${users.length} users email set as verified.`) | |
214 | this.reloadData() | |
215 | }, | |
fc2ec87a | 216 | |
1378c0d3 C |
217 | error: err => this.notifier.error(err.message) |
218 | }) | |
fc2ec87a JM |
219 | } |
220 | ||
791645e6 C |
221 | isInSelectionMode () { |
222 | return this.selectedUsers.length !== 0 | |
223 | } | |
dffd5d12 | 224 | |
2e46eb97 | 225 | protected reloadData () { |
dffd5d12 B |
226 | this.selectedUsers = [] |
227 | ||
8491293b RK |
228 | this.userService.getUsers({ |
229 | pagination: this.pagination, | |
230 | sort: this.sort, | |
231 | search: this.search | |
1378c0d3 C |
232 | }).subscribe({ |
233 | next: resultList => { | |
8491293b RK |
234 | this.users = resultList.data |
235 | this.totalRecords = resultList.total | |
236 | }, | |
f8b2c1b4 | 237 | |
1378c0d3 C |
238 | error: err => this.notifier.error(err.message) |
239 | }) | |
dffd5d12 | 240 | } |
7da18e44 | 241 | } |