]>
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' |
8569a870 | 7 | import { 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 | |
df98563e | 28 | constructor ( |
f8b2c1b4 | 29 | private notifier: Notifier, |
5769e1db | 30 | private confirmService: ConfirmService, |
fc2ec87a | 31 | private serverService: ServerService, |
b1d40cff | 32 | private userService: UserService, |
a95a4cc8 | 33 | private auth: AuthService, |
b1d40cff | 34 | private i18n: I18n |
28798b5d | 35 | ) { |
d592e0a9 | 36 | super() |
7da18e44 C |
37 | } |
38 | ||
a95a4cc8 C |
39 | get authUser () { |
40 | return this.auth.getUser() | |
41 | } | |
42 | ||
fc2ec87a JM |
43 | get requiresEmailVerification () { |
44 | return this.serverService.getConfig().signup.requiresEmailVerification | |
45 | } | |
46 | ||
ab998f7b | 47 | ngOnInit () { |
24b9417c | 48 | this.initialize() |
ab998f7b | 49 | |
791645e6 C |
50 | this.bulkUserActions = [ |
51 | { | |
52 | label: this.i18n('Delete'), | |
a95a4cc8 C |
53 | handler: users => this.removeUsers(users), |
54 | isDisplayed: users => users.every(u => this.authUser.canManage(u)) | |
791645e6 C |
55 | }, |
56 | { | |
57 | label: this.i18n('Ban'), | |
58 | handler: users => this.openBanUserModal(users), | |
a95a4cc8 | 59 | isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === false) |
791645e6 C |
60 | }, |
61 | { | |
62 | label: this.i18n('Unban'), | |
63 | handler: users => this.unbanUsers(users), | |
a95a4cc8 | 64 | isDisplayed: users => users.every(u => this.authUser.canManage(u) && u.blocked === true) |
fc2ec87a JM |
65 | }, |
66 | { | |
67 | label: this.i18n('Set Email as Verified'), | |
68 | handler: users => this.setEmailsAsVerified(users), | |
a95a4cc8 C |
69 | isDisplayed: users => { |
70 | return this.requiresEmailVerification && | |
71 | users.every(u => this.authUser.canManage(u) && !u.blocked && u.emailVerified === false) | |
72 | } | |
791645e6 C |
73 | } |
74 | ] | |
141b177d C |
75 | } |
76 | ||
791645e6 C |
77 | openBanUserModal (users: User[]) { |
78 | for (const user of users) { | |
79 | if (user.username === 'root') { | |
f8b2c1b4 | 80 | this.notifier.error(this.i18n('You cannot ban root.')) |
791645e6 C |
81 | return |
82 | } | |
83 | } | |
84 | ||
85 | this.userBanModal.openModal(users) | |
86 | } | |
87 | ||
2fbe7f19 | 88 | onUserChanged () { |
791645e6 C |
89 | this.loadData() |
90 | } | |
91 | ||
92 | async unbanUsers (users: User[]) { | |
93 | const message = this.i18n('Do you really want to unban {{num}} users?', { num: users.length }) | |
94 | ||
95 | const res = await this.confirmService.confirm(message, this.i18n('Unban')) | |
96 | if (res === false) return | |
97 | ||
98 | this.userService.unbanUsers(users) | |
99 | .subscribe( | |
100 | () => { | |
101 | const message = this.i18n('{{num}} users unbanned.', { num: users.length }) | |
102 | ||
f8b2c1b4 | 103 | this.notifier.success(message) |
791645e6 C |
104 | this.loadData() |
105 | }, | |
106 | ||
f8b2c1b4 | 107 | err => this.notifier.error(err.message) |
791645e6 C |
108 | ) |
109 | } | |
110 | ||
111 | async removeUsers (users: User[]) { | |
112 | for (const user of users) { | |
113 | if (user.username === 'root') { | |
f8b2c1b4 | 114 | this.notifier.error(this.i18n('You cannot delete root.')) |
791645e6 C |
115 | return |
116 | } | |
117 | } | |
118 | ||
119 | const message = this.i18n('If you remove these users, you will not be able to create others with the same username!') | |
120 | const res = await this.confirmService.confirm(message, this.i18n('Delete')) | |
121 | if (res === false) return | |
122 | ||
123 | this.userService.removeUser(users).subscribe( | |
124 | () => { | |
f8b2c1b4 | 125 | this.notifier.success(this.i18n('{{num}} users deleted.', { num: users.length })) |
791645e6 C |
126 | this.loadData() |
127 | }, | |
128 | ||
f8b2c1b4 | 129 | err => this.notifier.error(err.message) |
791645e6 C |
130 | ) |
131 | } | |
132 | ||
fc2ec87a JM |
133 | async setEmailsAsVerified (users: User[]) { |
134 | this.userService.updateUsers(users, { emailVerified: true }).subscribe( | |
135 | () => { | |
f8b2c1b4 | 136 | this.notifier.success(this.i18n('{{num}} users email set as verified.', { num: users.length })) |
fc2ec87a JM |
137 | this.loadData() |
138 | }, | |
139 | ||
f8b2c1b4 | 140 | err => this.notifier.error(err.message) |
fc2ec87a JM |
141 | ) |
142 | } | |
143 | ||
791645e6 C |
144 | isInSelectionMode () { |
145 | return this.selectedUsers.length !== 0 | |
146 | } | |
dffd5d12 B |
147 | |
148 | protected loadData () { | |
149 | this.selectedUsers = [] | |
150 | ||
151 | this.userService.getUsers(this.pagination, this.sort, this.search) | |
f8b2c1b4 C |
152 | .subscribe( |
153 | resultList => { | |
154 | this.users = resultList.data | |
155 | this.totalRecords = resultList.total | |
156 | }, | |
157 | ||
158 | err => this.notifier.error(err.message) | |
159 | ) | |
dffd5d12 | 160 | } |
7da18e44 | 161 | } |