X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2F%2Badmin%2Fusers%2Fuser-list%2Fuser-list.component.ts;h=66ab796f9bee2af8270663cf9710fbfc12b9a685;hb=5abb9fbbd12e7097e348d6a38622d364b1fa47ed;hp=03f4e5c0aa0f31e7f50fc0268c94e7ac0a49b2b9;hpb=b99290b1d5d736083513fb8f66e91f61bfe07e0b;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/+admin/users/user-list/user-list.component.ts b/client/src/app/+admin/users/user-list/user-list.component.ts index 03f4e5c0a..66ab796f9 100644 --- a/client/src/app/+admin/users/user-list/user-list.component.ts +++ b/client/src/app/+admin/users/user-list/user-list.component.ts @@ -1,42 +1,152 @@ -import { Component, OnInit } from '@angular/core'; - -import { User } from '../../../shared'; -import { UserService } from '../shared'; +import { Component, OnInit, ViewChild } from '@angular/core' +import { Notifier } from '@app/core' +import { SortMeta } from 'primeng/components/common/sortmeta' +import { ConfirmService, ServerService } from '../../../core' +import { RestPagination, RestTable, UserService } from '../../../shared' +import { I18n } from '@ngx-translate/i18n-polyfill' +import { User } from '../../../../../../shared' +import { UserBanModalComponent } from '@app/shared/moderation' +import { DropdownAction } from '@app/shared/buttons/action-dropdown.component' @Component({ selector: 'my-user-list', templateUrl: './user-list.component.html', styleUrls: [ './user-list.component.scss' ] }) -export class UserListComponent implements OnInit { - totalUsers: number; - users: User[]; +export class UserListComponent extends RestTable implements OnInit { + @ViewChild('userBanModal') userBanModal: UserBanModalComponent + + users: User[] = [] + totalRecords = 0 + rowsPerPage = 10 + sort: SortMeta = { field: 'createdAt', order: 1 } + pagination: RestPagination = { count: this.rowsPerPage, start: 0 } - constructor(private userService: UserService) {} + selectedUsers: User[] = [] + bulkUserActions: DropdownAction[] = [] + + constructor ( + private notifier: Notifier, + private confirmService: ConfirmService, + private serverService: ServerService, + private userService: UserService, + private i18n: I18n + ) { + super() + } - ngOnInit() { - this.getUsers(); + get requiresEmailVerification () { + return this.serverService.getConfig().signup.requiresEmailVerification } - getUsers() { - this.userService.getUsers().subscribe( - ({ users, totalUsers }) => { - this.users = users; - this.totalUsers = totalUsers; + ngOnInit () { + this.initialize() + + this.bulkUserActions = [ + { + label: this.i18n('Delete'), + handler: users => this.removeUsers(users) + }, + { + label: this.i18n('Ban'), + handler: users => this.openBanUserModal(users), + isDisplayed: users => users.every(u => u.blocked === false) }, + { + label: this.i18n('Unban'), + handler: users => this.unbanUsers(users), + isDisplayed: users => users.every(u => u.blocked === true) + }, + { + label: this.i18n('Set Email as Verified'), + handler: users => this.setEmailsAsVerified(users), + isDisplayed: users => this.requiresEmailVerification && users.every(u => !u.blocked && u.emailVerified === false) + } + ] + } - err => alert(err.text) - ); + openBanUserModal (users: User[]) { + for (const user of users) { + if (user.username === 'root') { + this.notifier.error(this.i18n('You cannot ban root.')) + return + } + } + + this.userBanModal.openModal(users) + } + + onUserChanged () { + this.loadData() } + async unbanUsers (users: User[]) { + const message = this.i18n('Do you really want to unban {{num}} users?', { num: users.length }) + + const res = await this.confirmService.confirm(message, this.i18n('Unban')) + if (res === false) return + + this.userService.unbanUsers(users) + .subscribe( + () => { + const message = this.i18n('{{num}} users unbanned.', { num: users.length }) + + this.notifier.success(message) + this.loadData() + }, - removeUser(user: User) { - if (confirm('Are you sure?')) { - this.userService.removeUser(user).subscribe( - () => this.getUsers(), + err => this.notifier.error(err.message) + ) + } - err => alert(err.text) - ); + async removeUsers (users: User[]) { + for (const user of users) { + if (user.username === 'root') { + this.notifier.error(this.i18n('You cannot delete root.')) + return + } } + + const message = this.i18n('If you remove these users, you will not be able to create others with the same username!') + const res = await this.confirmService.confirm(message, this.i18n('Delete')) + if (res === false) return + + this.userService.removeUser(users).subscribe( + () => { + this.notifier.success(this.i18n('{{num}} users deleted.', { num: users.length })) + this.loadData() + }, + + err => this.notifier.error(err.message) + ) + } + + async setEmailsAsVerified (users: User[]) { + this.userService.updateUsers(users, { emailVerified: true }).subscribe( + () => { + this.notifier.success(this.i18n('{{num}} users email set as verified.', { num: users.length })) + this.loadData() + }, + + err => this.notifier.error(err.message) + ) + } + + isInSelectionMode () { + return this.selectedUsers.length !== 0 + } + + protected loadData () { + this.selectedUsers = [] + + this.userService.getUsers(this.pagination, this.sort, this.search) + .subscribe( + resultList => { + this.users = resultList.data + this.totalRecords = resultList.total + }, + + err => this.notifier.error(err.message) + ) } }