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