]>
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 | { | |
39 | queryParams: { 'search': 'banned:true' }, | |
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 = [ | |
52c4976f C |
111 | { id: 'username', label: 'Username' }, |
112 | { id: 'email', label: 'Email' }, | |
113 | { id: 'quota', label: 'Video quota' }, | |
114 | { id: 'role', label: 'Role' }, | |
115 | { id: 'createdAt', label: 'Created' } | |
bc99dfe5 | 116 | ] |
52c4976f C |
117 | |
118 | this.selectedColumns = this.columns.map(c => c.id) | |
119 | ||
120 | this.columns.push({ id: 'quotaDaily', label: 'Daily quota' }) | |
121 | this.columns.push({ id: 'pluginAuth', label: 'Auth plugin' }) | |
122 | this.columns.push({ id: 'lastLoginDate', label: '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) | |
176 | .subscribe( | |
177 | () => { | |
66357162 | 178 | this.notifier.success($localize`${users.length} users unbanned.`) |
2e46eb97 | 179 | this.reloadData() |
791645e6 C |
180 | }, |
181 | ||
f8b2c1b4 | 182 | err => this.notifier.error(err.message) |
791645e6 C |
183 | ) |
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 | ||
198 | this.userService.removeUser(users).subscribe( | |
199 | () => { | |
66357162 | 200 | this.notifier.success($localize`${users.length} users deleted.`) |
2e46eb97 | 201 | this.reloadData() |
791645e6 C |
202 | }, |
203 | ||
f8b2c1b4 | 204 | err => this.notifier.error(err.message) |
791645e6 C |
205 | ) |
206 | } | |
207 | ||
fc2ec87a JM |
208 | async setEmailsAsVerified (users: User[]) { |
209 | this.userService.updateUsers(users, { emailVerified: true }).subscribe( | |
210 | () => { | |
66357162 | 211 | this.notifier.success($localize`${users.length} users email set as verified.`) |
2e46eb97 | 212 | this.reloadData() |
fc2ec87a JM |
213 | }, |
214 | ||
f8b2c1b4 | 215 | err => this.notifier.error(err.message) |
fc2ec87a JM |
216 | ) |
217 | } | |
218 | ||
791645e6 C |
219 | isInSelectionMode () { |
220 | return this.selectedUsers.length !== 0 | |
221 | } | |
dffd5d12 | 222 | |
2e46eb97 | 223 | protected reloadData () { |
dffd5d12 B |
224 | this.selectedUsers = [] |
225 | ||
8491293b RK |
226 | this.userService.getUsers({ |
227 | pagination: this.pagination, | |
228 | sort: this.sort, | |
229 | search: this.search | |
230 | }).subscribe( | |
231 | resultList => { | |
232 | this.users = resultList.data | |
233 | this.totalRecords = resultList.total | |
234 | }, | |
f8b2c1b4 | 235 | |
8491293b RK |
236 | err => this.notifier.error(err.message) |
237 | ) | |
dffd5d12 | 238 | } |
7da18e44 | 239 | } |