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