]>
Commit | Line | Data |
---|---|---|
791645e6 | 1 | import { Component, OnInit, ViewChild } from '@angular/core' |
a95a4cc8 | 2 | import { AuthService, Notifier } from '@app/core' |
f77eb73b | 3 | import { SortMeta } from 'primeng/api' |
fc2ec87a | 4 | import { ConfirmService, ServerService } from '../../../core' |
e724fa93 | 5 | import { RestPagination, RestTable, UserService } from '../../../shared' |
b1d40cff | 6 | import { I18n } from '@ngx-translate/i18n-polyfill' |
ba430d75 | 7 | import { ServerConfig, User } from '../../../../../../shared' |
791645e6 C |
8 | import { UserBanModalComponent } from '@app/shared/moderation' |
9 | import { DropdownAction } from '@app/shared/buttons/action-dropdown.component' | |
7da18e44 C |
10 | |
11 | @Component({ | |
12 | selector: 'my-user-list', | |
ec8d8440 C |
13 | templateUrl: './user-list.component.html', |
14 | styleUrls: [ './user-list.component.scss' ] | |
7da18e44 | 15 | }) |
ab998f7b | 16 | export class UserListComponent extends RestTable implements OnInit { |
f36da21e | 17 | @ViewChild('userBanModal', { static: true }) userBanModal: UserBanModalComponent |
791645e6 | 18 | |
d592e0a9 C |
19 | users: User[] = [] |
20 | totalRecords = 0 | |
21 | rowsPerPage = 10 | |
ab998f7b | 22 | sort: SortMeta = { field: 'createdAt', order: 1 } |
d592e0a9 | 23 | pagination: RestPagination = { count: this.rowsPerPage, start: 0 } |
141b177d | 24 | |
791645e6 | 25 | selectedUsers: User[] = [] |
9b82d49d | 26 | bulkUserActions: DropdownAction<User[]>[][] = [] |
791645e6 | 27 | |
ba430d75 C |
28 | private serverConfig: ServerConfig |
29 | ||
df98563e | 30 | constructor ( |
f8b2c1b4 | 31 | private notifier: Notifier, |
5769e1db | 32 | private confirmService: ConfirmService, |
fc2ec87a | 33 | private serverService: ServerService, |
b1d40cff | 34 | private userService: UserService, |
a95a4cc8 | 35 | private auth: AuthService, |
b1d40cff | 36 | private i18n: I18n |
28798b5d | 37 | ) { |
d592e0a9 | 38 | super() |
7da18e44 C |
39 | } |
40 | ||
a95a4cc8 C |
41 | get authUser () { |
42 | return this.auth.getUser() | |
43 | } | |
44 | ||
fc2ec87a | 45 | get requiresEmailVerification () { |
ba430d75 | 46 | return this.serverConfig.signup.requiresEmailVerification |
fc2ec87a JM |
47 | } |
48 | ||
ab998f7b | 49 | ngOnInit () { |
ba430d75 C |
50 | this.serverConfig = this.serverService.getTmpConfig() |
51 | this.serverService.getConfig() | |
52 | .subscribe(config => this.serverConfig = config) | |
53 | ||
24b9417c | 54 | this.initialize() |
ab998f7b | 55 | |
791645e6 | 56 | this.bulkUserActions = [ |
9b82d49d RK |
57 | [ |
58 | { | |
59 | label: this.i18n('Delete'), | |
60 | description: this.i18n('Videos will be deleted, comments will be tombstoned.'), | |
61 | handler: users => this.removeUsers(users), | |
62 | isDisplayed: users => users.every(u => this.authUser.canManage(u)) | |
63 | }, | |
64 | { | |
65 | label: this.i18n('Ban'), | |
f0ad4710 | 66 | description: this.i18n('User won\'t be able to login anymore, but videos and comments will be kept as is.'), |
9b82d49d RK |
67 | handler: users => this.openBanUserModal(users), |
68 | isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === false) | |
69 | }, | |
70 | { | |
71 | label: this.i18n('Unban'), | |
72 | handler: users => this.unbanUsers(users), | |
73 | isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === true) | |
a95a4cc8 | 74 | } |
9b82d49d RK |
75 | ], |
76 | [ | |
77 | { | |
78 | label: this.i18n('Set Email as Verified'), | |
79 | handler: users => this.setEmailsAsVerified(users), | |
80 | isDisplayed: users => { | |
81 | return this.requiresEmailVerification && | |
82 | users.every(u => this.authUser.canManage(u) && !u.blocked && u.emailVerified === false) | |
83 | } | |
84 | } | |
85 | ] | |
791645e6 | 86 | ] |
141b177d C |
87 | } |
88 | ||
791645e6 C |
89 | openBanUserModal (users: User[]) { |
90 | for (const user of users) { | |
91 | if (user.username === 'root') { | |
f8b2c1b4 | 92 | this.notifier.error(this.i18n('You cannot ban root.')) |
791645e6 C |
93 | return |
94 | } | |
95 | } | |
96 | ||
97 | this.userBanModal.openModal(users) | |
98 | } | |
99 | ||
2fbe7f19 | 100 | onUserChanged () { |
791645e6 C |
101 | this.loadData() |
102 | } | |
103 | ||
104 | async unbanUsers (users: User[]) { | |
105 | const message = this.i18n('Do you really want to unban {{num}} users?', { num: users.length }) | |
106 | ||
107 | const res = await this.confirmService.confirm(message, this.i18n('Unban')) | |
108 | if (res === false) return | |
109 | ||
110 | this.userService.unbanUsers(users) | |
111 | .subscribe( | |
112 | () => { | |
113 | const message = this.i18n('{{num}} users unbanned.', { num: users.length }) | |
114 | ||
f8b2c1b4 | 115 | this.notifier.success(message) |
791645e6 C |
116 | this.loadData() |
117 | }, | |
118 | ||
f8b2c1b4 | 119 | err => this.notifier.error(err.message) |
791645e6 C |
120 | ) |
121 | } | |
122 | ||
123 | async removeUsers (users: User[]) { | |
124 | for (const user of users) { | |
125 | if (user.username === 'root') { | |
f8b2c1b4 | 126 | this.notifier.error(this.i18n('You cannot delete root.')) |
791645e6 C |
127 | return |
128 | } | |
129 | } | |
130 | ||
131 | const message = this.i18n('If you remove these users, you will not be able to create others with the same username!') | |
132 | const res = await this.confirmService.confirm(message, this.i18n('Delete')) | |
133 | if (res === false) return | |
134 | ||
135 | this.userService.removeUser(users).subscribe( | |
136 | () => { | |
f8b2c1b4 | 137 | this.notifier.success(this.i18n('{{num}} users deleted.', { num: users.length })) |
791645e6 C |
138 | this.loadData() |
139 | }, | |
140 | ||
f8b2c1b4 | 141 | err => this.notifier.error(err.message) |
791645e6 C |
142 | ) |
143 | } | |
144 | ||
fc2ec87a JM |
145 | async setEmailsAsVerified (users: User[]) { |
146 | this.userService.updateUsers(users, { emailVerified: true }).subscribe( | |
147 | () => { | |
f8b2c1b4 | 148 | this.notifier.success(this.i18n('{{num}} users email set as verified.', { num: users.length })) |
fc2ec87a JM |
149 | this.loadData() |
150 | }, | |
151 | ||
f8b2c1b4 | 152 | err => this.notifier.error(err.message) |
fc2ec87a JM |
153 | ) |
154 | } | |
155 | ||
791645e6 C |
156 | isInSelectionMode () { |
157 | return this.selectedUsers.length !== 0 | |
158 | } | |
dffd5d12 B |
159 | |
160 | protected loadData () { | |
161 | this.selectedUsers = [] | |
162 | ||
163 | this.userService.getUsers(this.pagination, this.sort, this.search) | |
f8b2c1b4 C |
164 | .subscribe( |
165 | resultList => { | |
166 | this.users = resultList.data | |
167 | this.totalRecords = resultList.total | |
168 | }, | |
169 | ||
170 | err => this.notifier.error(err.message) | |
171 | ) | |
dffd5d12 | 172 | } |
7da18e44 | 173 | } |