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