]>
Commit | Line | Data |
---|---|---|
f77eb73b | 1 | import { SortMeta } from 'primeng/api' |
67ed6552 C |
2 | import { Component, OnInit, ViewChild } from '@angular/core' |
3 | import { AuthService, ConfirmService, Notifier, RestPagination, RestTable, ServerService, UserService } from '@app/core' | |
4 | import { Actor, DropdownAction } from '@app/shared/shared-main' | |
5 | import { UserBanModalComponent } from '@app/shared/shared-moderation' | |
b1d40cff | 6 | import { I18n } from '@ngx-translate/i18n-polyfill' |
bc99dfe5 | 7 | import { ServerConfig, User, UserRole } from '@shared/models' |
8491293b | 8 | import { Params, Router, ActivatedRoute } from '@angular/router' |
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 | }) |
ab998f7b | 22 | export class UserListComponent extends RestTable implements OnInit { |
f36da21e | 23 | @ViewChild('userBanModal', { static: true }) userBanModal: UserBanModalComponent |
791645e6 | 24 | |
d592e0a9 C |
25 | users: User[] = [] |
26 | totalRecords = 0 | |
ab998f7b | 27 | sort: SortMeta = { field: 'createdAt', order: 1 } |
d592e0a9 | 28 | pagination: RestPagination = { count: this.rowsPerPage, start: 0 } |
bc99dfe5 | 29 | highlightBannedUsers = false |
141b177d | 30 | |
791645e6 | 31 | selectedUsers: User[] = [] |
9b82d49d | 32 | bulkUserActions: DropdownAction<User[]>[][] = [] |
52c4976f | 33 | columns: { id: string, label: string }[] |
791645e6 | 34 | |
52c4976f | 35 | private _selectedColumns: string[] |
ba430d75 C |
36 | private serverConfig: ServerConfig |
37 | ||
df98563e | 38 | constructor ( |
f8b2c1b4 | 39 | private notifier: Notifier, |
5769e1db | 40 | private confirmService: ConfirmService, |
fc2ec87a | 41 | private serverService: ServerService, |
b1d40cff | 42 | private userService: UserService, |
a95a4cc8 | 43 | private auth: AuthService, |
8491293b RK |
44 | private route: ActivatedRoute, |
45 | private router: Router, | |
b1d40cff | 46 | private i18n: I18n |
28798b5d | 47 | ) { |
d592e0a9 | 48 | super() |
7da18e44 C |
49 | } |
50 | ||
a95a4cc8 C |
51 | get authUser () { |
52 | return this.auth.getUser() | |
53 | } | |
54 | ||
fc2ec87a | 55 | get requiresEmailVerification () { |
ba430d75 | 56 | return this.serverConfig.signup.requiresEmailVerification |
fc2ec87a JM |
57 | } |
58 | ||
bc99dfe5 RK |
59 | get selectedColumns () { |
60 | return this._selectedColumns | |
61 | } | |
62 | ||
52c4976f | 63 | set selectedColumns (val: string[]) { |
bc99dfe5 RK |
64 | this._selectedColumns = val |
65 | } | |
66 | ||
ab998f7b | 67 | ngOnInit () { |
ba430d75 C |
68 | this.serverConfig = this.serverService.getTmpConfig() |
69 | this.serverService.getConfig() | |
70 | .subscribe(config => this.serverConfig = config) | |
71 | ||
24b9417c | 72 | this.initialize() |
ab998f7b | 73 | |
8491293b RK |
74 | this.route.queryParams |
75 | .subscribe(params => { | |
76 | this.search = params.search || '' | |
77 | ||
78 | this.setTableFilter(this.search) | |
79 | this.loadData() | |
80 | }) | |
81 | ||
791645e6 | 82 | this.bulkUserActions = [ |
9b82d49d RK |
83 | [ |
84 | { | |
85 | label: this.i18n('Delete'), | |
86 | description: this.i18n('Videos will be deleted, comments will be tombstoned.'), | |
87 | handler: users => this.removeUsers(users), | |
88 | isDisplayed: users => users.every(u => this.authUser.canManage(u)) | |
89 | }, | |
90 | { | |
91 | label: this.i18n('Ban'), | |
f0ad4710 | 92 | description: this.i18n('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 | { | |
97 | label: this.i18n('Unban'), | |
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 | { | |
104 | label: this.i18n('Set Email as Verified'), | |
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') { | |
f8b2c1b4 | 163 | this.notifier.error(this.i18n('You cannot ban root.')) |
791645e6 C |
164 | return |
165 | } | |
166 | } | |
167 | ||
168 | this.userBanModal.openModal(users) | |
169 | } | |
170 | ||
2fbe7f19 | 171 | onUserChanged () { |
791645e6 C |
172 | this.loadData() |
173 | } | |
174 | ||
8491293b RK |
175 | /* Table filter functions */ |
176 | onUserSearch (event: Event) { | |
177 | this.onSearch(event) | |
178 | this.setQueryParams((event.target as HTMLInputElement).value) | |
179 | } | |
180 | ||
181 | setQueryParams (search: string) { | |
182 | const queryParams: Params = {} | |
183 | if (search) Object.assign(queryParams, { search }) | |
184 | ||
185 | this.router.navigate([ '/admin/users/list' ], { queryParams }) | |
186 | } | |
187 | ||
188 | resetTableFilter () { | |
189 | this.setTableFilter('') | |
190 | this.setQueryParams('') | |
191 | this.resetSearch() | |
192 | } | |
193 | /* END Table filter functions */ | |
194 | ||
d6af8146 RK |
195 | switchToDefaultAvatar ($event: Event) { |
196 | ($event.target as HTMLImageElement).src = Actor.GET_DEFAULT_AVATAR_URL() | |
197 | } | |
198 | ||
791645e6 C |
199 | async unbanUsers (users: User[]) { |
200 | const message = this.i18n('Do you really want to unban {{num}} users?', { num: users.length }) | |
201 | ||
202 | const res = await this.confirmService.confirm(message, this.i18n('Unban')) | |
203 | if (res === false) return | |
204 | ||
205 | this.userService.unbanUsers(users) | |
206 | .subscribe( | |
207 | () => { | |
208 | const message = this.i18n('{{num}} users unbanned.', { num: users.length }) | |
209 | ||
f8b2c1b4 | 210 | this.notifier.success(message) |
791645e6 C |
211 | this.loadData() |
212 | }, | |
213 | ||
f8b2c1b4 | 214 | err => this.notifier.error(err.message) |
791645e6 C |
215 | ) |
216 | } | |
217 | ||
218 | async removeUsers (users: User[]) { | |
219 | for (const user of users) { | |
220 | if (user.username === 'root') { | |
f8b2c1b4 | 221 | this.notifier.error(this.i18n('You cannot delete root.')) |
791645e6 C |
222 | return |
223 | } | |
224 | } | |
225 | ||
226 | const message = this.i18n('If you remove these users, you will not be able to create others with the same username!') | |
227 | const res = await this.confirmService.confirm(message, this.i18n('Delete')) | |
228 | if (res === false) return | |
229 | ||
230 | this.userService.removeUser(users).subscribe( | |
231 | () => { | |
f8b2c1b4 | 232 | this.notifier.success(this.i18n('{{num}} users deleted.', { num: users.length })) |
791645e6 C |
233 | this.loadData() |
234 | }, | |
235 | ||
f8b2c1b4 | 236 | err => this.notifier.error(err.message) |
791645e6 C |
237 | ) |
238 | } | |
239 | ||
fc2ec87a JM |
240 | async setEmailsAsVerified (users: User[]) { |
241 | this.userService.updateUsers(users, { emailVerified: true }).subscribe( | |
242 | () => { | |
f8b2c1b4 | 243 | this.notifier.success(this.i18n('{{num}} users email set as verified.', { num: users.length })) |
fc2ec87a JM |
244 | this.loadData() |
245 | }, | |
246 | ||
f8b2c1b4 | 247 | err => this.notifier.error(err.message) |
fc2ec87a JM |
248 | ) |
249 | } | |
250 | ||
791645e6 C |
251 | isInSelectionMode () { |
252 | return this.selectedUsers.length !== 0 | |
253 | } | |
dffd5d12 B |
254 | |
255 | protected loadData () { | |
256 | this.selectedUsers = [] | |
257 | ||
8491293b RK |
258 | this.userService.getUsers({ |
259 | pagination: this.pagination, | |
260 | sort: this.sort, | |
261 | search: this.search | |
262 | }).subscribe( | |
263 | resultList => { | |
264 | this.users = resultList.data | |
265 | this.totalRecords = resultList.total | |
266 | }, | |
f8b2c1b4 | 267 | |
8491293b RK |
268 | err => this.notifier.error(err.message) |
269 | ) | |
dffd5d12 | 270 | } |
7da18e44 | 271 | } |