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