]>
Commit | Line | Data |
---|---|---|
791645e6 | 1 | import { Component, OnInit, ViewChild } from '@angular/core' |
a95a4cc8 | 2 | import { AuthService, Notifier } from '@app/core' |
1f30a185 | 3 | import { SortMeta } from 'primeng/components/common/sortmeta' |
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[] = [] |
c199c427 | 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 C |
56 | this.bulkUserActions = [ |
57 | { | |
58 | label: this.i18n('Delete'), | |
a95a4cc8 C |
59 | handler: users => this.removeUsers(users), |
60 | isDisplayed: users => users.every(u => this.authUser.canManage(u)) | |
791645e6 C |
61 | }, |
62 | { | |
63 | label: this.i18n('Ban'), | |
64 | handler: users => this.openBanUserModal(users), | |
a95a4cc8 | 65 | isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === false) |
791645e6 C |
66 | }, |
67 | { | |
68 | label: this.i18n('Unban'), | |
69 | handler: users => this.unbanUsers(users), | |
a95a4cc8 | 70 | isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === true) |
fc2ec87a JM |
71 | }, |
72 | { | |
73 | label: this.i18n('Set Email as Verified'), | |
74 | handler: users => this.setEmailsAsVerified(users), | |
a95a4cc8 C |
75 | isDisplayed: users => { |
76 | return this.requiresEmailVerification && | |
77 | users.every(u => this.authUser.canManage(u) && !u.blocked && u.emailVerified === false) | |
78 | } | |
791645e6 C |
79 | } |
80 | ] | |
141b177d C |
81 | } |
82 | ||
791645e6 C |
83 | openBanUserModal (users: User[]) { |
84 | for (const user of users) { | |
85 | if (user.username === 'root') { | |
f8b2c1b4 | 86 | this.notifier.error(this.i18n('You cannot ban root.')) |
791645e6 C |
87 | return |
88 | } | |
89 | } | |
90 | ||
91 | this.userBanModal.openModal(users) | |
92 | } | |
93 | ||
2fbe7f19 | 94 | onUserChanged () { |
791645e6 C |
95 | this.loadData() |
96 | } | |
97 | ||
98 | async unbanUsers (users: User[]) { | |
99 | const message = this.i18n('Do you really want to unban {{num}} users?', { num: users.length }) | |
100 | ||
101 | const res = await this.confirmService.confirm(message, this.i18n('Unban')) | |
102 | if (res === false) return | |
103 | ||
104 | this.userService.unbanUsers(users) | |
105 | .subscribe( | |
106 | () => { | |
107 | const message = this.i18n('{{num}} users unbanned.', { num: users.length }) | |
108 | ||
f8b2c1b4 | 109 | this.notifier.success(message) |
791645e6 C |
110 | this.loadData() |
111 | }, | |
112 | ||
f8b2c1b4 | 113 | err => this.notifier.error(err.message) |
791645e6 C |
114 | ) |
115 | } | |
116 | ||
117 | async removeUsers (users: User[]) { | |
118 | for (const user of users) { | |
119 | if (user.username === 'root') { | |
f8b2c1b4 | 120 | this.notifier.error(this.i18n('You cannot delete root.')) |
791645e6 C |
121 | return |
122 | } | |
123 | } | |
124 | ||
125 | const message = this.i18n('If you remove these users, you will not be able to create others with the same username!') | |
126 | const res = await this.confirmService.confirm(message, this.i18n('Delete')) | |
127 | if (res === false) return | |
128 | ||
129 | this.userService.removeUser(users).subscribe( | |
130 | () => { | |
f8b2c1b4 | 131 | this.notifier.success(this.i18n('{{num}} users deleted.', { num: users.length })) |
791645e6 C |
132 | this.loadData() |
133 | }, | |
134 | ||
f8b2c1b4 | 135 | err => this.notifier.error(err.message) |
791645e6 C |
136 | ) |
137 | } | |
138 | ||
fc2ec87a JM |
139 | async setEmailsAsVerified (users: User[]) { |
140 | this.userService.updateUsers(users, { emailVerified: true }).subscribe( | |
141 | () => { | |
f8b2c1b4 | 142 | this.notifier.success(this.i18n('{{num}} users email set as verified.', { num: users.length })) |
fc2ec87a JM |
143 | this.loadData() |
144 | }, | |
145 | ||
f8b2c1b4 | 146 | err => this.notifier.error(err.message) |
fc2ec87a JM |
147 | ) |
148 | } | |
149 | ||
791645e6 C |
150 | isInSelectionMode () { |
151 | return this.selectedUsers.length !== 0 | |
152 | } | |
dffd5d12 B |
153 | |
154 | protected loadData () { | |
155 | this.selectedUsers = [] | |
156 | ||
157 | this.userService.getUsers(this.pagination, this.sort, this.search) | |
f8b2c1b4 C |
158 | .subscribe( |
159 | resultList => { | |
160 | this.users = resultList.data | |
161 | this.totalRecords = resultList.total | |
162 | }, | |
163 | ||
164 | err => this.notifier.error(err.message) | |
165 | ) | |
dffd5d12 | 166 | } |
7da18e44 | 167 | } |