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